|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +/** |
| 5 | + * Integration-style tests using the real @aws-amplify/core Amplify singleton |
| 6 | + * (initSingleton.test.ts mocks core). |
| 7 | + */ |
| 8 | +import { Amplify as CoreAmplify, ResourcesConfig } from '@aws-amplify/core'; |
| 9 | + |
| 10 | +import { cognitoUserPoolsTokenProvider } from '../src/auth/cognito'; |
| 11 | +import { Amplify as DefaultAmplify } from '../src'; |
| 12 | + |
| 13 | +const poolAConfig: ResourcesConfig = { |
| 14 | + Auth: { |
| 15 | + Cognito: { |
| 16 | + userPoolClientId: 'client-a', |
| 17 | + userPoolId: 'pool-a', |
| 18 | + }, |
| 19 | + }, |
| 20 | +}; |
| 21 | + |
| 22 | +const poolBConfig: ResourcesConfig = { |
| 23 | + Auth: { |
| 24 | + Cognito: { |
| 25 | + userPoolClientId: 'client-b', |
| 26 | + userPoolId: 'pool-b', |
| 27 | + }, |
| 28 | + }, |
| 29 | +}; |
| 30 | + |
| 31 | +const storageLibraryOptions = { |
| 32 | + Storage: { |
| 33 | + S3: { |
| 34 | + defaultAccessLevel: 'private' as const, |
| 35 | + isObjectLockEnabled: true, |
| 36 | + }, |
| 37 | + }, |
| 38 | +}; |
| 39 | + |
| 40 | +describe('DefaultAmplify.configure integration', () => { |
| 41 | + let setAuthConfigSpy: jest.SpyInstance; |
| 42 | + let setKeyValueStorageSpy: jest.SpyInstance; |
| 43 | + |
| 44 | + beforeEach(() => { |
| 45 | + CoreAmplify.libraryOptions = {}; |
| 46 | + CoreAmplify.resourcesConfig = {}; |
| 47 | + setAuthConfigSpy = jest.spyOn( |
| 48 | + cognitoUserPoolsTokenProvider, |
| 49 | + 'setAuthConfig', |
| 50 | + ); |
| 51 | + setKeyValueStorageSpy = jest.spyOn( |
| 52 | + cognitoUserPoolsTokenProvider, |
| 53 | + 'setKeyValueStorage', |
| 54 | + ); |
| 55 | + }); |
| 56 | + |
| 57 | + afterEach(() => { |
| 58 | + setAuthConfigSpy.mockRestore(); |
| 59 | + setKeyValueStorageSpy.mockRestore(); |
| 60 | + CoreAmplify.libraryOptions = {}; |
| 61 | + CoreAmplify.resourcesConfig = {}; |
| 62 | + }); |
| 63 | + |
| 64 | + it('keeps Storage and refreshes Cognito auth config on partial reconfigure', () => { |
| 65 | + DefaultAmplify.configure(poolAConfig, storageLibraryOptions); |
| 66 | + |
| 67 | + expect(CoreAmplify.libraryOptions.Storage).toEqual( |
| 68 | + storageLibraryOptions.Storage, |
| 69 | + ); |
| 70 | + expect(CoreAmplify.libraryOptions.Auth?.tokenProvider).toBe( |
| 71 | + cognitoUserPoolsTokenProvider, |
| 72 | + ); |
| 73 | + expect(setAuthConfigSpy).toHaveBeenCalledWith(poolAConfig.Auth); |
| 74 | + |
| 75 | + setAuthConfigSpy.mockClear(); |
| 76 | + |
| 77 | + DefaultAmplify.configure(poolBConfig, { ssr: false }); |
| 78 | + |
| 79 | + expect(CoreAmplify.libraryOptions.Storage).toEqual( |
| 80 | + storageLibraryOptions.Storage, |
| 81 | + ); |
| 82 | + expect(CoreAmplify.getConfig().Auth?.Cognito?.userPoolClientId).toBe( |
| 83 | + 'client-b', |
| 84 | + ); |
| 85 | + expect(setAuthConfigSpy).toHaveBeenCalledWith(poolBConfig.Auth); |
| 86 | + }); |
| 87 | + |
| 88 | + it('merges prior libraryOptions when libraryOptions.Auth overrides default provider', () => { |
| 89 | + DefaultAmplify.configure(poolAConfig, storageLibraryOptions); |
| 90 | + |
| 91 | + setAuthConfigSpy.mockClear(); |
| 92 | + |
| 93 | + DefaultAmplify.configure(poolBConfig, { |
| 94 | + Auth: { |
| 95 | + tokenProvider: cognitoUserPoolsTokenProvider, |
| 96 | + credentialsProvider: |
| 97 | + CoreAmplify.libraryOptions.Auth!.credentialsProvider!, |
| 98 | + }, |
| 99 | + }); |
| 100 | + |
| 101 | + expect(CoreAmplify.libraryOptions.Storage).toEqual( |
| 102 | + storageLibraryOptions.Storage, |
| 103 | + ); |
| 104 | + expect(setAuthConfigSpy).toHaveBeenCalledWith(poolBConfig.Auth); |
| 105 | + expect(CoreAmplify.getConfig().Auth?.Cognito?.userPoolClientId).toBe( |
| 106 | + 'client-b', |
| 107 | + ); |
| 108 | + }); |
| 109 | + |
| 110 | + it('syncs default Cognito auth config when only resource config is passed', () => { |
| 111 | + DefaultAmplify.configure(poolAConfig, storageLibraryOptions); |
| 112 | + |
| 113 | + setAuthConfigSpy.mockClear(); |
| 114 | + |
| 115 | + DefaultAmplify.configure(poolBConfig); |
| 116 | + |
| 117 | + expect(CoreAmplify.libraryOptions.Storage).toEqual( |
| 118 | + storageLibraryOptions.Storage, |
| 119 | + ); |
| 120 | + expect(setAuthConfigSpy).toHaveBeenCalledWith(poolBConfig.Auth); |
| 121 | + expect(CoreAmplify.getConfig().Auth?.Cognito?.userPoolId).toBe('pool-b'); |
| 122 | + }); |
| 123 | +}); |
0 commit comments