1
1
import { ResourcesConfig } from '@aws-amplify/core' ;
2
2
import {
3
3
generateServerClientUsingCookies ,
4
- generateServerClient ,
4
+ generateServerClientUsingReqRes ,
5
5
} from '../../src/api' ;
6
- import { runWithAmplifyServerContext } from './../../src' ;
7
- import { getAmplifyConfig } from '../../src/utils' ;
6
+ import {
7
+ getAmplifyConfig ,
8
+ createRunWithAmplifyServerContext ,
9
+ } from '../../src/utils' ;
8
10
import { NextApiRequestMock , NextApiResponseMock } from '../mocks/headers' ;
11
+ import { createServerRunnerForAPI } from '../../src/api/createServerRunnerForAPI' ;
9
12
10
- const headers = import ( 'next/headers' ) ;
13
+ const headers = import ( 'next/headers.js ' ) ;
11
14
( global as any ) . Headers = jest . requireActual ( 'node-fetch' ) . Headers ;
12
15
13
16
const mockAmplifyConfig : ResourcesConfig = {
@@ -29,13 +32,16 @@ const mockAmplifyConfig: ResourcesConfig = {
29
32
} ;
30
33
31
34
jest . mock ( '../../src/utils' , ( ) => ( {
35
+ createRunWithAmplifyServerContext : jest . fn ( ( ) => jest . fn ( ) ) ,
32
36
getAmplifyConfig : jest . fn ( ( ) => mockAmplifyConfig ) ,
33
37
createCookieStorageAdapterFromNextServerContext : jest . fn ( ) ,
34
38
} ) ) ;
35
39
36
40
jest . mock ( 'aws-amplify/adapter-core' ) ;
37
41
38
42
const mockGetAmplifyConfig = getAmplifyConfig as jest . Mock ;
43
+ const mockCreateRunWithAmplifyServerContext =
44
+ createRunWithAmplifyServerContext as jest . Mock ;
39
45
40
46
describe ( 'generateServerClientUsingCookies' , ( ) => {
41
47
it ( 'should throw error when used with req/res' , async ( ) => {
@@ -52,11 +58,13 @@ describe('generateServerClientUsingCookies', () => {
52
58
} ) . toThrowError ( ) ;
53
59
} ) ;
54
60
55
- it ( 'should call getAmlifyConfig ' , async ( ) => {
61
+ it ( 'should call createRunWithAmplifyServerContext to create runWithAmplifyServerContext function ' , async ( ) => {
56
62
const cookies = ( await headers ) . cookies ;
57
63
58
- generateServerClientUsingCookies ( { cookies } ) ;
59
- expect ( mockGetAmplifyConfig ) . toHaveBeenCalled ( ) ;
64
+ generateServerClientUsingCookies ( { config : mockAmplifyConfig , cookies } ) ;
65
+ expect ( mockCreateRunWithAmplifyServerContext ) . toHaveBeenCalledWith ( {
66
+ config : mockAmplifyConfig ,
67
+ } ) ;
60
68
} ) ;
61
69
} ) ;
62
70
@@ -67,12 +75,15 @@ describe('generateServerClient', () => {
67
75
} ) ;
68
76
69
77
it ( 'should call getAmlifyConfig' , async ( ) => {
70
- generateServerClient ( ) ;
78
+ generateServerClientUsingReqRes ( { config : mockAmplifyConfig } ) ;
71
79
expect ( mockGetAmplifyConfig ) . toHaveBeenCalled ( ) ;
72
80
} ) ;
73
81
74
82
// TODO: figure out proper mocks and unskip
75
83
it . skip ( 'wrapped client.graphql should pass context through' , async ( ) => {
84
+ const { runWithAmplifyServerContext } = createServerRunnerForAPI ( {
85
+ config : mockAmplifyConfig ,
86
+ } ) ;
76
87
const mockedReq = new NextApiRequestMock ( ) ;
77
88
const mockedRes = NextApiResponseMock ;
78
89
@@ -86,7 +97,9 @@ describe('generateServerClient', () => {
86
97
getAmplifyServerContext : ( ) => { } ,
87
98
} ) ) ;
88
99
89
- const client = generateServerClient ( ) ;
100
+ const client = generateServerClientUsingReqRes ( {
101
+ config : mockAmplifyConfig ,
102
+ } ) ;
90
103
91
104
await runWithAmplifyServerContext ( {
92
105
nextServerContext : {
0 commit comments