|
| 1 | +/*eslint-disable no-console */ |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +var expect = require('chai').expect; |
| 5 | +var BaseHrefWebpackPlugin = require('../../addon/ng2/utilities/base-href-webpack-plugin').BaseHrefWebpackPlugin; |
| 6 | + |
| 7 | +function mockCompiler(indexHtml, callback) { |
| 8 | + return { |
| 9 | + plugin: function (event, compilerCallback) { |
| 10 | + var compilation = { |
| 11 | + plugin: function (hook, compilationCallback) { |
| 12 | + var htmlPluginData = { |
| 13 | + html: indexHtml |
| 14 | + }; |
| 15 | + compilationCallback(htmlPluginData, callback); |
| 16 | + } |
| 17 | + }; |
| 18 | + compilerCallback(compilation); |
| 19 | + } |
| 20 | + }; |
| 21 | +} |
| 22 | + |
| 23 | +describe('base href webpack plugin', function () { |
| 24 | + it('should do nothing when baseHref is null', function () { |
| 25 | + var plugin = new BaseHrefWebpackPlugin({ baseHref: null }); |
| 26 | + |
| 27 | + var compiler = mockCompiler('<body><head></head></body>', function (x, htmlPluginData) { |
| 28 | + expect(htmlPluginData.html).to.equal('<body><head></head></body>'); |
| 29 | + }); |
| 30 | + plugin.apply(compiler); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should insert base tag when not exist', function () { |
| 34 | + var plugin = new BaseHrefWebpackPlugin({ baseHref: '/' }); |
| 35 | + |
| 36 | + var compiler = mockCompiler('<body><head></head></body>', function (x, htmlPluginData) { |
| 37 | + expect(htmlPluginData.html).to.equal('<body><head><base href="/"></head></body>'); |
| 38 | + }); |
| 39 | + plugin.apply(compiler); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should replace href attribute when base tag already exists', function () { |
| 43 | + var plugin = new BaseHrefWebpackPlugin({ baseHref: '/myUrl/' }); |
| 44 | + |
| 45 | + var compiler = mockCompiler('<body><head><base href="/" target="_blank"></head></body>', function (x, htmlPluginData) { |
| 46 | + expect(htmlPluginData.html).to.equal('<body><head><base href="/myUrl/" target="_blank"></head></body>'); |
| 47 | + }); |
| 48 | + plugin.apply(compiler); |
| 49 | + }); |
| 50 | +}); |
0 commit comments