-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMarkdownRepository.spec.ts
More file actions
130 lines (100 loc) · 4.46 KB
/
MarkdownRepository.spec.ts
File metadata and controls
130 lines (100 loc) · 4.46 KB
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
import { join } from "path";
import { describe, it, expect } from "vitest";
import { DirectoryNotFound, FileNotFound } from "./errors";
import { MarkdownRepository } from "./MarkdownRepository";
describe("MarkdownRepository", () => {
const FIXTURES_DIR_PATH = join(__dirname, "../../tests/fixtures/cms/lib/MarkdownRepository");
describe(".fromDirectory", () => {
it("throws an error if the root directory does not exist", async () => {
expect(() => MarkdownRepository.fromDirectory("not-found-directory")).toThrowError(DirectoryNotFound);
});
it("builds the instance otherwise", async () => {
expect(MarkdownRepository.fromDirectory(join(FIXTURES_DIR_PATH))).toBeInstanceOf(MarkdownRepository);
});
});
describe("#all", () => {
it("returns an empty collection if there are no markdowns", async () => {
const subject = MarkdownRepository.fromDirectory(join(FIXTURES_DIR_PATH, "./empty"));
const result = await subject.all();
expect(result).toEqual([]);
});
it("returns all the available markdown files sorted by date", async () => {
const subject = MarkdownRepository.fromDirectory(join(FIXTURES_DIR_PATH, "./examples"));
const result = await subject.all();
expect(result).toEqual([
expect.objectContaining({
metadata: expect.objectContaining({ date: "2019-04-01" }),
}),
expect.objectContaining({
metadata: expect.objectContaining({ date: "2019-03-01" }),
}),
expect.objectContaining({
metadata: expect.objectContaining({ date: "2019-01-01" }),
}),
]);
});
describe("working with drafts", () => {
it("does not return drafts by default", async () => {
const subject = MarkdownRepository.fromDirectory(join(FIXTURES_DIR_PATH, "./drafts"));
const results = await subject.all();
expect(results).toEqual([
expect.objectContaining({
metadata: expect.objectContaining({ title: "Not a draft" }),
}),
]);
});
it("does not return drafts if explicitly asked not to", async () => {
const subject = MarkdownRepository.fromDirectory(join(FIXTURES_DIR_PATH, "./drafts"));
const results = await subject.all({ drafts: false });
expect(results).toEqual([
expect.objectContaining({
metadata: expect.objectContaining({ title: "Not a draft" }),
}),
]);
});
it("returns drafts if asked to", async () => {
const subject = MarkdownRepository.fromDirectory(join(FIXTURES_DIR_PATH, "./drafts"));
const results = await subject.all({ drafts: true });
expect(results).toEqual([
expect.objectContaining({
metadata: expect.objectContaining({ title: "Not a draft" }),
}),
expect.objectContaining({
metadata: expect.objectContaining({ title: "A draft" }),
}),
]);
});
});
});
describe("#show", () => {
it("throws an exception if the file does not exist", async () => {
const subject = MarkdownRepository.fromDirectory(join(FIXTURES_DIR_PATH, "./examples"));
await expect(subject.show("non-existing")).rejects.toThrowError(FileNotFound);
});
it("returns the file otherwise", async () => {
const subject = MarkdownRepository.fromDirectory(join(FIXTURES_DIR_PATH, "./examples"));
const result = await subject.show("first-article.md");
expect(result).toEqual(
expect.objectContaining({
metadata: expect.objectContaining({ date: "2019-03-01", title: "Mid article" }),
}),
);
});
describe("working with drafts", () => {
it("throws an exception if the file is a draft and drafts are not allowed", async () => {
const subject = MarkdownRepository.fromDirectory(join(FIXTURES_DIR_PATH, "./drafts"));
await expect(subject.show("draft.md")).rejects.toThrowError(FileNotFound);
await expect(subject.show("draft.md", { drafts: false })).rejects.toThrowError(FileNotFound);
});
it("returns the file if it is a draft and drafts are allowed", async () => {
const subject = MarkdownRepository.fromDirectory(join(FIXTURES_DIR_PATH, "./drafts"));
const result = await subject.show("draft.md", { drafts: true });
expect(result).toEqual(
expect.objectContaining({
metadata: expect.objectContaining({ title: "A draft" }),
}),
);
});
});
});
});