Skip to content
This repository was archived by the owner on May 10, 2021. It is now read-only.

Commit 3af66f8

Browse files
committed
pre clean-up tests, no cypress yet
1 parent 94e6e02 commit 3af66f8

File tree

3 files changed

+349
-2
lines changed

3 files changed

+349
-2
lines changed

lib/pages/getStaticPropsWithRevalidate/redirects.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ const getNextConfig = require("../../helpers/getNextConfig");
77

88
const redirects = [];
99

10-
console.log("REVAL PAGES", pages);
11-
1210
const nextConfig = getNextConfig();
1311

1412
const srcRouteRedirectsAdded = [];

tests/__snapshots__/i18n.test.js.snap

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Routing creates Netlify redirects 1`] = `
4+
"# Next-on-Netlify Redirects
5+
/ /.netlify/functions/next_index 200
6+
/404 /en/404.html 200
7+
/_next/data/%BUILD_ID%/en/getStaticProps/1.json /.netlify/functions/next_getStaticProps_id 200! Cookie=__prerender_bypass,__next_preview_data
8+
/_next/data/%BUILD_ID%/en/getStaticProps/2.json /.netlify/functions/next_getStaticProps_id 200! Cookie=__prerender_bypass,__next_preview_data
9+
/_next/data/%BUILD_ID%/en/getStaticProps/withFallback/3.json /.netlify/functions/next_getStaticProps_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
10+
/_next/data/%BUILD_ID%/en/getStaticProps/withFallback/4.json /.netlify/functions/next_getStaticProps_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
11+
/_next/data/%BUILD_ID%/en/getStaticProps/withFallback/my/path/1.json /.netlify/functions/next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
12+
/_next/data/%BUILD_ID%/en/getStaticProps/withFallback/my/path/2.json /.netlify/functions/next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
13+
/_next/data/%BUILD_ID%/en/getStaticProps/withRevalidate/1.json /.netlify/functions/next_getStaticProps_withRevalidate_id 200
14+
/_next/data/%BUILD_ID%/getServerSideProps/all.json /.netlify/functions/next_getServerSideProps_all_slug 200
15+
/_next/data/%BUILD_ID%/getServerSideProps/all/* /.netlify/functions/next_getServerSideProps_all_slug 200
16+
/_next/data/%BUILD_ID%/getServerSideProps/static.json /.netlify/functions/next_getServerSideProps_static 200
17+
/_next/data/%BUILD_ID%/getServerSideProps/:id.json /.netlify/functions/next_getServerSideProps_id 200
18+
/_next/data/%BUILD_ID%/getStaticProps/static.json /.netlify/functions/next_getStaticProps_static 200! Cookie=__prerender_bypass,__next_preview_data
19+
/_next/data/%BUILD_ID%/getStaticProps/with-revalidate.json /.netlify/functions/next_getStaticProps_withrevalidate 200
20+
/_next/data/%BUILD_ID%/getStaticProps/withFallback/:id.json /.netlify/functions/next_getStaticProps_withFallback_id 200
21+
/_next/data/%BUILD_ID%/getStaticProps/withFallback/:slug/* /.netlify/functions/next_getStaticProps_withFallback_slug 200
22+
/_next/data/%BUILD_ID%/getStaticProps/withRevalidate/withFallback/:id.json /.netlify/functions/next_getStaticProps_withRevalidate_withFallback_id 200
23+
/api/shows/:id /.netlify/functions/next_api_shows_id 200
24+
/api/shows/:params/* /.netlify/functions/next_api_shows_params 200
25+
/api/static /.netlify/functions/next_api_static 200
26+
/en/getServerSideProps/all /.netlify/functions/next_getServerSideProps_all_slug 200
27+
/en/getServerSideProps/all/* /.netlify/functions/next_getServerSideProps_all_slug 200
28+
/en/getServerSideProps/static /.netlify/functions/next_getServerSideProps_static 200
29+
/en/getServerSideProps/:id /.netlify/functions/next_getServerSideProps_id 200
30+
/en/getStaticProps/static /.netlify/functions/next_getStaticProps_static 200! Cookie=__prerender_bypass,__next_preview_data
31+
/en/getStaticProps/with-revalidate /.netlify/functions/next_getStaticProps_withrevalidate 200
32+
/en/getStaticProps/withFallback/:id /.netlify/functions/next_getStaticProps_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
33+
/en/getStaticProps/withFallback/:slug/* /.netlify/functions/next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
34+
/en/getStaticProps/withRevalidate/:id /.netlify/functions/next_getStaticProps_withRevalidate_id 200
35+
/en/getStaticProps/:id /.netlify/functions/next_getStaticProps_id 200! Cookie=__prerender_bypass,__next_preview_data
36+
/en/static/:id /en/static/[id].html 200
37+
/es/getServerSideProps/all /.netlify/functions/next_getServerSideProps_all_slug 200
38+
/es/getServerSideProps/all/* /.netlify/functions/next_getServerSideProps_all_slug 200
39+
/es/getServerSideProps/static /.netlify/functions/next_getServerSideProps_static 200
40+
/es/getServerSideProps/:id /.netlify/functions/next_getServerSideProps_id 200
41+
/es/getStaticProps/static /.netlify/functions/next_getStaticProps_static 200! Cookie=__prerender_bypass,__next_preview_data
42+
/es/getStaticProps/with-revalidate /.netlify/functions/next_getStaticProps_withrevalidate 200
43+
/es/static/:id /es/static/[id].html 200
44+
/getServerSideProps/all /.netlify/functions/next_getServerSideProps_all_slug 200
45+
/getServerSideProps/all/* /.netlify/functions/next_getServerSideProps_all_slug 200
46+
/getServerSideProps/static /.netlify/functions/next_getServerSideProps_static 200
47+
/getServerSideProps/:id /.netlify/functions/next_getServerSideProps_id 200
48+
/getStaticProps/static /en/getStaticProps/static 200
49+
/getStaticProps/with-revalidate /.netlify/functions/next_getStaticProps_withrevalidate 200
50+
/getStaticProps/withFallback/:id /en/getStaticProps/withFallback/:id 200
51+
/getStaticProps/withFallback/:id /en/getStaticProps/withFallback/:id 200
52+
/getStaticProps/withFallback/:slug/* /en/getStaticProps/withFallback/:...slug 200
53+
/getStaticProps/withFallback/:slug/* /en/getStaticProps/withFallback/:...slug 200
54+
/getStaticProps/withRevalidate/withFallback/:id /.netlify/functions/next_getStaticProps_withRevalidate_withFallback_id 200
55+
/getStaticProps/withRevalidate/:id /.netlify/functions/next_getStaticProps_withRevalidate_id 200
56+
/getStaticProps/:id /en/getStaticProps/:id 200
57+
/shows/:id /.netlify/functions/next_shows_id 200
58+
/shows/:params/* /.netlify/functions/next_shows_params 200
59+
/static /en/static.html 200
60+
/static/:id /en/static/[id].html 200"
61+
`;

tests/i18n.test.js

Lines changed: 288 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
// Test next-on-netlify when i18n is set in next.config.js (Next 10+)
22

33
const { parse, join } = require("path");
4+
const {
5+
existsSync,
6+
readdirSync,
7+
readFileSync,
8+
readJsonSync,
9+
} = require("fs-extra");
410
const buildNextApp = require("./helpers/buildNextApp");
511

612
// The name of this test file (without extension)
@@ -11,6 +17,8 @@ const FILENAME = parse(__filename).name;
1117
// package.json file.
1218
const PROJECT_PATH = join(__dirname, "builds", FILENAME);
1319

20+
const DEFAULT_LOCALE = "en";
21+
1422
// Capture the output to verify successful build
1523
let buildOutput;
1624

@@ -33,3 +41,283 @@ describe("next-on-netlify", () => {
3341
expect(buildOutput).toMatch("Success! All done!");
3442
});
3543
});
44+
45+
describe("next-on-netlify", () => {
46+
test("builds successfully", () => {
47+
expect(buildOutput).toMatch("Next on Netlify");
48+
expect(buildOutput).toMatch("Copying static NextJS assets to out_publish/");
49+
expect(buildOutput).toMatch(
50+
"Setting up API endpoints as Netlify Functions in out_functions/"
51+
);
52+
expect(buildOutput).toMatch(
53+
"Setting up pages with getInitialProps as Netlify Functions in out_functions/"
54+
);
55+
expect(buildOutput).toMatch(
56+
"Setting up pages with getServerSideProps as Netlify Functions in out_functions/"
57+
);
58+
expect(buildOutput).toMatch(
59+
"Copying pre-rendered pages with getStaticProps and JSON data to out_publish/"
60+
);
61+
expect(buildOutput).toMatch(
62+
"Setting up pages with getStaticProps and fallback: true as Netlify Functions in out_functions/"
63+
);
64+
expect(buildOutput).toMatch(
65+
"Setting up pages with getStaticProps and revalidation interval as Netlify Functions in out_functions/"
66+
);
67+
expect(buildOutput).toMatch(
68+
"Copying pre-rendered pages without props to out_publish/"
69+
);
70+
expect(buildOutput).toMatch("Setting up redirects");
71+
expect(buildOutput).toMatch("Success! All done!");
72+
});
73+
});
74+
75+
describe("SSR Pages", () => {
76+
const functionsDir = join(PROJECT_PATH, "out_functions");
77+
78+
test("creates a Netlify Function for each SSR page", () => {
79+
expect(existsSync(join(functionsDir, "next_index", "next_index.js"))).toBe(
80+
true
81+
);
82+
expect(
83+
existsSync(join(functionsDir, "next_shows_id", "next_shows_id.js"))
84+
).toBe(true);
85+
expect(
86+
existsSync(
87+
join(functionsDir, "next_shows_params", "next_shows_params.js")
88+
)
89+
).toBe(true);
90+
expect(
91+
existsSync(
92+
join(
93+
functionsDir,
94+
"next_getServerSideProps_static",
95+
"next_getServerSideProps_static.js"
96+
)
97+
)
98+
).toBe(true);
99+
expect(
100+
existsSync(
101+
join(
102+
functionsDir,
103+
"next_getServerSideProps_id",
104+
"next_getServerSideProps_id.js"
105+
)
106+
)
107+
).toBe(true);
108+
});
109+
});
110+
111+
describe("API Pages", () => {
112+
const functionsDir = join(PROJECT_PATH, "out_functions");
113+
114+
test("creates a Netlify Function for each API endpoint", () => {
115+
expect(
116+
existsSync(join(functionsDir, "next_api_static", "next_api_static.js"))
117+
).toBe(true);
118+
expect(
119+
existsSync(
120+
join(functionsDir, "next_api_shows_id", "next_api_shows_id.js")
121+
)
122+
).toBe(true);
123+
expect(
124+
existsSync(
125+
join(functionsDir, "next_api_shows_params", "next_api_shows_params.js")
126+
)
127+
).toBe(true);
128+
});
129+
});
130+
131+
describe("SSG Pages with getStaticProps", () => {
132+
test("creates pre-rendered HTML file in output directory", () => {
133+
const defaultLocale = "en";
134+
const OUTPUT_PATH = join(PROJECT_PATH, "out_publish", DEFAULT_LOCALE);
135+
136+
expect(existsSync(join(OUTPUT_PATH, "getStaticProps", "static.html"))).toBe(
137+
true
138+
);
139+
expect(existsSync(join(OUTPUT_PATH, "getStaticProps", "1.html"))).toBe(
140+
true
141+
);
142+
expect(existsSync(join(OUTPUT_PATH, "getStaticProps", "2.html"))).toBe(
143+
true
144+
);
145+
expect(
146+
existsSync(join(OUTPUT_PATH, "getStaticProps", "withFallback", "3.html"))
147+
).toBe(true);
148+
expect(
149+
existsSync(join(OUTPUT_PATH, "getStaticProps", "withFallback", "4.html"))
150+
).toBe(true);
151+
expect(
152+
existsSync(
153+
join(
154+
OUTPUT_PATH,
155+
"getStaticProps",
156+
"withFallback",
157+
"my",
158+
"path",
159+
"1.html"
160+
)
161+
)
162+
).toBe(true);
163+
expect(
164+
existsSync(
165+
join(
166+
OUTPUT_PATH,
167+
"getStaticProps",
168+
"withFallback",
169+
"my",
170+
"path",
171+
"2.html"
172+
)
173+
)
174+
).toBe(true);
175+
});
176+
177+
test("creates data .json file in /_next/data/ directory", () => {
178+
// Get path to data files
179+
const dirs = readdirSync(
180+
join(PROJECT_PATH, "out_publish", "_next", "data")
181+
);
182+
expect(dirs.length).toBe(1);
183+
const dataDir = join(
184+
PROJECT_PATH,
185+
"out_publish",
186+
"_next",
187+
"data",
188+
dirs[0],
189+
DEFAULT_LOCALE
190+
);
191+
192+
expect(existsSync(join(dataDir, "getStaticProps", "static.json"))).toBe(
193+
true
194+
);
195+
expect(existsSync(join(dataDir, "getStaticProps", "1.json"))).toBe(true);
196+
expect(existsSync(join(dataDir, "getStaticProps", "2.json"))).toBe(true);
197+
expect(
198+
existsSync(join(dataDir, "getStaticProps", "withFallback", "3.json"))
199+
).toBe(true);
200+
expect(
201+
existsSync(join(dataDir, "getStaticProps", "withFallback", "4.json"))
202+
).toBe(true);
203+
expect(
204+
existsSync(
205+
join(dataDir, "getStaticProps", "withFallback", "my", "path", "1.json")
206+
)
207+
).toBe(true);
208+
expect(
209+
existsSync(
210+
join(dataDir, "getStaticProps", "withFallback", "my", "path", "2.json")
211+
)
212+
).toBe(true);
213+
});
214+
215+
test("creates Netlify Functions for pages with fallback", () => {
216+
const functionPath1 =
217+
"next_getStaticProps_withFallback_id/next_getStaticProps_withFallback_id.js";
218+
expect(existsSync(join(PROJECT_PATH, "out_functions", functionPath1))).toBe(
219+
true
220+
);
221+
222+
const functionPath2 =
223+
"next_getStaticProps_withFallback_slug/next_getStaticProps_withFallback_slug.js";
224+
expect(existsSync(join(PROJECT_PATH, "out_functions", functionPath2))).toBe(
225+
true
226+
);
227+
});
228+
});
229+
230+
describe("SSG Pages with getStaticProps and revalidate", () => {
231+
const functionsDir = join(PROJECT_PATH, "out_functions");
232+
233+
test("creates a Netlify Function for each page", () => {
234+
expect(
235+
existsSync(
236+
join(
237+
functionsDir,
238+
"next_getStaticProps_withrevalidate",
239+
"next_getStaticProps_withrevalidate.js"
240+
)
241+
)
242+
).toBe(true);
243+
expect(
244+
existsSync(
245+
join(
246+
functionsDir,
247+
"next_getStaticProps_withRevalidate_id",
248+
"next_getStaticProps_withRevalidate_id.js"
249+
)
250+
)
251+
).toBe(true);
252+
expect(
253+
existsSync(
254+
join(
255+
functionsDir,
256+
"next_getStaticProps_withRevalidate_withFallback_id",
257+
"next_getStaticProps_withRevalidate_withFallback_id.js"
258+
)
259+
)
260+
).toBe(true);
261+
});
262+
});
263+
264+
describe("Static Pages", () => {
265+
test("copies static pages to output directory", () => {
266+
const OUTPUT_PATH = join(PROJECT_PATH, "out_publish", DEFAULT_LOCALE);
267+
268+
expect(existsSync(join(OUTPUT_PATH, "static.html"))).toBe(true);
269+
expect(existsSync(join(OUTPUT_PATH, "static/[id].html"))).toBe(true);
270+
});
271+
272+
test("copies static assets to out_publish/_next/ directory", () => {
273+
const dirs = readdirSync(
274+
join(PROJECT_PATH, "out_publish", "_next", "static")
275+
);
276+
277+
expect(dirs.length).toBe(2);
278+
expect(dirs).toContain("chunks");
279+
});
280+
281+
test.each(["en", "es"])("multiple locales", (locale) => {
282+
const OUTPUT_PATH = join(PROJECT_PATH, "out_publish", locale);
283+
284+
expect(existsSync(join(OUTPUT_PATH, "static.html"))).toBe(true);
285+
expect(existsSync(join(OUTPUT_PATH, "static/[id].html"))).toBe(true);
286+
});
287+
288+
test("locale not included in config", () => {
289+
const notIncludedLocale = "fr";
290+
const OUTPUT_PATH = join(PROJECT_PATH, "out_publish", notIncludedLocale);
291+
292+
expect(existsSync(join(OUTPUT_PATH, "static.html"))).toBe(false);
293+
expect(existsSync(join(OUTPUT_PATH, "static/[id].html"))).toBe(false);
294+
});
295+
});
296+
297+
describe("404 Page", () => {
298+
test("copies 404.html to output directory", () => {
299+
const OUTPUT_PATH = join(PROJECT_PATH, "out_publish", DEFAULT_LOCALE);
300+
301+
expect(existsSync(join(OUTPUT_PATH, "404.html"))).toBe(true);
302+
});
303+
});
304+
305+
describe("Routing", () => {
306+
test("creates Netlify redirects", async () => {
307+
// Read _redirects file
308+
const contents = readFileSync(
309+
join(PROJECT_PATH, "out_publish", "_redirects")
310+
);
311+
let redirects = contents.toString();
312+
313+
// Replace non-persistent build ID with placeholder
314+
315+
redirects = redirects.replace(
316+
/\/_next\/data\/[^\/]+\//g,
317+
"/_next/data/%BUILD_ID%/"
318+
);
319+
320+
// Check that redirects match
321+
expect(redirects).toMatchSnapshot();
322+
});
323+
});

0 commit comments

Comments
 (0)