From 51ed7b4ecb1a47b6a17635011169056bec518df9 Mon Sep 17 00:00:00 2001 From: Chris F <5827964+cshfang@users.noreply.github.com> Date: Fri, 10 Nov 2023 09:48:47 -0800 Subject: [PATCH] test: Update unit tests for initSingleton (#12553) --- .../__tests__/initSingleton.test.ts | 229 +++++++++++------- 1 file changed, 147 insertions(+), 82 deletions(-) diff --git a/packages/aws-amplify/__tests__/initSingleton.test.ts b/packages/aws-amplify/__tests__/initSingleton.test.ts index 56b1261314e..0ff21e68766 100644 --- a/packages/aws-amplify/__tests__/initSingleton.test.ts +++ b/packages/aws-amplify/__tests__/initSingleton.test.ts @@ -5,7 +5,6 @@ import { Amplify as AmplifySingleton, CookieStorage, ResourcesConfig, - TokenProvider, defaultStorage, } from '@aws-amplify/core'; import { @@ -48,12 +47,11 @@ const mockResourceConfig: ResourcesConfig = { }; describe('initSingleton (DefaultAmplify)', () => { + const mockCookieStorageInstance = {}; + beforeAll(() => { + MockCookieStorage.mockImplementation(() => mockCookieStorageInstance); + }); beforeEach(() => { - mockCognitoUserPoolsTokenProviderSetAuthConfig.mockReset(); - mockCognitoUserPoolsTokenProviderSetKeyValueStorage.mockReset(); - mockAmplifySingletonConfigure.mockReset(); - mockAmplifySingletonGetConfig.mockReset(); - mockAmplifySingletonConfigure.mockImplementation((_, libraryOptions) => { AmplifySingleton.libraryOptions = libraryOptions ?? AmplifySingleton.libraryOptions; @@ -62,6 +60,14 @@ describe('initSingleton (DefaultAmplify)', () => { AmplifySingleton.libraryOptions = {}; }); + afterEach(() => { + MockCookieStorage.mockClear(); + mockCognitoUserPoolsTokenProviderSetAuthConfig.mockReset(); + mockCognitoUserPoolsTokenProviderSetKeyValueStorage.mockReset(); + mockAmplifySingletonConfigure.mockReset(); + mockAmplifySingletonGetConfig.mockReset(); + }); + describe('DefaultAmplify.configure()', () => { it('should take the legacy CLI shaped config object for configuring the underlying Amplify Singleton', () => { const mockLegacyConfig = { @@ -124,18 +130,46 @@ describe('initSingleton (DefaultAmplify)', () => { ); }); + it('should just configure with the provided config and options when ResourcesConfig.Auth is not defined', () => { + const resourceConfig = { Storage: mockResourceConfig.Storage }; + const libraryOptions = {}; + Amplify.configure(resourceConfig, libraryOptions); + + expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith( + resourceConfig, + libraryOptions + ); + }); + describe('when ResourcesConfig.Auth is defined', () => { - describe('when libraryOptions.Auth is undefined', () => { - it('should invoke AmplifySingleton.configure with the default auth providers', () => { - Amplify.configure(mockResourceConfig); + it('should just configure with the provided config and options when libraryOptions.Auth is defined', () => { + const libraryOptions = { + Auth: { tokenProvider: { getTokens: jest.fn() } }, + }; + Amplify.configure(mockResourceConfig, libraryOptions); - expect( - mockCognitoUserPoolsTokenProviderSetAuthConfig - ).toHaveBeenCalledWith(mockResourceConfig.Auth); + expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith( + mockResourceConfig, + libraryOptions + ); + }); + + describe('when the singleton libraryOptions have not yet been configured with Auth', () => { + it('should configure with default auth providers and a new CookieStorage instance', () => { + const libraryOptions = { ssr: true }; + Amplify.configure(mockResourceConfig, libraryOptions); + expect(mockCognitoUserPoolsTokenProviderSetAuthConfig).toBeCalledWith( + mockResourceConfig.Auth + ); + expect(MockCookieStorage).toHaveBeenCalledWith({ sameSite: 'lax' }); + expect( + mockCognitoUserPoolsTokenProviderSetKeyValueStorage + ).toBeCalledWith(mockCookieStorageInstance); expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith( mockResourceConfig, { + ...libraryOptions, Auth: { tokenProvider: cognitoUserPoolsTokenProvider, credentialsProvider: cognitoCredentialsProvider, @@ -144,108 +178,139 @@ describe('initSingleton (DefaultAmplify)', () => { ); }); - it('should preserve the default auth providers when Amplify.configure is called again without custom auth provider', () => { - mockAmplifySingletonGetConfig.mockReturnValueOnce(mockResourceConfig); + it('should configure with default auth providers and defaultStorage', () => { + const libraryOptions = {}; + Amplify.configure(mockResourceConfig, libraryOptions); - Amplify.configure(mockResourceConfig); - const defaultAuthProvidersHaveBeenConfigured = - AmplifySingleton.libraryOptions; - - Amplify.configure({ - ...Amplify.getConfig(), - Analytics: { - Kinesis: { - region: 'us-west-2', - }, - }, - }); - expect(AmplifySingleton.libraryOptions).toStrictEqual( - defaultAuthProvidersHaveBeenConfigured + expect(mockCognitoUserPoolsTokenProviderSetAuthConfig).toBeCalledWith( + mockResourceConfig.Auth ); - - Amplify.configure({ - ...Amplify.getConfig(), - Analytics: { - Personalize: { - trackingId: 'f1b2d240-f7e7-416a-af88-759d7e258994', - region: 'us-west-2', + expect( + mockCognitoUserPoolsTokenProviderSetKeyValueStorage + ).toBeCalledWith(defaultStorage); + expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith( + mockResourceConfig, + { + ...libraryOptions, + Auth: { + tokenProvider: cognitoUserPoolsTokenProvider, + credentialsProvider: cognitoCredentialsProvider, }, - }, - }); - expect(AmplifySingleton.libraryOptions).toStrictEqual( - defaultAuthProvidersHaveBeenConfigured + } ); }); + }); - it('should invoke AmplifySingleton.configure with other provided library options', () => { - const libraryOptionsWithStorage = { - Storage: { - S3: { - defaultAccessLevel: 'private', - isObjectLockEnabled: true, - }, + describe('when the singleton libraryOptions have been previously configured with Auth', () => { + beforeEach(() => { + AmplifySingleton.libraryOptions = { + Auth: { + tokenProvider: cognitoUserPoolsTokenProvider, + credentialsProvider: cognitoCredentialsProvider, }, }; + }); - Amplify.configure(mockResourceConfig, { - Storage: { - S3: { - defaultAccessLevel: 'private', - isObjectLockEnabled: true, - }, - }, - }); + it('should preserve current auth providers (default or otherwise) and configure provider with a new CookieStorage instance', () => { + const libraryOptions = { ssr: true }; + Amplify.configure(mockResourceConfig, libraryOptions); + expect( + mockCognitoUserPoolsTokenProviderSetAuthConfig + ).not.toBeCalled(); + expect(MockCookieStorage).toHaveBeenCalledWith({ sameSite: 'lax' }); + expect( + mockCognitoUserPoolsTokenProviderSetKeyValueStorage + ).toBeCalledWith(mockCookieStorageInstance); expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith( mockResourceConfig, { - Auth: { - tokenProvider: cognitoUserPoolsTokenProvider, - credentialsProvider: cognitoCredentialsProvider, - }, - ...libraryOptionsWithStorage, + Auth: AmplifySingleton.libraryOptions.Auth, + ...libraryOptions, } ); }); - it('should use defaultStorage for the default CognitoUserPoolsTokenProvider', () => { - Amplify.configure(mockResourceConfig); + it('should preserve current auth providers (default or otherwise) and configure provider with defaultStorage', () => { + const libraryOptions = { ssr: false }; + Amplify.configure(mockResourceConfig, libraryOptions); + expect( + mockCognitoUserPoolsTokenProviderSetAuthConfig + ).not.toBeCalled(); expect( mockCognitoUserPoolsTokenProviderSetKeyValueStorage - ).toHaveBeenCalledWith(defaultStorage); + ).toBeCalledWith(defaultStorage); + expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith( + mockResourceConfig, + { + Auth: AmplifySingleton.libraryOptions.Auth, + ...libraryOptions, + } + ); }); - it('should use cookie storage if LibraryOptions.ssr is set to true for the default CognitoUserPoolsTokenProvider', () => { - Amplify.configure(mockResourceConfig, { ssr: true }); + it('should preserve current auth providers (default or otherwise)', () => { + const libraryOptions = { + Storage: { S3: { isObjectLockEnabled: true } }, + }; + Amplify.configure(mockResourceConfig, libraryOptions); - expect(MockCookieStorage).toHaveBeenCalledWith({ - sameSite: 'lax', - }); + expect( + mockCognitoUserPoolsTokenProviderSetAuthConfig + ).not.toBeCalled(); + expect(MockCookieStorage).not.toBeCalled(); expect( mockCognitoUserPoolsTokenProviderSetKeyValueStorage - ).toHaveBeenCalledTimes(1); + ).not.toBeCalled(); + expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith( + mockResourceConfig, + { + Auth: AmplifySingleton.libraryOptions.Auth, + ...libraryOptions, + } + ); }); - }); - describe('when libraryOptions.Auth is defined', () => { - it('should forward the libraryOptions to AmplifySingleton.configure', () => { - const mockTokenProvider: TokenProvider = { - getTokens: jest.fn(), - }; - const mockLibraryOptions = { - Auth: { - tokenProvider: mockTokenProvider, - }, - }; - Amplify.configure(mockResourceConfig, mockLibraryOptions); + it('should just configure without touching libraryOptions', () => { + Amplify.configure(mockResourceConfig); expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith( - mockResourceConfig, - mockLibraryOptions + mockResourceConfig ); }); }); + + it('should invoke AmplifySingleton.configure with other provided library options', () => { + const libraryOptionsWithStorage = { + Storage: { + S3: { + defaultAccessLevel: 'private', + isObjectLockEnabled: true, + }, + }, + }; + + Amplify.configure(mockResourceConfig, { + Storage: { + S3: { + defaultAccessLevel: 'private', + isObjectLockEnabled: true, + }, + }, + }); + + expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith( + mockResourceConfig, + { + Auth: { + tokenProvider: cognitoUserPoolsTokenProvider, + credentialsProvider: cognitoCredentialsProvider, + }, + ...libraryOptionsWithStorage, + } + ); + }); }); });