-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathRegularExpression.test.ts
93 lines (78 loc) · 3.05 KB
/
RegularExpression.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
/* global jest, describe, test, expect */
import { Kind } from 'graphql/language';
import { RegularExpression } from '../src/RegularExpression.js';
describe('RegularExpression', () => {
const GraphQLAbc = new RegularExpression('Abc', /^abc$/);
describe('valid', () => {
test('serialize', () => {
expect(GraphQLAbc.serialize('abc')).toBe('abc');
});
test('parseValue', () => {
expect(GraphQLAbc.parseValue('abc')).toBe('abc');
});
test('parseLiteral', () => {
expect(GraphQLAbc.parseLiteral({ value: 'abc', kind: Kind.STRING }, {})).toBe('abc');
});
});
describe('invalid', () => {
describe('does not match', () => {
test('serialize', () => {
expect(() => GraphQLAbc.serialize('this does not match')).toThrow(/Value does not match/);
});
test('parseValue', () => {
expect(() => GraphQLAbc.parseValue('this does not match')).toThrow(/Value does not match/);
});
test('parseLiteral', () => {
expect(() =>
GraphQLAbc.parseLiteral({ value: 'this does not match', kind: Kind.STRING }, {}),
).toThrow(/Value does not match/);
});
});
describe('does not match (w/options)', () => {
const errorMessageFn = (regex: RegExp, value: string) =>
`This is a custom error message ${regex}: ${value}`;
test('serialize', () => {
const errorMessage = jest.fn(errorMessageFn);
const GraphQLAbcWithOptions = new RegularExpression('Abc', /^abc$/, {
errorMessage,
});
expect(() => GraphQLAbcWithOptions.serialize('this does not match')).toThrow(
/This is a custom error message/,
);
expect(errorMessage.mock.calls.length).toBe(1);
expect(errorMessage.mock.calls[0][0]).toEqual(/^abc$/);
expect(errorMessage.mock.calls[0][1]).toEqual('this does not match');
});
test('parseValue', () => {
const errorMessage = jest.fn(errorMessageFn);
const GraphQLAbcWithOptions = new RegularExpression('Abc', /^abc$/, {
errorMessage,
});
expect(() => GraphQLAbcWithOptions.parseValue('this does not match')).toThrow(
/This is a custom error message/,
);
expect(errorMessage.mock.calls.length).toBe(1);
expect(errorMessage.mock.calls[0][0]).toEqual(/^abc$/);
expect(errorMessage.mock.calls[0][1]).toEqual('this does not match');
});
test('parseLiteral', () => {
const errorMessage = jest.fn(errorMessageFn);
const AbcWithOptions = new RegularExpression('Abc', /^abc$/, {
errorMessage,
});
expect(() =>
AbcWithOptions.parseLiteral(
{
value: 'this does not match',
kind: Kind.STRING,
},
{},
),
).toThrow(/This is a custom error message/);
expect(errorMessage.mock.calls.length).toBe(1);
expect(errorMessage.mock.calls[0][0]).toEqual(/^abc$/);
expect(errorMessage.mock.calls[0][1]).toEqual('this does not match');
});
});
});
});