This repository was archived by the owner on Jan 28, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 465
/
Copy pathregeneration-handler.test.ts
110 lines (94 loc) · 2.58 KB
/
regeneration-handler.test.ts
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
import { RegenerationEvent } from "../../src";
import { createCloudFrontEvent } from "../test-utils";
// eslint-disable-next-line @typescript-eslint/no-var-requires
jest.mock("node-fetch", () => require("fetch-mock-jest").sandbox());
jest.mock(
"../../src/prerender-manifest.json",
() => require("./prerender-manifest.json"),
{
virtual: true
}
);
jest.mock(
"../../src/images-manifest.json",
() => require("./images-manifest.json"),
{
virtual: true
}
);
const sqsHandlerEvent = (body: RegenerationEvent) => {
return {
Records: [
{
body: JSON.stringify(body)
}
]
};
};
const mockPageRequire = (mockPagePath: string): void => {
jest.mock(
`../../src/${mockPagePath}`,
() => require(`../shared-fixtures/built-artifact/${mockPagePath}`),
{
virtual: true
}
);
};
describe("Regeneration Handler", () => {
let consoleWarnSpy: jest.SpyInstance;
beforeEach(() => {
consoleWarnSpy = jest.spyOn(console, "error").mockReturnValue();
jest.mock(
"../../src/manifest.json",
() => require("./default-build-manifest.json"),
{
virtual: true
}
);
jest.mock(`../../src/s3/s3StorePage`);
});
afterEach(() => {
consoleWarnSpy.mockRestore();
});
it.each`
basePath
${"/custom"}
${""}
`(
"should generate correct page when basePath = $basePath",
async ({ basePath }) => {
mockPageRequire("pages/preview.js");
const regenerationHandler =
require("../../src/regeneration-handler").handler; // eslint-disable-line @typescript-eslint/no-var-requires
// eslint-disable-next-line @typescript-eslint/no-var-requires
const s3StorePage = require("../../src/s3/s3StorePage").s3StorePage;
const event = createCloudFrontEvent({
uri: `/preview`,
host: "mydistribution.cloudfront.net",
config: {
eventType: "origin-request"
} as AWSLambda.CloudFrontEvent["config"],
querystring: undefined,
requestHeaders: {}
});
await regenerationHandler(
sqsHandlerEvent({
basePath,
bucketName: "my-bucket",
cloudFrontEventRequest: event.Records[0].cf.request,
region: "us-west-2",
pageS3Path: `static-pages/build-id/preview.js`,
pagePath: "pages/preview.js"
})
);
expect(s3StorePage).toBeCalledTimes(1);
expect(s3StorePage).toBeCalledWith(
expect.objectContaining({
basePath,
uri: "/preview",
pageData: { page: "pages/preview.js" }
})
);
}
);
});