From ce3e1e5bfed7bda0f5cc38cc517898bbe74b69d7 Mon Sep 17 00:00:00 2001 From: Hui Zhao Date: Tue, 30 Jan 2024 17:59:02 -0800 Subject: [PATCH] fix(core): Amplify.configure dispatches Hub event with unparsed config object --- .../__tests__/singleton/Singleton.test.ts | 135 +++++++++++------- packages/core/src/singleton/Amplify.ts | 2 +- 2 files changed, 83 insertions(+), 54 deletions(-) diff --git a/packages/core/__tests__/singleton/Singleton.test.ts b/packages/core/__tests__/singleton/Singleton.test.ts index b79f338346e..c98e7fcd448 100644 --- a/packages/core/__tests__/singleton/Singleton.test.ts +++ b/packages/core/__tests__/singleton/Singleton.test.ts @@ -1,10 +1,21 @@ import { Amplify } from '../../src/singleton'; +import { Hub, AMPLIFY_SYMBOL } from '../../src/Hub'; import { AuthClass as Auth } from '../../src/singleton/Auth'; import { decodeJWT } from '../../src/singleton/Auth/utils'; import { CredentialsAndIdentityId } from '../../src/singleton/Auth/types'; import { TextEncoder, TextDecoder } from 'util'; import { fetchAuthSession, ResourcesConfig } from '../../src'; Object.assign(global, { TextDecoder, TextEncoder }); + +jest.mock('../../src/Hub', () => ({ + ...jest.requireActual('../../src/Hub'), + Hub: { + dispatch: jest.fn(), + }, +})); + +const mockHubDispatch = Hub.dispatch as jest.Mock; + type ArgumentTypes = F extends (...args: infer A) => any ? A : never; @@ -85,64 +96,82 @@ const modelIntrospection: ModelIntrospection = { }; describe('Amplify.configure() and Amplify.getConfig()', () => { - it('should take the legacy CLI shaped config object for configuring and return it from getConfig()', () => { - const mockLegacyConfig = { - aws_project_region: 'us-west-2', - aws_cognito_identity_pool_id: 'aws_cognito_identity_pool_id', - aws_cognito_region: 'aws_cognito_region', - aws_user_pools_id: 'aws_user_pools_id', - aws_user_pools_web_client_id: 'aws_user_pools_web_client_id', - oauth: {}, - aws_cognito_username_attributes: [], - aws_cognito_social_providers: [], - aws_cognito_signup_attributes: [], - aws_cognito_mfa_configuration: 'OFF', - aws_cognito_mfa_types: ['SMS'], - aws_cognito_password_protection_settings: { - passwordPolicyMinLength: 8, - passwordPolicyCharacters: [], + const mockLegacyConfig = { + aws_project_region: 'us-west-2', + aws_cognito_identity_pool_id: 'aws_cognito_identity_pool_id', + aws_cognito_region: 'aws_cognito_region', + aws_user_pools_id: 'aws_user_pools_id', + aws_user_pools_web_client_id: 'aws_user_pools_web_client_id', + oauth: {}, + aws_cognito_username_attributes: [], + aws_cognito_social_providers: [], + aws_cognito_signup_attributes: [], + aws_cognito_mfa_configuration: 'OFF', + aws_cognito_mfa_types: ['SMS'], + aws_cognito_password_protection_settings: { + passwordPolicyMinLength: 8, + passwordPolicyCharacters: [], + }, + aws_cognito_verification_mechanisms: ['PHONE_NUMBER'], + aws_appsync_graphqlEndpoint: 'https://some.domain.com/graphql', + aws_appsync_region: 'us-west-1', + aws_appsync_authenticationType: 'AMAZON_COGNITO_USER_POOLS', + aws_appsync_apiKey: 'some-key', + modelIntrospection, + }; + const expectedResourceConfig: ResourcesConfig = { + Auth: { + Cognito: { + allowGuestAccess: true, + identityPoolId: 'aws_cognito_identity_pool_id', + userPoolClientId: 'aws_user_pools_web_client_id', + userPoolId: 'aws_user_pools_id', + loginWith: { email: false, phone: false, username: true }, + mfa: { smsEnabled: true, status: 'off', totpEnabled: false }, + passwordFormat: { + minLength: 8, + requireLowercase: false, + requireNumbers: false, + requireSpecialCharacters: false, + requireUppercase: false, + }, + userAttributes: { phone_number: { required: true } }, }, - aws_cognito_verification_mechanisms: ['PHONE_NUMBER'], - aws_appsync_graphqlEndpoint: 'https://some.domain.com/graphql', - aws_appsync_region: 'us-west-1', - aws_appsync_authenticationType: 'AMAZON_COGNITO_USER_POOLS', - aws_appsync_apiKey: 'some-key', - modelIntrospection, - }; + }, + API: { + GraphQL: { + apiKey: 'some-key', + defaultAuthMode: 'userPool', + endpoint: 'https://some.domain.com/graphql', + region: 'us-west-1', + modelIntrospection, + }, + }, + }; + + afterEach(() => { + mockHubDispatch.mockClear(); + }); + it('should take the legacy CLI shaped config object for configuring and return it from getConfig()', () => { Amplify.configure(mockLegacyConfig); const result = Amplify.getConfig(); - const expected: ResourcesConfig = { - Auth: { - Cognito: { - allowGuestAccess: true, - identityPoolId: 'aws_cognito_identity_pool_id', - userPoolClientId: 'aws_user_pools_web_client_id', - userPoolId: 'aws_user_pools_id', - loginWith: { email: false, phone: false, username: true }, - mfa: { smsEnabled: true, status: 'off', totpEnabled: false }, - passwordFormat: { - minLength: 8, - requireLowercase: false, - requireNumbers: false, - requireSpecialCharacters: false, - requireUppercase: false, - }, - userAttributes: { phone_number: { required: true } }, - }, - }, - API: { - GraphQL: { - apiKey: 'some-key', - defaultAuthMode: 'userPool', - endpoint: 'https://some.domain.com/graphql', - region: 'us-west-1', - modelIntrospection, - }, - }, - }; - expect(result).toEqual(expected); + expect(result).toEqual(expectedResourceConfig); + }); + + it('dispatches hub event with parsed ResourceConfig from the legacy config', () => { + Amplify.configure(mockLegacyConfig); + + expect(mockHubDispatch).toHaveBeenCalledWith( + 'core', + { + event: 'configure', + data: expectedResourceConfig, + }, + 'Configure', + AMPLIFY_SYMBOL + ); }); it('should take the v6 shaped config object for configuring and return it from getConfig()', () => { diff --git a/packages/core/src/singleton/Amplify.ts b/packages/core/src/singleton/Amplify.ts index 1eaaae8cdd5..039134f0a17 100644 --- a/packages/core/src/singleton/Amplify.ts +++ b/packages/core/src/singleton/Amplify.ts @@ -72,7 +72,7 @@ export class AmplifyClass { 'core', { event: 'configure', - data: resourcesConfig, + data: this.resourcesConfig, }, 'Configure', AMPLIFY_SYMBOL