-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathgetTypeFromValues.spec.ts
93 lines (81 loc) · 2.83 KB
/
getTypeFromValues.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import {
GraphQLBoolean,
GraphQLFloat,
GraphQLID,
GraphQLInt,
GraphQLList,
GraphQLNonNull,
GraphQLString,
} from 'graphql';
import { GraphQLJSON } from 'graphql-type-json';
import { expect, test } from 'vitest';
import DateType from './DateType';
import getTypeFromValues from './getTypeFromValues';
test('returns GraphQLID for fields named id or xxx_id', () => {
expect(getTypeFromValues('id')).toEqual(GraphQLID);
expect(getTypeFromValues('foo_id')).toEqual(GraphQLID);
});
test('returns GraphQLList for arrays', () => {
expect(getTypeFromValues('foo', [[true, false], [true]])).toEqual(
new GraphQLList(GraphQLBoolean),
);
expect(
getTypeFromValues('foo', [
['a', 'b'],
['c', 'd'],
]),
).toEqual(new GraphQLList(GraphQLString));
expect(
getTypeFromValues('foo', [
[123, 456],
[789, 123],
]),
).toEqual(new GraphQLList(GraphQLInt));
expect(
getTypeFromValues('foo', [
[1.23, 456],
[-5, 123],
]),
).toEqual(new GraphQLList(GraphQLFloat));
});
test('returns GraphQLBoolean for booleans', () =>
expect(getTypeFromValues('foo', [true, true, false])).toEqual(
GraphQLBoolean,
));
test('returns GraphQLString for strings', () => {
expect(getTypeFromValues('foo', ['123', '456'])).toEqual(GraphQLString);
expect(getTypeFromValues('foo', ['abc', '123'])).toEqual(GraphQLString);
});
test('returns GraphQLInt for integers', () =>
expect(getTypeFromValues('foo', [-1, 445, 34, 0])).toEqual(GraphQLInt));
test('returns GraphQLFloat for floats', () =>
expect(getTypeFromValues('foo', [-12, 1.2, 445, 0])).toEqual(GraphQLFloat));
test('returns DateType for Dates', () =>
expect(
getTypeFromValues('foo', [new Date('2017-03-15'), new Date()]),
).toEqual(DateType));
test('returns DateType for Dates as ISO Strings', () =>
expect(
getTypeFromValues('foo', [
'2022-01-01T00:00:00.000Z',
'2022-12-01T12:34:56.000Z',
]),
).toEqual(DateType));
test('returns GraphQLJSON for objects', () =>
expect(
getTypeFromValues('foo', [{ foo: 1 }, { bar: 2 }, { id: 'a' }]),
).toEqual(GraphQLJSON));
test('returns GraphQLJSON for arrays of objects', () =>
expect(
getTypeFromValues('foo', [[{ foo: 1 }, { bar: 2 }], [{ id: 'a' }]]),
).toEqual(GraphQLJSON));
test('returns GraphQLString for mixed values', () =>
expect(getTypeFromValues('foo', [0, '&', new Date()])).toEqual(
GraphQLString,
));
test('returns GraphQLString for no values', () =>
expect(getTypeFromValues('foo')).toEqual(GraphQLString));
test('returns GraphQLNonNull when all values are filled', () =>
expect(getTypeFromValues('foo', [], true)).toEqual(
new GraphQLNonNull(GraphQLString),
));