This repository has been archived by the owner on Jul 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.test.js
163 lines (126 loc) · 4.2 KB
/
index.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
const path = require("path");
const EleventyLoad = require("./EleventyLoad");
const createConfig = require("./utils/createConfig");
const {
mockValues,
mockTransform,
mockShortcode,
} = require("./testUtils/eleventyMock");
jest.mock("./EleventyLoad");
jest.mock("./utils/createConfig");
const mockConfig = {
addTransform: jest.fn(),
addShortcode: jest.fn(),
versionCheck: jest.fn(),
on: jest.fn(),
};
const rules = [{ test: /\.html$/, loaders: [] }];
// Lazy-load plugin in tests so we can mock its dependences
let plugin = undefined;
afterEach(() => {
jest.clearAllMocks();
});
describe("eleventy-load config", () => {
const mockWarn = jest.spyOn(console, "warn").mockImplementation();
afterEach(() => {
jest.clearAllMocks();
});
afterAll(() => {
mockWarn.mockRestore();
});
test("Warns if rules not provided", () => {
plugin = require("./index");
plugin(mockConfig, { rules: undefined });
expect(mockWarn).toBeCalledWith(
"[eleventy-load] Try giving me some rules!"
);
});
test("Checks the 11ty version for compatibility", () => {
mockConfig.versionCheck.mockImplementationOnce(() => {
throw Error("not compatible");
});
plugin = require("./index");
plugin(mockConfig, { rules });
expect(mockConfig.versionCheck).toBeCalledWith(">=0.5.0 <2.x");
expect(mockWarn).toBeCalledWith(
"WARN: Eleventy Plugin (eleventy-load) Compatibility: not compatible"
);
});
});
describe.each(["v0_x_x", "v1_x_x"])(
"eleventy-load plugin (11ty %s)",
(eleventyVersion) => {
const { inputDir, outputDir, outputPath } = mockValues;
class StubEleventyLoad {}
const mockCreatedConfig = {
transform: () => ({ inputDir, outputDir }),
shortcode: () => ({ inputDir, outputDir }),
};
beforeEach(() => {
EleventyLoad.mockReturnValue(StubEleventyLoad);
createConfig.mockReturnValue(mockCreatedConfig);
plugin = require("./index");
});
test("Adds the `eleventy-load` transform", () => {
plugin(mockConfig, { rules });
const { addTransform } = mockConfig;
expect(addTransform).toBeCalledTimes(1);
expect(addTransform).toBeCalledWith(
"eleventy-load",
expect.any(Function)
);
const callback = addTransform.mock.calls[0][1];
expect(callback.length).toBe(2);
});
test("Returns an EleventyLoad instance on transform", () => {
const content = ["content"];
const resource = path.join("src", "index.md");
const mockEleventyContext = mockTransform[eleventyVersion]();
plugin(mockConfig, { rules });
const callback = mockConfig.addTransform.mock.calls[0][1];
callback.apply(mockEleventyContext, [content, outputPath]);
expect(EleventyLoad).toBeCalledTimes(1);
expect(EleventyLoad).toBeCalledWith(
{ rules },
{}, // empty cache
resource,
content,
mockCreatedConfig,
outputPath
);
});
test("Adds the `load` shortcode", () => {
plugin(mockConfig, { rules });
const { addShortcode } = mockConfig;
expect(addShortcode).toBeCalledTimes(1);
expect(addShortcode).toBeCalledWith("load", expect.any(Function));
const callback = addShortcode.mock.calls[0][1];
expect(callback.length).toBe(1);
});
test("Returns an EleventyLoad instance on shortcode `load`", () => {
const resource = "resource";
const mockEleventyContext = mockShortcode[eleventyVersion]();
plugin(mockConfig, { rules });
const callback = mockConfig.addShortcode.mock.calls[0][1];
callback.apply(mockEleventyContext, [resource]);
expect(EleventyLoad).toBeCalledTimes(1);
expect(EleventyLoad).toBeCalledWith(
{ rules },
{}, // empty cache
resource,
null,
mockCreatedConfig,
null
);
});
test("Adds a cache reset on beforeWatch event", () => {
plugin(mockConfig, { rules });
const { on } = mockConfig;
expect(on).toBeCalledTimes(1);
expect(on).toBeCalledWith("beforeWatch", expect.any(Function));
const callback = on.mock.calls[0][1];
expect(callback.length).toBe(0);
// Cannot test internal cache state
});
}
);