docusaurus-plugin-generate-schema-docs 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -6
- package/__tests__/ExampleDataLayer.test.js +78 -155
- package/__tests__/__fixtures__/static/schemas/add-to-cart-event.json +4 -15
- package/__tests__/__fixtures__/static/schemas/choice-event.json +72 -0
- package/__tests__/__fixtures__/static/schemas/components/dataLayer.json +52 -54
- package/__tests__/__fixtures__/static/schemas/components/product.json +124 -210
- package/__tests__/__fixtures__/static/schemas/nested/child-event.json +10 -0
- package/__tests__/__fixtures__/static/schemas/nested/grandchild-a.json +9 -0
- package/__tests__/__fixtures__/static/schemas/nested/grandchild-b.json +9 -0
- package/__tests__/__fixtures__/static/schemas/nested/parent-event.json +7 -0
- package/__tests__/__fixtures__/static/schemas/root-any-of-event.json +34 -0
- package/__tests__/__fixtures__/static/schemas/root-choice-event.json +36 -0
- package/__tests__/__fixtures__/validateSchemas/circular-schema.json +6 -6
- package/__tests__/__fixtures__/validateSchemas/components/referenced.json +9 -7
- package/__tests__/__fixtures__/validateSchemas/invalid-example-schema.json +7 -7
- package/__tests__/__fixtures__/validateSchemas/main-schema-with-missing-ref.json +7 -7
- package/__tests__/__fixtures__/validateSchemas/main-schema-with-ref.json +7 -7
- package/__tests__/__fixtures__/validateSchemas/no-example-schema.json +11 -11
- package/__tests__/__fixtures__/validateSchemas/schema-A.json +5 -5
- package/__tests__/__fixtures__/validateSchemas/schema-B.json +5 -5
- package/__tests__/__fixtures__/validateSchemas/valid-schema.json +7 -7
- package/__tests__/__fixtures_versioned__/static/schemas/1.1.1/add-to-cart-event.json +44 -0
- package/__tests__/__fixtures_versioned__/static/schemas/1.1.1/components/dataLayer.json +56 -0
- package/__tests__/__fixtures_versioned__/static/schemas/1.1.1/components/product.json +125 -0
- package/__tests__/__fixtures_versioned__/static/schemas/next/add-to-cart-event.json +44 -0
- package/__tests__/__fixtures_versioned__/static/schemas/next/components/dataLayer.json +56 -0
- package/__tests__/__fixtures_versioned__/static/schemas/next/components/product.json +125 -0
- package/__tests__/__fixtures_versioned__/versions.json +1 -0
- package/__tests__/__snapshots__/ExampleDataLayer.test.js.snap +117 -0
- package/__tests__/__snapshots__/generateEventDocs.nested.test.js.snap +92 -0
- package/__tests__/__snapshots__/generateEventDocs.test.js.snap +113 -15
- package/__tests__/__snapshots__/generateEventDocs.versioned.test.js.snap +53 -0
- package/__tests__/components/FoldableRows.test.js +330 -0
- package/__tests__/components/PropertiesTable.test.js +31 -14
- package/__tests__/components/PropertyRow.test.js +471 -51
- package/__tests__/components/SchemaJsonViewer.test.js +23 -19
- package/__tests__/components/SchemaRows.test.js +96 -66
- package/__tests__/components/SchemaViewer.test.js +34 -17
- package/__tests__/components/TableHeader.test.js +12 -12
- package/__tests__/generateEventDocs.nested.test.js +80 -0
- package/__tests__/generateEventDocs.test.js +77 -71
- package/__tests__/generateEventDocs.versioned.test.js +69 -0
- package/__tests__/helpers/buildExampleFromSchema.test.js +160 -160
- package/__tests__/helpers/file-system.test.js +44 -0
- package/__tests__/helpers/getConstraints.test.js +48 -48
- package/__tests__/helpers/loadSchema.test.js +11 -5
- package/__tests__/helpers/path-helpers.test.js +34 -0
- package/__tests__/helpers/processSchema.test.js +42 -22
- package/__tests__/helpers/schema-processing.test.js +82 -0
- package/__tests__/helpers/schemaToExamples.test.js +56 -0
- package/__tests__/helpers/schemaToTableData.filtering.test.js +65 -0
- package/__tests__/helpers/schemaToTableData.hierarchicalLines.test.js +539 -0
- package/__tests__/helpers/schemaToTableData.test.js +222 -0
- package/__tests__/helpers/update-schema-ids.test.js +107 -0
- package/__tests__/update-schema-ids.test.js +39 -0
- package/__tests__/validateSchemas.test.js +125 -88
- package/components/ExampleDataLayer.js +59 -28
- package/components/FoldableRows.js +164 -0
- package/components/PropertiesTable.js +10 -7
- package/components/PropertyRow.js +169 -60
- package/components/SchemaJsonViewer.js +6 -6
- package/components/SchemaRows.css +236 -14
- package/components/SchemaRows.js +24 -41
- package/components/SchemaViewer.js +19 -13
- package/components/TableHeader.js +12 -12
- package/generateEventDocs.js +141 -61
- package/helpers/buildExampleFromSchema.js +58 -72
- package/helpers/choice-index-template.js +22 -0
- package/helpers/file-system.js +32 -0
- package/helpers/getConstraints.js +43 -44
- package/helpers/loadSchema.js +2 -2
- package/helpers/path-helpers.js +22 -0
- package/helpers/processSchema.js +19 -19
- package/helpers/{mdx-template.js → schema-doc-template.js} +12 -12
- package/helpers/schema-processing.js +75 -0
- package/helpers/schemaToExamples.js +99 -0
- package/helpers/schemaToTableData.js +311 -0
- package/helpers/update-schema-ids.js +47 -0
- package/index.js +143 -54
- package/package.json +1 -1
- package/validateSchemas.js +54 -71
|
@@ -4,77 +4,107 @@ import { render } from '@testing-library/react';
|
|
|
4
4
|
import SchemaRows from '../../components/SchemaRows';
|
|
5
5
|
|
|
6
6
|
// Mock child components
|
|
7
|
-
jest.mock('../../components/PropertyRow', () =>
|
|
8
|
-
|
|
7
|
+
jest.mock('../../components/PropertyRow', () => {
|
|
8
|
+
const MockPropertyRow = (props) => (
|
|
9
|
+
<tr>
|
|
10
|
+
<td>Mocked PropertyRow: {props.row.name}</td>
|
|
11
|
+
</tr>
|
|
12
|
+
);
|
|
13
|
+
MockPropertyRow.displayName = 'MockPropertyRow';
|
|
14
|
+
return MockPropertyRow;
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
jest.mock('../../components/FoldableRows', () => {
|
|
18
|
+
const MockFoldableRows = (props) => (
|
|
19
|
+
<tr>
|
|
20
|
+
<td>Mocked FoldableRows: {props.row.choiceType}</td>
|
|
21
|
+
</tr>
|
|
22
|
+
);
|
|
23
|
+
MockFoldableRows.displayName = 'MockFoldableRows';
|
|
24
|
+
return MockFoldableRows;
|
|
25
|
+
});
|
|
9
26
|
|
|
10
27
|
describe('SchemaRows', () => {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
28
|
+
it('renders a PropertyRow for each property type item in tableData', () => {
|
|
29
|
+
const tableData = [
|
|
30
|
+
{ type: 'property', name: 'name', path: ['name'] },
|
|
31
|
+
{ type: 'property', name: 'age', path: ['age'] },
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
const { getByText } = render(
|
|
35
|
+
<table>
|
|
36
|
+
<tbody>
|
|
37
|
+
<SchemaRows tableData={tableData} />
|
|
38
|
+
</tbody>
|
|
39
|
+
</table>,
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
expect(getByText('Mocked PropertyRow: name')).toBeInTheDocument();
|
|
43
|
+
expect(getByText('Mocked PropertyRow: age')).toBeInTheDocument();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('renders nested properties from a flat list', () => {
|
|
47
|
+
const tableData = [
|
|
48
|
+
{ type: 'property', name: 'user', path: ['user'], level: 0 },
|
|
49
|
+
{ type: 'property', name: 'id', path: ['user', 'id'], level: 1 },
|
|
50
|
+
];
|
|
51
|
+
|
|
52
|
+
const { getByText } = render(
|
|
53
|
+
<table>
|
|
54
|
+
<tbody>
|
|
55
|
+
<SchemaRows tableData={tableData} />
|
|
56
|
+
</tbody>
|
|
57
|
+
</table>,
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
// It should render both the parent and child property from the flat list
|
|
61
|
+
expect(getByText('Mocked PropertyRow: user')).toBeInTheDocument();
|
|
62
|
+
expect(getByText('Mocked PropertyRow: id')).toBeInTheDocument();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('renders a FoldableRows for choice type items in tableData', () => {
|
|
66
|
+
const tableData = [
|
|
67
|
+
{
|
|
68
|
+
type: 'choice',
|
|
69
|
+
choiceType: 'oneOf',
|
|
70
|
+
path: ['choice'],
|
|
71
|
+
options: [],
|
|
72
|
+
},
|
|
73
|
+
];
|
|
23
74
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
75
|
+
const { getByText } = render(
|
|
76
|
+
<table>
|
|
77
|
+
<tbody>
|
|
78
|
+
<SchemaRows tableData={tableData} />
|
|
79
|
+
</tbody>
|
|
80
|
+
</table>,
|
|
81
|
+
);
|
|
27
82
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
user: {
|
|
31
|
-
type: 'object',
|
|
32
|
-
properties: {
|
|
33
|
-
name: { type: 'string' },
|
|
34
|
-
},
|
|
35
|
-
},
|
|
36
|
-
};
|
|
37
|
-
const { getByText } = render(
|
|
38
|
-
<table>
|
|
39
|
-
<tbody>
|
|
40
|
-
<SchemaRows properties={properties} getConstraints={() => []} />
|
|
41
|
-
</tbody>
|
|
42
|
-
</table>
|
|
43
|
-
);
|
|
83
|
+
expect(getByText('Mocked FoldableRows: oneOf')).toBeInTheDocument();
|
|
84
|
+
});
|
|
44
85
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
86
|
+
it('renders a mix of properties and choices', () => {
|
|
87
|
+
const tableData = [
|
|
88
|
+
{ type: 'property', name: 'prop1', path: ['prop1'] },
|
|
89
|
+
{
|
|
90
|
+
type: 'choice',
|
|
91
|
+
choiceType: 'anyOf',
|
|
92
|
+
path: ['choice'],
|
|
93
|
+
options: [],
|
|
94
|
+
},
|
|
95
|
+
{ type: 'property', name: 'prop2', path: ['prop2'] },
|
|
96
|
+
];
|
|
51
97
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
name: { type: 'string' },
|
|
60
|
-
price: { type: 'number' },
|
|
61
|
-
},
|
|
62
|
-
},
|
|
63
|
-
},
|
|
64
|
-
};
|
|
65
|
-
const { getByText } = render(
|
|
66
|
-
<table>
|
|
67
|
-
<tbody>
|
|
68
|
-
<SchemaRows properties={properties} getConstraints={() => []} />
|
|
69
|
-
</tbody>
|
|
70
|
-
</table>
|
|
71
|
-
);
|
|
98
|
+
const { getByText } = render(
|
|
99
|
+
<table>
|
|
100
|
+
<tbody>
|
|
101
|
+
<SchemaRows tableData={tableData} />
|
|
102
|
+
</tbody>
|
|
103
|
+
</table>,
|
|
104
|
+
);
|
|
72
105
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
expect(getByText('Mocked PropertyRow: name')).toBeInTheDocument();
|
|
78
|
-
expect(getByText('Mocked PropertyRow: price')).toBeInTheDocument();
|
|
79
|
-
});
|
|
106
|
+
expect(getByText('Mocked PropertyRow: prop1')).toBeInTheDocument();
|
|
107
|
+
expect(getByText('Mocked FoldableRows: anyOf')).toBeInTheDocument();
|
|
108
|
+
expect(getByText('Mocked PropertyRow: prop2')).toBeInTheDocument();
|
|
109
|
+
});
|
|
80
110
|
});
|
|
@@ -1,27 +1,44 @@
|
|
|
1
1
|
import '@testing-library/jest-dom';
|
|
2
2
|
import React from 'react';
|
|
3
|
-
import { render } from '@testing-library/react';
|
|
3
|
+
import { render, screen, within } from '@testing-library/react';
|
|
4
4
|
import SchemaViewer from '../../components/SchemaViewer';
|
|
5
|
+
import choiceEventSchema from '../__fixtures__/static/schemas/choice-event.json';
|
|
5
6
|
|
|
6
|
-
// Mock
|
|
7
|
+
// Mock the CodeBlock component as it's not relevant to this test
|
|
8
|
+
jest.mock('@theme/CodeBlock', () => {
|
|
9
|
+
const MockCodeBlock = ({ children }) => <pre>{children}</pre>;
|
|
10
|
+
MockCodeBlock.displayName = 'MockCodeBlock';
|
|
11
|
+
return MockCodeBlock;
|
|
12
|
+
});
|
|
7
13
|
|
|
8
|
-
|
|
9
|
-
jest.mock('
|
|
14
|
+
// Mock the Heading component
|
|
15
|
+
jest.mock('@theme/Heading', () => {
|
|
16
|
+
const MockHeading = ({ as: Component, ...props }) => <Component {...props} />;
|
|
17
|
+
MockHeading.displayName = 'MockHeading';
|
|
18
|
+
return MockHeading;
|
|
19
|
+
});
|
|
10
20
|
|
|
11
21
|
describe('SchemaViewer', () => {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
22
|
+
it('renders complex schema with payment_method and required indicators', () => {
|
|
23
|
+
render(<SchemaViewer schema={choiceEventSchema} />);
|
|
24
|
+
|
|
25
|
+
// Check that payment_method property name is rendered within the table
|
|
26
|
+
const table = screen.getByRole('table');
|
|
27
|
+
expect(within(table).getByText('payment_method')).toBeInTheDocument();
|
|
28
|
+
|
|
29
|
+
// Check that the "required" text is rendered for the user_id choice
|
|
30
|
+
const userIdChoiceHeader = screen.getByText(
|
|
31
|
+
(content, element) =>
|
|
32
|
+
element.tagName.toLowerCase() === 'h4' &&
|
|
33
|
+
content.includes('Select one of the following options:'),
|
|
34
|
+
);
|
|
35
|
+
expect(userIdChoiceHeader).toBeInTheDocument();
|
|
19
36
|
|
|
20
|
-
|
|
37
|
+
// The required text is rendered as a 'required' constraint code block.
|
|
38
|
+
// We expect it for 'event', 'user_id', and 'payment_method' at the top level.
|
|
39
|
+
const requiredConstraints = within(table).getAllByText('required');
|
|
21
40
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
expect(getByText('Mocked PropertiesTable')).toBeInTheDocument();
|
|
26
|
-
});
|
|
41
|
+
// There should be at least 3 required constraints visible at the top level.
|
|
42
|
+
expect(requiredConstraints.length).toBeGreaterThanOrEqual(3);
|
|
43
|
+
});
|
|
27
44
|
});
|
|
@@ -4,17 +4,17 @@ import { render } from '@testing-library/react';
|
|
|
4
4
|
import TableHeader from '../../components/TableHeader';
|
|
5
5
|
|
|
6
6
|
describe('TableHeader', () => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
it('renders the table header with correct columns', () => {
|
|
8
|
+
const { getByText } = render(
|
|
9
|
+
<table>
|
|
10
|
+
<TableHeader />
|
|
11
|
+
</table>,
|
|
12
|
+
);
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
14
|
+
expect(getByText('Property')).toBeInTheDocument();
|
|
15
|
+
expect(getByText('Type')).toBeInTheDocument();
|
|
16
|
+
expect(getByText('Constraints')).toBeInTheDocument();
|
|
17
|
+
expect(getByText('Examples')).toBeInTheDocument();
|
|
18
|
+
expect(getByText('Description')).toBeInTheDocument();
|
|
19
|
+
});
|
|
20
20
|
});
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jest-environment node
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import generateEventDocs from '../generateEventDocs';
|
|
6
|
+
import fs from 'fs';
|
|
7
|
+
import path from 'path';
|
|
8
|
+
|
|
9
|
+
jest.mock('fs', () => {
|
|
10
|
+
const memfs = require('memfs');
|
|
11
|
+
return memfs;
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
describe('generateEventDocs (nested oneOf)', () => {
|
|
15
|
+
const testSiteDir = path.resolve(__dirname, '__fixtures_nested__');
|
|
16
|
+
const options = {
|
|
17
|
+
organizationName: 'test-org',
|
|
18
|
+
projectName: 'test-project',
|
|
19
|
+
siteDir: testSiteDir,
|
|
20
|
+
url: 'https://tracking-docs-demo.buchert.digital',
|
|
21
|
+
};
|
|
22
|
+
const baseOutputDir = path.join(options.siteDir, 'docs');
|
|
23
|
+
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
fs.vol.reset();
|
|
26
|
+
const realFs = jest.requireActual('fs');
|
|
27
|
+
const nestedSchemasDir = path.resolve(
|
|
28
|
+
__dirname,
|
|
29
|
+
'__fixtures__/static/schemas/nested',
|
|
30
|
+
);
|
|
31
|
+
const targetSchemasDir = path.join(testSiteDir, 'static/schemas');
|
|
32
|
+
fs.vol.mkdirSync(targetSchemasDir, { recursive: true });
|
|
33
|
+
|
|
34
|
+
const files = realFs.readdirSync(nestedSchemasDir, { withFileTypes: true });
|
|
35
|
+
for (const file of files) {
|
|
36
|
+
const filePath = path.join(nestedSchemasDir, file.name);
|
|
37
|
+
const content = realFs.readFileSync(filePath);
|
|
38
|
+
fs.vol.writeFileSync(path.join(targetSchemasDir, file.name), content);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('should generate nested documentation correctly', async () => {
|
|
43
|
+
console.log = jest.fn(); // suppress console.log
|
|
44
|
+
|
|
45
|
+
await generateEventDocs(options);
|
|
46
|
+
|
|
47
|
+
const parentDir = path.join(baseOutputDir, 'parent-event');
|
|
48
|
+
// The child directory is prefixed because it's an item in the parent's oneOf
|
|
49
|
+
const childDir = path.join(parentDir, '01-child-event');
|
|
50
|
+
|
|
51
|
+
// Check for directory structure
|
|
52
|
+
expect(fs.existsSync(parentDir)).toBe(true);
|
|
53
|
+
expect(fs.existsSync(childDir)).toBe(true);
|
|
54
|
+
|
|
55
|
+
const parentIndex = fs.readFileSync(
|
|
56
|
+
path.join(parentDir, 'index.mdx'),
|
|
57
|
+
'utf-8',
|
|
58
|
+
);
|
|
59
|
+
expect(parentIndex).toMatchSnapshot();
|
|
60
|
+
|
|
61
|
+
const childIndex = fs.readFileSync(
|
|
62
|
+
path.join(childDir, 'index.mdx'),
|
|
63
|
+
'utf-8',
|
|
64
|
+
);
|
|
65
|
+
expect(childIndex).toMatchSnapshot();
|
|
66
|
+
|
|
67
|
+
// Check content of generated files
|
|
68
|
+
const grandchildA = fs.readFileSync(
|
|
69
|
+
path.join(childDir, '01-grandchild-a.mdx'),
|
|
70
|
+
'utf-8',
|
|
71
|
+
);
|
|
72
|
+
expect(grandchildA).toMatchSnapshot();
|
|
73
|
+
|
|
74
|
+
const grandchildB = fs.readFileSync(
|
|
75
|
+
path.join(childDir, '02-grandchild-b.mdx'),
|
|
76
|
+
'utf-8',
|
|
77
|
+
);
|
|
78
|
+
expect(grandchildB).toMatchSnapshot();
|
|
79
|
+
});
|
|
80
|
+
});
|
|
@@ -7,78 +7,84 @@ import fs from 'fs';
|
|
|
7
7
|
import path from 'path';
|
|
8
8
|
|
|
9
9
|
jest.mock('fs', () => {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
...originalFs,
|
|
13
|
-
writeFileSync: jest.fn(),
|
|
14
|
-
mkdirSync: jest.fn(),
|
|
15
|
-
existsSync: jest.fn(),
|
|
16
|
-
readdirSync: originalFs.readdirSync,
|
|
17
|
-
readFileSync: originalFs.readFileSync,
|
|
18
|
-
};
|
|
10
|
+
const memfs = require('memfs');
|
|
11
|
+
return memfs;
|
|
19
12
|
});
|
|
20
13
|
|
|
21
|
-
describe('generateEventDocs', () => {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
14
|
+
describe('generateEventDocs (non-versioned)', () => {
|
|
15
|
+
const options = {
|
|
16
|
+
organizationName: 'test-org',
|
|
17
|
+
projectName: 'test-project',
|
|
18
|
+
// Use the fixtures directory as the siteDir for tests
|
|
19
|
+
siteDir: path.resolve(__dirname, '__fixtures__'),
|
|
20
|
+
url: 'https://tracking-docs-demo.buchert.digital',
|
|
21
|
+
};
|
|
22
|
+
const outputDir = path.join(options.siteDir, 'docs');
|
|
23
|
+
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
fs.vol.reset();
|
|
26
|
+
const realFs = jest.requireActual('fs');
|
|
27
|
+
const fixturesDir = path.resolve(__dirname, '__fixtures__');
|
|
28
|
+
|
|
29
|
+
function readDirRecursive(dir) {
|
|
30
|
+
const files = realFs.readdirSync(dir, { withFileTypes: true });
|
|
31
|
+
for (const file of files) {
|
|
32
|
+
const filePath = path.join(dir, file.name);
|
|
33
|
+
if (file.isDirectory()) {
|
|
34
|
+
fs.vol.mkdirSync(filePath, { recursive: true });
|
|
35
|
+
readDirRecursive(filePath);
|
|
36
|
+
} else {
|
|
37
|
+
const content = realFs.readFileSync(filePath);
|
|
38
|
+
fs.vol.writeFileSync(filePath, content);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
28
41
|
}
|
|
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
|
-
expect(fs.writeFileSync).toHaveBeenCalledTimes(1);
|
|
78
|
-
|
|
79
|
-
// Check the content of the generated file
|
|
80
|
-
const [filePath, content] = fs.writeFileSync.mock.calls[0];
|
|
81
|
-
expect(filePath).toBe(path.join(outputDir, 'add-to-cart-event.mdx'));
|
|
82
|
-
expect(content).toMatchSnapshot();
|
|
83
|
-
});
|
|
42
|
+
readDirRecursive(fixturesDir);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('should generate documentation correctly', async () => {
|
|
46
|
+
console.log = jest.fn(); // suppress console.log
|
|
47
|
+
|
|
48
|
+
await generateEventDocs(options);
|
|
49
|
+
|
|
50
|
+
const choiceEventDir = path.join(outputDir, 'root-choice-event');
|
|
51
|
+
expect(fs.existsSync(choiceEventDir)).toBe(true);
|
|
52
|
+
|
|
53
|
+
// Check content of generated files
|
|
54
|
+
const addToCart = fs.readFileSync(
|
|
55
|
+
path.join(outputDir, 'add-to-cart-event.mdx'),
|
|
56
|
+
'utf-8',
|
|
57
|
+
);
|
|
58
|
+
expect(addToCart).toMatchSnapshot();
|
|
59
|
+
|
|
60
|
+
const choiceEvent = fs.readFileSync(
|
|
61
|
+
path.join(outputDir, 'choice-event.mdx'),
|
|
62
|
+
'utf-8',
|
|
63
|
+
);
|
|
64
|
+
expect(choiceEvent).toMatchSnapshot();
|
|
65
|
+
|
|
66
|
+
const rootAnyOf = fs.readFileSync(
|
|
67
|
+
path.join(outputDir, 'root-any-of-event.mdx'),
|
|
68
|
+
'utf-8',
|
|
69
|
+
);
|
|
70
|
+
expect(rootAnyOf).toMatchSnapshot();
|
|
71
|
+
|
|
72
|
+
const rootChoiceIndex = fs.readFileSync(
|
|
73
|
+
path.join(choiceEventDir, 'index.mdx'),
|
|
74
|
+
'utf-8',
|
|
75
|
+
);
|
|
76
|
+
expect(rootChoiceIndex).toMatchSnapshot();
|
|
77
|
+
|
|
78
|
+
const rootChoiceA = fs.readFileSync(
|
|
79
|
+
path.join(choiceEventDir, '01-option-a.mdx'),
|
|
80
|
+
'utf-8',
|
|
81
|
+
);
|
|
82
|
+
expect(rootChoiceA).toMatchSnapshot();
|
|
83
|
+
|
|
84
|
+
const rootChoiceB = fs.readFileSync(
|
|
85
|
+
path.join(choiceEventDir, '02-option-b.mdx'),
|
|
86
|
+
'utf-8',
|
|
87
|
+
);
|
|
88
|
+
expect(rootChoiceB).toMatchSnapshot();
|
|
89
|
+
});
|
|
84
90
|
});
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jest-environment node
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import generateEventDocs from '../generateEventDocs';
|
|
6
|
+
import fs from 'fs';
|
|
7
|
+
import path from 'path';
|
|
8
|
+
|
|
9
|
+
jest.mock('fs', () => {
|
|
10
|
+
const originalFs = jest.requireActual('fs');
|
|
11
|
+
return {
|
|
12
|
+
...originalFs,
|
|
13
|
+
writeFileSync: jest.fn(),
|
|
14
|
+
mkdirSync: jest.fn(),
|
|
15
|
+
existsSync: jest.fn(),
|
|
16
|
+
readdirSync: originalFs.readdirSync,
|
|
17
|
+
readFileSync: originalFs.readFileSync,
|
|
18
|
+
};
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
describe('generateEventDocs (versioned)', () => {
|
|
22
|
+
const options = {
|
|
23
|
+
organizationName: 'test-org',
|
|
24
|
+
projectName: 'test-project',
|
|
25
|
+
url: 'https://tracking-docs-demo.buchert.digital/',
|
|
26
|
+
// Use the fixtures_versioned directory as the siteDir for tests
|
|
27
|
+
siteDir: path.resolve(__dirname, '__fixtures_versioned__'),
|
|
28
|
+
};
|
|
29
|
+
const partialsDir = path.join(options.siteDir, 'docs/partials');
|
|
30
|
+
|
|
31
|
+
beforeEach(() => {
|
|
32
|
+
// Clear all instances and calls to constructor and all methods:
|
|
33
|
+
fs.writeFileSync.mockClear();
|
|
34
|
+
fs.mkdirSync.mockClear();
|
|
35
|
+
fs.existsSync.mockClear();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('should generate documentation for "current" version', async () => {
|
|
39
|
+
// Mock fs.writeFileSync to capture the output
|
|
40
|
+
fs.writeFileSync = jest.fn();
|
|
41
|
+
|
|
42
|
+
const currentOptions = { ...options, version: 'current' };
|
|
43
|
+
await generateEventDocs(currentOptions);
|
|
44
|
+
|
|
45
|
+
expect(fs.writeFileSync).toHaveBeenCalled();
|
|
46
|
+
const [filePath, content] = fs.writeFileSync.mock.calls[0];
|
|
47
|
+
const outputDir = path.join(options.siteDir, 'docs');
|
|
48
|
+
expect(filePath).toBe(path.join(outputDir, 'add-to-cart-event.mdx'));
|
|
49
|
+
expect(content).toMatchSnapshot();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('should generate documentation for a specific version', async () => {
|
|
53
|
+
// Mock fs.writeFileSync to capture the output
|
|
54
|
+
fs.writeFileSync = jest.fn();
|
|
55
|
+
|
|
56
|
+
const versionedOptions = { ...options, version: '1.1.1' };
|
|
57
|
+
await generateEventDocs(versionedOptions);
|
|
58
|
+
|
|
59
|
+
expect(fs.writeFileSync).toHaveBeenCalled();
|
|
60
|
+
const [filePath, content] = fs.writeFileSync.mock.calls[0];
|
|
61
|
+
const outputDir = path.join(
|
|
62
|
+
options.siteDir,
|
|
63
|
+
'versioned_docs',
|
|
64
|
+
'version-1.1.1',
|
|
65
|
+
);
|
|
66
|
+
expect(filePath).toBe(path.join(outputDir, 'add-to-cart-event.mdx'));
|
|
67
|
+
expect(content).toMatchSnapshot();
|
|
68
|
+
});
|
|
69
|
+
});
|