@@ -105,6 +105,54 @@ describe('SSR Pages', () => {
105105 } )
106106} )
107107
108+ describe ( 'API Pages' , ( ) => {
109+ const router = join ( PROJECT_PATH , "functions" , "nextRouter" )
110+
111+ test ( 'creates nextRouter.js Netlify Function' , ( ) => {
112+ expect ( existsSync ( join ( router , "nextRouter.js" ) ) ) . toBe ( true )
113+ } )
114+
115+ test ( 'lists all routes in routes.json' , ( ) => {
116+ // read routes
117+ const { routes } = readJsonSync ( join ( router , "routes.json" ) )
118+
119+ // check entries
120+ expect ( routes ) . toContainEqual ( {
121+ file : "pages/api/static.js" ,
122+ regex : "^\\/api\\/static(?:\\/)?$"
123+ } )
124+ expect ( routes ) . toContainEqual ( {
125+ file : "pages/api/shows/[id].js" ,
126+ regex : "^\\/api\\/shows\\/([^\\/]+?)(?:\\/)?$"
127+ } )
128+ expect ( routes ) . toContainEqual ( {
129+ file : "pages/api/shows/[...params].js" ,
130+ regex : "^\\/api\\/shows(?:\\/((?:[^\\/]+?)(?:\\/(?:[^\\/]+?))*))?(?:\\/)?$"
131+ } )
132+ } )
133+
134+ test ( 'requires all pages in allPages.js' , ( ) => {
135+ // read allPages.js
136+ const contents = readFileSync ( join ( router , "allPages.js" ) )
137+
138+ // Convert contents into an array, each line being one element
139+ const requires = contents . toString ( ) . split ( "\n" )
140+
141+ // Verify presence of require statements
142+ expect ( requires ) . toContain ( 'require("./pages/api/static.js")' )
143+ expect ( requires ) . toContain ( 'require("./pages/api/shows/[id].js")' )
144+ expect ( requires ) . toContain ( 'require("./pages/api/shows/[...params].js")' )
145+ } )
146+
147+ test ( 'bundles all SSR-pages in /pages' , ( ) => {
148+ const pages = join ( PROJECT_PATH , "public" , "_next" , "pages" )
149+
150+ expect ( existsSync ( join ( router , "pages" , "api" , "static.js" ) ) ) . toBe ( true )
151+ expect ( existsSync ( join ( router , "pages" , "api" , "shows" , "[id].js" ) ) ) . toBe ( true )
152+ expect ( existsSync ( join ( router , "pages" , "api" , "shows" , "[...params].js" ) ) ) . toBe ( true )
153+ } )
154+ } )
155+
108156describe ( 'Static Pages' , ( ) => {
109157 test ( 'copies static pages to public/_next/ directory' , ( ) => {
110158 const pages = join ( PROJECT_PATH , "public" , "_next" , "pages" )
@@ -137,5 +185,8 @@ describe('Routing',() => {
137185 expect ( redirects ) . toContain ( "/index /.netlify/functions/nextRouter 200" )
138186 expect ( redirects ) . toContain ( "/shows/:id /.netlify/functions/nextRouter 200" )
139187 expect ( redirects ) . toContain ( "/shows/* /.netlify/functions/nextRouter 200" )
188+ expect ( redirects ) . toContain ( "/api/static /.netlify/functions/nextRouter 200" )
189+ expect ( redirects ) . toContain ( "/api/shows/:id /.netlify/functions/nextRouter 200" )
190+ expect ( redirects ) . toContain ( "/api/shows/* /.netlify/functions/nextRouter 200" )
140191 } )
141192} )
0 commit comments