forked from Urigo/graphql-scalars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocalDateTime.test.ts
101 lines (97 loc) · 3.48 KB
/
LocalDateTime.test.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
94
95
96
97
98
99
100
101
import { Kind } from 'graphql';
import { GraphQLLocalDateTime } from '../src';
const VALID_LOCAL_DATE_TIMES = [
'2016-02-01T00:00:15Z',
'2016-02-01T00:00:00-11:00',
'2017-01-07T11:25:00+01:00',
'2017-01-07T00:00:00+01:20',
'2016-02-01T00:00:00.1Z',
'2016-02-01T00:00:00.000Z',
'2016-02-01T00:00:00.990Z',
'2016-02-01T00:00:00.23498Z',
'2017-01-07T11:25:00.450+01:00',
'2016-02-01t00:00:00.000z',
];
describe(`LocalDateTime`, () => {
describe('valid', () => {
it('serialize', () => {
VALID_LOCAL_DATE_TIMES.forEach(value => {
expect(GraphQLLocalDateTime.serialize(value)).toEqual(value);
});
});
it('parseValue', () => {
VALID_LOCAL_DATE_TIMES.forEach(value => {
expect(GraphQLLocalDateTime.parseValue(value)).toEqual(value);
});
});
it('parseLiteral', () => {
VALID_LOCAL_DATE_TIMES.forEach(value => {
expect(
GraphQLLocalDateTime.parseLiteral(
{
value,
kind: Kind.STRING,
},
{},
),
).toEqual(value);
});
});
});
describe('invalid', () => {
describe('not string', () => {
it('serialize', () => {
expect(() => GraphQLLocalDateTime.serialize(123)).toThrow(/Value is not string/);
expect(() => GraphQLLocalDateTime.serialize(false)).toThrow(/Value is not string/);
});
it('parseValue', () => {
expect(() => GraphQLLocalDateTime.parseValue(123)).toThrow(/Value is not string/);
expect(() => GraphQLLocalDateTime.parseValue(false)).toThrow(/Value is not string/);
});
it('parseLiteral', () => {
expect(() =>
GraphQLLocalDateTime.parseLiteral({ value: 123 as any, kind: Kind.INT }, {}),
).toThrow(/Can only validate strings as local date-times but got a: IntValue/);
expect(() =>
GraphQLLocalDateTime.parseLiteral({ value: false, kind: Kind.BOOLEAN }, {}),
).toThrow(/Can only validate strings as local date-times but got a: BooleanValue/);
});
});
describe('not a valid local date time string', () => {
const invalidDateTime = '2015-02-24T00:00:00.000+0100';
it('serialize', () => {
expect(() => GraphQLLocalDateTime.serialize(invalidDateTime)).toThrow(
/LocalDateTime cannot represent an invalid local date-time-string/,
);
});
it('parseValue', () => {
expect(() => GraphQLLocalDateTime.parseValue(invalidDateTime)).toThrow(
/LocalDateTime cannot represent an invalid local date-time-string/,
);
});
it('parseLiteral', () => {
expect(() =>
GraphQLLocalDateTime.parseLiteral({ value: invalidDateTime, kind: Kind.STRING }, {}),
).toThrow(/LocalDateTime cannot represent an invalid local date-time-string/);
});
});
describe('invalid date', () => {
const invalidDateTime = '2017-13-46T11:25:00.450+01:00';
it('serialize', () => {
expect(() => GraphQLLocalDateTime.serialize(invalidDateTime)).toThrow(
/Value is not a valid LocalDateTime/,
);
});
it('parseValue', () => {
expect(() => GraphQLLocalDateTime.parseValue(invalidDateTime)).toThrow(
/Value is not a valid LocalDateTime/,
);
});
it('parseLiteral', () => {
expect(() =>
GraphQLLocalDateTime.parseLiteral({ value: invalidDateTime, kind: Kind.STRING }, {}),
).toThrow(/Value is not a valid LocalDateTime/);
});
});
});
});