Skip to content

Commit

Permalink
test: Update unit tests for initSingleton (#12553)
Browse files Browse the repository at this point in the history
  • Loading branch information
cshfang authored Nov 10, 2023
1 parent 17ffbdd commit 51ed7b4
Showing 1 changed file with 147 additions and 82 deletions.
229 changes: 147 additions & 82 deletions packages/aws-amplify/__tests__/initSingleton.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
Amplify as AmplifySingleton,
CookieStorage,
ResourcesConfig,
TokenProvider,
defaultStorage,
} from '@aws-amplify/core';
import {
Expand Down Expand Up @@ -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;
Expand All @@ -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 = {
Expand Down Expand Up @@ -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,
Expand All @@ -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,
}
);
});
});
});

Expand Down

0 comments on commit 51ed7b4

Please sign in to comment.