Skip to content

Commit 069189f

Browse files
committed
Test cover preprocessing
1 parent d78c296 commit 069189f

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

test/fixtures/style-valid.html

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<button>+1</button>
2+
3+
<style type="text/scss">
4+
button {
5+
width: $size;
6+
height: $size;
7+
}
8+
</style>

test/loader.spec.js

+73
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,79 @@ describe('loader', () => {
261261
)
262262
);
263263
});
264+
265+
describe('preprocess', () => {
266+
it('should preprocess successfully', (done) => {
267+
function callback(err, code, map) {
268+
expect(err).not.to.exist;
269+
expect(code).to.exist;
270+
expect(code).to.contain('button{width:50px;height:50px}');
271+
expect(map).to.exist;
272+
}
273+
274+
function cb() {
275+
try {
276+
callback(...[].slice.call(arguments));
277+
} catch (err) {
278+
expect(callbackSpy).to.have.been.called;
279+
return done(err);
280+
}
281+
expect(callbackSpy).to.have.been.called;
282+
done();
283+
}
284+
285+
const fileContents = readFileSync('test/fixtures/style-valid.html',
286+
'utf-8');
287+
const cacheableSpy = spy(() => {
288+
});
289+
const callbackSpy = spy(cb);
290+
const options = {
291+
style: ({ content }) => {
292+
return {
293+
code: content.replace(/\$size/gi, '50px'),
294+
};
295+
},
296+
};
297+
298+
loader.call(
299+
{
300+
cacheable: cacheableSpy,
301+
async: () => callbackSpy,
302+
resourcePath: 'test/fixtures/style-valid.html',
303+
options,
304+
},
305+
fileContents,
306+
null
307+
);
308+
309+
expect(cacheableSpy).to.have.been.called;
310+
});
311+
312+
it('should not preprocess successfully', () => {
313+
const fileContents = readFileSync('test/fixtures/style-valid.html', 'utf-8');
314+
const cacheableSpy = spy(() => {
315+
});
316+
const options = {
317+
style: () => {
318+
throw new Error('Error while preprocessing');
319+
},
320+
};
321+
322+
loader.call(
323+
{
324+
cacheable: cacheableSpy,
325+
async: () => (err) => {
326+
expect(err).to.exist;
327+
},
328+
resourcePath: 'test/fixtures/style-valid.html',
329+
options,
330+
},
331+
fileContents,
332+
null
333+
);
334+
335+
});
336+
});
264337
});
265338
});
266339

0 commit comments

Comments
 (0)