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); + }); + }); +});