|
| 1 | +import {oneLineTrim} from 'common-tags'; |
| 2 | +import {BaseHrefWebpackPlugin} from './base-href-webpack-plugin'; |
| 3 | + |
| 4 | + |
| 5 | +function mockCompiler(indexHtml, callback) { |
| 6 | + return { |
| 7 | + plugin: function (event, compilerCallback) { |
| 8 | + const compilation = { |
| 9 | + plugin: function (hook, compilationCallback) { |
| 10 | + const htmlPluginData = { |
| 11 | + html: indexHtml |
| 12 | + }; |
| 13 | + compilationCallback(htmlPluginData, callback); |
| 14 | + } |
| 15 | + }; |
| 16 | + compilerCallback(compilation); |
| 17 | + } |
| 18 | + }; |
| 19 | +} |
| 20 | + |
| 21 | +describe('base href webpack plugin', () => { |
| 22 | + const html = oneLineTrim` |
| 23 | + <html> |
| 24 | + <head></head> |
| 25 | + <body></body> |
| 26 | + </html> |
| 27 | + `; |
| 28 | + |
| 29 | + it('should do nothing when baseHref is null', () => { |
| 30 | + const plugin = new BaseHrefWebpackPlugin({ baseHref: null }); |
| 31 | + |
| 32 | + const compiler = mockCompiler(html, (x, htmlPluginData) => { |
| 33 | + expect(htmlPluginData.html).toEqual('<body><head></head></body>'); |
| 34 | + }); |
| 35 | + plugin.apply(compiler); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should insert base tag when not exist', function () { |
| 39 | + const plugin = new BaseHrefWebpackPlugin({ baseHref: '/' }); |
| 40 | + const compiler = mockCompiler(html, (x, htmlPluginData) => { |
| 41 | + expect(htmlPluginData.html).toEqual(oneLineTrim` |
| 42 | + <html> |
| 43 | + <head><base href="/"></head> |
| 44 | + <body></body> |
| 45 | + </html> |
| 46 | + `); |
| 47 | + }); |
| 48 | + |
| 49 | + plugin.apply(compiler); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should replace href attribute when base tag already exists', function () { |
| 53 | + const plugin = new BaseHrefWebpackPlugin({ baseHref: '/myUrl/' }); |
| 54 | + |
| 55 | + const compiler = mockCompiler(oneLineTrim` |
| 56 | + <head><base href="/" target="_blank"></head> |
| 57 | + <body></body> |
| 58 | + `, (x, htmlPluginData) => { |
| 59 | + expect(htmlPluginData.html).toEqual(oneLineTrim` |
| 60 | + <head><base href="/myUrl/" target="_blank"></head> |
| 61 | + <body></body> |
| 62 | + `); |
| 63 | + }); |
| 64 | + plugin.apply(compiler); |
| 65 | + }); |
| 66 | +}); |
0 commit comments