|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow strict-local |
| 8 | + * @format |
| 9 | + * @oncall react_native |
| 10 | + */ |
| 11 | + |
| 12 | +'use-strict'; |
| 13 | + |
| 14 | +const {wrapNullable, unwrapNullable} = require('../parsers-commons.js'); |
| 15 | + |
| 16 | +describe('wrapNullable', () => { |
| 17 | + describe('when nullable is true', () => { |
| 18 | + it('returns nullable type annotation', () => { |
| 19 | + const result = wrapNullable(true, { |
| 20 | + type: 'BooleanTypeAnnotation', |
| 21 | + }); |
| 22 | + const expected = { |
| 23 | + type: 'NullableTypeAnnotation', |
| 24 | + typeAnnotation: { |
| 25 | + type: 'BooleanTypeAnnotation', |
| 26 | + }, |
| 27 | + }; |
| 28 | + |
| 29 | + expect(result).toEqual(expected); |
| 30 | + }); |
| 31 | + }); |
| 32 | + describe('when nullable is false', () => { |
| 33 | + it('returns non nullable type annotation', () => { |
| 34 | + const result = wrapNullable(false, { |
| 35 | + type: 'BooleanTypeAnnotation', |
| 36 | + }); |
| 37 | + const expected = { |
| 38 | + type: 'BooleanTypeAnnotation', |
| 39 | + }; |
| 40 | + |
| 41 | + expect(result).toEqual(expected); |
| 42 | + }); |
| 43 | + }); |
| 44 | +}); |
| 45 | + |
| 46 | +describe('unwrapNullable', () => { |
| 47 | + describe('when type annotation is nullable', () => { |
| 48 | + it('returns original type annotation', () => { |
| 49 | + const result = unwrapNullable({ |
| 50 | + type: 'NullableTypeAnnotation', |
| 51 | + typeAnnotation: { |
| 52 | + type: 'BooleanTypeAnnotation', |
| 53 | + }, |
| 54 | + }); |
| 55 | + const expected = [ |
| 56 | + { |
| 57 | + type: 'BooleanTypeAnnotation', |
| 58 | + }, |
| 59 | + true, |
| 60 | + ]; |
| 61 | + |
| 62 | + expect(result).toEqual(expected); |
| 63 | + }); |
| 64 | + }); |
| 65 | + describe('when type annotation is not nullable', () => { |
| 66 | + it('returns original type annotation', () => { |
| 67 | + const result = unwrapNullable({ |
| 68 | + type: 'BooleanTypeAnnotation', |
| 69 | + }); |
| 70 | + const expected = [ |
| 71 | + { |
| 72 | + type: 'BooleanTypeAnnotation', |
| 73 | + }, |
| 74 | + false, |
| 75 | + ]; |
| 76 | + |
| 77 | + expect(result).toEqual(expected); |
| 78 | + }); |
| 79 | + }); |
| 80 | +}); |
0 commit comments