Skip to content

Commit 8c42a94

Browse files
committed
CCM-13675: Event code generation improvements
1 parent 03b5e82 commit 8c42a94

6 files changed

Lines changed: 67 additions & 11 deletions

File tree

src/typescript-schema-generator/jest.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const config = {
44
...baseJestConfig,
55
coveragePathIgnorePatterns: [
66
...(baseJestConfig.coveragePathIgnorePatterns ?? []),
7+
'generate-guard-functions-cli.ts',
78
'src/generate-types-cli.ts',
89
'src/generate-validators-cli.ts',
910
],
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* eslint-disable security/detect-non-literal-fs-filename */
2+
3+
import { destinationPackageName } from 'file-utils';
4+
import { generateGuardFunctions } from 'generate-guard-functions';
5+
import mockFs from 'mock-fs';
6+
import { readFileSync, readdirSync } from 'node:fs';
7+
import path from 'node:path';
8+
import { eventSchemasDir } from 'utils';
9+
10+
jest.mock('json-schema-to-typescript');
11+
12+
describe('generate-guard-functions', () => {
13+
const outputDir = path.resolve(
14+
__dirname,
15+
'..',
16+
'..',
17+
'..',
18+
destinationPackageName,
19+
'guard-functions',
20+
);
21+
22+
beforeEach(() => {
23+
mockFs({
24+
[eventSchemasDir]: {
25+
'one.flattened.schema.json': '{"title": "One"}',
26+
'two.flattened.schema.json': '{"title": "Two"}',
27+
'three.flattened.schema.json': '{"title": "Three"}',
28+
},
29+
});
30+
31+
jest.spyOn(console, 'log').mockImplementation(() => {});
32+
jest.spyOn(console, 'group').mockImplementation(() => {});
33+
});
34+
35+
afterEach(() => {
36+
mockFs.restore();
37+
});
38+
39+
it('should generate a guard function file for each schema', async () => {
40+
await generateGuardFunctions();
41+
42+
const typeDeclarationFiles = readdirSync(outputDir);
43+
44+
expect(typeDeclarationFiles.length).toBe(4);
45+
expect(typeDeclarationFiles).toEqual(
46+
expect.arrayContaining(['index.ts', 'One.ts', 'Two.ts', 'Three.ts']),
47+
);
48+
});
49+
50+
it('should create an index file exporting all generated guard function', async () => {
51+
await generateGuardFunctions();
52+
53+
const indexFileContents = readFileSync(
54+
path.join(outputDir, 'index.ts'),
55+
'utf8',
56+
);
57+
expect(indexFileContents).toContain("export * from './One';");
58+
expect(indexFileContents).toContain("export * from './Two';");
59+
expect(indexFileContents).toContain("export * from './Three';");
60+
});
61+
});

src/typescript-schema-generator/src/__tests__/generate-types.test.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,15 @@ describe('generate-types', () => {
4646

4747
expect(typeDeclarationFiles.length).toBe(4);
4848
expect(typeDeclarationFiles).toEqual(
49-
expect.arrayContaining([
50-
'index.d.ts',
51-
'One.d.ts',
52-
'Two.d.ts',
53-
'Three.d.ts',
54-
]),
49+
expect.arrayContaining(['index.ts', 'One.ts', 'Two.ts', 'Three.ts']),
5550
);
5651
});
5752

5853
it('should create an index file exporting all generated types', async () => {
5954
await generateTypes();
6055

6156
const indexFileContents = readFileSync(
62-
path.join(outputDir, 'index.d.ts'),
57+
path.join(outputDir, 'index.ts'),
6358
'utf8',
6459
);
6560
expect(indexFileContents).toContain("export * from './One';");

src/typescript-schema-generator/src/generate-guard-functions-cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable no-console */
22

3-
import { generateGuardFunctions } from 'generate-guard-functions'
3+
import { generateGuardFunctions } from 'generate-guard-functions';
44

55
generateGuardFunctions().catch((error) => {
66
console.error('Error generating guard functions:', error);

src/typescript-schema-generator/src/generate-guard-functions.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable no-console */
2-
import { createOutputDir, writeFile, writeTypesIndex } from 'file-utils';
2+
import { createOutputDir, writeFile } from 'file-utils';
33
import path from 'node:path';
44
import { writeFileSync } from 'node:fs';
55
import { eventSchemasDir, listEventSchemas, loadSchema } from 'utils';
@@ -35,7 +35,6 @@ export async function generateGuardFunctions() {
3535
guardFunction += ` }\n`;
3636
guardFunction += `}\n`;
3737

38-
3938
const typeDeclarationName = `${typeName}`;
4039
const typeDeclarationFilename = `${typeDeclarationName}.ts`;
4140
writeFile(outputDir, typeDeclarationFilename, guardFunction);

src/typescript-schema-generator/src/generate-types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable no-console */
2-
import { createOutputDir, writeFile, writeTypesIndex } from 'file-utils';
2+
import { createOutputDir, writeFile } from 'file-utils';
33
import { compile } from 'json-schema-to-typescript';
44
import { writeFileSync } from 'node:fs';
55
import path from 'node:path';

0 commit comments

Comments
 (0)