Skip to content
This repository was archived by the owner on Jan 21, 2021. It is now read-only.

Commit 4ee1ec3

Browse files
committed
Improving test coverage.
1 parent 7fa0dfb commit 4ee1ec3

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

test/spec.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,4 +488,86 @@ module.exports = ({descriptionPrefix, webpack, HtmlWebpackPlugin}) => {
488488
compiler.outputFileSystem = new MemoryFileSystem();
489489
});
490490
});
491+
492+
describe(`${descriptionPrefix} When excludeHtmlNames is used,`, function() {
493+
it(`should not modify the HTML of an asset that's listed`, function(done) {
494+
const compiler = webpack({
495+
entry: {
496+
js: path.join(__dirname, 'fixtures', 'file.js')
497+
},
498+
output: {
499+
path: OUTPUT_DIR,
500+
filename: 'bundle.js',
501+
chunkFilename: '[name].[chunkhash].js',
502+
publicPath: '/',
503+
},
504+
plugins: [
505+
new HtmlWebpackPlugin({
506+
filename: 'ignored.html',
507+
}),
508+
new PreloadPlugin({
509+
excludeHtmlNames: ['ignored.html'],
510+
})
511+
]
512+
}, function(err, result) {
513+
expect(err).toBeFalsy(err);
514+
expect(result.compilation.errors.length).toBe(0,
515+
result.compilation.errors.join('\n=========\n'));
516+
517+
const html = result.compilation.assets['ignored.html'].source();
518+
const dom = new JSDOM(html);
519+
520+
const links = dom.window.document.head.querySelectorAll('link');
521+
expect(links.length).toBe(0);
522+
523+
done();
524+
});
525+
compiler.outputFileSystem = new MemoryFileSystem();
526+
});
527+
528+
it(`should not modify the HTML of an asset that's listed, but modify the HTML of the asset that isn't listed`, function(done) {
529+
const compiler = webpack({
530+
entry: {
531+
js: path.join(__dirname, 'fixtures', 'file.js')
532+
},
533+
output: {
534+
path: OUTPUT_DIR,
535+
filename: 'bundle.js',
536+
chunkFilename: '[name].[chunkhash].js',
537+
publicPath: '/',
538+
},
539+
plugins: [
540+
new HtmlWebpackPlugin({
541+
filename: 'ignored.html',
542+
}),
543+
new HtmlWebpackPlugin(),
544+
new PreloadPlugin({
545+
excludeHtmlNames: ['ignored.html'],
546+
})
547+
]
548+
}, function(err, result) {
549+
expect(err).toBeFalsy(err);
550+
expect(result.compilation.errors.length).toBe(0,
551+
result.compilation.errors.join('\n=========\n'));
552+
553+
const ignoredHtml = result.compilation.assets['ignored.html'].source();
554+
const ignoredDom = new JSDOM(ignoredHtml);
555+
556+
const ignoredLinks = ignoredDom.window.document.head.querySelectorAll('link');
557+
expect(ignoredLinks.length).toBe(0);
558+
559+
const html = result.compilation.assets['index.html'].source();
560+
const dom = new JSDOM(html);
561+
562+
const links = dom.window.document.head.querySelectorAll('link');
563+
expect(links.length).toBe(1);
564+
expect(links[0].getAttribute('rel')).toBe('preload');
565+
expect(links[0].getAttribute('as')).toBe('script');
566+
expect(links[0].getAttribute('href')).toMatch(new RegExp('^/home\\.'));
567+
568+
done();
569+
});
570+
compiler.outputFileSystem = new MemoryFileSystem();
571+
});
572+
});
491573
};

0 commit comments

Comments
 (0)