|
1 | 1 | import renderer from 'react-test-renderer';
|
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | +import { screen, render, waitFor } from '@testing-library/react'; |
2 | 4 | import JsonView from './';
|
3 | 5 |
|
| 6 | +const mySet = new Set(); |
| 7 | +mySet.add(1); // Set(1) { 1 } |
| 8 | +mySet.add(5); // Set(2) { 1, 5 } |
| 9 | +mySet.add(5); // Set(2) { 1, 5 } |
| 10 | +mySet.add('some text'); // Set(3) { 1, 5, 'some text' } |
| 11 | + |
| 12 | +const myMap = new Map(); |
| 13 | +myMap.set('www', 'foo'); |
| 14 | +myMap.set(1, 'bar'); |
| 15 | + |
4 | 16 | const avatar = 'https://i.imgur.com/MK3eW3As.jpg';
|
5 | 17 | const longArray = new Array(1000).fill(1);
|
6 | 18 | const example = {
|
@@ -47,3 +59,133 @@ it('renders <JsonView /> test case', () => {
|
47 | 59 | expect(tree).toHaveProperty('props.onMouseEnter');
|
48 | 60 | expect(tree).toHaveProperty('props.onMouseLeave');
|
49 | 61 | });
|
| 62 | + |
| 63 | +it('renders <JsonView.String /> test case', async () => { |
| 64 | + const demo = { |
| 65 | + string: 'Lorem ipsum dolor sit amet', |
| 66 | + }; |
| 67 | + const { container } = render( |
| 68 | + <JsonView value={demo}> |
| 69 | + <JsonView.String |
| 70 | + render={(props, { type }) => { |
| 71 | + if (type === 'type') { |
| 72 | + expect(props.style).toHaveProperty('color', 'var(--w-rjv-type-string-color, #cb4b16)'); |
| 73 | + return <em {...props} data-testid="str-type" />; |
| 74 | + } |
| 75 | + return <span {...props} data-testid="str-value" />; |
| 76 | + }} |
| 77 | + /> |
| 78 | + </JsonView>, |
| 79 | + ); |
| 80 | + expect(container.firstElementChild).toBeInstanceOf(Element); |
| 81 | + await waitFor(() => { |
| 82 | + const type = screen.getByTestId('str-type'); |
| 83 | + expect(type.className).toBe('w-rjv-type'); |
| 84 | + expect(type.tagName).toBe('EM'); |
| 85 | + expect(type.innerHTML).toBe('string'); |
| 86 | + const value = screen.getByTestId('str-value'); |
| 87 | + expect(value.className).toBe('w-rjv-value'); |
| 88 | + expect(value.tagName).toBe('SPAN'); |
| 89 | + expect(value).toHaveProperty('style.cursor', 'pointer'); |
| 90 | + }); |
| 91 | +}); |
| 92 | + |
| 93 | +[ |
| 94 | + { |
| 95 | + name: 'Bigint', |
| 96 | + Comp: JsonView.Bigint, |
| 97 | + value: BigInt(10086), |
| 98 | + field: 'bigint', |
| 99 | + color: 'var(--w-rjv-type-bigint-color, #268bd2)', |
| 100 | + }, |
| 101 | + // { |
| 102 | + // name: 'Date', |
| 103 | + // Comp: JsonView.Date, |
| 104 | + // value: new Date('2023/02/12'), |
| 105 | + // field: 'date', |
| 106 | + // color: 'var(--w-rjv-type-date-color, #268bd2)', |
| 107 | + // }, |
| 108 | + { |
| 109 | + name: 'False', |
| 110 | + Comp: JsonView.False, |
| 111 | + value: false, |
| 112 | + field: 'bool', |
| 113 | + color: 'var(--w-rjv-type-boolean-color, #2aa198)', |
| 114 | + }, |
| 115 | + { |
| 116 | + name: 'Float', |
| 117 | + Comp: JsonView.Float, |
| 118 | + value: 0.3, |
| 119 | + field: 'float', |
| 120 | + color: 'var(--w-rjv-type-float-color, #859900)', |
| 121 | + }, |
| 122 | + { |
| 123 | + name: 'Int', |
| 124 | + Comp: JsonView.Int, |
| 125 | + value: 123, |
| 126 | + field: 'int', |
| 127 | + color: 'var(--w-rjv-type-int-color, #268bd2)', |
| 128 | + }, |
| 129 | + { |
| 130 | + name: 'True', |
| 131 | + Comp: JsonView.True, |
| 132 | + value: true, |
| 133 | + field: 'bool', |
| 134 | + color: 'var(--w-rjv-type-boolean-color, #2aa198)', |
| 135 | + }, |
| 136 | + { |
| 137 | + name: 'Null', |
| 138 | + Comp: JsonView.Null, |
| 139 | + value: null, |
| 140 | + field: 'null', |
| 141 | + color: 'var(--w-rjv-type-null-color, #d33682)', |
| 142 | + }, |
| 143 | + { |
| 144 | + name: 'Undefined', |
| 145 | + Comp: JsonView.Undefined, |
| 146 | + value: undefined, |
| 147 | + field: 'undefined', |
| 148 | + color: 'var(--w-rjv-type-undefined-color, #586e75)', |
| 149 | + }, |
| 150 | + // { |
| 151 | + // name: 'Url', |
| 152 | + // Comp: JsonView.Url, |
| 153 | + // value: new URL('https://www.google.com'), |
| 154 | + // field: 'url', |
| 155 | + // color: 'var(--w-rjv-type-url-color, #0969da)', |
| 156 | + // }, |
| 157 | + // { |
| 158 | + // name: 'Map', |
| 159 | + // Comp: JsonView.Map, |
| 160 | + // value: myMap, |
| 161 | + // field: 'map', |
| 162 | + // color: 'var(--w-rjv-type-map-color, #268bd2)', |
| 163 | + // }, |
| 164 | +].forEach(({ name, value: val, field, color, Comp }) => { |
| 165 | + it(`renders <JsonView.${name} /> test case`, async () => { |
| 166 | + const demo = { |
| 167 | + value: val, |
| 168 | + }; |
| 169 | + const { container } = render( |
| 170 | + <JsonView value={demo}> |
| 171 | + <Comp |
| 172 | + render={(props, { type, value }) => { |
| 173 | + expect(value).toBe(val); |
| 174 | + expect(props.style).toHaveProperty('color', color); |
| 175 | + return <span {...props} data-testid={`${name}-${type}`} />; |
| 176 | + }} |
| 177 | + /> |
| 178 | + </JsonView>, |
| 179 | + ); |
| 180 | + expect(container.firstElementChild).toBeInstanceOf(Element); |
| 181 | + await waitFor(() => { |
| 182 | + const type = screen.getByTestId(`${name}-type`); |
| 183 | + expect(type.className).toBe('w-rjv-type'); |
| 184 | + expect(type.tagName).toBe('SPAN'); |
| 185 | + expect(type.innerHTML).toBe(field); |
| 186 | + const value = screen.getByTestId(`${name}-value`); |
| 187 | + expect(value.className).toBe('w-rjv-value'); |
| 188 | + expect(value.tagName).toBe('SPAN'); |
| 189 | + }); |
| 190 | + }); |
| 191 | +}); |
0 commit comments