Skip to content

Commit

Permalink
chore: added tests - react-native-codegen/ for;
Browse files Browse the repository at this point in the history
- `isObjectProperty` fn and,
- `parseObjectProperty` fn
  • Loading branch information
Pranav-yadav committed Nov 5, 2022
1 parent 91e016a commit 4197bf4
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import {
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
isObjectProperty,
parseObjectProperty,
} from '../parsers-commons';
import type {ParserType} from '../errors';
Expand All @@ -22,7 +23,12 @@ const {
unwrapNullable,
emitUnionTypeAnnotation,
} = require('../parsers-commons.js');
const {UnsupportedUnionTypeAnnotationParserError} = require('../errors');
const flowTranslateTypeAnnotation = require('../flow/modules/index');
const typeScriptTranslateTypeAnnotation = require('../typescript/modules/index');
const {
UnsupportedUnionTypeAnnotationParserError,
UnsupportedObjectPropertyTypeAnnotationParserError,
} = require('../errors');

describe('wrapNullable', () => {
describe('when nullable is true', () => {
Expand Down Expand Up @@ -252,101 +258,195 @@ describe('assertGenericTypeAnnotationHasExactlyOneTypeParameter', () => {
});
});

/*
describe('isObjectProperty', () => {
const propertyStub = {
/* type: 'notObjectTypeProperty', */
typeAnnotation: {
typeAnnotation: 'wrongTypeAnnotation',
},
value: 'wrongValue',
name: 'wrongName',
};

describe("when 'language' is 'Flow'", () => {
const language: ParserType = 'Flow';
it("returns 'true' if 'property.type' is 'ObjectTypeProperty'", () => {
const result = isObjectProperty(
{
type: 'ObjectTypeProperty',
...propertyStub,
},
language,
);
expect(result).toEqual(true);
});

it("returns 'true' if 'property.type' is 'ObjectTypeIndexer'", () => {
const result = isObjectProperty(
{
type: 'ObjectTypeIndexer',
...propertyStub,
},
language,
);
expect(result).toEqual(true);
});

it("returns 'false' if 'property.type' is not 'ObjectTypeProperty' or 'ObjectTypeIndexer'", () => {
const result = isObjectProperty(
{
type: 'notObjectTypeProperty',
...propertyStub,
},
language,
);
expect(result).toEqual(false);
});
});

describe("when 'language' is 'TypeScript'", () => {
const language: ParserType = 'TypeScript';
it("returns 'true' if 'property.type' is 'TSPropertySignature'", () => {
const result = isObjectProperty(
{
type: 'TSPropertySignature',
...propertyStub,
},
language,
);
expect(result).toEqual(true);
});

it("returns 'true' if 'property.type' is 'TSIndexSignature'", () => {
const result = isObjectProperty(
{
type: 'TSIndexSignature',
...propertyStub,
},
language,
);
expect(result).toEqual(true);
});

it("returns 'false' if 'property.type' is not 'TSPropertySignature' or 'TSIndexSignature'", () => {
const result = isObjectProperty(
{
type: 'notTSPropertySignature',
...propertyStub,
},
language,
);
expect(result).toEqual(false);
});
});
});

