refactor(updated tests)

Co-authored-by: Sebastian Palmqvist <PalmN72@users.noreply.github.com>
This commit is contained in:
Lee 2023-10-17 11:16:25 +02:00
parent cbb62d04ed
commit 2c085e9877
1 changed files with 77 additions and 0 deletions

View File

@ -0,0 +1,77 @@
import React from 'react';
import {renderHook, act, waitFor} from '@testing-library/react';
import {ApiProvider} from './provider';
import {useEtjanstChildren} 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.getPersonalNumber.mockReturnValue('123');
api.getChildren.mockImplementation(
() =>
new Promise(res => {
setTimeout(() => res(response), 50);
}),
);
storage = createStorage(
{
'123_etjanst_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;
renderHook(() => useEtjanstChildren(), {wrapper});
await act(async () => {
api.isLoggedIn = false;
api.emitter.emit('logout');
});
const {result} = renderHook(() => useEtjanstChildren(), {wrapper});
await waitFor(() => {
expect(result.current.data).toHaveLength(0);
});
await act(async () => {
api.isLoggedIn = true;
api.emitter.emit('login');
});
const {result: result2} = renderHook(() => useEtjanstChildren(), {
wrapper,
});
await waitFor(async () => {
expect(result2.current.data).toHaveLength(1);
});
});
// });
});