|
| 1 | +import { ReactTestRendererJSON } from 'react-test-renderer'; |
| 2 | +import { defaultMapProps } from '../format-default'; |
| 3 | + |
| 4 | +const node: ReactTestRendererJSON = { |
| 5 | + type: 'View', |
| 6 | + props: {}, |
| 7 | + children: null, |
| 8 | +}; |
| 9 | + |
| 10 | +describe('mapPropsForQueryError', () => { |
| 11 | + test('preserves props that are helpful for debugging', () => { |
| 12 | + const props = { |
| 13 | + accessibilityElementsHidden: true, |
| 14 | + accessibilityViewIsModal: true, |
| 15 | + importantForAccessibility: 'yes', |
| 16 | + testID: 'TEST_ID', |
| 17 | + nativeID: 'NATIVE_ID', |
| 18 | + accessibilityLabel: 'LABEL', |
| 19 | + accessibilityLabelledBy: 'LABELLED_BY', |
| 20 | + accessibilityRole: 'ROLE', |
| 21 | + accessibilityHint: 'HINT', |
| 22 | + placeholder: 'PLACEHOLDER', |
| 23 | + value: 'VALUE', |
| 24 | + defaultValue: 'DEFAULT_VALUE', |
| 25 | + }; |
| 26 | + |
| 27 | + const result = defaultMapProps(props, node); |
| 28 | + |
| 29 | + expect(result).toStrictEqual(props); |
| 30 | + }); |
| 31 | + |
| 32 | + test('does not preserve less helpful props', () => { |
| 33 | + const result = defaultMapProps( |
| 34 | + { |
| 35 | + style: [{ flex: 1 }, { display: 'flex' }], |
| 36 | + onPress: () => null, |
| 37 | + key: 'foo', |
| 38 | + }, |
| 39 | + node |
| 40 | + ); |
| 41 | + |
| 42 | + expect(result).toStrictEqual({}); |
| 43 | + }); |
| 44 | + |
| 45 | + test('preserves "display: none" style but no other style', () => { |
| 46 | + const result = defaultMapProps( |
| 47 | + { style: [{ flex: 1 }, { display: 'none', flex: 2 }] }, |
| 48 | + node |
| 49 | + ); |
| 50 | + |
| 51 | + expect(result).toStrictEqual({ |
| 52 | + style: { display: 'none' }, |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + test('removes undefined keys from accessibilityState', () => { |
| 57 | + const result = defaultMapProps( |
| 58 | + { accessibilityState: { checked: undefined, selected: false } }, |
| 59 | + node |
| 60 | + ); |
| 61 | + |
| 62 | + expect(result).toStrictEqual({ |
| 63 | + accessibilityState: { selected: false }, |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + test('removes accessibilityState if all keys are undefined', () => { |
| 68 | + const result = defaultMapProps( |
| 69 | + { accessibilityState: { checked: undefined, selected: undefined } }, |
| 70 | + node |
| 71 | + ); |
| 72 | + |
| 73 | + expect(result).toStrictEqual({}); |
| 74 | + }); |
| 75 | + |
| 76 | + test('does not fail if accessibilityState is a string, passes through', () => { |
| 77 | + const result = defaultMapProps({ accessibilityState: 'foo' }, node); |
| 78 | + expect(result).toStrictEqual({ accessibilityState: 'foo' }); |
| 79 | + }); |
| 80 | + |
| 81 | + test('does not fail if accessibilityState is an array, passes through', () => { |
| 82 | + const result = defaultMapProps({ accessibilityState: [1] }, node); |
| 83 | + expect(result).toStrictEqual({ accessibilityState: [1] }); |
| 84 | + }); |
| 85 | + |
| 86 | + test('does not fail if accessibilityState is null, passes through', () => { |
| 87 | + const result = defaultMapProps({ accessibilityState: null }, node); |
| 88 | + expect(result).toStrictEqual({ accessibilityState: null }); |
| 89 | + }); |
| 90 | + |
| 91 | + test('does not fail if accessibilityState is nested object, passes through', () => { |
| 92 | + const accessibilityState = { 1: { 2: 3 }, 2: undefined }; |
| 93 | + const result = defaultMapProps({ accessibilityState }, node); |
| 94 | + expect(result).toStrictEqual({ accessibilityState: { 1: { 2: 3 } } }); |
| 95 | + }); |
| 96 | + |
| 97 | + test('removes undefined keys from accessibilityValue', () => { |
| 98 | + const result = defaultMapProps( |
| 99 | + { accessibilityValue: { min: 1, max: undefined } }, |
| 100 | + node |
| 101 | + ); |
| 102 | + |
| 103 | + expect(result).toStrictEqual({ accessibilityValue: { min: 1 } }); |
| 104 | + }); |
| 105 | + |
| 106 | + test('removes accessibilityValue if all keys are undefined', () => { |
| 107 | + const result = defaultMapProps( |
| 108 | + { accessibilityValue: { min: undefined } }, |
| 109 | + node |
| 110 | + ); |
| 111 | + |
| 112 | + expect(result).toStrictEqual({}); |
| 113 | + }); |
| 114 | +}); |
0 commit comments