|
1 | 1 | // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
4 | | -import { |
5 | | - createAmplifyServerContext, |
6 | | - destroyAmplifyServerContext, |
7 | | -} from '@aws-amplify/core/internals/adapter-core'; |
8 | | - |
9 | 4 | 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; |
10 | 9 |
|
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; |
16 | 10 | 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 | + }, |
26 | 19 | }; |
27 | 20 |
|
28 | 21 | describe('runWithAmplifyServerContext', () => { |
| 22 | + const mockCtx = { resourcesConfig: {}, libraryOptions: {} }; |
| 23 | + |
29 | 24 | beforeEach(() => { |
30 | | - mockCreateAmplifyServerContext.mockReturnValueOnce(mockContextSpec); |
| 25 | + mockConfigure.mockReturnValue(mockCtx); |
31 | 26 | }); |
32 | 27 |
|
33 | 28 | afterEach(() => { |
34 | | - mockDestroyAmplifyServerContext.mockReset(); |
| 29 | + mockConfigure.mockReset(); |
35 | 30 | }); |
36 | 31 |
|
37 | | - it('should run the operation with the context', () => { |
| 32 | + it('should call configure and pass the context to the operation', async () => { |
38 | 33 | const mockOperation = jest.fn(); |
39 | | - runWithAmplifyServerContext( |
| 34 | + await runWithAmplifyServerContext( |
40 | 35 | mockAmplifyConfig, |
41 | | - { |
42 | | - Auth: { |
43 | | - tokenProvider: mockTokenProvider, |
44 | | - credentialsProvider: mockCredentialAndIdentityProvider, |
45 | | - }, |
46 | | - }, |
| 36 | + mockLibraryOptions, |
47 | 37 | mockOperation, |
48 | 38 | ); |
49 | 39 |
|
50 | | - expect(mockOperation).toHaveBeenCalledWith(mockContextSpec); |
| 40 | + expect(mockConfigure).toHaveBeenCalledWith( |
| 41 | + mockAmplifyConfig, |
| 42 | + mockLibraryOptions, |
| 43 | + ); |
| 44 | + expect(mockOperation).toHaveBeenCalledWith(mockCtx); |
51 | 45 | }); |
52 | 46 |
|
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( |
56 | 51 | mockAmplifyConfig, |
57 | | - { |
58 | | - Auth: { |
59 | | - tokenProvider: mockTokenProvider, |
60 | | - credentialsProvider: mockCredentialAndIdentityProvider, |
61 | | - }, |
62 | | - }, |
| 52 | + mockLibraryOptions, |
63 | 53 | mockOperation, |
64 | 54 | ); |
65 | 55 |
|
66 | | - expect(mockDestroyAmplifyServerContext).toHaveBeenCalledWith( |
67 | | - mockContextSpec, |
68 | | - ); |
| 56 | + expect(result).toStrictEqual(mockResult); |
69 | 57 | }); |
70 | 58 |
|
71 | | - it('should destroy the context when the operation throws', async () => { |
| 59 | + it('should propagate errors from the operation', async () => { |
72 | 60 | const testError = new Error('some error'); |
73 | | - const mockOperation = jest.fn(); |
74 | | - mockOperation.mockRejectedValueOnce(testError); |
| 61 | + const mockOperation = jest.fn().mockRejectedValueOnce(testError); |
75 | 62 |
|
76 | 63 | await expect( |
77 | 64 | runWithAmplifyServerContext( |
78 | 65 | mockAmplifyConfig, |
79 | | - { |
80 | | - Auth: { |
81 | | - tokenProvider: mockTokenProvider, |
82 | | - credentialsProvider: mockCredentialAndIdentityProvider, |
83 | | - }, |
84 | | - }, |
| 66 | + mockLibraryOptions, |
85 | 67 | mockOperation, |
86 | 68 | ), |
87 | 69 | ).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); |
111 | 70 | }); |
112 | 71 | }); |
0 commit comments