Skip to content

Commit

Permalink
Merge branch 'dataset_security_feature' of https://github.com/bcgov/b…
Browse files Browse the repository at this point in the history
…iohubbc-platform into dataset_security_feature
  • Loading branch information
NickPhura committed Dec 8, 2023
2 parents 0e2b034 + 3182066 commit c8157e0
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
21 changes: 21 additions & 0 deletions app/src/hooks/useDebounce.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { act, renderHook } from '@testing-library/react-hooks';
import { waitFor } from 'test-helpers/test-utils';
import useDebounce from './useDebounce';

const mockCallback = jest.fn();

describe('useDebounce', () => {
it('should debounce repeated calls', async () => {
const { result } = renderHook(() => useDebounce(mockCallback, 500));
const debounce = result.current;
act(() => debounce());
await waitFor(() => {
expect(mockCallback.mock.calls[0]).toBeDefined();
});
// this request should fail as it is being requested too quickly
act(() => debounce());
await waitFor(() => {
expect(mockCallback.mock.calls[1]).not.toBeDefined();
});
});
});
File renamed without changes.
102 changes: 102 additions & 0 deletions app/src/hooks/useFuzzySearch.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { act, renderHook } from '@testing-library/react-hooks';
import { FuseResult } from 'fuse.js';
import useFuzzySearch from './useFuzzySearch';

const item = { a: 'zzz', b: 'hello world' };
const mockDataArray = [item];

const mockFuzzyData: FuseResult<any>[] = [
{
item,
matches: [],
refIndex: 0
}
];

const mockOptions = { minMatchCharLength: 5 };

describe('useFuzzySearch', () => {
describe('mounting conditions', () => {
const { result } = renderHook(() => useFuzzySearch(mockDataArray, {}));

it('should mount with empty search string', () => {
expect(result.current.searchValue).toBe('');
});

it('should mount with fuzzyData array in FuseResult structure', () => {
expect(result.current.fuzzyData).toStrictEqual(mockFuzzyData);
});
});
describe('handleFuzzyData', () => {
it('should set fuzzyData with new array', () => {
const { result } = renderHook(() => useFuzzySearch(mockDataArray, {}));
act(() => result.current.handleFuzzyData([{ item: { a: 'test', b: 'test' }, matches: [], refIndex: 0 }]));
expect(result.current.fuzzyData[0].item.a).toBe('test');
});
});

describe('handleSearch', () => {
it('should set searchValue', () => {
const { result } = renderHook(() => useFuzzySearch(mockDataArray, {}));
act(() => result.current.handleSearch({ target: { value: 'test' } } as any));
expect(result.current.searchValue).toBe('test');
});

it('should setFuzzyData to default when no search value provided', () => {
const { result } = renderHook(() => useFuzzySearch(mockDataArray, {}));
act(() => result.current.handleSearch({ target: { value: '' } } as any));
expect(result.current.searchValue).toBe('');
expect(result.current.fuzzyData).toStrictEqual(mockFuzzyData);
});

it('should setFuzzyData to default when no search value provided', () => {
const { result } = renderHook(() => useFuzzySearch(mockDataArray, {}));
act(() => result.current.handleSearch({ target: { value: '' } } as any));
expect(result.current.searchValue).toBe('');
expect(result.current.fuzzyData).toStrictEqual(mockFuzzyData);
});

it('should setFuzzyData to default when character count is less than minMatchCharLength', () => {
const { result } = renderHook(() => useFuzzySearch(mockDataArray, mockOptions));
act(() => result.current.handleSearch({ target: { value: 'aaaa' } } as any));
expect(result.current.searchValue).toBe('aaaa');
expect(result.current.fuzzyData).toStrictEqual(mockFuzzyData);
});
});
describe('highlight', () => {
const { result } = renderHook(() => useFuzzySearch(mockDataArray, { highlightColour: '#ffff' }));
it('should return formatted html if highlight indices provided', () => {
act(() => {
const jsx = result.current.highlight('abc', [[0, 1]]);
const shouldEqual = (
<>
<mark style={{ backgroundColor: '#ffff' }}>ab</mark>c
</>
);
expect(jsx.toString()).toEqual(shouldEqual.toString());
});
});

it('should return string value if no indices', () => {
act(() => {
const jsx = result.current.highlight('abc', []);
expect(jsx.toString()).toEqual('abc');
});
});

it('should highlight whole string', () => {
act(() => {
const jsx = result.current.highlight('abc', [
[0, 1],
[2, 2]
]);
const shouldEqual = (
<>
<mark style={{ backgroundColor: '#ffff' }}>abc</mark>
</>
);
expect(jsx.toString()).toEqual(shouldEqual.toString());
});
});
});
});

0 comments on commit c8157e0

Please sign in to comment.