From cb3a5cc6913d81b3e16bcb3a9d5f29e5cfa1de73 Mon Sep 17 00:00:00 2001 From: Jesse Katsumata Date: Mon, 10 Oct 2022 04:23:30 -0700 Subject: [PATCH] Add unit test for common parsers method (#34915) Summary: This PR is part of https://github.com/facebook/react-native/issues/34872 This PR adds unit test for method that were refactored into parsers-commons.js in codegen in https://github.com/facebook/react-native/issues/34898 ## Changelog [Internal] [Changed] - Add unit test for common parsers method Pull Request resolved: https://github.com/facebook/react-native/pull/34915 Test Plan: `yarn jest react-native-codegen` Reviewed By: cipolleschi Differential Revision: D40219036 Pulled By: cipolleschi fbshipit-source-id: 2fb666c016f7822feaf2156d23ce7ffb9572c756 --- .../parsers/__tests__/parsers-commons-test.js | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 packages/react-native-codegen/src/parsers/__tests__/parsers-commons-test.js diff --git a/packages/react-native-codegen/src/parsers/__tests__/parsers-commons-test.js b/packages/react-native-codegen/src/parsers/__tests__/parsers-commons-test.js new file mode 100644 index 00000000000000..991198a1e33173 --- /dev/null +++ b/packages/react-native-codegen/src/parsers/__tests__/parsers-commons-test.js @@ -0,0 +1,80 @@ +/** + * 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 {wrapNullable, unwrapNullable} = require('../parsers-commons.js'); + +describe('wrapNullable', () => { + describe('when nullable is true', () => { + it('returns nullable type annotation', () => { + const result = wrapNullable(true, { + type: 'BooleanTypeAnnotation', + }); + const expected = { + type: 'NullableTypeAnnotation', + typeAnnotation: { + type: 'BooleanTypeAnnotation', + }, + }; + + expect(result).toEqual(expected); + }); + }); + describe('when nullable is false', () => { + it('returns non nullable type annotation', () => { + const result = wrapNullable(false, { + type: 'BooleanTypeAnnotation', + }); + const expected = { + type: 'BooleanTypeAnnotation', + }; + + expect(result).toEqual(expected); + }); + }); +}); + +describe('unwrapNullable', () => { + describe('when type annotation is nullable', () => { + it('returns original type annotation', () => { + const result = unwrapNullable({ + type: 'NullableTypeAnnotation', + typeAnnotation: { + type: 'BooleanTypeAnnotation', + }, + }); + const expected = [ + { + type: 'BooleanTypeAnnotation', + }, + true, + ]; + + expect(result).toEqual(expected); + }); + }); + describe('when type annotation is not nullable', () => { + it('returns original type annotation', () => { + const result = unwrapNullable({ + type: 'BooleanTypeAnnotation', + }); + const expected = [ + { + type: 'BooleanTypeAnnotation', + }, + false, + ]; + + expect(result).toEqual(expected); + }); + }); +});