-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathISBN.test.ts
104 lines (93 loc) · 2.66 KB
/
ISBN.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
102
103
104
/* global describe, it, expect */
import { Kind } from 'graphql/language';
import { GraphQLISBN } from '../src/scalars/ISBN.js';
// Taken from https://github.com/hapijs/joi/blob/master/test/types/string.js
const serialNumbers = [
// 10 chars with ISBN prefix
`ISBN 1561616161`,
`ISBN 1577677171`,
`ISBN 978 787 78 78`,
`ISBN 978 787 7878`,
`ISBN 978-0615-856`,
`ISBN 9790765335`,
`ISBN 979076533X`,
`ISBN: 978-0615-856`,
`ISBN: 9780615856`,
// 10 chars without ISBN prefix
`1577677171`,
`978 787 78 78`,
`978 787 7878`,
`978-0615-856`,
`9790765335`,
`979076533X`,
`978-0615-856`,
`9780615856`,
`9780615856`,
`978 787 7878`,
// 13 chars with ISBN prefix
`ISBN 978 787 78 78788`,
`ISBN 978-0615-856-73-5`,
`ISBN 9790765335999`,
`ISBN-13: 978-0615-856-73-5`,
`ISBN-13: 978-0615856735`,
`ISBN-13: 9780765335999`,
`ISBN: 978-0615-856-73-5`,
`ISBN: 9780615856735`,
// 13 chars without ISBN prefix
`978-0615-856-73-5`,
`978-0615856735`,
`9780765335999`,
`978-0615-856-73-5`,
`9780615856735`,
];
describe(`ISBN`, () => {
describe(`valid`, () => {
it(`serialize`, () => {
for (const value of serialNumbers) {
expect(GraphQLISBN.serialize(value)).toEqual(value);
}
});
it(`parseValue`, () => {
for (const value of serialNumbers) {
expect(GraphQLISBN.parseValue(value)).toEqual(value);
}
});
it(`parseLiteral`, () => {
for (const value of serialNumbers) {
expect(
GraphQLISBN.parseLiteral(
{
value,
kind: Kind.STRING,
},
{},
),
).toEqual(value);
}
});
});
describe(`invalid`, () => {
describe(`not a valid ISBN address`, () => {
it(`serialize`, () => {
expect(() => GraphQLISBN.serialize(123)).toThrow(/Value is not string/);
expect(() => GraphQLISBN.serialize(`this is not an ISBN number`)).toThrow(
/Value is not a valid ISBN number/,
);
});
it(`parseValue`, () => {
expect(() => GraphQLISBN.serialize(123)).toThrow(/Value is not string/);
expect(() => GraphQLISBN.parseValue(`this is not an ISBN number`)).toThrow(
/Value is not a valid ISBN number/,
);
});
it(`parseLiteral`, () => {
expect(() => GraphQLISBN.parseLiteral({ value: 123, kind: Kind.INT } as any, {})).toThrow(
/Can only validate strings as ISBN numbers but got a/,
);
expect(() =>
GraphQLISBN.parseLiteral({ value: `this is not an ISBN number`, kind: Kind.STRING }, {}),
).toThrow(/Value is not a valid ISBN number/);
});
});
});
});