1
+ /* eslint-disable max-lines-per-function */
1
2
jest . mock ( '../plugin-data' , ( ) => ( {
2
3
__esModule : true ,
3
4
default : jest . fn ( ) . mockReturnValue ( {
@@ -14,10 +15,12 @@ jest.mock('fs-extra', () => ({
14
15
existsSync : jest . fn ( ) ,
15
16
readFile : jest . fn ( ) ,
16
17
writeFile : jest . fn ( ) ,
18
+ writeJson : jest . fn ( ) ,
19
+ remove : jest . fn ( ) ,
17
20
} ) )
18
21
19
22
// Importing writeFile here gives us access to the mocked method to assert the correct content is written to the file within test cases
20
- import { writeFile } from 'fs-extra'
23
+ import { writeFile , writeJson , remove } from 'fs-extra'
21
24
import { testPluginOptionsSchema } from 'gatsby-plugin-utils'
22
25
23
26
import { pluginOptionsSchema , onPostBuild } from '../gatsby-node'
@@ -67,12 +70,12 @@ describe(`gatsby-node.js`, () => {
67
70
} )
68
71
69
72
describe ( 'onPostBuild' , ( ) => {
70
- let store = { }
71
73
const reporter = {
72
74
info : jest . fn ( ) ,
73
75
}
74
- beforeEach ( ( ) => {
75
- store = {
76
+
77
+ it ( 'creates redirects for SSR and DSG pages in format Netlify expects' , async ( ) => {
78
+ const store = {
76
79
getState : jest . fn ( ) . mockReturnValue ( {
77
80
redirects : [ ] ,
78
81
pages : [
@@ -89,13 +92,6 @@ describe(`gatsby-node.js`, () => {
89
92
program : { directory : '' } ,
90
93
} ) ,
91
94
}
92
- } )
93
-
94
- afterEach ( ( ) => {
95
- jest . resetAllMocks ( )
96
- } )
97
-
98
- it ( 'creates redirects for SSR and DSG pages in format Netlify expects' , async ( ) => {
99
95
const expectedValue = [
100
96
'' ,
101
97
'## Created with gatsby-plugin-netlify' ,
@@ -106,7 +102,55 @@ describe(`gatsby-node.js`, () => {
106
102
] . join ( `\n` )
107
103
108
104
await onPostBuild ( { store, pathPrefix : '' , reporter } , { } )
109
- expect ( writeFile ) . toHaveBeenCalledWith ( 'mock-file-path' , expectedValue )
105
+ expect ( writeFile ) . toHaveBeenLastCalledWith ( 'mock-file-path' , expectedValue )
106
+ } )
107
+
108
+ it ( 'creates skip file for unneeded function bundles' , async ( ) => {
109
+ const store = {
110
+ getState : jest . fn ( ) . mockReturnValue ( {
111
+ redirects : [ ] ,
112
+ pages : [
113
+ {
114
+ mode : 'DSG' ,
115
+ path : 'some/other/path' ,
116
+ } ,
117
+ ] ,
118
+ functions : [ ] ,
119
+ program : { directory : '' } ,
120
+ } ) ,
121
+ }
122
+ const expectedValue = {
123
+ API : false ,
124
+ SSR : false ,
125
+ DSG : true ,
126
+ }
127
+
128
+ await onPostBuild ( { store, pathPrefix : '' , reporter } , { } )
129
+ expect ( writeJson ) . toHaveBeenLastCalledWith ( '.cache/.nf-skip-gatsby-functions' , expectedValue )
130
+ } )
131
+
132
+ it ( 'removes skip file when all function bundles needed' , async ( ) => {
133
+ const store = {
134
+ getState : jest . fn ( ) . mockReturnValue ( {
135
+ redirects : [ ] ,
136
+ pages : [
137
+ {
138
+ mode : 'SSR' ,
139
+ path : 'some/path' ,
140
+ } ,
141
+ {
142
+ mode : 'DSG' ,
143
+ path : 'some/other/path' ,
144
+ } ,
145
+ ] ,
146
+ functions : [ true ] ,
147
+ program : { directory : '' } ,
148
+ } ) ,
149
+ }
150
+
151
+ await onPostBuild ( { store, pathPrefix : '' , reporter } , { } )
152
+ expect ( remove ) . toHaveBeenCalled ( )
110
153
} )
111
154
} )
112
155
} )
156
+ /* eslint-enable max-lines-per-function */
0 commit comments