Skip to content

Commit a43a1f7

Browse files
committed
fix(aws-amplify): add aws-jwt-verify dep, fix tests for V7 AmplifyContext
1 parent 353d64b commit a43a1f7

File tree

3 files changed

+36
-512
lines changed

3 files changed

+36
-512
lines changed
Lines changed: 33 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,71 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
import {
5-
createAmplifyServerContext,
6-
destroyAmplifyServerContext,
7-
} from '@aws-amplify/core/internals/adapter-core';
8-
94
import { runWithAmplifyServerContext } from '../../src/adapter-core';
5+
import { configure } from '../../src/configure';
6+
7+
jest.mock('../../src/configure');
8+
const mockConfigure = configure as jest.Mock;
109

11-
// mock serverContext
12-
jest.mock('@aws-amplify/core/internals/adapter-core');
13-
const mockCreateAmplifyServerContext = createAmplifyServerContext as jest.Mock;
14-
const mockDestroyAmplifyServerContext =
15-
destroyAmplifyServerContext as jest.Mock;
1610
const mockAmplifyConfig = {};
17-
const mockTokenProvider = {
18-
getTokens: jest.fn(),
19-
};
20-
const mockCredentialAndIdentityProvider = {
21-
getCredentialsAndIdentityId: jest.fn(),
22-
clearCredentialsAndIdentityId: jest.fn(),
23-
};
24-
const mockContextSpec = {
25-
token: { value: Symbol('AmplifyServerContextToken') },
11+
const mockLibraryOptions = {
12+
Auth: {
13+
tokenProvider: { getTokens: jest.fn() },
14+
credentialsProvider: {
15+
getCredentialsAndIdentityId: jest.fn(),
16+
clearCredentialsAndIdentityId: jest.fn(),
17+
},
18+
},
2619
};
2720

2821
describe('runWithAmplifyServerContext', () => {
22+
const mockCtx = { resourcesConfig: {}, libraryOptions: {} };
23+
2924
beforeEach(() => {
30-
mockCreateAmplifyServerContext.mockReturnValueOnce(mockContextSpec);
25+
mockConfigure.mockReturnValue(mockCtx);
3126
});
3227

3328
afterEach(() => {
34-
mockDestroyAmplifyServerContext.mockReset();
29+
mockConfigure.mockReset();
3530
});
3631

37-
it('should run the operation with the context', () => {
32+
it('should call configure and pass the context to the operation', async () => {
3833
const mockOperation = jest.fn();
39-
runWithAmplifyServerContext(
34+
await runWithAmplifyServerContext(
4035
mockAmplifyConfig,
41-
{
42-
Auth: {
43-
tokenProvider: mockTokenProvider,
44-
credentialsProvider: mockCredentialAndIdentityProvider,
45-
},
46-
},
36+
mockLibraryOptions,
4737
mockOperation,
4838
);
4939

50-
expect(mockOperation).toHaveBeenCalledWith(mockContextSpec);
40+
expect(mockConfigure).toHaveBeenCalledWith(
41+
mockAmplifyConfig,
42+
mockLibraryOptions,
43+
);
44+
expect(mockOperation).toHaveBeenCalledWith(mockCtx);
5145
});
5246

53-
it('should destroy the context after the operation completed', async () => {
54-
const mockOperation = jest.fn();
55-
await runWithAmplifyServerContext(
47+
it('should return the result from the operation', async () => {
48+
const mockResult = { url: 'http://123.com' };
49+
const mockOperation = jest.fn(() => Promise.resolve(mockResult));
50+
const result = await runWithAmplifyServerContext(
5651
mockAmplifyConfig,
57-
{
58-
Auth: {
59-
tokenProvider: mockTokenProvider,
60-
credentialsProvider: mockCredentialAndIdentityProvider,
61-
},
62-
},
52+
mockLibraryOptions,
6353
mockOperation,
6454
);
6555

66-
expect(mockDestroyAmplifyServerContext).toHaveBeenCalledWith(
67-
mockContextSpec,
68-
);
56+
expect(result).toStrictEqual(mockResult);
6957
});
7058

71-
it('should destroy the context when the operation throws', async () => {
59+
it('should propagate errors from the operation', async () => {
7260
const testError = new Error('some error');
73-
const mockOperation = jest.fn();
74-
mockOperation.mockRejectedValueOnce(testError);
61+
const mockOperation = jest.fn().mockRejectedValueOnce(testError);
7562

7663
await expect(
7764
runWithAmplifyServerContext(
7865
mockAmplifyConfig,
79-
{
80-
Auth: {
81-
tokenProvider: mockTokenProvider,
82-
credentialsProvider: mockCredentialAndIdentityProvider,
83-
},
84-
},
66+
mockLibraryOptions,
8567
mockOperation,
8668
),
8769
).rejects.toThrow(testError);
88-
89-
expect(mockDestroyAmplifyServerContext).toHaveBeenCalledWith(
90-
mockContextSpec,
91-
);
92-
});
93-
94-
it('should return the result returned by the operation callback function', async () => {
95-
const mockResultValue = {
96-
url: 'http://123.com',
97-
};
98-
const mockOperation = jest.fn(() => Promise.resolve(mockResultValue));
99-
const result = await runWithAmplifyServerContext(
100-
mockAmplifyConfig,
101-
{
102-
Auth: {
103-
tokenProvider: mockTokenProvider,
104-
credentialsProvider: mockCredentialAndIdentityProvider,
105-
},
106-
},
107-
mockOperation,
108-
);
109-
110-
expect(result).toStrictEqual(mockResultValue);
11170
});
11271
});

packages/aws-amplify/__tests__/exports.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ import * as storageS3Exports from '../src/storage/s3';
2424
describe('aws-amplify Exports', () => {
2525
describe('Top-level exports', () => {
2626
it('should only export expected symbols', () => {
27-
expect(Object.keys(topLevelExports).sort()).toEqual(['Amplify'].sort());
27+
expect(Object.keys(topLevelExports).sort()).toEqual(
28+
['configure', 'createConfigurationBuilder'].sort(),
29+
);
2830
});
2931
});
3032

@@ -178,7 +180,6 @@ describe('aws-amplify Exports', () => {
178180
'forgetDevice',
179181
'fetchDevices',
180182
'autoSignIn',
181-
'fetchAuthSession',
182183
'decodeJWT',
183184
'associateWebAuthnCredential',
184185
'listWebAuthnCredentials',

0 commit comments

Comments
 (0)