-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ed5df2
commit 644cbcd
Showing
4 changed files
with
84 additions
and
5 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,72 @@ | ||
import React from 'react' | ||
import { renderHook, act } from '@testing-library/react-hooks' | ||
import { ApiProvider } from './provider' | ||
import { useChildList } from './hooks' | ||
import store from './store' | ||
import init from './__mocks__/@skolplattformen/embedded-api' | ||
import createStorage from './__mocks__/AsyncStorage' | ||
import reporter from './__mocks__/reporter' | ||
|
||
const pause = (ms = 0) => new Promise((r) => setTimeout(r, ms)) | ||
|
||
describe('logout - cleanup', () => { | ||
let api | ||
let storage | ||
let response | ||
const wrapper = ({ children }) => ( | ||
<ApiProvider | ||
api={api} | ||
storage={storage} | ||
reporter={reporter} | ||
> | ||
{children} | ||
</ApiProvider> | ||
) | ||
beforeEach(() => { | ||
response = [{ id: 1 }] | ||
api = init() | ||
api.getChildren.mockImplementation(() => ( | ||
new Promise((res) => { | ||
setTimeout(() => res(response), 50) | ||
}) | ||
)) | ||
storage = createStorage({ | ||
children: [{ id: 2 }], | ||
}, 2) | ||
}) | ||
afterEach(async () => { | ||
await act(async () => { | ||
await pause(70) | ||
store.dispatch({ entity: 'ALL', type: 'CLEAR' }) | ||
}) | ||
}) | ||
it('cleans up on logout', async () => { | ||
await act(async () => { | ||
api.isLoggedIn = true | ||
api.isFake = false | ||
|
||
const { waitForNextUpdate: wait1 } = renderHook(() => useChildList(), { wrapper }) | ||
|
||
await wait1() | ||
await wait1() | ||
await wait1() | ||
await pause(20) | ||
|
||
api.isLoggedIn = false | ||
api.emitter.emit('logout') | ||
|
||
const { result } = renderHook(() => useChildList(), { wrapper }) | ||
|
||
expect(result.current.data).toHaveLength(0) | ||
|
||
api.isLoggedIn = true | ||
api.emitter.emit('login') | ||
|
||
const { result: result2, waitForNextUpdate: wait2 } = renderHook(() => useChildList(), { wrapper }) | ||
|
||
await wait2() | ||
|
||
expect(result2.current.data).toHaveLength(1) | ||
}) | ||
}) | ||
}) |
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