This repository has been archived by the owner on Dec 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.ts
169 lines (156 loc) · 5.4 KB
/
index.test.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
157
158
159
160
161
162
163
164
165
166
167
168
169
/** @license Apache-2.0
*
* Copyright 2024 8 Hobbies, LLC <hong@8hobbies.com>
*
* Licensed under the Apache License, Version 2.0(the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { JSDOM } from "jsdom";
import fs from "fs";
import path from "path";
import { spawnSync } from "child_process";
const npmExec = "npm" as const;
const npxExec = "npx" as const;
describe("All", () => {
// Dir that contains files used for test.
const testSrcDir = path.join(__dirname, "test-dir");
// Dir in which the test performs.
const testDir = path.join(__dirname, "test-dir-tmp");
// html file names.
const htmlNames = [
"index.html",
"modules.html",
"functions/func.html",
] as const;
// Default extra footer class name.
const defaultExtraFooterClass = "extra-footer" as const;
// Package version number.
const packageJson: unknown = JSON.parse(
fs.readFileSync("package.json", "utf-8"),
);
if (
typeof packageJson !== "object" ||
packageJson === null ||
!("version" in packageJson) ||
typeof packageJson.version !== "string"
) {
throw new Error("Invalid package version in package.json.");
}
const packageVersion = packageJson.version;
const minTypedocConfig = {
$schema: "https://typedoc.org/schema.json",
entryPoints: ["./index.ts"],
plugin: ["typedoc-plugin-extra-footer"],
} as const;
// Same as spawnSync, except it throws an error if the spawn fails.
function spawnSyncWithError(
...args: Parameters<typeof spawnSync>
): ReturnType<typeof spawnSync> {
const result = spawnSync(...args);
if ("error" in result) {
throw new Error(JSON.stringify(result));
}
return result;
}
beforeEach(() => {
if (fs.existsSync(testDir)) {
fs.rmSync(testDir, { recursive: true, force: true });
}
// Fill the test dir with needed content.
fs.mkdirSync(testDir);
for (const f of [
"README.md",
"index.ts",
"package.json",
"tsconfig.json",
]) {
fs.cpSync(path.join(testSrcDir, f), path.join(testDir, f));
}
});
// Prepare the directory, run `npm install` and run typedoc.
function runTypedoc(
typedocConfig: typeof minTypedocConfig & {
extraFooter?: string;
extraFooterNoDefaultWrapper?: boolean;
},
): void {
fs.writeFileSync(
path.join(testDir, "typedoc.json"),
JSON.stringify(typedocConfig),
);
spawnSyncWithError(npmExec, ["pack"], { shell: true });
spawnSyncWithError(npmExec, ["install"], {
cwd: testDir,
shell: true,
});
spawnSyncWithError(
npmExec,
["install", `../typedoc-plugin-extra-footer-${packageVersion}.tgz`],
{
cwd: testDir,
shell: true,
},
);
spawnSyncWithError(npxExec, ["typedoc"], {
cwd: testDir,
shell: true,
});
}
test("When extraFooter is unspecified, does not generate the extra footer", () => {
runTypedoc(minTypedocConfig);
const htmlPaths = htmlNames.map((elem) => path.join(testDir, "docs", elem));
for (const htmlPath of htmlPaths) {
expect(fs.readFileSync(htmlPath, "utf-8")).not.toContain(
defaultExtraFooterClass,
);
}
});
test("When extraFooter is specified, generates extra footer with the wrapper", async () => {
const typedocConfig = {
...minTypedocConfig,
extraFooter: "Great <strong>footer</strong>",
} as const;
runTypedoc(typedocConfig);
const htmlPaths = htmlNames.map((elem) => path.join(testDir, "docs", elem));
for (const htmlPath of htmlPaths) {
const dom = await JSDOM.fromFile(htmlPath, { contentType: "text/html" });
const footer = dom.window.document.getElementsByTagName("footer")[0];
const extraFooter = footer.getElementsByClassName(
defaultExtraFooterClass,
);
expect(extraFooter).toHaveLength(1);
expect(extraFooter[0].innerHTML).toBe(typedocConfig.extraFooter);
}
});
test("When extraFooterNoDefaultWrapper is specified, generates extra footer without the wrapper", async () => {
const innerContent = "Great <strong>footer</strong>" as const;
const extraFooterClassName = "custom-extra-footer" as const;
const typedocConfig = {
...minTypedocConfig,
extraFooter: `<div class="${extraFooterClassName}">${innerContent}</div>`,
extraFooterNoDefaultWrapper: true,
} as const;
runTypedoc(typedocConfig);
const htmlPaths = htmlNames.map((elem) => path.join(testDir, "docs", elem));
for (const htmlPath of htmlPaths) {
const dom = await JSDOM.fromFile(htmlPath, { contentType: "text/html" });
const footer = dom.window.document.getElementsByTagName("footer")[0];
const defaultExtraFooter = footer.getElementsByClassName(
defaultExtraFooterClass,
);
expect(defaultExtraFooter).toHaveLength(0);
const extraFooter = footer.getElementsByClassName(extraFooterClassName);
expect(extraFooter).toHaveLength(1);
expect(extraFooter[0].innerHTML).toBe(innerContent);
}
});
});