Skip to content

Commit 4c9f5af

Browse files
committed
Re-organized tests a bit
src/__tests__/DateTime.test.js src/__tests__/EmailAddress.test.js src/__tests__/NonNegativeFloat.test.js src/__tests__/NonNegativeInt.test.js src/__tests__/PositiveFloat.test.js src/__tests__/PositiveInt.test.js src/__tests__/URL.test.js
1 parent 739edc7 commit 4c9f5af

File tree

7 files changed

+556
-469
lines changed

7 files changed

+556
-469
lines changed

src/__tests__/DateTime.test.js

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
1-
/* global test, expect */
1+
/* global describe, test, expect */
22

33
import { Kind } from 'graphql/language';
44

55
import { DateTime } from '../';
66

7-
// Valid Date
8-
test('DateTime - valid - serialize', () => {
9-
const now = new Date();
10-
expect(DateTime.serialize(now)).toEqual(now.toJSON());
11-
});
12-
13-
test('DateTime - valid - parseValue', () => {
14-
const now = new Date();
15-
expect(DateTime.parseValue(now)).toEqual(now);
16-
});
17-
18-
test('DateTime - valid - parseLiteral', () => {
19-
const result = new Date(Date.UTC(2017, 0, 2, 3, 4, 5, 0));
20-
expect(DateTime.parseLiteral({ value: '2017-01-02T03:04:05.000Z', kind: Kind.STRING })).toEqual(result);
21-
});
22-
23-
24-
// Not a valid Date
25-
test('DateTime - not a valid date - serialize', () => {
26-
expect(() => DateTime.serialize('this is not a date')).toThrow(/Value is not an instance of Date/);
27-
});
28-
29-
test('DateTime - not a valid date - parseValue', () => {
30-
expect(() => DateTime.parseValue('this is not a date')).toThrow(/Value is not a valid Date/);
31-
});
32-
33-
test('DateTime - not a valid date - parseLiteral', () => {
34-
expect(() => DateTime.parseLiteral({ value: 'this is not a date', kind: Kind.STRING })).toThrow(/Value is not a valid Date/);
7+
describe('DateTime', () => {
8+
describe('valid', () => {
9+
test('serialize', () => {
10+
const now = new Date();
11+
expect(DateTime.serialize(now)).toEqual(now.toJSON());
12+
});
13+
14+
test('parseValue', () => {
15+
const now = new Date();
16+
expect(DateTime.parseValue(now)).toEqual(now);
17+
});
18+
19+
test('parseLiteral', () => {
20+
const result = new Date(Date.UTC(2017, 0, 2, 3, 4, 5, 0));
21+
expect(DateTime.parseLiteral({ value: '2017-01-02T03:04:05.000Z', kind: Kind.STRING })).toEqual(result);
22+
});
23+
});
24+
25+
describe('invalid', () => {
26+
describe('not a valid date', () => {
27+
test('serialize', () => {
28+
expect(() => DateTime.serialize('this is not a date')).toThrow(/Value is not an instance of Date/);
29+
});
30+
31+
test('parseValue', () => {
32+
expect(() => DateTime.parseValue('this is not a date')).toThrow(/Value is not a valid Date/);
33+
});
34+
35+
test('parseLiteral', () => {
36+
expect(() => DateTime.parseLiteral({ value: 'this is not a date', kind: Kind.STRING })).toThrow(/Value is not a valid Date/);
37+
});
38+
});
39+
});
3540
});

src/__tests__/EmailAddress.test.js

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,51 @@
1-
/* global test, expect */
1+
/* global describe, test, expect */
22

33
import { Kind } from 'graphql/language';
44

55
import { EmailAddress } from '../';
66

7-
// Valid email address
8-
test('EmailAddress - valid - serialize', () => {
9-
expect(EmailAddress.serialize('test@test.com')).toBe('test@test.com');
10-
});
11-
12-
test('EmailAddress - valid - parseValue', () => {
13-
expect(EmailAddress.parseValue('test@test.com')).toBe('test@test.com');
14-
});
15-
16-
test('EmailAddress - valid - parseLiteral', () => {
17-
expect(EmailAddress.parseLiteral({ value: 'test@test.com', kind: Kind.STRING })).toBe('test@test.com');
18-
});
19-
20-
21-
// Not an email address
22-
test('EmailAddress - not an email address - serialize', () => {
23-
expect(() => EmailAddress.serialize('this is not an email address')).toThrow(/Value is not a valid email address/);
24-
});
25-
26-
test('EmailAddress - not an email address - parseValue', () => {
27-
expect(() => EmailAddress.parseValue('this is not an email address')).toThrow(/Value is not a valid email address/);
28-
});
29-
30-
test('EmailAddress - not an email address - parseLiteral', () => {
31-
expect(() => EmailAddress.parseLiteral({ value: 'this is not an email address', kind: Kind.STRING })).toThrow(/Value is not a valid email address/);
32-
});
33-
34-
35-
// Not a string
36-
test('EmailAddress - not a string - serialize', () => {
37-
expect(() => EmailAddress.serialize(123)).toThrow(/Value is not string/);
38-
});
39-
40-
test('EmailAddress - not a string - parseValue', () => {
41-
expect(() => EmailAddress.parseValue(123)).toThrow(/Value is not string/);
42-
});
43-
44-
test('EmailAddress - not a string - parseLiteral', () => {
45-
expect(() => EmailAddress.parseLiteral({ value: 123, kind: Kind.INT })).toThrow(/Can only validate strings as email addresses but got a/);
7+
describe('EmailAddress', () => {
8+
describe('valid', () => {
9+
test('serialize', () => {
10+
expect(EmailAddress.serialize('test@test.com')).toBe('test@test.com');
11+
});
12+
13+
test('parseValue', () => {
14+
expect(EmailAddress.parseValue('test@test.com')).toBe('test@test.com');
15+
});
16+
17+
test('parseLiteral', () => {
18+
expect(EmailAddress.parseLiteral({ value: 'test@test.com', kind: Kind.STRING })).toBe('test@test.com');
19+
});
20+
});
21+
22+
describe('invalid', () => {
23+
describe('not an email address', () => {
24+
test('serialize', () => {
25+
expect(() => EmailAddress.serialize('this is not an email address')).toThrow(/Value is not a valid email address/);
26+
});
27+
28+
test('parseValue', () => {
29+
expect(() => EmailAddress.parseValue('this is not an email address')).toThrow(/Value is not a valid email address/);
30+
});
31+
32+
test('parseLiteral', () => {
33+
expect(() => EmailAddress.parseLiteral({ value: 'this is not an email address', kind: Kind.STRING })).toThrow(/Value is not a valid email address/);
34+
});
35+
});
36+
37+
describe('not a string', () => {
38+
test('serialize', () => {
39+
expect(() => EmailAddress.serialize(123)).toThrow(/Value is not string/);
40+
});
41+
42+
test('parseValue', () => {
43+
expect(() => EmailAddress.parseValue(123)).toThrow(/Value is not string/);
44+
});
45+
46+
test('parseLiteral', () => {
47+
expect(() => EmailAddress.parseLiteral({ value: 123, kind: Kind.INT })).toThrow(/Can only validate strings as email addresses but got a/);
48+
});
49+
});
50+
});
4651
});
Lines changed: 109 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,115 @@
1-
/* global test, expect */
1+
/* global describe, test, expect */
22

33
import { Kind } from 'graphql/language';
44

55
import { NonNegativeFloat } from '../';
66

7-
// Number greater than zero
8-
test('NonNegativeFloat - valid as float - serialize', () => {
9-
expect(NonNegativeFloat.serialize(123.45)).toBe(123.45);
10-
});
11-
12-
test('NonNegativeFloat - valid as float - parseValue', () => {
13-
expect(NonNegativeFloat.parseValue(123.45)).toBe(123.45);
14-
});
15-
16-
test('NonNegativeFloat - valid as float - parseLiteral', () => {
17-
expect(NonNegativeFloat.parseLiteral({ value: 123.45, kind: Kind.FLOAT })).toBe(123.45);
18-
});
19-
20-
test('NonNegativeFloat - valid as string - serialize', () => {
21-
expect(NonNegativeFloat.serialize('123.45')).toBe(123.45);
22-
});
23-
24-
test('NonNegativeFloat - valid as string - parseValue', () => {
25-
expect(NonNegativeFloat.parseValue('123.45')).toBe(123.45);
26-
});
27-
28-
test('NonNegativeFloat - valid as string - parseLiteral', () => {
29-
expect(NonNegativeFloat.parseLiteral({ value: '123.45', kind: Kind.FLOAT })).toBe(123.45);
30-
});
31-
32-
33-
// Number equal to zero
34-
test('NonNegativeFloat - valid zero as float - serialize', () => {
35-
expect(NonNegativeFloat.serialize(0.0)).toBe(0.0);
36-
});
37-
38-
test('NonNegativeFloat - valid zero as float - parseValue', () => {
39-
expect(NonNegativeFloat.parseValue(0.0)).toBe(0.0);
40-
});
41-
42-
test('NonNegativeFloat - valid zero as float - parseLiteral', () => {
43-
expect(NonNegativeFloat.parseLiteral({ value: 0.0, kind: Kind.FLOAT })).toBe(0.0);
44-
});
45-
46-
test('NonNegativeFloat - valid zero as string - serialize', () => {
47-
expect(NonNegativeFloat.serialize('0.0')).toBe(0.0);
48-
});
49-
50-
test('NonNegativeFloat - valid zero as string - parseValue', () => {
51-
expect(NonNegativeFloat.parseValue('0.0')).toBe(0.0);
52-
});
53-
54-
test('NonNegativeFloat - valid zero as string - parseLiteral', () => {
55-
expect(NonNegativeFloat.parseLiteral({ value: '0.0', kind: Kind.FLOAT })).toBe(0.0);
56-
});
57-
58-
59-
// Number less than zero
60-
test('NonNegativeFloat - less than zero as float - serialize', () => {
61-
expect(() => NonNegativeFloat.serialize(-1.0)).toThrow(/Value is a negative number/);
62-
});
63-
64-
test('NonNegativeFloat - less than zero as float - parseValue', () => {
65-
expect(() => NonNegativeFloat.parseValue(-1.0)).toThrow(/Value is a negative number/);
66-
});
67-
68-
test('NonNegativeFloat - less than zero as float - parseLiteral', () => {
69-
expect(() => NonNegativeFloat.parseLiteral({ value: -1.0, kind: Kind.FLOAT })).toThrow(/Value is a negative number/);
70-
});
71-
72-
test('NonNegativeFloat - less than zero as string - serialize', () => {
73-
expect(() => NonNegativeFloat.serialize('-1.0')).toThrow(/Value is a negative number/);
74-
});
75-
76-
test('NonNegativeFloat - less than zero as string - parseValue', () => {
77-
expect(() => NonNegativeFloat.parseValue('-1.0')).toThrow(/Value is a negative number/);
78-
});
79-
80-
test('NonNegativeFloat - less than zero as string - parseLiteral', () => {
81-
expect(() => NonNegativeFloat.parseLiteral({ value: '-1.0', kind: Kind.FLOAT })).toThrow(/Value is a negative number/);
82-
});
83-
84-
85-
// Not a number
86-
test('NonNegativeFloat - not a number - serialize', () => {
87-
expect(() => NonNegativeFloat.serialize('not a number')).toThrow(/Value is not a number/);
88-
});
89-
90-
test('NonNegativeFloat - not a number - parseValue', () => {
91-
expect(() => NonNegativeFloat.parseValue('not a number')).toThrow(/Value is not a number/);
92-
});
93-
94-
test('NonNegativeFloat - not a number - parseLiteral', () => {
95-
expect(() => NonNegativeFloat.parseLiteral({ value: 'not a number', kind: Kind.STRING })).toThrow(/Can only validate floating point numbers as non-negative floating point numbers but got a/);
7+
describe('NonNegativeFloat', () => {
8+
describe('valid', () => {
9+
describe('greater than zero', () => {
10+
describe('as float', () => {
11+
test('serialize', () => {
12+
expect(NonNegativeFloat.serialize(123.45)).toBe(123.45);
13+
});
14+
15+
test('parseValue', () => {
16+
expect(NonNegativeFloat.parseValue(123.45)).toBe(123.45);
17+
});
18+
19+
test('parseLiteral', () => {
20+
expect(NonNegativeFloat.parseLiteral({ value: 123.45, kind: Kind.FLOAT })).toBe(123.45);
21+
});
22+
});
23+
24+
describe('as string', () => {
25+
test('serialize', () => {
26+
expect(NonNegativeFloat.serialize('123.45')).toBe(123.45);
27+
});
28+
29+
test('parseValue', () => {
30+
expect(NonNegativeFloat.parseValue('123.45')).toBe(123.45);
31+
});
32+
33+
test('parseLiteral', () => {
34+
expect(NonNegativeFloat.parseLiteral({ value: '123.45', kind: Kind.FLOAT })).toBe(123.45);
35+
});
36+
});
37+
});
38+
39+
describe('zero', () => {
40+
describe('as float', () => {
41+
test('serialize', () => {
42+
expect(NonNegativeFloat.serialize(0.0)).toBe(0.0);
43+
});
44+
45+
test('parseValue', () => {
46+
expect(NonNegativeFloat.parseValue(0.0)).toBe(0.0);
47+
});
48+
49+
test('parseLiteral', () => {
50+
expect(NonNegativeFloat.parseLiteral({ value: 0.0, kind: Kind.FLOAT })).toBe(0.0);
51+
});
52+
});
53+
54+
describe('as string', () => {
55+
test('serialize', () => {
56+
expect(NonNegativeFloat.serialize('0.0')).toBe(0.0);
57+
});
58+
59+
test('parseValue', () => {
60+
expect(NonNegativeFloat.parseValue('0.0')).toBe(0.0);
61+
});
62+
63+
test('parseLiteral', () => {
64+
expect(NonNegativeFloat.parseLiteral({ value: '0.0', kind: Kind.FLOAT })).toBe(0.0);
65+
});
66+
});
67+
});
68+
});
69+
70+
describe('invalid', () => {
71+
describe('less than zero', () => {
72+
describe('as float', () => {
73+
test('serialize', () => {
74+
expect(() => NonNegativeFloat.serialize(-1.0)).toThrow(/Value is a negative number/);
75+
});
76+
77+
test('parseValue', () => {
78+
expect(() => NonNegativeFloat.parseValue(-1.0)).toThrow(/Value is a negative number/);
79+
});
80+
81+
test('parseLiteral', () => {
82+
expect(() => NonNegativeFloat.parseLiteral({ value: -1.0, kind: Kind.FLOAT })).toThrow(/Value is a negative number/);
83+
});
84+
});
85+
86+
describe('as string', () => {
87+
test('serialize', () => {
88+
expect(() => NonNegativeFloat.serialize('-1.0')).toThrow(/Value is a negative number/);
89+
});
90+
91+
test('parseValue', () => {
92+
expect(() => NonNegativeFloat.parseValue('-1.0')).toThrow(/Value is a negative number/);
93+
});
94+
95+
test('parseLiteral', () => {
96+
expect(() => NonNegativeFloat.parseLiteral({ value: '-1.0', kind: Kind.FLOAT })).toThrow(/Value is a negative number/);
97+
});
98+
});
99+
});
100+
101+
describe('not a number', () => {
102+
test('serialize', () => {
103+
expect(() => NonNegativeFloat.serialize('not a number')).toThrow(/Value is not a number/);
104+
});
105+
106+
test('parseValue', () => {
107+
expect(() => NonNegativeFloat.parseValue('not a number')).toThrow(/Value is not a number/);
108+
});
109+
110+
test('parseLiteral', () => {
111+
expect(() => NonNegativeFloat.parseLiteral({ value: 'not a number', kind: Kind.STRING })).toThrow(/Can only validate floating point numbers as non-negative floating point numbers but got a/);
112+
});
113+
});
114+
});
96115
});

0 commit comments

Comments
 (0)