Skip to content

Commit 135f176

Browse files
committed
chore: Add documenter tests
1 parent 8abde3a commit 135f176

File tree

3 files changed

+133
-3
lines changed

3 files changed

+133
-3
lines changed

src/__tests__/__snapshots__/documenter.test.ts.snap

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,98 @@ Defaults to \`false\`.",
9494
"releaseStatus": "stable",
9595
}
9696
`;
97+
98+
exports[`definition for code-view matches the snapshot > code-view 1`] = `
99+
{
100+
"dashCaseName": "code-view",
101+
"events": [],
102+
"functions": [],
103+
"name": "CodeView",
104+
"properties": [
105+
{
106+
"description": "Adds an \`aria-label\` to the component. Use this label when there is not enough context around the code snippet to describe its purpose or content.",
107+
"name": "ariaLabel",
108+
"optional": true,
109+
"type": "string",
110+
},
111+
{
112+
"description": "Adds \`aria-labelledby\` to the component. Use this property to reference the ID of an existing element that provides a descriptive label for the code snippet.",
113+
"name": "ariaLabelledby",
114+
"optional": true,
115+
"type": "string",
116+
},
117+
{
118+
"description": "The code content to be displayed.",
119+
"name": "content",
120+
"optional": false,
121+
"type": "string",
122+
},
123+
{
124+
"description": "A function to perform custom syntax highlighting.",
125+
"inlineType": {
126+
"name": "(code: string) => React.ReactNode",
127+
"parameters": [
128+
{
129+
"name": "code",
130+
"type": "string",
131+
},
132+
],
133+
"returnType": "React.ReactNode",
134+
"type": "function",
135+
},
136+
"name": "highlight",
137+
"optional": true,
138+
"type": "((code: string) => React.ReactNode)",
139+
},
140+
{
141+
"description": "An object containing all the necessary localized strings required by the component. The object should contain:
142+
143+
* \`lineNumberLabel\` - Label for the column that displays line numbers (when line numbers are visible)
144+
* \`codeLabel\` - Label for the column that displays the code content (when line numbers are visible)",
145+
"inlineType": {
146+
"name": "CodeViewProps.I18nStrings",
147+
"properties": [
148+
{
149+
"name": "codeLabel",
150+
"optional": true,
151+
"type": "string",
152+
},
153+
{
154+
"name": "lineNumberLabel",
155+
"optional": true,
156+
"type": "string",
157+
},
158+
],
159+
"type": "object",
160+
},
161+
"name": "i18nStrings",
162+
"optional": true,
163+
"type": "CodeViewProps.I18nStrings",
164+
},
165+
{
166+
"description": "Controls the display of line numbers.
167+
168+
Defaults to \`false\`.",
169+
"name": "lineNumbers",
170+
"optional": true,
171+
"type": "boolean",
172+
},
173+
{
174+
"description": "Controls whether line-wrapping is enabled when content would overflow the component.
175+
176+
Defaults to \`false\`.",
177+
"name": "wrapLines",
178+
"optional": true,
179+
"type": "boolean",
180+
},
181+
],
182+
"regions": [
183+
{
184+
"description": "An optional slot to display a button to enable users to perform actions, such as copy or download the code snippet.",
185+
"isDefault": false,
186+
"name": "actions",
187+
},
188+
],
189+
"releaseStatus": "stable",
190+
}
191+
`;

src/__tests__/documenter.test.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@
22
// SPDX-License-Identifier: Apache-2.0
33
import { expect, test } from "vitest";
44

5-
import apiDocs from "../../lib/components/internal/api-docs/components";
5+
import componentDefinitions from "../../lib/components/internal/api-docs/components";
6+
import { getAllComponents } from "./utils";
67

7-
test.each(Object.entries(apiDocs))("definition for $0 matches the snapshot", (_name, definition) => {
8-
expect(definition).toMatchSnapshot();
8+
test.each<string>(getAllComponents())(`definition for %s matches the snapshot`, (componentName: string) => {
9+
const definition = componentDefinitions[componentName];
10+
11+
// overriding with a fake value so that when there are icon changes in components this test doesn't block it
12+
const iconNameDefinition = definition.properties.find(({ name }: { name: string }) => name === "iconName");
13+
if (iconNameDefinition && iconNameDefinition.inlineType?.type === "union") {
14+
iconNameDefinition.inlineType.values = ["comes from @cloudscape-design/components"];
15+
}
16+
expect(definition).toMatchSnapshot(componentName);
917
});

src/__tests__/utils.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* eslint-env node */
2+
/* eslint-disable header/header */
3+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
// SPDX-License-Identifier: Apache-2.0
5+
import * as fs from "node:fs";
6+
import * as path from "node:path";
7+
8+
const componentsDir = path.resolve(__dirname, "../../lib/components");
9+
10+
export function getAllComponents(): string[] {
11+
return fs
12+
.readdirSync(componentsDir)
13+
.filter(
14+
(name) =>
15+
name !== "internal" &&
16+
name !== "test-utils" &&
17+
!name.includes(".") &&
18+
!name.includes("LICENSE") &&
19+
!name.includes("NOTICE"),
20+
);
21+
}
22+
23+
export async function requireComponent(componentName: string) {
24+
// eslint-disable-next-line no-unsanitized/method
25+
const { default: Component } = await import(path.join(componentsDir, componentName));
26+
return Component;
27+
}

0 commit comments

Comments
 (0)