Skip to content

Commit

Permalink
fix(aws-amplify): unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashwin Kumar committed Jan 5, 2024
1 parent dbd1b2e commit a33255d
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 15 deletions.
157 changes: 155 additions & 2 deletions packages/aws-amplify/__tests__/initSingleton.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ import {
CookieStorage,
ResourcesConfig,
defaultStorage,
LibraryOptions,
consoleProvider,
cloudWatchProvider,
} from '@aws-amplify/core';
import {
ConsoleConfig,
ConsoleProvider as ConsoleProviderType,
} from '@aws-amplify/core/src/logger/providers/console';
import { LogParams } from '@aws-amplify/core/src/logger/types';
import {
cognitoUserPoolsTokenProvider,
cognitoCredentialsProvider,
Expand Down Expand Up @@ -45,6 +53,11 @@ const mockResourceConfig: ResourcesConfig = {
},
},
};
const libraryLoggerOptions: LibraryOptions = {
Logger: {
console: consoleProvider,
},
};

describe('initSingleton (DefaultAmplify)', () => {
const mockCookieStorageInstance = {};
Expand Down Expand Up @@ -133,7 +146,7 @@ describe('initSingleton (DefaultAmplify)', () => {

expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith(
resourceConfig,
libraryOptions
{ ...libraryLoggerOptions, ...libraryOptions }
);
});

Expand All @@ -146,7 +159,7 @@ describe('initSingleton (DefaultAmplify)', () => {

expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith(
mockResourceConfig,
libraryOptions
{ ...libraryLoggerOptions, ...libraryOptions }
);
});

Expand All @@ -165,6 +178,7 @@ describe('initSingleton (DefaultAmplify)', () => {
expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith(
mockResourceConfig,
{
...libraryLoggerOptions,
...libraryOptions,
Auth: {
tokenProvider: cognitoUserPoolsTokenProvider,
Expand All @@ -187,6 +201,7 @@ describe('initSingleton (DefaultAmplify)', () => {
expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith(
mockResourceConfig,
{
...libraryLoggerOptions,
...libraryOptions,
Auth: {
tokenProvider: cognitoUserPoolsTokenProvider,
Expand All @@ -200,6 +215,7 @@ describe('initSingleton (DefaultAmplify)', () => {
describe('when the singleton libraryOptions have been previously configured with Auth', () => {
beforeEach(() => {
AmplifySingleton.libraryOptions = {
...libraryLoggerOptions,
Auth: {
tokenProvider: cognitoUserPoolsTokenProvider,
credentialsProvider: cognitoCredentialsProvider,
Expand All @@ -221,6 +237,7 @@ describe('initSingleton (DefaultAmplify)', () => {
expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith(
mockResourceConfig,
{
Logger: AmplifySingleton.libraryOptions.Logger,
Auth: AmplifySingleton.libraryOptions.Auth,
...libraryOptions,
}
Expand All @@ -240,6 +257,7 @@ describe('initSingleton (DefaultAmplify)', () => {
expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith(
mockResourceConfig,
{
Logger: AmplifySingleton.libraryOptions.Logger,
Auth: AmplifySingleton.libraryOptions.Auth,
...libraryOptions,
}
Expand All @@ -262,6 +280,7 @@ describe('initSingleton (DefaultAmplify)', () => {
expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith(
mockResourceConfig,
{
Logger: AmplifySingleton.libraryOptions.Logger,
Auth: AmplifySingleton.libraryOptions.Auth,
...libraryOptions,
}
Expand Down Expand Up @@ -304,10 +323,144 @@ describe('initSingleton (DefaultAmplify)', () => {
credentialsProvider: cognitoCredentialsProvider,
},
...libraryOptionsWithStorage,
...libraryLoggerOptions,
}
);
});
});

describe('when configuring logger providers', () => {
const resourceConfig = { Auth: mockResourceConfig.Auth };
describe('when the singleton libraryOptions has not yet been configured with logger', () => {
it('should configure default console provider', () => {
Amplify.configure(resourceConfig);

expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith(
resourceConfig,
{
Auth: AmplifySingleton.libraryOptions.Auth,
...libraryLoggerOptions,
}
);
});

it('should configure console provider', () => {
Amplify.configure(resourceConfig, {
Logger: {
console: consoleProvider,
},
});
expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith(
resourceConfig,
{
Auth: AmplifySingleton.libraryOptions.Auth,
Logger: {
console: consoleProvider,
},
}
);
});

it('should configure additional providers and default console provider', () => {
Amplify.configure(resourceConfig, {
Logger: {
additionalProviders: [cloudWatchProvider],
},
});

expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith(
resourceConfig,
{
Auth: AmplifySingleton.libraryOptions.Auth,
Logger: {
console: consoleProvider,
additionalProviders: [cloudWatchProvider],
},
}
);
});

it('should configure additional providers and console provider', () => {
Amplify.configure(resourceConfig, {
Logger: {
console: consoleProvider,
additionalProviders: [cloudWatchProvider],
},
});

expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith(
resourceConfig,
{
Auth: AmplifySingleton.libraryOptions.Auth,
Logger: {
console: consoleProvider,
additionalProviders: [cloudWatchProvider],
},
}
);
});
});

describe('when the singleton libraryOptions has been configured with Logger', () => {
beforeEach(() => {
AmplifySingleton.libraryOptions = {
Auth: {
tokenProvider: cognitoUserPoolsTokenProvider,
credentialsProvider: cognitoCredentialsProvider,
},
...libraryLoggerOptions,
};
});

it('should override configured console provider', () => {
const mockConsoleProvider: ConsoleProviderType = {
LOG_LEVEL: null,
initialize: function (): void {},
log: function (): void {},
flushLogs: function (): Promise<void> {
return Promise.resolve();
},
enable: function (): void {},
disable: function (): void {},
};

Amplify.configure(resourceConfig, {
Logger: {
console: mockConsoleProvider,
},
});

expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith(
resourceConfig,
{
Auth: AmplifySingleton.libraryOptions.Auth,
Logger: {
console: mockConsoleProvider,
},
}
);
});

it('should configure additional providers and preserve configured console provider', () => {
Amplify.configure(resourceConfig, {
Logger: {
additionalProviders: [cloudWatchProvider],
},
});

expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith(
resourceConfig,
{
Auth: AmplifySingleton.libraryOptions.Auth,
Logger: {
console: AmplifySingleton.libraryOptions.Logger?.console,
additionalProviders: [cloudWatchProvider],
},
}
);
});
});
});
});

describe('DefaultAmplify.getConfig()', () => {
Expand Down
27 changes: 14 additions & 13 deletions packages/aws-amplify/src/initSingleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ export const DefaultAmplify = {
libraryOptions?: LibraryOptions
): void {
let resolvedResourceConfig: ResourcesConfig;
let resolvedLibraryOptions = libraryOptions;

// add console logger provider by default
if (!Amplify.libraryOptions?.Logger) {
libraryOptions = {
resolvedLibraryOptions = {
...libraryOptions,
Logger: {
...libraryOptions?.Logger,
Expand All @@ -56,26 +57,26 @@ export const DefaultAmplify = {
// If no Auth config is provided, no special handling will be required, configure as is.
// Otherwise, we can assume an Auth config is provided from here on.
if (!resolvedResourceConfig.Auth) {
return Amplify.configure(resolvedResourceConfig, libraryOptions);
return Amplify.configure(resolvedResourceConfig, resolvedLibraryOptions);
}

// If Auth options are provided, always just configure as is.
// Otherwise, we can assume no Auth libraryOptions were provided from here on.
if (libraryOptions?.Auth) {
return Amplify.configure(resolvedResourceConfig, libraryOptions);
if (resolvedLibraryOptions?.Auth) {
return Amplify.configure(resolvedResourceConfig, resolvedLibraryOptions);
}

// If no Auth libraryOptions were previously configured, then always add default providers.
if (!Amplify.libraryOptions.Auth) {
cognitoUserPoolsTokenProvider.setAuthConfig(resolvedResourceConfig.Auth);
cognitoUserPoolsTokenProvider.setKeyValueStorage(
// TODO: allow configure with a public interface
libraryOptions?.ssr
resolvedLibraryOptions?.ssr
? new CookieStorage({ sameSite: 'lax' })
: defaultStorage
);
return Amplify.configure(resolvedResourceConfig, {
...libraryOptions,
...resolvedLibraryOptions,
Auth: {
tokenProvider: cognitoUserPoolsTokenProvider,
credentialsProvider: cognitoCredentialsProvider,
Expand All @@ -85,24 +86,24 @@ export const DefaultAmplify = {

// At this point, Auth libraryOptions would have been previously configured and no overriding
// Auth options were given, so we should preserve the currently configured Auth libraryOptions.
if (libraryOptions) {
if (resolvedLibraryOptions) {
// If ssr is provided through libraryOptions, we should respect the intentional reconfiguration.
if (libraryOptions.ssr !== undefined) {
if (resolvedLibraryOptions.ssr !== undefined) {
cognitoUserPoolsTokenProvider.setKeyValueStorage(
// TODO: allow configure with a public interface
libraryOptions.ssr
resolvedLibraryOptions.ssr
? new CookieStorage({ sameSite: 'lax' })
: defaultStorage
);
}
return Amplify.configure(resolvedResourceConfig, {
Auth: Amplify.libraryOptions.Auth,
...libraryOptions,
// console logger is always present
...resolvedLibraryOptions,
Logger: {
...libraryOptions?.Logger,
...resolvedLibraryOptions?.Logger,
// preserve console logger
console:
libraryOptions?.Logger?.console ??
resolvedLibraryOptions?.Logger?.console ??
Amplify.libraryOptions.Logger?.console,
},
});
Expand Down

0 comments on commit a33255d

Please sign in to comment.