1+ /* eslint-disable max-lines-per-function */
12jest . mock ( '../plugin-data' , ( ) => ( {
23 __esModule : true ,
34 default : jest . fn ( ) . mockReturnValue ( {
@@ -14,10 +15,12 @@ jest.mock('fs-extra', () => ({
1415 existsSync : jest . fn ( ) ,
1516 readFile : jest . fn ( ) ,
1617 writeFile : jest . fn ( ) ,
18+ writeJson : jest . fn ( ) ,
19+ remove : jest . fn ( ) ,
1720} ) )
1821
1922// 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'
2124import { testPluginOptionsSchema } from 'gatsby-plugin-utils'
2225
2326import { pluginOptionsSchema , onPostBuild } from '../gatsby-node'
@@ -67,12 +70,12 @@ describe(`gatsby-node.js`, () => {
6770 } )
6871
6972 describe ( 'onPostBuild' , ( ) => {
70- let store = { }
7173 const reporter = {
7274 info : jest . fn ( ) ,
7375 }
74- beforeEach ( ( ) => {
75- store = {
76+
77+ it ( 'creates redirects for SSR and DSG pages in format Netlify expects' , async ( ) => {
78+ const store = {
7679 getState : jest . fn ( ) . mockReturnValue ( {
7780 redirects : [ ] ,
7881 pages : [
@@ -89,13 +92,6 @@ describe(`gatsby-node.js`, () => {
8992 program : { directory : '' } ,
9093 } ) ,
9194 }
92- } )
93-
94- afterEach ( ( ) => {
95- jest . resetAllMocks ( )
96- } )
97-
98- it ( 'creates redirects for SSR and DSG pages in format Netlify expects' , async ( ) => {
9995 const expectedValue = [
10096 '' ,
10197 '## Created with gatsby-plugin-netlify' ,
@@ -106,7 +102,55 @@ describe(`gatsby-node.js`, () => {
106102 ] . join ( `\n` )
107103
108104 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 ( )
110153 } )
111154 } )
112155} )
156+ /* eslint-enable max-lines-per-function */
0 commit comments