Files
react-native/packages/react-native-codegen/src/parsers/__tests__/utils-test.js
T
MaeIg dc4d73c954 Extract the buildSchema function in the parsers-commons.js (#35158)
Summary:
This PR aims to extract  the buildSchema function into parsers-commons that is shared between typescript and flow. It is a task of https://github.com/facebook/react-native/issues/34872:
> Extract the buildSchema function ([Flow](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/flow/index.js#L66), [TypeScript](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/typescript/index.js#L72)) in the parsers-commons.js file to a top level buildSchema function which takes additional parameters to properly parse the content, get the config type and to build the schema, based on the language used.

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->
[Internal] [Changed] - Extract the buildSchema function in the parsers-commons.js

Pull Request resolved: https://github.com/facebook/react-native/pull/35158

Test Plan:
yarn test:
<img width="380" alt="image" src="https://user-images.githubusercontent.com/40902940/209584411-40f66047-e25d-43d4-975d-af10cd202f24.png">

yarn flow:
<img width="150" alt="image" src="https://user-images.githubusercontent.com/40902940/209584423-4cf2cb5a-a300-40a6-962c-e57934f19ad2.png">

yarn lint:
<img width="510" alt="image" src="https://user-images.githubusercontent.com/40902940/209584440-2d2b2658-73d8-47e2-bb8c-64d4633369a2.png">

Reviewed By: cortinico

Differential Revision: D42386804

Pulled By: cipolleschi

fbshipit-source-id: 2a238f7cec982d8ef3fd57a34dc9f58171e32b53
2023-01-12 04:23:42 -08:00

296 lines
8.8 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
* @oncall react_native
*/
'use strict';
const {
extractNativeModuleName,
createParserErrorCapturer,
verifyPlatforms,
visit,
} = require('../utils.js');
const {ParserError} = require('../errors');
beforeEach(() => {
jest.clearAllMocks();
});
describe('extractnativeModuleName', () => {
it('return filename when it ends with .js', () => {
const filename = '/some_folder/NativeModule.js';
const nativeModuleName = extractNativeModuleName(filename);
expect(nativeModuleName).toBe('NativeModule');
});
it('return filename when it ends with .ts', () => {
const filename = '/some_folder/NativeModule.ts';
const nativeModuleName = extractNativeModuleName(filename);
expect(nativeModuleName).toBe('NativeModule');
});
it('return filename when it ends with .tsx', () => {
const filename = '/some_folder/NativeModule.tsx';
const nativeModuleName = extractNativeModuleName(filename);
expect(nativeModuleName).toBe('NativeModule');
});
it('return filename when it ends with .android.js', () => {
const filename = '/some_folder/NativeModule.android.js';
const nativeModuleName = extractNativeModuleName(filename);
expect(nativeModuleName).toBe('NativeModule');
});
it('return filename when it ends with .android.ts', () => {
const filename = '/some_folder/NativeModule.android.ts';
const nativeModuleName = extractNativeModuleName(filename);
expect(nativeModuleName).toBe('NativeModule');
});
it('return filename when it ends with .android.tsx', () => {
const filename = '/some_folder/NativeModule.android.tsx';
const nativeModuleName = extractNativeModuleName(filename);
expect(nativeModuleName).toBe('NativeModule');
});
it('return filename when it ends with .ios.js', () => {
const filename = '/some_folder/NativeModule.ios.ts';
const nativeModuleName = extractNativeModuleName(filename);
expect(nativeModuleName).toBe('NativeModule');
});
it('return filename when it ends with .ios.ts', () => {
const filename = '/some_folder/NativeModule.ios.ts';
const nativeModuleName = extractNativeModuleName(filename);
expect(nativeModuleName).toBe('NativeModule');
});
it('return filename when it ends with .ios.tsx', () => {
const filename = '/some_folder/NativeModule.ios.tsx';
const nativeModuleName = extractNativeModuleName(filename);
expect(nativeModuleName).toBe('NativeModule');
});
it('return filename when it ends with .windows.js', () => {
const filename = '/some_folder/NativeModule.windows.js';
const nativeModuleName = extractNativeModuleName(filename);
expect(nativeModuleName).toBe('NativeModule');
});
});
describe('createParserErrorCapturer', () => {
describe("when function doesn't throw", () => {
it("returns result and doesn't change errors array", () => {
const [errors, guard] = createParserErrorCapturer();
const fn = () => 'result';
const result = guard(fn);
expect(result).toBe('result');
expect(errors).toHaveLength(0);
});
});
describe('when function throws a ParserError', () => {
it('returns null and adds the error in errors array instead of throwing it', () => {
const [errors, guard] = createParserErrorCapturer();
const ErrorThrown = new ParserError(
'moduleName',
null,
'Something went wrong :(',
);
const fn = () => {
throw ErrorThrown;
};
const result = guard(fn);
expect(result).toBe(null);
expect(errors).toHaveLength(1);
expect(errors[0]).toEqual(ErrorThrown);
expect(() => guard(fn)).not.toThrow();
});
});
describe('when function throws another error', () => {
it("throws the error and doesn't change errors array", () => {
const [errors, guard] = createParserErrorCapturer();
const errorMessage = 'Something else went wrong :(';
const fn = () => {
throw new Error(errorMessage);
};
expect(() => guard(fn)).toThrow(errorMessage);
expect(errors).toHaveLength(0);
});
});
});
describe('verifyPlatforms', () => {
it('exclude android given an iOS only module', () => {
let result = verifyPlatforms(
'NativeSampleTurboModule',
'SampleTurboModuleIOS',
);
expect(result.cxxOnly).toBe(false);
expect(result.excludedPlatforms).toEqual(['android']);
result = verifyPlatforms('NativeSampleTurboModuleIOS', 'SampleTurboModule');
expect(result.cxxOnly).toBe(false);
expect(result.excludedPlatforms).toEqual(['android']);
result = verifyPlatforms(
'NativeSampleTurboModuleIOS',
'SampleTurboModuleIOS',
);
expect(result.cxxOnly).toBe(false);
expect(result.excludedPlatforms).toEqual(['android']);
});
it('exclude iOS given an android only module', () => {
let result = verifyPlatforms(
'NativeSampleTurboModule',
'SampleTurboModuleAndroid',
);
expect(result.cxxOnly).toBe(false);
expect(result.excludedPlatforms).toEqual(['iOS']);
result = verifyPlatforms(
'NativeSampleTurboModuleAndroid',
'SampleTurboModule',
);
expect(result.cxxOnly).toBe(false);
expect(result.excludedPlatforms).toEqual(['iOS']);
result = verifyPlatforms(
'NativeSampleTurboModuleAndroid',
'SampleTurboModuleAndroid',
);
expect(result.cxxOnly).toBe(false);
expect(result.excludedPlatforms).toEqual(['iOS']);
});
it('exclude iOS and android given a Cxx only module', () => {
let result = verifyPlatforms(
'NativeSampleTurboModule',
'SampleTurboModuleCxx',
);
expect(result.cxxOnly).toBe(true);
expect(result.excludedPlatforms).toEqual(['iOS', 'android']);
result = verifyPlatforms('NativeSampleTurboModuleCxx', 'SampleTurboModule');
expect(result.cxxOnly).toBe(true);
expect(result.excludedPlatforms).toEqual(['iOS', 'android']);
result = verifyPlatforms(
'NativeSampleTurboModuleCxx',
'SampleTurboModuleCxx',
);
expect(result.cxxOnly).toBe(true);
expect(result.excludedPlatforms).toEqual(['iOS', 'android']);
});
});
describe('visit', () => {
describe('when the astNode is null', () => {
it("doesn't call the visitor function", () => {
const visitorFunction = jest.fn();
const visitor = {
itemType: visitorFunction,
};
const astNode = null;
visit(astNode, visitor);
expect(visitorFunction).not.toHaveBeenCalled();
});
});
describe('when the astNode is not an object', () => {
it("doesn't call the visitor function", () => {
const visitorFunction = jest.fn();
const visitor = {
itemType: visitorFunction,
};
const astNode = 'astNode';
visit(astNode, visitor);
expect(visitorFunction).not.toHaveBeenCalled();
});
});
describe('when the astNode is an object', () => {
describe("when the astNode has a string type that doesn't exist in the visitor object", () => {
it("doesn't call the visitor function", () => {
const visitorFunction = jest.fn();
const visitor = {
itemType: visitorFunction,
};
const astNode = {type: 'itemTypeNotInVisitor'};
visit(astNode, visitor);
expect(visitorFunction).not.toHaveBeenCalled();
});
});
describe('when the astNode has a string type that exists in the visitor object', () => {
it("doesn't call the visitor function", () => {
const visitorFunction = jest.fn();
const visitor = {
itemType: visitorFunction,
};
const astNode = {type: 'itemType'};
visit(astNode, visitor);
expect(visitorFunction).toHaveBeenCalledTimes(1);
});
});
describe("when the astNode doesn't have a string type", () => {
it('iterates on every values of the astNode', () => {
const visitorFunction = jest.fn();
const visitor = {
itemType1: visitorFunction,
itemType2: visitorFunction,
};
const astNode = {
firstChildNode: {type: 'itemType1'},
secondChildNode: {type: 'itemType2'},
thirdChildNode: {type: 'itemType3'},
};
visit(astNode, visitor);
expect(visitorFunction).toHaveBeenCalledTimes(2);
});
});
});
describe('when the astNode is an array', () => {
it('iterates on every values of the astNode', () => {
const visitorFunction = jest.fn();
const visitor = {
itemType1: visitorFunction,
itemType2: visitorFunction,
};
const astNode = [
{type: 'itemType1'},
{type: 'itemType2'},
{type: 'itemType3'},
];
visit(astNode, visitor);
expect(visitorFunction).toHaveBeenCalledTimes(2);
});
});
});