diff --git a/packages/auth/__tests__/auth-unit-test.ts b/packages/auth/__tests__/auth-unit-test.ts index ecafed17013..781910b2fa7 100644 --- a/packages/auth/__tests__/auth-unit-test.ts +++ b/packages/auth/__tests__/auth-unit-test.ts @@ -277,7 +277,7 @@ const createMockLocalStorage = () => removeItem(key: string) { delete this._items[key]; }, - } as unknown as Storage); + }) as unknown as Storage; import { AuthOptions, SignUpParams, AwsCognitoOAuthOpts } from '../src/types'; import { AuthClass as Auth } from '../src/Auth'; @@ -1407,38 +1407,38 @@ describe('auth unit test', () => { spyon.mockClear(); }); - test('currentUserPoolUser fails but hub event still dispatches', async () => { - const auth = new Auth(authOptions); - const spyon = jest - .spyOn(CognitoUser.prototype, 'sendMFACode') - .mockImplementationOnce((code, callback) => { - callback.onSuccess(session); - }); - - const spyon2 = jest - .spyOn(auth, 'currentUserPoolUser') - .mockImplementationOnce(() => { - return Promise.reject('Could not get current user.'); - }); - const hubSpy = jest.spyOn(Hub, 'dispatch'); - const user = new CognitoUser({ - Username: 'username', - Pool: userPool, - }); - const result = await auth.confirmSignIn(user, 'code', null); - expect(result).toEqual(user); - expect(hubSpy).toHaveBeenCalledWith( - 'auth', - { - data: user, - event: 'signIn', - message: 'A user username has been signed in', - }, - 'Auth', - Symbol.for('amplify_default') - ); - spyon.mockClear(); - }); + test('currentUserPoolUser fails but hub event still dispatches', async () => { + const auth = new Auth(authOptions); + const spyon = jest + .spyOn(CognitoUser.prototype, 'sendMFACode') + .mockImplementationOnce((code, callback) => { + callback.onSuccess(session); + }); + + const spyon2 = jest + .spyOn(auth, 'currentUserPoolUser') + .mockImplementationOnce(() => { + return Promise.reject('Could not get current user.'); + }); + const hubSpy = jest.spyOn(Hub, 'dispatch'); + const user = new CognitoUser({ + Username: 'username', + Pool: userPool, + }); + const result = await auth.confirmSignIn(user, 'code', null); + expect(result).toEqual(user); + expect(hubSpy).toHaveBeenCalledWith( + 'auth', + { + data: user, + event: 'signIn', + message: 'A user username has been signed in', + }, + 'Auth', + Symbol.for('amplify_default') + ); + spyon.mockClear(); + }); test('onFailure', async () => { const spyon = jest @@ -3058,12 +3058,13 @@ describe('auth unit test', () => { spyon.mockClear(); }); - test('error hub event', async (done) => { + test('error hub event', async done => { expect.assertions(3); - const spyon = jest.spyOn(CognitoUser.prototype, 'updateAttributes') + const spyon = jest + .spyOn(CognitoUser.prototype, 'updateAttributes') .mockImplementationOnce((attrs, callback: any) => { callback(new Error('Error'), null, null); - }); + }); const auth = new Auth(authOptions); @@ -3097,19 +3098,20 @@ describe('auth unit test', () => { spyon.mockClear(); }); - test('happy case code delivery details hub event', async (done) => { + test('happy case code delivery details hub event', async done => { expect.assertions(2); - + const codeDeliverDetailsResult: any = { - 'CodeDeliveryDetailsList': [ - { - 'AttributeName': 'email', - 'DeliveryMedium': 'EMAIL', - 'Destination': 'e***@e***' - } - ] + CodeDeliveryDetailsList: [ + { + AttributeName: 'email', + DeliveryMedium: 'EMAIL', + Destination: 'e***@e***', + }, + ], }; - const spyon = jest.spyOn(CognitoUser.prototype, 'updateAttributes') + const spyon = jest + .spyOn(CognitoUser.prototype, 'updateAttributes') .mockImplementationOnce((attrs, callback: any) => { callback(null, 'SUCCESS', codeDeliverDetailsResult); }); @@ -3126,20 +3128,20 @@ describe('auth unit test', () => { sub: 'sub', }; const payloadData = { - 'email': { + email: { isUpdated: false, codeDeliveryDetails: { AttributeName: 'email', DeliveryMedium: 'EMAIL', - Destination: 'e***@e***' - } + Destination: 'e***@e***', + }, }, - 'phone_number': { - isUpdated: true + phone_number: { + isUpdated: true, + }, + sub: { + isUpdated: true, }, - 'sub': { - isUpdated: true - } }; const listenToHub = Hub.listen('auth', ({ payload }) => { const { event } = payload; @@ -3588,8 +3590,8 @@ describe('auth unit test', () => { expect(handleAuthResponseSpy).toHaveBeenCalledWith(url); expect(replaceStateSpy).toHaveBeenCalledWith( - {}, - null, + window.history.state, + '', (options.oauth as AwsCognitoOAuthOpts).redirectSignIn ); @@ -3601,7 +3603,7 @@ describe('auth unit test', () => { "Hub.dispatch('auth', { data: ..., event: 'parsingCallbackUrl' })", ], Array [ - "window.history.replaceState({}, null, 'http://localhost:3000/')", + "window.history.replaceState(null, "", 'http://localhost:3000/')", ], Array [ "Hub.dispatch('auth', { data: ..., event: 'signIn' })", @@ -3653,8 +3655,8 @@ describe('auth unit test', () => { expect(handleAuthResponseSpy).toHaveBeenCalledWith(url); expect(replaceStateSpy).toHaveBeenCalledWith( - {}, - null, + window.history.state, + '', (options.oauth as AwsCognitoOAuthOpts).redirectSignIn ); }); @@ -3678,7 +3680,7 @@ describe('auth unit test', () => { ); }); - test('User Pools and Identity Pools', async () => { + test.only('User Pools and Identity Pools', async () => { const options: AuthOptions = { region: 'region', userPoolId: 'userPoolId', @@ -3717,8 +3719,8 @@ describe('auth unit test', () => { expect(handleAuthResponseSpy).toHaveBeenCalledWith(url); expect(replaceStateSpy).toHaveBeenCalledWith( - {}, - null, + window.history.state, + '', (options.oauth as AwsCognitoOAuthOpts).redirectSignIn ); }); diff --git a/packages/auth/src/Auth.ts b/packages/auth/src/Auth.ts index 685fff7eaf1..0d2b506232a 100644 --- a/packages/auth/src/Auth.ts +++ b/packages/auth/src/Auth.ts @@ -2552,8 +2552,8 @@ export class AuthClass { if (window && typeof window.history !== 'undefined') { window.history.replaceState( - {}, - null, + window.history.state, + '', (this._config.oauth as AwsCognitoOAuthOpts).redirectSignIn ); } @@ -2588,8 +2588,8 @@ export class AuthClass { // Otherwise, reloading the page will throw errors as the `code` has already been spent. if (window && typeof window.history !== 'undefined') { window.history.replaceState( - {}, - null, + window.history.state, + '', (this._config.oauth as AwsCognitoOAuthOpts).redirectSignIn ); }