describe('parseObjectProperty', () => {
const translateTypeAnnotation = (
hasteModuleName: string,
languageTypeAnnotation: string,
types: {[string]: string},
aliasMap: {[string]: string},
tryParse: () => null,
cxxOnly: boolean,
optional: boolean,
) => {};
const moduleName = 'testModuleName';
const types = {['wrongName']: 'wrongType'};
const aliasMap = {['wrongAlias']: 'wrongValue'};
const aliasMap = {};
const tryParse = () => null;
const cxxOnly = false;
let language = 'TypeScript';
const languageTypeAnnotation =
language === 'TypeScript' ? 'wrongTypeAnnotation' : 'wrongValue';
it("throws an 'UnsupportedObjectPropertyTypeAnnotationParserError' error if 'property.type' is not 'ObjectTypeProperty'.", () => {
const property = {
type: 'notObjectTypeProperty',
typeAnnotation: {
typeAnnotation: 'wrongTypeAnnotation',
},
value: 'wrongValue',
name: 'wrongName',
};
expect(() =>
parseObjectProperty(
property,
const nullable = true;

describe("when 'language' is 'Flow'", () => {
const language: ParserType = 'Flow';
it("throws an 'UnsupportedObjectPropertyTypeAnnotationParserError' error if 'property.type' is not 'ObjectTypeProperty' or 'ObjectTypeIndexer'.", () => {
const property = {
type: 'notObjectTypeProperty',
typeAnnotation: {
type: 'notObjectTypeProperty',
typeAnnotation: 'wrongTypeAnnotation',
},
value: 'wrongValue',
name: 'wrongName',
};
const expected = new UnsupportedObjectPropertyTypeAnnotationParserError(
moduleName,
types,
aliasMap,
tryParse,
cxxOnly,
'Flow',
translateTypeAnnotation,
),
).toThrowErrorMatchingInlineSnapshot(
`"Module testModuleName: 'ObjectTypeAnnotation' cannot contain 'notObjectTypeProperty'."`,
);
property,
property.type,
language,
);
expect(() =>
parseObjectProperty(
property,
moduleName,
types,
aliasMap,
tryParse,
cxxOnly,
language,
nullable,
flowTranslateTypeAnnotation,
),
).toThrow(expected);
});
});

it("throws an 'UnsupportedObjectPropertyTypeAnnotationParserError' error if 'property.type' is not 'TSPropertySignature'.", () => {
const property = {
type: 'notTSPropertySignature',
typeAnnotation: {
typeAnnotation: 'wrongTypeAnnotation',
},
value: 'wrongValue',
name: 'wrongName',
};
expect(() =>
parseObjectProperty(
describe("when 'language' is 'TypeScript'", () => {
const language: ParserType = 'TypeScript';
it("throws an 'UnsupportedObjectPropertyTypeAnnotationParserError' error if 'property.type' is not 'TSPropertySignature' or 'TSIndexSignature'.", () => {
const property = {
type: 'notTSPropertySignature',
typeAnnotation: {
typeAnnotation: 'wrongTypeAnnotation',
},
value: 'wrongValue',
name: 'wrongName',
};
const expected = new UnsupportedObjectPropertyTypeAnnotationParserError(
moduleName,
property,
property.type,
language,
);
expect(() =>
parseObjectProperty(
property,
moduleName,
types,
aliasMap,
tryParse,
cxxOnly,
language,
nullable,
typeScriptTranslateTypeAnnotation,
),
).toThrow(expected);
});

it("returns a 'NativeModuleBaseTypeAnnotation' object with 'typeAnnotation.type' equal to 'GenericObjectTypeAnnotation', if 'property.type' is 'TSIndexSignature'.", () => {
const property = {
type: 'TSIndexSignature',
typeAnnotation: {
type: 'TSIndexSignature',
typeAnnotation: 'TSIndexSignature',
},
key: {
name: 'testKeyName',
},
value: 'wrongValue',
name: 'wrongName',
parameters: [{name: 'testName'}],
};
const result = parseObjectProperty(
property,
moduleName,
types,
aliasMap,
tryParse,
cxxOnly,
language,
translateTypeAnnotation,
),
).toThrowErrorMatchingInlineSnapshot(
`"Module testModuleName: 'ObjectTypeAnnotation' cannot contain 'notTSPropertySignature'."`,
);
});
// TODO: Add more tests like for `throwIfPropertyValueTypeIsUnsupported`
});
*/

describe('emitMixedTypeAnnotation', () => {
describe('when nullable is true', () => {
it('returns nullable type annotation', () => {
const result = emitMixedTypeAnnotation(true);
nullable,
typeScriptTranslateTypeAnnotation,
);
const expected = {
type: 'NullableTypeAnnotation',
typeAnnotation: {
type: 'MixedTypeAnnotation',
},
name: 'testName',
optional: false,
typeAnnotation: wrapNullable(nullable, {
type: 'GenericObjectTypeAnnotation',
}),
};

expect(result).toEqual(expected);
});
});
describe('when nullable is false', () => {
it('returns non nullable type annotation', () => {
const result = emitMixedTypeAnnotation(false);
const expected = {
type: 'MixedTypeAnnotation',
};

expect(result).toEqual(expected);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const {
wrapNullable,
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
parseObjectProperty,
emitMixedTypeAnnotation,
emitUnionTypeAnnotation,
translateDefault,
} = require('../../parsers-commons');
Expand Down Expand Up @@ -642,4 +641,5 @@ function buildModuleSchema(

module.exports = {
buildModuleSchema,
flowTranslateTypeAnnotation: translateTypeAnnotation,
};
12 changes: 0 additions & 12 deletions packages/react-native-codegen/src/parsers/parsers-commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@ import type {
NamedShape,
NativeModuleSchema,
NativeModuleTypeAnnotation,
Nullable,
NativeModuleAliasMap,
UnionTypeAnnotationMemberType,
NativeModuleEnumDeclaration,
NativeModuleTypeAnnotation,
NativeModuleBaseTypeAnnotation,
NativeModuleUnionTypeAnnotation,
NativeModuleMixedTypeAnnotation,
Nullable,
} from '../CodegenSchema.js';
import type {ParserType} from './errors';
Expand Down Expand Up @@ -200,14 +197,6 @@ function parseObjectProperty(
};
}

function emitMixedTypeAnnotation(
nullable: boolean,
): Nullable<NativeModuleMixedTypeAnnotation> {
return wrapNullable(nullable, {
type: 'MixedTypeAnnotation',
});
}

function remapUnionTypeAnnotationMemberNames(
types: $FlowFixMe,
language: ParserType,
Expand Down Expand Up @@ -328,7 +317,6 @@ module.exports = {
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
isObjectProperty,
parseObjectProperty,
emitMixedTypeAnnotation,
emitUnionTypeAnnotation,
translateDefault,
getKeyName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ const {
wrapNullable,
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
parseObjectProperty,
emitMixedTypeAnnotation,
emitUnionTypeAnnotation,
translateDefault,
} = require('../../parsers-commons');
Expand Down Expand Up @@ -655,4 +654,5 @@ function buildModuleSchema(

module.exports = {
buildModuleSchema,
typeScriptTranslateTypeAnnotation: translateTypeAnnotation,
};

0 comments on commit 4197bf4

Please sign in to comment.