|
| 1 | +/* global describe, beforeEach, it */ |
| 2 | +"use strict"; |
| 3 | + |
| 4 | +const should = require("should"); |
| 5 | +const TemplatePluginEnvironment = require("./helpers/TemplatePluginEnvironment"); |
| 6 | +const ConcatSource = require("webpack-sources").ConcatSource; |
| 7 | +const UmdMainTemplatePlugin = require("../lib/UmdMainTemplatePlugin"); |
| 8 | + |
| 9 | +describe("UmdMainTemplatePlugin", () => { |
| 10 | + |
| 11 | + const setupBasicTemplatePlugin = name => { |
| 12 | + const plugin = new UmdMainTemplatePlugin({ |
| 13 | + amd: name |
| 14 | + }, { |
| 15 | + auxiliaryComment: {} |
| 16 | + }); |
| 17 | + const templatePlugin = new TemplatePluginEnvironment(); |
| 18 | + const environment = templatePlugin.getEnvironmentStub(); |
| 19 | + environment.mainTemplate.applyPluginsWaterfall = () => []; |
| 20 | + plugin.apply(environment); |
| 21 | + return templatePlugin; |
| 22 | + }; |
| 23 | + |
| 24 | + let templatePlugin; |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + templatePlugin = setupBasicTemplatePlugin("foo"); |
| 28 | + }); |
| 29 | + |
| 30 | + describe("when applied", () => { |
| 31 | + describe("event handlers", () => { |
| 32 | + let eventBindings; |
| 33 | + |
| 34 | + beforeEach(() => { |
| 35 | + eventBindings = templatePlugin.getEventBindings(); |
| 36 | + }); |
| 37 | + |
| 38 | + describe("handling render-with-entry", () => { |
| 39 | + let eventHandler; |
| 40 | + |
| 41 | + beforeEach(() => { |
| 42 | + eventHandler = eventBindings |
| 43 | + .filter(eventBinding => eventBinding.name === 'render-with-entry') |
| 44 | + .map(eventBinding => eventBinding.handler) |
| 45 | + .pop(); |
| 46 | + }); |
| 47 | + |
| 48 | + it("creates source that safely detects the global object", () => { |
| 49 | + const source = eventHandler("{ foo: true }", { |
| 50 | + getModules: () => [] |
| 51 | + }, "bar"); |
| 52 | + |
| 53 | + source.should.be.instanceof(ConcatSource); |
| 54 | + source.source().should.be.exactly(`(function webpackUniversalModuleDefinition(root, factory) { |
| 55 | + if(typeof exports === 'object' && typeof module === 'object') |
| 56 | + module.exports = factory(); |
| 57 | + else if(typeof define === 'function' && define.amd) |
| 58 | + define([], factory); |
| 59 | + else { |
| 60 | + var a = factory(); |
| 61 | + for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i]; |
| 62 | + } |
| 63 | +})(typeof self !== 'undefined' ? self : this, function() { |
| 64 | +return { foo: true }; |
| 65 | +})`); |
| 66 | + }); |
| 67 | + }); |
| 68 | + }); |
| 69 | + }); |
| 70 | +}); |
0 commit comments