/
JSONPod.spec.ts
156 lines (146 loc) · 3.94 KB
/
JSONPod.spec.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
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
import { DNodeRawProps, Note, NoteRawProps } from "@dendronhq/common-all";
import {
EngineTestUtils,
FileTestUtils,
node2MdFile,
} from "@dendronhq/common-server";
import fs from "fs-extra";
import _ from "lodash";
import path from "path";
import { genPodConfig, getPodConfigPath } from "../..";
import { JSONExportPod } from "../JSONPod";
import { ExportConfig } from "packages/pods-core/lib/base";
const createNotes = (vaultPath: string, notes: Partial<NoteRawProps>[]) => {
node2MdFile(new Note({ fname: "root", id: "root", title: "root" }), {
root: vaultPath,
});
notes.map((n) => {
// @ts-ignore
node2MdFile(new Note(n), {
root: vaultPath,
});
});
};
const assertNodeMeta = (opts: {
expect: jest.Expect;
payload: DNodeRawProps[];
fields: string[];
expected: Partial<DNodeRawProps>[];
}) => {
const { expect, payload, fields, expected } = opts;
expect(_.sortBy(_.map(payload, (ent) => _.pick(ent, fields)))).toEqual(
expected
);
};
const assertNodeBody = (opts: {
expect: jest.Expect;
payload: DNodeRawProps[];
expected: { fname: string; body: string }[];
}) => {
const { expect, payload, expected } = opts;
expect(
_.sortBy(
_.map(payload, (ent) => {
const { body, fname } = _.pick(ent, ["body", "fname"]);
return {
fname,
body: _.trim(body),
};
})
)
).toEqual(expected);
};
function setup(opts: { notes: Partial<NoteRawProps>[] }) {
const podsDir = FileTestUtils.tmpDir().name;
const storeDir = EngineTestUtils.setupStoreDir({
copyFixtures: false,
initDirCb: (dirPath: string) => {
createNotes(dirPath, opts.notes);
},
});
return { podsDir, storeDir };
}
describe("JSONExportPod", () => {
let storeDir: string;
let podsDir: string;
const createNotes = (): Partial<NoteRawProps>[] => [
{ fname: "foo", body: "foo body" },
{ fname: "bar", body: "bar body" },
];
beforeEach(async () => {
({ storeDir, podsDir } = await setup({ notes: createNotes() }));
});
test("basic", async () => {
const pod = new JSONExportPod({ roots: [storeDir], podsDir });
const mode = "notes";
const metaOnly = false;
const destDir = FileTestUtils.tmpDir().name;
const destPath = path.join(destDir, "export.json");
const config = { dest: destPath };
await pod.plant({ mode, metaOnly, config });
const payload = fs.readJSONSync(destPath) as NoteRawProps[];
assertNodeMeta({
expect,
payload,
fields: ["fname"],
expected: [
{
fname: "root",
},
{ fname: "bar" },
{ fname: "foo" },
],
});
assertNodeBody({
expect,
payload,
expected: [
{
fname: "root",
body: "",
},
{ fname: "bar", body: "bar body" },
{ fname: "foo", body: "foo body" },
],
});
});
test("basic no body", async () => {
const pod = new JSONExportPod({ roots: [storeDir], podsDir });
const mode = "notes";
const destDir = FileTestUtils.tmpDir().name;
const destPath = path.join(destDir, "export.json");
const config: ExportConfig = { dest: destPath, includeBody: false };
await pod.plant({ mode, config });
const payload = fs.readJSONSync(destPath) as NoteRawProps[];
assertNodeMeta({
expect,
payload,
fields: ["fname"],
expected: [
{
fname: "root",
},
{ fname: "bar" },
{ fname: "foo" },
],
});
assertNodeBody({
expect,
payload,
expected: [
{
fname: "root",
body: "",
},
{ fname: "bar", body: "" },
{ fname: "foo", body: "" },
],
});
});
test("write config", () => {
genPodConfig(podsDir, JSONExportPod);
const configPath = getPodConfigPath(podsDir, JSONExportPod);
genPodConfig(podsDir, JSONExportPod);
expect(fs.readFileSync(configPath, { encoding: "utf8" })).toMatchSnapshot();
});
});