forked from aws-amplify/amplify-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitSingleton.integration.test.ts
More file actions
103 lines (83 loc) · 2.71 KB
/
Copy pathinitSingleton.integration.test.ts
File metadata and controls
103 lines (83 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
/**
* Integration-style tests using the real @aws-amplify/core Amplify singleton
* (initSingleton.test.ts mocks core).
*/
import { Amplify as CoreAmplify, ResourcesConfig } from '@aws-amplify/core';
import { cognitoUserPoolsTokenProvider } from '../src/auth/cognito';
import { Amplify as DefaultAmplify } from '../src';
const poolAConfig: ResourcesConfig = {
Auth: {
Cognito: {
userPoolClientId: 'client-a',
userPoolId: 'pool-a',
},
},
};
const poolBConfig: ResourcesConfig = {
Auth: {
Cognito: {
userPoolClientId: 'client-b',
userPoolId: 'pool-b',
},
},
};
describe('DefaultAmplify.configure integration', () => {
let setAuthConfigSpy: jest.SpyInstance;
let setKeyValueStorageSpy: jest.SpyInstance;
beforeEach(() => {
CoreAmplify.libraryOptions = {};
CoreAmplify.resourcesConfig = {};
setAuthConfigSpy = jest.spyOn(
cognitoUserPoolsTokenProvider,
'setAuthConfig',
);
setKeyValueStorageSpy = jest.spyOn(
cognitoUserPoolsTokenProvider,
'setKeyValueStorage',
);
});
afterEach(() => {
setAuthConfigSpy.mockRestore();
setKeyValueStorageSpy.mockRestore();
CoreAmplify.libraryOptions = {};
CoreAmplify.resourcesConfig = {};
});
it('refreshes Cognito auth config on reconfigure with partial libraryOptions', () => {
DefaultAmplify.configure(poolAConfig);
expect(setAuthConfigSpy).toHaveBeenCalledWith(poolAConfig.Auth);
setAuthConfigSpy.mockClear();
setKeyValueStorageSpy.mockClear();
DefaultAmplify.configure(poolBConfig, { ssr: false });
expect(setAuthConfigSpy).toHaveBeenCalledWith(poolBConfig.Auth);
expect(setKeyValueStorageSpy).toHaveBeenCalled();
expect(CoreAmplify.getConfig().Auth?.Cognito?.userPoolClientId).toBe(
'client-b',
);
});
it('passes through when libraryOptions.Auth is provided', () => {
DefaultAmplify.configure(poolAConfig);
setAuthConfigSpy.mockClear();
DefaultAmplify.configure(poolBConfig, {
Auth: {
tokenProvider: cognitoUserPoolsTokenProvider,
credentialsProvider:
CoreAmplify.libraryOptions.Auth!.credentialsProvider!,
},
});
expect(setAuthConfigSpy).not.toHaveBeenCalled();
expect(CoreAmplify.getConfig().Auth?.Cognito?.userPoolClientId).toBe(
'client-b',
);
});
it('refreshes Cognito auth config when only resource config is passed', () => {
DefaultAmplify.configure(poolAConfig);
setAuthConfigSpy.mockClear();
setKeyValueStorageSpy.mockClear();
DefaultAmplify.configure(poolBConfig);
expect(setAuthConfigSpy).toHaveBeenCalledWith(poolBConfig.Auth);
expect(setKeyValueStorageSpy).toHaveBeenCalled();
expect(CoreAmplify.getConfig().Auth?.Cognito?.userPoolId).toBe('pool-b');
});
});