Skip to content

Commit eadc6a9

Browse files
tests: replaces introspection queries with printing schemas (#327)
Reuse `dedent` from `graphql-js`
1 parent a4150aa commit eadc6a9

File tree

5 files changed

+247
-451
lines changed

5 files changed

+247
-451
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import { expect } from 'chai';
2+
import { describe, it } from 'mocha';
3+
4+
import { dedent, dedentString } from '../dedent';
5+
6+
describe('dedentString', () => {
7+
it('removes indentation in typical usage', () => {
8+
const output = dedentString(`
9+
type Query {
10+
me: User
11+
}
12+
13+
type User {
14+
id: ID
15+
name: String
16+
}
17+
`);
18+
expect(output).to.equal(
19+
[
20+
'type Query {',
21+
' me: User',
22+
'}',
23+
'',
24+
'type User {',
25+
' id: ID',
26+
' name: String',
27+
'}',
28+
].join('\n'),
29+
);
30+
});
31+
32+
it('removes only the first level of indentation', () => {
33+
const output = dedentString(`
34+
first
35+
second
36+
third
37+
fourth
38+
`);
39+
expect(output).to.equal(
40+
['first', ' second', ' third', ' fourth'].join('\n'),
41+
);
42+
});
43+
44+
it('does not escape special characters', () => {
45+
const output = dedentString(`
46+
type Root {
47+
field(arg: String = "wi\th de\fault"): String
48+
}
49+
`);
50+
expect(output).to.equal(
51+
[
52+
'type Root {',
53+
' field(arg: String = "wi\th de\fault"): String',
54+
'}',
55+
].join('\n'),
56+
);
57+
});
58+
59+
it('also removes indentation using tabs', () => {
60+
const output = dedentString(`
61+
\t\t type Query {
62+
\t\t me: User
63+
\t\t }
64+
`);
65+
expect(output).to.equal(['type Query {', ' me: User', '}'].join('\n'));
66+
});
67+
68+
it('removes leading and trailing newlines', () => {
69+
const output = dedentString(`
70+
71+
72+
type Query {
73+
me: User
74+
}
75+
76+
77+
`);
78+
expect(output).to.equal(['type Query {', ' me: User', '}'].join('\n'));
79+
});
80+
81+
it('removes all trailing spaces and tabs', () => {
82+
const output = dedentString(`
83+
type Query {
84+
me: User
85+
}
86+
\t\t \t `);
87+
expect(output).to.equal(['type Query {', ' me: User', '}'].join('\n'));
88+
});
89+
90+
it('works on text without leading newline', () => {
91+
const output = dedentString(` type Query {
92+
me: User
93+
}
94+
`);
95+
expect(output).to.equal(['type Query {', ' me: User', '}'].join('\n'));
96+
});
97+
});
98+
99+
describe('dedent', () => {
100+
it('removes indentation in typical usage', () => {
101+
const output = dedent`
102+
type Query {
103+
me: User
104+
}
105+
`;
106+
expect(output).to.equal(['type Query {', ' me: User', '}'].join('\n'));
107+
});
108+
109+
it('supports expression interpolation', () => {
110+
const name = 'John';
111+
const surname = 'Doe';
112+
const output = dedent`
113+
{
114+
"me": {
115+
"name": "${name}",
116+
"surname": "${surname}"
117+
}
118+
}
119+
`;
120+
expect(output).to.equal(
121+
[
122+
'{',
123+
' "me": {',
124+
' "name": "John",',
125+
' "surname": "Doe"',
126+
' }',
127+
'}',
128+
].join('\n'),
129+
);
130+
});
131+
});

src/__testUtils__/dedent.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
export function dedentString(string: string): string {
2+
const trimmedStr = string
3+
.replace(/^\n*/m, '') // remove leading newline
4+
.replace(/[ \t\n]*$/, ''); // remove trailing spaces and tabs
5+
6+
// fixes indentation by removing leading spaces and tabs from each line
7+
let indent = '';
8+
for (const char of trimmedStr) {
9+
if (char !== ' ' && char !== '\t') {
10+
break;
11+
}
12+
indent += char;
13+
}
14+
15+
return trimmedStr.replace(RegExp('^' + indent, 'mg'), ''); // remove indent
16+
}
17+
18+
/**
19+
* An ES6 string tag that fixes indentation and also trims string.
20+
*
21+
* Example usage:
22+
* const str = dedent`
23+
* {
24+
* test
25+
* }
26+
* `;
27+
* str === "{\n test\n}";
28+
*/
29+
export function dedent(
30+
strings: $ReadOnlyArray<string>,
31+
...values: $ReadOnlyArray<string>
32+
): string {
33+
let str = strings[0];
34+
35+
for (let i = 1; i < strings.length; ++i) {
36+
str += values[i - 1] + strings[i]; // interpolation
37+
}
38+
return dedentString(str);
39+
}

0 commit comments

Comments
 (0)