/
dedent-test.js
120 lines (106 loc) · 2.46 KB
/
dedent-test.js
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
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { dedent } from '../dedent';
describe('dedent', () => {
it('removes indentation in typical usage', () => {
const output = dedent`
type Query {
me: User
}
type User {
id: ID
name: String
}
`;
expect(output).to.equal(
[
'type Query {',
' me: User',
'}',
'',
'type User {',
' id: ID',
' name: String',
'}',
].join('\n'),
);
});
it('removes only the first level of indentation', () => {
const output = dedent`
first
second
third
fourth
`;
expect(output).to.equal(
['first', ' second', ' third', ' fourth'].join('\n'),
);
});
it('does not escape special characters', () => {
const output = dedent`
type Root {
field(arg: String = "wi\th de\fault"): String
}
`;
expect(output).to.equal(
[
'type Root {',
' field(arg: String = "wi\th de\fault"): String',
'}',
].join('\n'),
);
});
it('also removes indentation using tabs', () => {
const output = dedent`
\t\t type Query {
\t\t me: User
\t\t }
`;
expect(output).to.equal(['type Query {', ' me: User', '}'].join('\n'));
});
it('removes leading and trailing newlines', () => {
const output = dedent`
type Query {
me: User
}
`;
expect(output).to.equal(['type Query {', ' me: User', '}'].join('\n'));
});
it('removes all trailing spaces and tabs', () => {
const output = dedent`
type Query {
me: User
}
\t\t \t `;
expect(output).to.equal(['type Query {', ' me: User', '}'].join('\n'));
});
it('works on text without leading newline', () => {
const output = dedent` type Query {
me: User
}
`;
expect(output).to.equal(['type Query {', ' me: User', '}'].join('\n'));
});
it('supports expression interpolation', () => {
const name = 'John';
const surname = 'Doe';
const output = dedent`
{
"me": {
"name": "${name}",
"surname": "${surname}"
}
}
`;
expect(output).to.equal(
[
'{',
' "me": {',
' "name": "John",',
' "surname": "Doe"',
' }',
'}',
].join('\n'),
);
});
});