Skip to content

Commit

Permalink
chore: fix linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammer5 committed Aug 6, 2021
1 parent 2f24272 commit bb23bc5
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 35 deletions.
60 changes: 37 additions & 23 deletions components/organisation-unit-tree/src/helpers/use-query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,25 @@ describe('useQuery', () => {

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,
}))
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' },
}))
expect(result.current).toEqual(
expect.objectContaining({
loading: true,
error: null,
data: { foo: 'bar' },
})
)
})

it('should perform the initial request right away', () => {
Expand All @@ -53,37 +57,47 @@ describe('useQuery', () => {
})

it('should set the data after a successful initial request', async () => {
const { result, waitForNextUpdate } = renderHook(() => useQuery(fetchFn))
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' },
}))
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))
onTimeout.mockImplementationOnce((resolve, reject) =>
reject(failureError)
)

const { result, waitForNextUpdate } = renderHook(() => useQuery(fetchFn))
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,
}))
expect(result.current).toEqual(
expect.objectContaining({
loading: false,
error: failureError,
data: null,
})
)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export const useFetchOrgData = () => {
const [persistedEngine] = useState(engine)

const fetchOrgData = useCallback(
({ variables, signal }) => persistedEngine.query(ORG_DATA_QUERY, { variables, signal }),
({ variables, signal }) =>
persistedEngine.query(ORG_DATA_QUERY, { variables, signal }),
[persistedEngine]
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ describe('useFetchOrgData', () => {
displayName: 'Org Unit 1',
children: [
{
id: 'A0000000001',
path: '/A0000000000/A0000000001',
children: [],
displayName: 'Org Unit 2'
}
]
}
id: 'A0000000001',
path: '/A0000000000/A0000000001',
children: [],
displayName: 'Org Unit 2',
},
],
},
})
})

Expand All @@ -80,7 +80,9 @@ describe('useFetchOrgData', () => {
</CustomDataProvider>
)

const { result } = renderHook(useFetchOrgData, { wrapper: wrapperWithFailure })
const { result } = renderHook(useFetchOrgData, {
wrapper: wrapperWithFailure,
})
const variables = { id: 'A0000000000' }

expect(result.current({ variables })).rejects.toBe('Foobar')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ describe('useFetchRootOrgData', () => {
id: 'A0000000001',
path: '/A0000000001',
displayName: 'Org Unit 2',
}
},
})
})

it('should preserve the referential identity of the returned function', () => {
const { result, rerender } = renderHook(useFetchRootOrgData, { wrapper })
const { result, rerender } = renderHook(useFetchRootOrgData, {
wrapper,
})
const afterFirstRender = result.current

rerender()
Expand All @@ -77,7 +79,9 @@ describe('useFetchRootOrgData', () => {
</CustomDataProvider>
)

const { result } = renderHook(useFetchRootOrgData, { wrapper: wrapperWithFailure })
const { result } = renderHook(useFetchRootOrgData, {
wrapper: wrapperWithFailure,
})
const ids = ['A0000000000', 'A0000000001']

expect(result.current({ ids })).rejects.toBe('Foobar')
Expand Down

0 comments on commit bb23bc5

Please sign in to comment.