Skip to content

Commit

Permalink
fix: clear history before and updates tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Samaritan1011001 committed Feb 3, 2025
1 parent 7b97855 commit e9bbf47
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ describe('completeOAuthFlow', () => {
token_type: 'token_type',
expires_in: 'expires_in',
};
const executionOrder: string[] = [];

mockValidateState.mockReturnValueOnce('myState-valid_state');
(oAuthStore.loadPKCE as jest.Mock).mockResolvedValueOnce('pkce23234a');
const mockJsonMethod = jest.fn(() => Promise.resolve(expectedTokens));
Expand All @@ -162,6 +164,12 @@ describe('completeOAuthFlow', () => {
mockFetch.mockResolvedValueOnce({
json: mockJsonMethod,
});
mockReplaceState.mockImplementation((..._args) =>
executionOrder.push('replaceState'),
);
mockHubDispatch.mockImplementation(() =>
executionOrder.push('hubDispatch'),
);

await completeOAuthFlow(testInput);

Expand All @@ -180,17 +188,27 @@ describe('completeOAuthFlow', () => {
TokenType: expectedTokens.token_type,
ExpiresIn: expectedTokens.expires_in,
});

expect(oAuthStore.clearOAuthData).toHaveBeenCalledTimes(1);
expect(oAuthStore.storeOAuthSignIn).toHaveBeenCalledWith(true, undefined);

expect(mockResolveAndClearInflightPromises).toHaveBeenCalledTimes(1);

expect(mockReplaceState).toHaveBeenCalledWith(
'http://localhost:3000/?code=aaaa-111-222&state=aaaaa',
'',
testInput.redirectUri,
);

expect(oAuthStore.clearOAuthData).toHaveBeenCalledTimes(1);
expect(oAuthStore.storeOAuthSignIn).toHaveBeenCalledWith(true, undefined);

expect(mockHubDispatch).toHaveBeenCalledTimes(3);
expect(mockResolveAndClearInflightPromises).toHaveBeenCalledTimes(1);

// Verify we replace browser tab location before dispatching hub events
expect(executionOrder).toEqual([
'replaceState',
'hubDispatch',
'hubDispatch',
'hubDispatch',
]);
});

it('throws when `fetch` call resolves error', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ const completeFlow = async ({
// `fetchAuthSession` can be resolved
resolveAndClearInflightPromises();

// clear history before sending out final Hub events
clearHistory(redirectUri);

if (isCustomState(state)) {
Hub.dispatch(
'auth',
Expand All @@ -252,7 +255,6 @@ const completeFlow = async ({
}
Hub.dispatch('auth', { event: 'signInWithRedirect' }, 'Auth', AMPLIFY_SYMBOL);
await dispatchSignedInHubEvent();
clearHistory(redirectUri);
};

const isCustomState = (state: string): boolean => {
Expand Down

0 comments on commit e9bbf47

Please sign in to comment.