-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.test.js
211 lines (189 loc) · 6.6 KB
/
gatsby-node.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
import gn from "./gatsby-node";
describe("the main gatsby thing", () => {
it("aggregates categories which are the same", async () => {
const node = {
node: { frontmatter: {}, fields: { category: "test-stuff", source: "some-source" } }
};
const graphql = jest.fn().mockResolvedValue({
data: {
allMarkdownRemark: {
edges: [node, node, node]
}
}
});
const actions = { createPage: jest.fn() };
await gn.createPages({ graphql, actions });
// The categories are all the same so should be consolidated
const calls = actions.createPage.mock.calls;
expect(calls.length).toBe(1);
expect(calls[0][0].component).toContain("CategoryTemplate.js");
});
it("makes pages for each category", async () => {
const node = {
node: { frontmatter: {}, fields: { category: "test-stuff", source: "some-source" } }
};
const otherNode = {
node: { frontmatter: {}, fields: { category: "other-stuff", source: "some-source" } }
};
const graphql = jest.fn().mockResolvedValue({
data: {
allMarkdownRemark: {
edges: [node, otherNode, node]
}
}
});
const actions = { createPage: jest.fn() };
await gn.createPages({ graphql, actions });
// The categories are all different so should not be consolidated
expect(actions.createPage.mock.calls.length).toBe(2);
});
it("aggregates types which are the same", async () => {
const node = {
node: { frontmatter: { type: "interview" }, fields: { source: "some-source" } }
};
const graphql = jest.fn().mockResolvedValue({
data: {
allMarkdownRemark: {
edges: [node, node, node]
}
}
});
const actions = { createPage: jest.fn() };
await gn.createPages({ graphql, actions });
// The types are all the same so should be consolidated
const calls = actions.createPage.mock.calls;
expect(calls.length).toBe(1);
expect(calls[0][0].component).toContain("TypeTemplate.js");
});
it("makes pages for each type", async () => {
const node = {
node: {
frontmatter: { type: "interpretive dance" },
fields: { source: "some-source" }
}
};
const otherNode = {
node: {
frontmatter: { type: "tv show" },
fields: { source: "some-source" }
}
};
const graphql = jest.fn().mockResolvedValue({
data: {
allMarkdownRemark: {
edges: [node, otherNode, node]
}
}
});
const actions = { createPage: jest.fn() };
await gn.createPages({ graphql, actions });
// The types are all different so should be distinct calls
expect(actions.createPage.mock.calls.length).toBe(2);
});
it("makes pages for posts", async () => {
const slug = "slug-path";
const node = {
node: {
frontmatter: {},
fields: { category: "test-stuff", source: "posts", slug, prefix: "a-proper-date" }
}
};
const edges = [node, node];
const graphql = jest.fn().mockResolvedValue({
data: {
allMarkdownRemark: { edges }
}
});
const actions = { createPage: jest.fn() };
await gn.createPages({ graphql, actions });
// One call for the category, then a second for the post
const calls = actions.createPage.mock.calls;
// One post for each node, plus one for each QR code, plus one for the category
expect(calls.length).toBe(2 * edges.length + 1);
// The first argument to the second call ...
expect(calls[1][0].path).toEqual(slug);
expect(calls[1][0].component).toContain("PostTemplate.js");
// The first argument to the third call ...
expect(calls[2][0].path).toContain(slug);
expect(calls[2][0].path).toContain("qr");
expect(calls[2][0].component).toContain("QrCodeTemplate.js");
});
it("makes pages for talks", async () => {
const slug = "slug-path";
const node = {
node: {
frontmatter: {},
fields: { category: "test-stuff", source: "talks", slug, prefix: "a-proper-date" }
}
};
const edges = [node, node];
const graphql = jest.fn().mockResolvedValue({
data: {
allMarkdownRemark: { edges }
}
});
const actions = { createPage: jest.fn() };
await gn.createPages({ graphql, actions });
// One call for the category, then a second for the post
const calls = actions.createPage.mock.calls;
// One post for each node, one qr code for each node, plus one for the category
expect(calls.length).toBe(2 * edges.length + 1);
// The first argument to the second call ...
expect(calls[1][0].path).toEqual(slug);
expect(calls[1][0].component).toContain("PostTemplate.js");
expect(calls[2][0].path).toContain(slug);
expect(calls[2][0].path).toContain("qr");
expect(calls[2][0].component).toContain("QrCodeTemplate.js");
});
it("makes pages for publications (and also makes a category)", async () => {
const url = "http://myfamous.story";
const slug = "slug-path";
const node = {
node: {
frontmatter: { url },
fields: { category: "pub-stuff", source: "publications", slug }
}
};
const edges = [node];
const graphql = jest.fn().mockResolvedValue({
data: {
allMarkdownRemark: { edges }
}
});
const actions = { createPage: jest.fn() };
await gn.createPages({ graphql, actions });
// One call for the category, then a second for the post
const calls = actions.createPage.mock.calls;
// One posts for the nodes plus one for the category
expect(calls.length).toBe(2);
// The first argument to the second call ...
expect(calls[1][0].context.url).toEqual(url);
expect(calls[1][0].path).toEqual(slug);
// The first argument to the first call ...
expect(calls[0][0].path).toEqual("/category/pub-stuff/");
});
it("makes pages for pages", async () => {
const slug = "slug-path";
const node = {
node: {
frontmatter: {},
fields: { category: "test-stuff", source: "pages", slug }
}
};
const edges = [node, node, node];
const graphql = jest.fn().mockResolvedValue({
data: {
allMarkdownRemark: { edges }
}
});
const actions = { createPage: jest.fn() };
await gn.createPages({ graphql, actions });
// One call for the category, then a second for the post
const calls = actions.createPage.mock.calls;
// One post for each node plus one for the category
expect(calls.length).toBe(edges.length + 1);
// The first argument to the second call ...
expect(calls[1][0].path).toEqual(slug);
expect(calls[1][0].component).toContain("PageTemplate.js");
});
});