|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its 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 | + */ |
| 8 | + |
| 9 | +/*global describe, it, expect*/ |
| 10 | + |
| 11 | +import { unwrapUtilityType, isSupportedUtilityType } from '../flowUtilityTypes'; |
| 12 | + |
| 13 | +import { statement } from '../../../tests/utils'; |
| 14 | + |
| 15 | +describe('flowTypeUtilities', () => { |
| 16 | + describe('unwrapUtilityType', () => { |
| 17 | + it('correctly unwraps', () => { |
| 18 | + const def = statement(` |
| 19 | + type A = $ReadOnly<{ foo: string }> |
| 20 | + `); |
| 21 | + |
| 22 | + expect(unwrapUtilityType(def.get('right'))).toBe( |
| 23 | + def.get('right', 'typeParameters', 'params', 0), |
| 24 | + ); |
| 25 | + }); |
| 26 | + |
| 27 | + it('correctly unwraps double', () => { |
| 28 | + const def = statement(` |
| 29 | + type A = $ReadOnly<$ReadOnly<{ foo: string }>> |
| 30 | + `); |
| 31 | + |
| 32 | + expect(unwrapUtilityType(def.get('right'))).toBe( |
| 33 | + def.get( |
| 34 | + 'right', |
| 35 | + 'typeParameters', |
| 36 | + 'params', |
| 37 | + 0, |
| 38 | + 'typeParameters', |
| 39 | + 'params', |
| 40 | + 0, |
| 41 | + ), |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + it('correctly unwraps triple', () => { |
| 46 | + const def = statement(` |
| 47 | + type A = $ReadOnly<$ReadOnly<$ReadOnly<{ foo: string }>>> |
| 48 | + `); |
| 49 | + |
| 50 | + expect(unwrapUtilityType(def.get('right'))).toBe( |
| 51 | + def.get( |
| 52 | + 'right', |
| 53 | + 'typeParameters', |
| 54 | + 'params', |
| 55 | + 0, |
| 56 | + 'typeParameters', |
| 57 | + 'params', |
| 58 | + 0, |
| 59 | + 'typeParameters', |
| 60 | + 'params', |
| 61 | + 0, |
| 62 | + ), |
| 63 | + ); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('isSupportedUtilityType', () => { |
| 68 | + it('correctly returns true for valid type', () => { |
| 69 | + const def = statement(` |
| 70 | + type A = $Exact<{ foo: string }> |
| 71 | + `); |
| 72 | + |
| 73 | + expect(isSupportedUtilityType(def.get('right'))).toBe(true); |
| 74 | + }); |
| 75 | + |
| 76 | + it('correctly returns false for invalid type', () => { |
| 77 | + const def = statement(` |
| 78 | + type A = $Homer<{ foo: string }> |
| 79 | + `); |
| 80 | + |
| 81 | + expect(isSupportedUtilityType(def.get('right'))).toBe(false); |
| 82 | + }); |
| 83 | + }); |
| 84 | +}); |
0 commit comments