From ae10b406472c05505fd48170607b9f4f4d0b490e Mon Sep 17 00:00:00 2001 From: David Lyon Date: Fri, 3 May 2024 10:39:49 -0700 Subject: [PATCH] test cleanup --- src/features/auth/authSlice.test.tsx | 161 ++++++++++++++------------- 1 file changed, 84 insertions(+), 77 deletions(-) diff --git a/src/features/auth/authSlice.test.tsx b/src/features/auth/authSlice.test.tsx index e7ba1b1a..98d35352 100644 --- a/src/features/auth/authSlice.test.tsx +++ b/src/features/auth/authSlice.test.tsx @@ -14,97 +14,104 @@ describe('authSlice', () => { testStore = createTestStore({}); }); - test('useTryAuthFromToken sets auth token and username when successful', async () => { - const mock = jest.spyOn(authFromToken, 'useQuery'); - mock.mockImplementation(() => { - return { - isSuccess: true, - data: { user: 'someUser' }, - } as unknown as ReturnType; // Assert mocked response type - }); + describe('useTryAuthFromToken', () => { + let authMock = jest.spyOn(authFromToken, 'useQuery'); + let token: string | undefined; const Component = () => { - useTryAuthFromToken('some token'); + useTryAuthFromToken(token); return <>; }; - render( - - - - ); - await waitFor(() => { - // token gets normalized to uppercase - expect(testStore.getState().auth.token).toBe('SOME TOKEN'); - expect(testStore.getState().auth.username).toBe('someUser'); - expect(testStore.getState().auth.initialized).toBe(true); + + beforeEach(() => { + authMock = jest.spyOn(authFromToken, 'useQuery'); + token = undefined; }); - mock.mockClear(); - }); - test('useTryAuthFromToken fails quietly with invalid token, does not initialize state', async () => { - const mock = jest.spyOn(authFromToken, 'useQuery'); - mock.mockImplementation(() => { - return { - isSuccess: false, - isError: true, - } as unknown as ReturnType; // Assert mocked response type + afterEach(() => { + authMock.mockClear(); }); - const Component = () => { - useTryAuthFromToken('some token'); - return <>; - }; - render( - - - - ); - await waitFor(() => { - expect(testStore.getState().auth.token).toBe(undefined); - expect(testStore.getState().auth.username).toBe(undefined); - expect(testStore.getState().auth.initialized).toBe(false); + + test('sets auth token and username when successful', async () => { + token = 'some token'; + authMock.mockImplementation(() => { + return { + isSuccess: true, + data: { user: 'someUser' }, + } as unknown as ReturnType; // Assert mocked response type + }); + + render( + + + + ); + + await waitFor(() => { + // token gets normalized to uppercase + expect(testStore.getState().auth.token).toBe('SOME TOKEN'); + expect(testStore.getState().auth.username).toBe('someUser'); + expect(testStore.getState().auth.initialized).toBe(true); + }); + }); + + test('fails quietly with invalid token, does not initialize state', async () => { + token = 'some token'; + authMock.mockImplementation(() => { + return { + isSuccess: false, + isError: true, + } as unknown as ReturnType; // Assert mocked response type + }); + render( + + + + ); + await waitFor(() => { + expect(testStore.getState().auth.token).toBe(undefined); + expect(testStore.getState().auth.username).toBe(undefined); + expect(testStore.getState().auth.initialized).toBe(false); + }); }); - mock.mockClear(); }); - test('Auth token gets removed from state if revoked', async () => { - const testStore = createTestStore({ - auth: { - username: 'someUser', - token: 'foo', - // eslint-disable-next-line @typescript-eslint/no-explicit-any - tokenInfo: { id: 'existing-tokenid' } as TokenInfo, - initialized: true, - }, + describe('revokeToken', () => { + beforeEach(() => { + testStore = createTestStore({ + auth: { + username: 'someUser', + token: 'foo', + // eslint-disable-next-line @typescript-eslint/no-explicit-any + tokenInfo: { id: 'existing-tokenid' } as TokenInfo, + initialized: true, + }, + }); + fetchMock.enableMocks(); }); - fetchMock.enableMocks(); - fetchMock.mockOnce(''); // force the next call to succeed - await testStore.dispatch(revokeToken.initiate('existing-tokenid')); - await waitFor(() => { - expect(testStore.getState().auth.token).toBe(undefined); - expect(testStore.getState().auth.username).toBe(undefined); - expect(testStore.getState().auth.initialized).toBe(true); + + afterEach(() => { + fetchMock.disableMocks(); }); - fetchMock.disableMocks(); - }); - test('Auth token remains if other token revoked', async () => { - const testStore = createTestStore({ - auth: { - username: 'someUser', - token: 'foo', - // eslint-disable-next-line @typescript-eslint/no-explicit-any - tokenInfo: { id: 'existing-tokenid' } as TokenInfo, - initialized: true, - }, + test('Auth token gets removed from state if identical token is revoked', async () => { + fetchMock.mockOnce(''); // force the revokeToken call to succeed + await testStore.dispatch(revokeToken.initiate('existing-tokenid')); + await waitFor(() => { + expect(testStore.getState().auth.token).toBe(undefined); + expect(testStore.getState().auth.username).toBe(undefined); + expect(testStore.getState().auth.initialized).toBe(true); + }); }); - fetchMock.enableMocks(); - fetchMock.mockOnce(''); // force the next call to succeed - await testStore.dispatch(revokeToken.initiate('other-tokenid')); - await waitFor(() => { - expect(testStore.getState().auth.token).toBe('foo'); - expect(testStore.getState().auth.username).toBe('someUser'); - expect(testStore.getState().auth.initialized).toBe(true); + + test('Auth token remains in state if some other token is revoked', async () => { + fetchMock.mockOnce(''); // force the revokeToken call to succeed + await testStore.dispatch(revokeToken.initiate('other-tokenid')); + await waitFor(() => { + expect(testStore.getState().auth.token).toBe('foo'); + expect(testStore.getState().auth.username).toBe('someUser'); + expect(testStore.getState().auth.initialized).toBe(true); + }); }); - fetchMock.disableMocks(); }); describe('useTokenCookie', () => {