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.
Files changed (81) hide show
  1. package/README.md +42 -6
  2. package/__tests__/ExampleDataLayer.test.js +78 -155
  3. package/__tests__/__fixtures__/static/schemas/add-to-cart-event.json +4 -15
  4. package/__tests__/__fixtures__/static/schemas/choice-event.json +72 -0
  5. package/__tests__/__fixtures__/static/schemas/components/dataLayer.json +52 -54
  6. package/__tests__/__fixtures__/static/schemas/components/product.json +124 -210
  7. package/__tests__/__fixtures__/static/schemas/nested/child-event.json +10 -0
  8. package/__tests__/__fixtures__/static/schemas/nested/grandchild-a.json +9 -0
  9. package/__tests__/__fixtures__/static/schemas/nested/grandchild-b.json +9 -0
  10. package/__tests__/__fixtures__/static/schemas/nested/parent-event.json +7 -0
  11. package/__tests__/__fixtures__/static/schemas/root-any-of-event.json +34 -0
  12. package/__tests__/__fixtures__/static/schemas/root-choice-event.json +36 -0
  13. package/__tests__/__fixtures__/validateSchemas/circular-schema.json +6 -6
  14. package/__tests__/__fixtures__/validateSchemas/components/referenced.json +9 -7
  15. package/__tests__/__fixtures__/validateSchemas/invalid-example-schema.json +7 -7
  16. package/__tests__/__fixtures__/validateSchemas/main-schema-with-missing-ref.json +7 -7
  17. package/__tests__/__fixtures__/validateSchemas/main-schema-with-ref.json +7 -7
  18. package/__tests__/__fixtures__/validateSchemas/no-example-schema.json +11 -11
  19. package/__tests__/__fixtures__/validateSchemas/schema-A.json +5 -5
  20. package/__tests__/__fixtures__/validateSchemas/schema-B.json +5 -5
  21. package/__tests__/__fixtures__/validateSchemas/valid-schema.json +7 -7
  22. package/__tests__/__fixtures_versioned__/static/schemas/1.1.1/add-to-cart-event.json +44 -0
  23. package/__tests__/__fixtures_versioned__/static/schemas/1.1.1/components/dataLayer.json +56 -0
  24. package/__tests__/__fixtures_versioned__/static/schemas/1.1.1/components/product.json +125 -0
  25. package/__tests__/__fixtures_versioned__/static/schemas/next/add-to-cart-event.json +44 -0
  26. package/__tests__/__fixtures_versioned__/static/schemas/next/components/dataLayer.json +56 -0
  27. package/__tests__/__fixtures_versioned__/static/schemas/next/components/product.json +125 -0
  28. package/__tests__/__fixtures_versioned__/versions.json +1 -0
  29. package/__tests__/__snapshots__/ExampleDataLayer.test.js.snap +117 -0
  30. package/__tests__/__snapshots__/generateEventDocs.nested.test.js.snap +92 -0
  31. package/__tests__/__snapshots__/generateEventDocs.test.js.snap +113 -15
  32. package/__tests__/__snapshots__/generateEventDocs.versioned.test.js.snap +53 -0
  33. package/__tests__/components/FoldableRows.test.js +330 -0
  34. package/__tests__/components/PropertiesTable.test.js +31 -14
  35. package/__tests__/components/PropertyRow.test.js +471 -51
  36. package/__tests__/components/SchemaJsonViewer.test.js +23 -19
  37. package/__tests__/components/SchemaRows.test.js +96 -66
  38. package/__tests__/components/SchemaViewer.test.js +34 -17
  39. package/__tests__/components/TableHeader.test.js +12 -12
  40. package/__tests__/generateEventDocs.nested.test.js +80 -0
  41. package/__tests__/generateEventDocs.test.js +77 -71
  42. package/__tests__/generateEventDocs.versioned.test.js +69 -0
  43. package/__tests__/helpers/buildExampleFromSchema.test.js +160 -160
  44. package/__tests__/helpers/file-system.test.js +44 -0
  45. package/__tests__/helpers/getConstraints.test.js +48 -48
  46. package/__tests__/helpers/loadSchema.test.js +11 -5
  47. package/__tests__/helpers/path-helpers.test.js +34 -0
  48. package/__tests__/helpers/processSchema.test.js +42 -22
  49. package/__tests__/helpers/schema-processing.test.js +82 -0
  50. package/__tests__/helpers/schemaToExamples.test.js +56 -0
  51. package/__tests__/helpers/schemaToTableData.filtering.test.js +65 -0
  52. package/__tests__/helpers/schemaToTableData.hierarchicalLines.test.js +539 -0
  53. package/__tests__/helpers/schemaToTableData.test.js +222 -0
  54. package/__tests__/helpers/update-schema-ids.test.js +107 -0
  55. package/__tests__/update-schema-ids.test.js +39 -0
  56. package/__tests__/validateSchemas.test.js +125 -88
  57. package/components/ExampleDataLayer.js +59 -28
  58. package/components/FoldableRows.js +164 -0
  59. package/components/PropertiesTable.js +10 -7
  60. package/components/PropertyRow.js +169 -60
  61. package/components/SchemaJsonViewer.js +6 -6
  62. package/components/SchemaRows.css +236 -14
  63. package/components/SchemaRows.js +24 -41
  64. package/components/SchemaViewer.js +19 -13
  65. package/components/TableHeader.js +12 -12
  66. package/generateEventDocs.js +141 -61
  67. package/helpers/buildExampleFromSchema.js +58 -72
  68. package/helpers/choice-index-template.js +22 -0
  69. package/helpers/file-system.js +32 -0
  70. package/helpers/getConstraints.js +43 -44
  71. package/helpers/loadSchema.js +2 -2
  72. package/helpers/path-helpers.js +22 -0
  73. package/helpers/processSchema.js +19 -19
  74. package/helpers/{mdx-template.js → schema-doc-template.js} +12 -12
  75. package/helpers/schema-processing.js +75 -0
  76. package/helpers/schemaToExamples.js +99 -0
  77. package/helpers/schemaToTableData.js +311 -0
  78. package/helpers/update-schema-ids.js +47 -0
  79. package/index.js +143 -54
  80. package/package.json +1 -1
  81. 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', () => (props) => <tr><td>Mocked PropertyRow: {props.propertyKey}</td></tr>);
