Repository navigation
Expand file tree
/
Copy pathreplace-imports.test.js
More file actions
259 lines (230 loc) · 7.13 KB
/
Copy pathreplace-imports.test.js
File metadata and controls
259 lines (230 loc) · 7.13 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
const path = require('path');
const cloneDeep = require('lodash/cloneDeep');
const toVfile = require('to-vfile');
const selectAll = require('unist-util-select').selectAll;
const originalImportsExtraAST = require('../__fixtures__/ast-imports-extra.json');
const originalImportsTwoAST = require('../__fixtures__/ast-imports-two.json');
const originalImportsAST = require('../__fixtures__/ast-imports.json');
const originalMarkerAST = require('../__fixtures__/ast-marker-imports.json');
const originalSimpleAST = require('../__fixtures__/ast-simple.json');
const addImports = require('./replace-imports');
describe('replace-imports', () => {
let importsAST;
let importsTwoAST;
let importsExtraAST;
let simpleAST;
let markerAST;
let correctFile;
let incorrectFile;
beforeEach(() => {
importsAST = cloneDeep(originalImportsAST);
importsTwoAST = cloneDeep(originalImportsTwoAST);
importsExtraAST = cloneDeep(originalImportsExtraAST);
simpleAST = cloneDeep(originalSimpleAST);
markerAST = cloneDeep(originalMarkerAST);
correctFile = toVfile(
path.resolve(__dirname, '../__fixtures__/with-imports.md')
);
incorrectFile = toVfile(
path.resolve(__dirname, '../__fixtures__/incorrect-path/with-imports.md')
);
});
it('should return a function', () => {
expect.assertions(1);
const plugin = addImports();
expect(typeof plugin).toEqual('function');
});
it('should fail when the imported file is null', done => {
const plugin = addImports();
const next = err => {
if (err) {
done();
} else {
done('An error should have been thrown by addImports');
}
};
plugin(importsAST, null, next);
});
it('should proceed when the imported file exists', done => {
const plugin = addImports();
plugin(importsAST, correctFile, done);
});
it('should fail when the imported file cannot be found', done => {
expect.assertions(1);
console.error = jest.fn();
const plugin = addImports();
// we have to rely on the next callback, because that is how you get error
// messages out of transformers
const next = err => {
if (err) {
expect(console.error).toHaveBeenCalledTimes(2);
done();
} else {
done('An error should have been thrown by addImports');
}
};
plugin(importsAST, incorrectFile, next);
});
it('should modify the tree when there are imports', done => {
expect.assertions(1);
const plugin = addImports();
const next = err => {
if (err) {
done(err);
} else {
expect(importsAST).not.toEqual(originalImportsAST);
done();
}
};
plugin(importsAST, correctFile, next);
});
it('should NOT modify the tree when there are NO imports', done => {
expect.assertions(1);
const plugin = addImports();
const next = err => {
if (err) {
done(err);
} else {
expect(simpleAST).toEqual(originalSimpleAST);
done();
}
};
plugin(simpleAST, correctFile, next);
});
it('should remove all import statements', done => {
expect.assertions(2);
const selector = 'leafDirective[name=import]';
const plugin = addImports();
const importNodes = selectAll(selector, importsAST);
expect(importNodes.length).toBe(1);
const next = err => {
if (err) {
done(err);
} else {
const importNodes = selectAll(selector, importsAST);
expect(importNodes.length).toBe(0);
done();
}
};
plugin(importsAST, correctFile, next);
});
it('should not remove an ::import without the required attributes', done => {
expect.assertions(2);
const selector = 'leafDirective[name=import]';
const plugin = addImports();
const importNodes = selectAll(selector, importsExtraAST);
expect(importNodes.length).toBe(3);
const next = err => {
if (err) {
done(err);
} else {
const importNodes = selectAll(selector, importsExtraAST);
expect(importNodes.length).toBe(1);
done();
}
};
plugin(importsExtraAST, correctFile, next);
});
it('should remove all matching ::use statements', done => {
expect.assertions(2);
const selector = 'leafDirective[name=use]';
const plugin = addImports();
const components = selectAll(selector, importsAST);
// one matching component and two other jsx nodes
expect(components.length).toBe(1);
const next = err => {
if (err) {
done(err);
} else {
const components = selectAll(selector, importsAST);
expect(components.length).toBe(0);
done();
}
};
plugin(importsAST, correctFile, next);
});
it('should replace the ::use statement with the imported content', done => {
// checks the contents of script.md are there after the import step
expect.assertions(2);
const plugin = addImports();
const next = err => {
if (err) {
done(err);
} else {
const jsNodes = selectAll('code[lang=js]', importsAST);
expect(jsNodes.length).toBe(4);
const codeValues = jsNodes.map(({ value }) => value);
expect(codeValues).toEqual(
expect.arrayContaining([
`for (let index = 0; index < array.length; index++) {
const element = array[index];
// imported from script.md
}`
])
);
done();
}
};
plugin(importsAST, correctFile, next);
});
it('should handle multiple import statements', done => {
// checks the contents of script.md are there after the import step
expect.assertions(4);
const plugin = addImports();
const next = err => {
if (err) {
done(err);
} else {
const jsNodes = selectAll('code[lang=js]', importsTwoAST);
expect(jsNodes.length).toBe(4);
const codeValues = jsNodes.map(({ value }) => value);
expect(codeValues).toEqual(
expect.arrayContaining([
`for (let index = 0; index < array.length; index++) {
const element = array[index];
// imported from script.md
}`
])
);
const cssNodes = selectAll('code[lang=css]', importsTwoAST);
expect(cssNodes.length).toBe(2);
const cssValues = cssNodes.map(({ value }) => value);
expect(cssValues).toEqual(
expect.arrayContaining([
`div {
background: red
}`
])
);
done();
}
};
plugin(importsTwoAST, correctFile, next);
});
it('should reject imported files with editable region markers', done => {
expect.assertions(1);
console.error = jest.fn();
const plugin = addImports();
const next = err => {
if (err) {
expect(console.error).toHaveBeenCalledTimes(2);
done();
} else {
done('An error should have been thrown by addImports');
}
};
plugin(markerAST, correctFile, next);
});
it('should have an output to match the snapshot', done => {
const plugin = addImports();
const next = err => {
if (err) {
done(err);
} else {
expect(importsAST).toMatchSnapshot();
done();
}
};
plugin(importsAST, correctFile, next);
});
});