-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(healSearchDebounce): Initial commit * feat(healSearchDebounce): Wrote unit test and organized code * feat(healSearchDebounce): Ran eslint on changed files * feat(healSearchDebounce): Changed import of DebounceSearch for Jest test * test(addReactTestingLibrary): Refactored and separated out code so tests can be written and so debouncing works * feat(healSearchDebounce): Renamed function dbs to debounceSearch for clarity * feat(healSearchDebounce): Renamed organized functions only used for search into a search folder and authored a test for doDebounceSearch * feat(healSearchDebounce): Created test for doDebounceSearch.ts * test(addReactTestingLibrary): Removed console.log * feat(healSearchDebounce): Converted JS function to TS and changed array to object to avoid needing spread operator * feat(healSearchDebounce): Ran eslint on changed files * feat(healSearchDebounce): Fixed TS errors * feat(healSearchDebounce): Formatted code * feat(healSearchDebounce): Removed old comment * feat(healSearchDebounce): Refactored test to use a test object * feat(healSearchDebounce): Removed duplicated code, added import for FilterState in doSearchFilterSort.ts * feat(healSearchDebounce): ran eslint and fixed imports for doSearchFilterSort
- Loading branch information
1 parent
94e0235
commit 73bbbf8
Showing
6 changed files
with
271 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import doDebounceSearch from './doDebounceSearch'; | ||
import doSearchFilterSort from './doSearchFilterSort'; | ||
|
||
jest.mock('./doSearchFilterSort'); | ||
|
||
describe('doDebounceSearch', () => { | ||
let memoizedDebouncedSearch; | ||
let setExecutedSearchesCount; | ||
beforeEach(() => { | ||
memoizedDebouncedSearch = jest.fn(() => {}); | ||
setExecutedSearchesCount = jest.fn(() => {}); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should execute doSearchFilterSort initially without debouncing', () => { | ||
doDebounceSearch({ test: 'test' }, memoizedDebouncedSearch, 0, setExecutedSearchesCount); | ||
expect(doSearchFilterSort).toHaveBeenCalledWith({ test: 'test' }); | ||
expect(memoizedDebouncedSearch).not.toHaveBeenCalled(); | ||
expect(setExecutedSearchesCount).toHaveBeenCalledWith(1); | ||
|
||
jest.clearAllMocks(); | ||
doDebounceSearch({ test: 'test2' }, memoizedDebouncedSearch, 1, setExecutedSearchesCount); | ||
expect(doSearchFilterSort).toHaveBeenCalledWith({ test: 'test2' }); | ||
expect(memoizedDebouncedSearch).not.toHaveBeenCalled(); | ||
expect(setExecutedSearchesCount).toHaveBeenCalledWith(2); | ||
}); | ||
|
||
it('should debounce the doSearchFilterSort call when executedSearchesCount >= 2', () => { | ||
doDebounceSearch({ test: 'test3' }, memoizedDebouncedSearch, 2, setExecutedSearchesCount); | ||
expect(doSearchFilterSort).not.toHaveBeenCalled(); | ||
expect(memoizedDebouncedSearch).toHaveBeenCalledWith({ test: 'test3' }); | ||
expect(setExecutedSearchesCount).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import doSearchFilterSort from './doSearchFilterSort'; | ||
|
||
const doDebounceSearch = ( | ||
parametersForDoSearchFilterSort: {}, | ||
memoizedDebouncedSearch: (...args: any[]) => void, | ||
executedSearchesCount: number, | ||
setExecutedSearchesCount: (count: number) => void, | ||
) => { | ||
const initialSearchesWithoutDebounce = 2; | ||
// Execute searches initially without debounce to decrease page load time | ||
if (executedSearchesCount < initialSearchesWithoutDebounce) { | ||
setExecutedSearchesCount(executedSearchesCount + 1); | ||
return doSearchFilterSort(parametersForDoSearchFilterSort); | ||
} | ||
// Otherwise debounce the calls | ||
return memoizedDebouncedSearch(parametersForDoSearchFilterSort); | ||
}; | ||
export default doDebounceSearch; |
Oops, something went wrong.