8
- jest.mock('../../components/TableHeader', () => () => <thead><tr><th>Mocked TableHeader</th></tr></thead>);
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
- it('renders a PropertyRow for each property', () => {
12
- const properties = {
13
- name: { type: 'string' },
14
- age: { type: 'integer' },
15
- };
16
- const { getByText } = render(
17
- <table>
18
- <tbody>
19
- <SchemaRows properties={properties} getConstraints={() => []} />
20
- </tbody>
21
- </table>
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
- expect(getByText('Mocked PropertyRow: name')).toBeInTheDocument();
25
- expect(getByText('Mocked PropertyRow: age')).toBeInTheDocument();
26
- });
75
+ const { getByText } = render(
76
+ <table>
77
+ <tbody>
78
+ <SchemaRows tableData={tableData} />
79
+ </tbody>
80
+ </table>,
81
+ );
27
82
 
28
- it('recursively renders for nested objects', () => {
29
- const properties = {
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
- expect(getByText('Mocked PropertyRow: user')).toBeInTheDocument();
46
- expect(getByText('Mocked TableHeader')).toBeInTheDocument();
47
- expect(getByText('user { }')).toBeInTheDocument();
48
- // This will be inside the nested table
49
- expect(getByText('Mocked PropertyRow: name')).toBeInTheDocument();
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
- it('recursively renders for nested arrays of objects', () => {
53
- const properties = {
54
- products: {
55
- type: 'array',
56
- items: {
57
- type: 'object',
58
- properties: {
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
- expect(getByText('Mocked PropertyRow: products')).toBeInTheDocument();
74
- expect(getByText('Mocked TableHeader')).toBeInTheDocument();
75
- expect(getByText('products [ ]')).toBeInTheDocument();
76
- // These will be inside the nested table
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 child components
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
- jest.mock('../../components/ExampleDataLayer', () => () => <div>Mocked ExampleDataLayer</div>);
9
- jest.mock('../../components/PropertiesTable', () => () => <div>Mocked PropertiesTable</div>);
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
- it('renders the schema viewer with all child components', () => {
13
- const schema = {
14
- type: 'object',
15
- properties: {
16
- name: { type: 'string' },
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
- const { getByText } = render(<SchemaViewer schema={schema} />);
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
- expect(getByText('DataLayer Example')).toBeInTheDocument();
23
- expect(getByText('Mocked ExampleDataLayer')).toBeInTheDocument();
24
- expect(getByText('Event Properties')).toBeInTheDocument();
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
- it('renders the table header with correct columns', () => {
8
- const { getByText } = render(
9
- <table>
10
- <TableHeader />
11
- </table>
12
- );
7
+ it('renders the table header with correct columns', () => {
8
+ const { getByText } = render(
9
+ <table>
10
+ <TableHeader />
11
+ </table>,
12
+ );
13
13
 
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
- });
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
- 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
- };
10
+ const memfs = require('memfs');
11
+ return memfs;
19
12
  });
20
13
 
21
- describe('generateEventDocs', () => {
22
-
23
- const options = {
24
- organizationName: 'test-org',
25
- projectName: 'test-project',
26
- // Use the fixtures directory as the siteDir for tests
27
- siteDir: path.resolve(__dirname, '__fixtures__')
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
- const outputDir = path.join(options.siteDir, 'docs/events');
30
- const partialsDir = path.join(options.siteDir, 'docs/partials');
31
-
32
-
33
- beforeEach(() => {
34
- // Clear all instances and calls to constructor and all methods:
35
- fs.writeFileSync.mockClear();
36
- fs.mkdirSync.mockClear();
37
- fs.existsSync.mockClear();
38
- });
39
-
40
- it('should generate documentation correctly when no partials exist', async () => {
41
- console.log = jest.fn(); // suppress console.log
42
-
43
- // Simulate that no partials exist
44
- fs.existsSync.mockReturnValue(false);
45
-
46
- await generateEventDocs(options);
47
-
48
- // Expect writeFileSync to have been called once for each schema
49
- expect(fs.writeFileSync).toHaveBeenCalledTimes(1);
50
-
51
- // Check the content of the generated file
52
- const [filePath, content] = fs.writeFileSync.mock.calls[0];
53
- expect(filePath).toBe(path.join(outputDir, 'add-to-cart-event.mdx'));
54
- expect(content).toMatchSnapshot();
55
- });
56
-
57
- it('should generate documentation with top and bottom partials when they exist', async () => {
58
- console.log = jest.fn(); // suppress console.log
59
-
60
- // Simulate that the output directory and partials exist
61
- fs.existsSync.mockImplementation((filePath) => {
62
- if (filePath === outputDir) {
63
- return true;
64
- }
65
- if (filePath === path.join(partialsDir, 'add-to-cart-event.mdx')) {
66
- return true;
67
- }
68
- if (filePath === path.join(partialsDir, 'add-to-cart-event_bottom.mdx')) {
69
- return true;
70
- }
71
- return false;
72
- });
73
-
74
- await generateEventDocs(options);
75
-
76
- // Expect writeFileSync to have been called once for each schema
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
+ });