Skip to content

Commit 7d1ab15

Browse files
committed
test(aws-amplify): fix lint errors and add coverage tests
1 parent c902138 commit 7d1ab15

7 files changed

Lines changed: 248 additions & 503 deletions

File tree

Lines changed: 3 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,12 @@
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';
105

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-
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') },
26-
};
27-
286
describe('runWithAmplifyServerContext', () => {
29-
beforeEach(() => {
30-
mockCreateAmplifyServerContext.mockReturnValueOnce(mockContextSpec);
31-
});
32-
33-
afterEach(() => {
34-
mockDestroyAmplifyServerContext.mockReset();
35-
});
36-
37-
it('should run the operation with the context', () => {
38-
const mockOperation = jest.fn();
39-
runWithAmplifyServerContext(
40-
mockAmplifyConfig,
41-
{
42-
Auth: {
43-
tokenProvider: mockTokenProvider,
44-
credentialsProvider: mockCredentialAndIdentityProvider,
45-
},
46-
},
47-
mockOperation,
7+
it('should throw indicating the function is deprecated', () => {
8+
expect(() => runWithAmplifyServerContext({})).toThrow(
9+
'runWithAmplifyServerContext is no longer supported. Use configure() to create an AmplifyContext instead.',
4810
);
49-
50-
expect(mockOperation).toHaveBeenCalledWith(mockContextSpec);
51-
});
52-
53-
it('should destroy the context after the operation completed', async () => {
54-
const mockOperation = jest.fn();
55-
await runWithAmplifyServerContext(
56-
mockAmplifyConfig,
57-
{
58-
Auth: {
59-
tokenProvider: mockTokenProvider,
60-
credentialsProvider: mockCredentialAndIdentityProvider,
61-
},
62-
},
63-
mockOperation,
64-
);
65-
66-
expect(mockDestroyAmplifyServerContext).toHaveBeenCalledWith(
67-
mockContextSpec,
68-
);
69-
});
70-
71-
it('should destroy the context when the operation throws', async () => {
72-
const testError = new Error('some error');
73-
const mockOperation = jest.fn();
74-
mockOperation.mockRejectedValueOnce(testError);
75-
76-
await expect(
77-
runWithAmplifyServerContext(
78-
mockAmplifyConfig,
79-
{
80-
Auth: {
81-
tokenProvider: mockTokenProvider,
82-
credentialsProvider: mockCredentialAndIdentityProvider,
83-
},
84-
},
85-
mockOperation,
86-
),
87-
).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);
11111
});
11212
});

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

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
import { configure } from '../src/configure';
54
import { createConfigurationBuilder } from '@aws-amplify/core';
5+
6+
import { configure } from '../src/configure';
7+
68
import { amplifyOutputsFixture } from './fixtures/amplifyOutputs';
79

810
describe('configure()', () => {
@@ -45,6 +47,7 @@ describe('configure()', () => {
4547
...amplifyOutputsFixture,
4648
auth: {
4749
...amplifyOutputsFixture.auth,
50+
// eslint-disable-next-line camelcase
4851
user_pool_id: 'eu-north-1_NewPoolId',
4952
},
5053
});
@@ -58,6 +61,60 @@ describe('configure()', () => {
5861
});
5962
});
6063

64+
describe('configure() — resolveLocalLibraryOptions branches', () => {
65+
it('returns empty options when no Auth config', () => {
66+
const ctx = configure({
67+
version: '1.4',
68+
storage: amplifyOutputsFixture.storage,
69+
});
70+
expect(ctx.resourcesConfig.Auth).toBeUndefined();
71+
expect(ctx.resourcesConfig.Storage?.S3?.bucket).toBe(
72+
'my-test-app-storage-bucket-abcdef123456',
73+
);
74+
});
75+
76+
it('passes through custom Auth libraryOptions', () => {
77+
const mockTokenProvider = {
78+
getTokens: jest.fn().mockResolvedValue(undefined),
79+
};
80+
const mockCredentialsProvider = {
81+
getCredentialsAndIdentityId: jest.fn().mockResolvedValue(undefined),
82+
clearCredentialsAndIdentityId: jest.fn(),
83+
};
84+
const ctx = configure(amplifyOutputsFixture, {
85+
Auth: {
86+
tokenProvider: mockTokenProvider as any,
87+
credentialsProvider: mockCredentialsProvider as any,
88+
},
89+
});
90+
expect(ctx.resourcesConfig.Auth?.Cognito.userPoolId).toBe(
91+
'eu-north-1_Ab12CdEfG',
92+
);
93+
});
94+
95+
it('uses cookie storage when ssr is true', () => {
96+
const ctx = configure(amplifyOutputsFixture, { ssr: true });
97+
expect(ctx.resourcesConfig.Auth?.Cognito.userPoolId).toBe(
98+
'eu-north-1_Ab12CdEfG',
99+
);
100+
});
101+
102+
it('delegates fetchAuthSession to AuthClass', () => {
103+
const ctx = configure(amplifyOutputsFixture);
104+
expect(typeof ctx.fetchAuthSession).toBe('function');
105+
});
106+
107+
it('delegates clearCredentials to AuthClass', () => {
108+
const ctx = configure(amplifyOutputsFixture);
109+
expect(typeof ctx.clearCredentials).toBe('function');
110+
});
111+
112+
it('delegates getTokens to AuthClass', () => {
113+
const ctx = configure(amplifyOutputsFixture);
114+
expect(typeof ctx.getTokens).toBe('function');
115+
});
116+
});
117+
61118
describe('createConfigurationBuilder()', () => {
62119
it('round-trips through configure()', () => {
63120
const config = createConfigurationBuilder()
@@ -87,6 +144,7 @@ describe('createConfigurationBuilder()', () => {
87144
.auth(amplifyOutputsFixture.auth)
88145
.auth({
89146
...amplifyOutputsFixture.auth,
147+
// eslint-disable-next-line camelcase
90148
user_pool_id: 'eu-north-1_Replaced',
91149
})
92150
.build();

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+
['Amplify', '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)