-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(use query): abort request on unmount; updated tests & lint i…
…ssues
- Loading branch information
Showing
15 changed files
with
276 additions
and
499 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
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
2 changes: 1 addition & 1 deletion
2
components/organisation-unit-tree/src/__stories__/dx-wrapper.js
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
70 changes: 45 additions & 25 deletions
70
components/organisation-unit-tree/src/helpers/use-query.js
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
89 changes: 89 additions & 0 deletions
89
components/organisation-unit-tree/src/helpers/use-query.test.js
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,89 @@ | ||
import EventEmitter from 'events' | ||
import { renderHook, act } from '@testing-library/react-hooks' | ||
import { useQuery } from './use-query.js' | ||
|
||
jest.useFakeTimers() | ||
|
||
describe('useQuery', () => { | ||
const fetchFn = jest.fn() | ||
const onTimeout = jest.fn(resolve => { | ||
resolve({ foo: 'bar' }) | ||
}) | ||
|
||
// make sure request can be aborted to prevent state mutation after unmount | ||
const emitter = new EventEmitter() | ||
jest.spyOn(AbortController.prototype, 'abort').mockImplementation(() => { | ||
emitter.emit('abort') | ||
}) | ||
|
||
fetchFn.mockImplementation(() => { | ||
return new Promise((resolve, reject) => { | ||
const timeout = setTimeout(() => onTimeout(resolve, reject), 500) | ||
emitter.on('abort', () => clearTimeout(timeout)) | ||
}) | ||
}) | ||
|
||
afterEach(() => { | ||
fetchFn.mockClear() | ||
}) | ||
|
||
it('should start with loading: true, error: null and data: null', async () => { | ||
const hookState = renderHook(() => useQuery(fetchFn)) | ||
expect(hookState.result.current).toEqual(expect.objectContaining({ | ||
loading: true, | ||
error: null, | ||
data: null, | ||
})) | ||
}) | ||
|
||
it('should use the "defaultData" option as initial data when provided', () => { | ||
const defaultData = { foo: 'bar' } | ||
const { result } = renderHook(() => useQuery(fetchFn, { defaultData })) | ||
expect(result.current).toEqual(expect.objectContaining({ | ||
loading: true, | ||
error: null, | ||
data: { foo: 'bar' }, | ||
})) | ||
}) | ||
|
||
it('should perform the initial request right away', () => { | ||
const defaultData = { foo: 'bar' } | ||
renderHook(() => useQuery(fetchFn, { defaultData })) | ||
expect(fetchFn).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('should set the data after a successful initial request', async () => { | ||
const { result, waitForNextUpdate } = renderHook(() => useQuery(fetchFn)) | ||
expect(fetchFn).toHaveBeenCalledTimes(1) | ||
|
||
await act(async () => { | ||
jest.advanceTimersByTime(1000) | ||
await waitForNextUpdate() | ||
}) | ||
|
||
expect(result.current).toEqual(expect.objectContaining({ | ||
loading: false, | ||
error: null, | ||
data: { foo: 'bar' }, | ||
})) | ||
}) | ||
|
||
it('should set the error after a failed initial request', async () => { | ||
const failureError = new Error('I am a failure') | ||
onTimeout.mockImplementationOnce((resolve, reject) => reject(failureError)) | ||
|
||
const { result, waitForNextUpdate } = renderHook(() => useQuery(fetchFn)) | ||
expect(fetchFn).toHaveBeenCalledTimes(1) | ||
|
||
await act(async () => { | ||
jest.advanceTimersByTime(1000) | ||
await waitForNextUpdate() | ||
}) | ||
|
||
expect(result.current).toEqual(expect.objectContaining({ | ||
loading: false, | ||
error: failureError, | ||
data: null, | ||
})) | ||
}) | ||
}) |
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
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
Oops, something went wrong.