-
-
Notifications
You must be signed in to change notification settings - Fork 450
/
NextPage.test.js
221 lines (172 loc) · 6.05 KB
/
NextPage.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
const path = require("path");
const NextPage = require("../NextPage");
const PluginBuildDir = require("../PluginBuildDir");
describe("NextPage", () => {
describe("#constructor", () => {
it("should set a pagePath", () => {
const pagePath = `${PluginBuildDir.BUILD_DIR_NAME}/home.js`;
const page = new NextPage(pagePath);
expect(page.pagePath).toEqual(pagePath);
});
});
describe("When is the index page", () => {
const buildDir = PluginBuildDir.BUILD_DIR_NAME;
const pagePath = path.join(buildDir, "index.js");
let page;
beforeEach(() => {
page = new NextPage(pagePath);
});
describe("#serverlessFunction", () => {
it("should return function http event path /", () => {
const { events } = page.serverlessFunction.indexPage;
expect(events).toHaveLength(1);
const httpEvent = events[0].http;
expect(httpEvent.path).toEqual("/");
});
});
});
describe("When is the _error page", () => {
const buildDir = PluginBuildDir.BUILD_DIR_NAME;
const pagePath = path.join(buildDir, "_error.js");
let page;
beforeEach(() => {
page = new NextPage(pagePath);
});
describe("#serverlessFunction", () => {
it("should name the function notFoundErrorPage", () => {
expect(page.serverlessFunction.notFoundErrorPage).toBeDefined();
});
it("should return function http event path /{proxy+}", () => {
const { events } = page.serverlessFunction.notFoundErrorPage;
expect(events).toHaveLength(1);
const httpEvent = events[0].http;
expect(httpEvent.path).toEqual("/{proxy+}");
});
});
});
describe("When is a nested page", () => {
const buildDir = PluginBuildDir.BUILD_DIR_NAME;
const pagePath = path.join(buildDir, "categories/fridge/fridges.js");
let page;
beforeEach(() => {
page = new NextPage(pagePath);
});
describe("#serverlessFunction", () => {
it("should have URI path matching subdirectories", () => {
const { events } = page.serverlessFunction.fridgesPage;
expect(events).toHaveLength(1);
const httpEvent = events[0].http;
expect(httpEvent.path).toEqual("categories/fridge/fridges");
expect(httpEvent.method).toEqual("get");
});
});
});
describe("When pagePath has win format", () => {
const buildDir = PluginBuildDir.BUILD_DIR_NAME;
const pagePath = `${buildDir}\\admin.js`;
let page;
beforeEach(() => {
page = new NextPage(pagePath);
});
it("should return posix pageHandler", () => {
expect(page.pageHandler).toEqual(
`${PluginBuildDir.BUILD_DIR_NAME}/admin.render`
);
});
});
describe("When the build directory is a subdirectory", () => {
const buildDir = path.join("app", PluginBuildDir.BUILD_DIR_NAME);
const pagePath = path.join(buildDir, "admin.js");
let page;
beforeEach(() => {
page = new NextPage(pagePath);
});
it("should return pageHandler", () => {
expect(page.pageHandler).toEqual(
`app/${PluginBuildDir.BUILD_DIR_NAME}/admin.render`
);
});
it("should return pageRoute", () => {
expect(page.pageRoute).toEqual("admin");
});
});
describe("When a new instance is created", () => {
const buildDir = PluginBuildDir.BUILD_DIR_NAME;
const pagePath = `${buildDir}/admin.js`;
let page;
beforeEach(() => {
page = new NextPage(pagePath);
});
it("should have pageCompatPath", () => {
expect(page.pageCompatPath).toEqual(
path.join(buildDir, "admin.compat.js")
);
});
it("should return pageOriginalPath", () => {
expect(page.pageOriginalPath).toEqual(
path.join(buildDir, "admin.original.js")
);
});
it("should return pageDir", () => {
expect(page.pageDir).toEqual(buildDir);
});
it("should return pageName", () => {
expect(page.pageName).toEqual("admin");
});
it("should return pageHandler", () => {
expect(page.pageHandler).toEqual(
`${PluginBuildDir.BUILD_DIR_NAME}/admin.render`
);
});
it("should return pageFunctionName", () => {
expect(page.functionName).toEqual("adminPage");
});
describe("#serverlessFunction", () => {
it("should return function name", () => {
const pageFunction = page.serverlessFunction;
expect(pageFunction.adminPage).toBeDefined();
});
it("should return function handler", () => {
const { handler } = page.serverlessFunction.adminPage;
expect(handler).toEqual(`${buildDir}/admin.render`);
});
it("should return function http event", () => {
const { events } = page.serverlessFunction.adminPage;
expect(events).toHaveLength(1);
const httpEvent = events[0].http;
expect(httpEvent.path).toEqual("admin");
expect(httpEvent.method).toEqual("get");
});
it("should override serverlessFunction with provided pageConfig", () => {
const serverlessFunctionOverrides = { foo: "bar" };
const pageWithCustomConfig = new NextPage(
pagePath,
serverlessFunctionOverrides
);
expect(pageWithCustomConfig.serverlessFunction.adminPage.foo).toBe(
"bar"
);
});
it("should NOT change handler with provided pageConfig", () => {
const serverlessFunctionOverrides = { handler: "invalid/handler" };
const pageWithCustomConfig = new NextPage(
pagePath,
serverlessFunctionOverrides
);
expect(pageWithCustomConfig.serverlessFunction.adminPage.handler).toBe(
pageWithCustomConfig.pageHandler
);
});
it("should NOT change runtime with provided pageConfig", () => {
const serverlessFunctionOverrides = { runtime: "python2.7" };
const pageWithCustomConfig = new NextPage(
pagePath,
serverlessFunctionOverrides
);
expect(pageWithCustomConfig.serverlessFunction.adminPage.runtime).toBe(
undefined
);
});
});
});
});