Skip to content

Commit d7f8749

Browse files
committed
test(parsers-commons): add tests for buildSchema
1 parent 12212ba commit d7f8749

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed

packages/react-native-codegen/src/parsers/__tests__/parsers-commons-test.js

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@ import {
1717
parseObjectProperty,
1818
wrapNullable,
1919
unwrapNullable,
20+
buildSchema,
2021
} from '../parsers-commons';
2122
import type {ParserType} from '../errors';
2223

24+
const {Visitor} = require('../flow/Visitor');
25+
const {wrapComponentSchema} = require('../flow/components/schema');
26+
const {buildComponentSchema} = require('../flow/components');
27+
const {buildModuleSchema} = require('../flow/modules');
2328
const {
2429
UnsupportedObjectPropertyTypeAnnotationParserError,
2530
} = require('../errors');
@@ -31,6 +36,10 @@ const parser = new MockedParser();
3136
const flowTranslateTypeAnnotation = require('../flow/modules/index');
3237
const typeScriptTranslateTypeAnnotation = require('../typescript/modules/index');
3338

39+
beforeEach(() => {
40+
jest.clearAllMocks();
41+
});
42+
3443
describe('wrapNullable', () => {
3544
describe('when nullable is true', () => {
3645
it('returns nullable type annotation', () => {
@@ -336,3 +345,146 @@ describe('parseObjectProperty', () => {
336345
});
337346
});
338347
});
348+
349+
describe('buildSchema', () => {
350+
const getConfigTypeSpy = jest.spyOn(require('../utils'), 'getConfigType');
351+
352+
describe('when there is no codegenNativeComponent and no TurboModule', () => {
353+
const contents = '';
354+
355+
it('returns an empty module', () => {
356+
const schema = buildSchema(
357+
contents,
358+
'fileName',
359+
wrapComponentSchema,
360+
buildComponentSchema,
361+
buildModuleSchema,
362+
Visitor,
363+
parser,
364+
);
365+
366+
expect(getConfigTypeSpy).not.toHaveBeenCalled();
367+
expect(schema).toEqual({modules: {}});
368+
});
369+
});
370+
371+
describe('when there is a codegenNativeComponent', () => {
372+
const contents = `
373+
import type {ViewProps} from 'ViewPropTypes';
374+
import type {HostComponent} from 'react-native';
375+
376+
const codegenNativeComponent = require('codegenNativeComponent');
377+
378+
export type ModuleProps = $ReadOnly<{|
379+
...ViewProps,
380+
|}>;
381+
382+
export default (codegenNativeComponent<ModuleProps>(
383+
'Module',
384+
): HostComponent<ModuleProps>);
385+
`;
386+
387+
it('returns a module with good properties', () => {
388+
const schema = buildSchema(
389+
contents,
390+
'fileName',
391+
wrapComponentSchema,
392+
buildComponentSchema,
393+
buildModuleSchema,
394+
Visitor,
395+
parser,
396+
);
397+
398+
expect(getConfigTypeSpy).toHaveBeenCalledTimes(1);
399+
expect(getConfigTypeSpy).toHaveBeenCalledWith(
400+
parser.getAst(contents),
401+
Visitor,
402+
);
403+
expect(schema).toEqual({
404+
modules: {
405+
Module: {
406+
type: 'Component',
407+
components: {
408+
Module: {
409+
extendsProps: [
410+
{
411+
type: 'ReactNativeBuiltInType',
412+
knownTypeName: 'ReactNativeCoreViewProps',
413+
},
414+
],
415+
events: [],
416+
props: [],
417+
commands: [],
418+
},
419+
},
420+
},
421+
},
422+
});
423+
});
424+
});
425+
426+
describe('when there is a TurboModule', () => {
427+
const contents = `
428+
import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport';
429+
import * as TurboModuleRegistry from 'react-native/Libraries/TurboModule/TurboModuleRegistry';
430+
431+
export interface Spec extends TurboModule {
432+
+getArray: (a: Array<any>) => Array<string>;
433+
}
434+
435+
export default (TurboModuleRegistry.getEnforcing<Spec>(
436+
'SampleTurboModule',
437+
): Spec);
438+
`;
439+
440+
it('returns a module with good properties', () => {
441+
const schema = buildSchema(
442+
contents,
443+
'fileName',
444+
wrapComponentSchema,
445+
buildComponentSchema,
446+
buildModuleSchema,
447+
Visitor,
448+
parser,
449+
);
450+
451+
expect(getConfigTypeSpy).toHaveBeenCalledTimes(1);
452+
expect(getConfigTypeSpy).toHaveBeenCalledWith(
453+
parser.getAst(contents),
454+
Visitor,
455+
);
456+
expect(schema).toEqual({
457+
modules: {
458+
fileName: {
459+
type: 'NativeModule',
460+
aliases: {},
461+
spec: {
462+
properties: [
463+
{
464+
name: 'getArray',
465+
optional: false,
466+
typeAnnotation: {
467+
type: 'FunctionTypeAnnotation',
468+
returnTypeAnnotation: {
469+
type: 'ArrayTypeAnnotation',
470+
elementType: {type: 'StringTypeAnnotation'},
471+
},
472+
params: [
473+
{
474+
name: 'a',
475+
optional: false,
476+
typeAnnotation: {type: 'ArrayTypeAnnotation'},
477+
},
478+
],
479+
},
480+
},
481+
],
482+
},
483+
moduleName: 'SampleTurboModule',
484+
excludedPlatforms: undefined,
485+
},
486+
},
487+
});
488+
});
489+
});
490+
});

0 commit comments

Comments
 (0)