-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
mode-test.ts
123 lines (106 loc) · 3.33 KB
/
mode-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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* Copyright (c) 2021 GraphQL Contributors
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
import CodeMirror from 'codemirror';
import 'codemirror/addon/runmode/runmode';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import '../mode';
describe('graphql-mode', () => {
it('provides correct tokens and styles after parsing', () => {
const queryStr = 'query name { }';
const tokens: string[] = [];
const styles: string[] = [];
CodeMirror.runMode(queryStr, 'graphql', (token, style) => {
if (style && style !== 'ws') {
tokens.push(token);
styles.push(style);
}
});
expect(tokens).toEqual(['query', 'name', '{', '}']);
expect(styles).toEqual(['keyword', 'def', 'punctuation', 'punctuation']);
});
it('parses Relay-style anonymous FragmentDefinitions', () => {
CodeMirror.runMode('fragment on Test { id }', 'graphql', (_token, style) =>
expect(style).not.toBe('invalidchar'),
);
});
it('parses inline fragments with optional syntax correctly', () => {
CodeMirror.runMode(
'{ ... on OptionalType { name } }',
'graphql',
(_token, style) => expect(style).not.toBe('invalidchar'),
);
CodeMirror.runMode('{ ... { name } }', 'graphql', (_token, style) =>
expect(style).not.toBe('invalidchar'),
);
CodeMirror.runMode(
'{ ... @optionalDirective { name } }',
'graphql',
(_token, style) => expect(style).not.toBe('invalidchar'),
);
});
it('returns "invalidchar" message when there is no matching token', () => {
CodeMirror.runMode('invalidKeyword name', 'graphql', (token, style) => {
if (token.trim()) {
expect(style).toBe('invalidchar');
}
});
CodeMirror.runMode('query %', 'graphql', (token, style) => {
if (token === '%') {
expect(style).toBe('invalidchar');
}
});
});
it('parses kitchen-sink query without invalidchar', () => {
const kitchenSink = readFileSync(join(__dirname, '/kitchen-sink.graphql'), {
encoding: 'utf8',
});
CodeMirror.runMode(kitchenSink, 'graphql', (_token, style) => {
expect(style).not.toBe('invalidchar');
});
});
it('parses schema-kitchen-sink query without invalidchar', () => {
const schemaKitchenSink = readFileSync(
join(__dirname, '/schema-kitchen-sink.graphql'),
{ encoding: 'utf8' },
);
CodeMirror.runMode(schemaKitchenSink, 'graphql', (_token, style) => {
expect(style).not.toBe('invalidchar');
});
});
it('parses anonymous operations without invalidchar', () => {
CodeMirror.runMode('{ id }', 'graphql', (_token, style) => {
expect(style).not.toBe('invalidchar');
});
CodeMirror.runMode(
`
mutation {
setString(value: "newString")
}
`,
'graphql',
(_token, style) => {
expect(style).not.toBe('invalidchar');
},
);
CodeMirror.runMode(
`
subscription {
subscribeToTest(id: "anId") {
id
}
}
`,
'graphql',
(_token, style) => {
expect(style).not.toBe('invalidchar');
},
);
});
});