Skip to content

Commit

Permalink
test cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dauglyon committed May 16, 2024
1 parent 662214f commit ae10b40
Showing 1 changed file with 84 additions and 77 deletions.
161 changes: 84 additions & 77 deletions src/features/auth/authSlice.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof authFromToken['useQuery']>; // 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(
<Provider store={testStore}>
<Component />
</Provider>
);
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<typeof authFromToken['useQuery']>; // Assert mocked response type
afterEach(() => {
authMock.mockClear();
});
const Component = () => {
useTryAuthFromToken('some token');
return <></>;
};
render(
<Provider store={testStore}>
<Component />
</Provider>
);
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<typeof authFromToken['useQuery']>; // Assert mocked response type
});

render(
<Provider store={testStore}>
<Component />
</Provider>
);

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<typeof authFromToken['useQuery']>; // Assert mocked response type
});
render(
<Provider store={testStore}>
<Component />
</Provider>
);
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', () => {
Expand Down

0 comments on commit ae10b40

Please sign in to comment.