-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
generateServerClient.test.ts
118 lines (99 loc) · 3.33 KB
/
generateServerClient.test.ts
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { ResourcesConfig } from '@aws-amplify/core';
import { parseAmplifyConfig } from '@aws-amplify/core/internals/utils';
import {
generateServerClientUsingCookies,
generateServerClientUsingReqRes,
} from '../../src/api';
import { createRunWithAmplifyServerContext } from '../../src/utils';
import { NextApiRequestMock, NextApiResponseMock } from '../mocks/headers';
import { createServerRunnerForAPI } from '../../src/api/createServerRunnerForAPI';
const headers = import('next/headers.js');
(global as any).Headers = jest.requireActual('node-fetch').Headers;
const mockAmplifyConfig: ResourcesConfig = {
Auth: {
Cognito: {
identityPoolId: '123',
userPoolId: 'abc',
userPoolClientId: 'def',
},
},
API: {
GraphQL: {
defaultAuthMode: 'apiKey',
apiKey: 'FAKE-KEY',
endpoint: 'https://localhost/graphql',
region: 'local-host-h4x',
},
},
};
jest.mock('../../src/utils', () => ({
createRunWithAmplifyServerContext: jest.fn(() => jest.fn()),
createCookieStorageAdapterFromNextServerContext: jest.fn(),
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
parseAmplifyConfig: jest.fn(() => mockAmplifyConfig),
}));
jest.mock('aws-amplify/adapter-core');
const mockParseAmplifyConfig = parseAmplifyConfig as jest.Mock;
const mockCreateRunWithAmplifyServerContext =
createRunWithAmplifyServerContext as jest.Mock;
describe('generateServerClientUsingCookies', () => {
it('should throw error when used with req/res', async () => {
const mockedReq = new NextApiRequestMock();
const mockedRes = NextApiResponseMock;
expect(() => {
// as any here to avoid type error from passing invalid input.
// this tests runtime exception
(generateServerClientUsingCookies as any)({
request: mockedReq,
response: mockedRes,
});
}).toThrow();
});
it('should call createRunWithAmplifyServerContext to create runWithAmplifyServerContext function', async () => {
const { cookies } = await headers;
generateServerClientUsingCookies({ config: mockAmplifyConfig, cookies });
expect(mockCreateRunWithAmplifyServerContext).toHaveBeenCalledWith({
config: mockAmplifyConfig,
});
});
});
describe('generateServerClient', () => {
afterAll(() => {
jest.resetAllMocks();
jest.clearAllMocks();
});
it('should call getAmlifyConfig', async () => {
generateServerClientUsingReqRes({ config: mockAmplifyConfig });
expect(mockParseAmplifyConfig).toHaveBeenCalled();
});
// TODO: figure out proper mocks and unskip
it.skip('wrapped client.graphql should pass context through', async () => {
const { runWithAmplifyServerContext } = createServerRunnerForAPI({
config: mockAmplifyConfig,
});
const mockedReq = new NextApiRequestMock();
const mockedRes = NextApiResponseMock;
const mockGraphql = jest.fn();
jest.mock('@aws-amplify/api-graphql/internals', () => ({
graphql: mockGraphql,
}));
jest.mock('@aws-amplify/core/internals/adapter-core', () => ({
getAmplifyServerContext: jest.fn(),
}));
const client = generateServerClientUsingReqRes({
config: mockAmplifyConfig,
});
await runWithAmplifyServerContext({
nextServerContext: {
request: mockedReq,
response: mockedRes,
},
operation: async contextSpec => {
await client.graphql(contextSpec, { query: '' });
},
});
expect(mockGraphql).toHaveBeenCalled();
});
});