-
Notifications
You must be signed in to change notification settings - Fork 0
/
static.config.test.js
302 lines (253 loc) Β· 7.15 KB
/
static.config.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
const jdown = require("jdown");
const R = require("ramda");
const {
contentDir,
isPublished,
relatedSlugs,
relatedPosts,
injectRelatedPosts,
postsToPostPages,
rawDataToGetData,
stripPostContents,
stripRelatedPostsContent,
injectRelatedPostAuthors,
injectAuthor,
} = require("./static.config");
// state to load content to
let content;
beforeAll(async () => {
// kinda assumes that contentDir is correct before testing it
content = await jdown(contentDir);
});
test("contentDir is `./content`", () => {
expect(contentDir).toBe("./content");
});
describe("isPublished()", () => {
it("returns false if `publishedOn` is not presentl", () => {
expect(isPublished({})).toBeFalsy();
});
it("returns false if `publishedOn` is present but null", () => {
expect(isPublished({ publishedOn: null })).toBeFalsy();
});
it("returns true if `publishedOn` is not null", () => {
expect(isPublished({ publishedOn: "2020-10-10" })).toBeTruthy();
});
});
describe("relatedSlugs()", () => {
it("returns empty array if `relatedSlugs` are not present", () => {
expect(relatedSlugs({})).toEqual([]);
});
it("returns og array if `relatedSlugs` are present", () => {
expect(
relatedSlugs({
data: {
relatedSlugs: ["s1", "s2"],
},
}),
).toEqual(["s1", "s2"]);
});
});
describe("relatedPosts()", () => {
const posts = [
{
data: {
slug: 1,
relatedSlugs: [2],
},
},
{
data: {
slug: 2,
},
},
];
it("returns empty array if no related slugs exist", () => {
const post = posts[1];
const related = relatedPosts(posts, post);
expect(related).toEqual([]);
});
it("returns related posts if related slugs exist", () => {
const post = posts[0];
const related = relatedPosts(posts, post);
expect(related).toBeInstanceOf(Array);
expect(related).toHaveLength(1);
expect(related[0]).toHaveProperty("data.slug", 2);
});
});
describe("injectRelatedPosts()", () => {
const posts = [
{
data: {
slug: 1,
relatedSlugs: [2],
},
},
{
data: {
slug: 2,
},
},
];
const injectFn = injectRelatedPosts(posts);
it("injects empty array of no related slugs exist", () => {
const post = posts[1];
const injected = injectFn(post);
expect(injected.data.relatedPosts).toEqual([]);
});
it("injects related array of related slugs exist", () => {
const post = posts[0];
const injected = injectFn(post);
expect(injected.data.relatedPosts).toBeInstanceOf(Array);
expect(injected.data.relatedPosts).toHaveLength(1);
expect(injected.data.relatedPosts[0]).toHaveProperty("data.slug", 2);
});
it("maintains exisiting data post injection", () => {
const post = posts[0];
const injected = injectFn(post);
expect(injected.data.relatedSlugs).toEqual([2]);
});
});
describe("jdown()", () => {
it("has posts", () => {
expect(content).toHaveProperty("posts");
});
it("loads known posts", () => {
expect(content.posts).toHaveProperty(
"14LifeLessonsLearntAfter1YearOfGraduation",
);
expect(content.posts).toHaveProperty("5InternetPeopleIFollow");
expect(content.posts).toHaveProperty("clojure424Days");
});
it("loads post metadata", () => {
const clojure424 = content.posts["clojure424Days"];
const expectedKeys = [
"title",
"subTitle",
"slug",
"canonicalUrl",
"publishedOn",
"heroImg",
"tags",
"relatedSlugs",
"author",
"contents",
];
R.forEach((k) => {
expect(clojure424).toHaveProperty(k);
}, expectedKeys);
});
test("relatedSlugs and tags are arrays", () => {
const clojure424 = content.posts["clojure424Days"];
expect(clojure424.tags).toBeInstanceOf(Array);
expect(clojure424.relatedSlugs).toBeInstanceOf(Array);
});
it("has tags", () => {
expect(content).toHaveProperty("tags");
});
it("has authors", () => {
expect(content).toHaveProperty("authors");
});
it("loads author metadata", () => {
const me = content.authors["shivekKhurana"];
const requiredKeys = [
"slug",
"name",
"profilePicture",
"shortBio",
"contents",
];
R.forEach((k) => {
expect(me).toHaveProperty(k);
}, requiredKeys);
});
});
describe("postsToPostPages()", () => {
const postPages = postsToPostPages([{ slug: "test-slug", flag: "x" }]);
it("returns RS meta data", () => {
const first = postPages[0];
expect(first).toHaveProperty("path", "/blog/test-slug");
expect(first).toHaveProperty("template", "src/templates/blog/Post");
expect(first).toHaveProperty("data");
expect(first.data).toHaveProperty("flag", "x");
});
});
describe("stipPostContents()", () => {
const post = { data: { contents: "x" } };
it("strips contents from data", () => {
expect(stripPostContents(post)).not.toHaveProperty("data.contents");
});
});
describe("stripRelatedPostsContent()", () => {
const post = {
data: {
relatedPosts: [
{
data: { contents: "x" },
},
{
data: { contents: "y" },
},
],
},
};
it("strips contents of related posts", () => {
const stripped = stripRelatedPostsContent(post);
expect(stripped).not.toHaveProperty("data.relatedPosts.0.data.contents");
expect(stripped).not.toHaveProperty("data.relatedPosts.1.data.contents");
});
});
describe("rawDataToGetData()", () => {
const post = { data: "x" };
const withGetData = rawDataToGetData(post);
it("converts data to a getData fn", () => {
expect(withGetData.getData).toBeInstanceOf(Function);
expect(withGetData.getData()).toEqual(post.data);
});
});
describe("injectRelatedPostauthors()", () => {
const authors = {
x: { slug: "x", id: 1, data: "x-data" },
y: { slug: "y", id: 2, data: "y-data" },
};
const post = {
data: {
relatedPosts: [
{
data: {
author: "x",
},
},
{
data: {
author: "y",
},
},
],
},
};
const withAuthors = injectRelatedPostAuthors(authors)(post);
it("should inject author data in place of author slug", () => {
expect(withAuthors.data.relatedPosts).toBeInstanceOf(Array);
expect(withAuthors.data.relatedPosts).toHaveLength(2);
const first = withAuthors.data.relatedPosts[0];
const second = withAuthors.data.relatedPosts[1];
expect(first).toHaveProperty("data.author", authors.x);
expect(second).toHaveProperty("data.author", authors.y);
});
});
describe("injectAuthor()", () => {
const authors = [
{ slug: "x", contents: "x contents", name: "xname" },
{ slug: "y", contents: "yc" },
];
const post = { data: { author: "x" } };
const injectFn = injectAuthor(authors);
const injectedPost = injectFn(post);
it("replaces author slug with author in post", () => {
expect(injectedPost).toHaveProperty("data.author.slug", "x");
expect(injectedPost).toHaveProperty("data.author.name", "xname");
});
it("strips author contents before injection", () => {
expect(injectedPost).not.toHaveProperty("data.author.contents");
});
});