1import { render } from '@testing-library/react';
2import * as React from 'react';
3
4import { resolveTypeName } from './APISectionUtils';
5
6describe('APISectionUtils.resolveTypeName', () => {
7  test('void', () => {
8    const { container } = render(<>{resolveTypeName({ type: 'intrinsic', name: 'void' })}</>);
9    expect(container).toMatchSnapshot();
10  });
11
12  test('generic type', () => {
13    const { container } = render(<>{resolveTypeName({ type: 'intrinsic', name: 'string' })}</>);
14    expect(container).toMatchSnapshot();
15  });
16
17  test('custom type', () => {
18    const { container } = render(
19      <>{resolveTypeName({ type: 'reference', name: 'SpeechSynthesisEvent' })}</>
20    );
21    expect(container).toMatchSnapshot();
22  });
23
24  test('custom type array', () => {
25    const { container } = render(
26      <>
27        {resolveTypeName({
28          type: 'array',
29          elementType: { type: 'reference', name: 'AppleAuthenticationScope' },
30        })}
31      </>
32    );
33    expect(container).toMatchSnapshot();
34  });
35
36  test('custom type non-linkable array', () => {
37    const { container } = render(
38      <>
39        {resolveTypeName({
40          type: 'array',
41          elementType: { type: 'reference', name: 'T' },
42        })}
43      </>
44    );
45    expect(container).toMatchSnapshot();
46  });
47
48  test('query type', () => {
49    const { container } = render(
50      <>
51        {resolveTypeName({
52          type: 'reference',
53          typeArguments: [{ queryType: { type: 'reference', name: 'View' }, type: 'query' }],
54          name: 'React.ComponentProps',
55        })}
56      </>
57    );
58    expect(container).toMatchSnapshot();
59  });
60
61  test('Promise', () => {
62    const { container } = render(
63      <>
64        {resolveTypeName({
65          type: 'reference',
66          typeArguments: [{ type: 'intrinsic', name: 'void' }],
67          name: 'Promise',
68        })}
69      </>
70    );
71    expect(container).toMatchSnapshot();
72  });
73
74  test('Promise with custom type', () => {
75    const { container } = render(
76      <>
77        {resolveTypeName({
78          type: 'reference',
79          typeArguments: [{ type: 'reference', name: 'AppleAuthenticationCredential' }],
80          name: 'Promise',
81        })}
82      </>
83    );
84    expect(container).toMatchSnapshot();
85  });
86
87  test('Record', () => {
88    const { container } = render(
89      <>
90        {resolveTypeName({
91          type: 'reference',
92          typeArguments: [
93            { type: 'intrinsic', name: 'string' },
94            { type: 'intrinsic', name: 'any' },
95          ],
96          name: 'Record',
97        })}
98      </>
99    );
100    expect(container).toMatchSnapshot();
101  });
102
103  test('Record with union', () => {
104    const { container } = render(
105      <>
106        {resolveTypeName({
107          type: 'reference',
108          typeArguments: [
109            { type: 'intrinsic', name: 'string' },
110            {
111              type: 'union',
112              types: [
113                { type: 'intrinsic', name: 'number' },
114                { type: 'intrinsic', name: 'boolean' },
115                { type: 'intrinsic', name: 'string' },
116              ],
117            },
118          ],
119          name: 'Record',
120        })}
121      </>
122    );
123    expect(container).toMatchSnapshot();
124  });
125
126  test('union', () => {
127    const { container } = render(
128      <>
129        {resolveTypeName({
130          type: 'union',
131          types: [
132            { type: 'reference', name: 'SpeechEventCallback' },
133            { type: 'literal', value: null },
134          ],
135        })}
136      </>
137    );
138    expect(container).toMatchSnapshot();
139  });
140
141  test('union with array', () => {
142    const { container } = render(
143      <>
144        {resolveTypeName({
145          type: 'union',
146          types: [
147            { type: 'array', elementType: { type: 'intrinsic', name: 'number' } },
148            { type: 'literal', value: null },
149          ],
150        })}
151      </>
152    );
153    expect(container).toMatchSnapshot();
154  });
155
156  test('union with custom type and array', () => {
157    const { container } = render(
158      <>
159        {resolveTypeName({
160          type: 'union',
161          types: [
162            { type: 'array', elementType: { type: 'reference', name: 'AssetRef' } },
163            { type: 'reference', name: 'AssetRef' },
164          ],
165        })}
166      </>
167    );
168    expect(container).toMatchSnapshot();
169  });
170
171  test('generic type', () => {
172    const { container } = render(
173      <>
174        {resolveTypeName({
175          type: 'reference',
176          typeArguments: [{ type: 'reference', name: 'Asset' }],
177          name: 'PagedInfo',
178        })}
179      </>
180    );
181    expect(container).toMatchSnapshot();
182  });
183
184  test('tuple type', () => {
185    const { container } = render(
186      <>
187        {resolveTypeName({
188          type: 'tuple',
189          elements: [
190            { type: 'reference', name: 'SortByKey' },
191            { type: 'intrinsic', name: 'boolean' },
192          ],
193        })}
194      </>
195    );
196    expect(container).toMatchSnapshot();
197  });
198
199  test('generic type in Promise', () => {
200    const { container } = render(
201      <>
202        {resolveTypeName({
203          type: 'reference',
204          typeArguments: [
205            {
206              type: 'reference',
207              typeArguments: [{ type: 'reference', name: 'Asset' }],
208              name: 'PagedInfo',
209            },
210          ],
211          name: 'Promise',
212        })}
213      </>
214    );
215    expect(container).toMatchSnapshot();
216  });
217
218  test('function', () => {
219    const { container } = render(
220      <>
221        {resolveTypeName({
222          type: 'reflection',
223          declaration: {
224            signatures: [
225              {
226                type: {
227                  type: 'union',
228                  types: [
229                    { type: 'intrinsic', name: 'void' },
230                    {
231                      type: 'reference',
232                      name: 'SpeechEventCallback',
233                    },
234                  ],
235                },
236              },
237            ],
238          },
239        })}
240      </>
241    );
242    expect(container).toMatchSnapshot();
243  });
244
245  test('function with arguments', () => {
246    const { container } = render(
247      <>
248        {resolveTypeName({
249          type: 'reflection',
250          declaration: {
251            signatures: [
252              {
253                parameters: [
254                  {
255                    name: 'error',
256                    type: { type: 'reference', name: 'Error' },
257                  },
258                ],
259                type: {
260                  type: 'union',
261                  types: [
262                    { type: 'intrinsic', name: 'void' },
263                    { type: 'reference', name: 'SpeechEventCallback' },
264                  ],
265                },
266              },
267            ],
268          },
269        })}
270      </>
271    );
272    expect(container).toMatchSnapshot();
273  });
274
275  test('function with non-linkable custom type', () => {
276    const { container } = render(
277      <>
278        {resolveTypeName({
279          type: 'reflection',
280          declaration: {
281            signatures: [
282              {
283                parameters: [
284                  {
285                    name: 'error',
286                    type: { type: 'reference', name: 'Error' },
287                  },
288                ],
289                type: { type: 'intrinsic', name: 'void' },
290              },
291            ],
292          },
293        })}
294      </>
295    );
296    expect(container).toMatchSnapshot();
297  });
298
299  test('extended props with multiple omits', () => {
300    const { container } = render(
301      <>
302        {resolveTypeName({
303          type: 'reference',
304          typeArguments: [
305            {
306              type: 'reference',
307              typeArguments: [
308                { type: 'reference', name: 'ViewStyle' },
309                {
310                  type: 'union',
311                  types: [
312                    { type: 'literal', value: 'backgroundColor' },
313                    {
314                      type: 'literal',
315                      value: 'borderRadius',
316                    },
317                  ],
318                },
319              ],
320              name: 'Omit',
321            },
322          ],
323          name: 'StyleProp',
324        })}
325      </>
326    );
327    expect(container).toMatchSnapshot();
328  });
329});
330