From 9a1df0277e3f8a41926b42d3f99bae0c4b086c15 Mon Sep 17 00:00:00 2001 From: Tim Sebastian Date: Sun, 19 Feb 2017 13:44:42 +1100 Subject: [PATCH] more tests for external modules --- test/ExternalModule.test.js | 85 +++++++++++++++++++++++++++++++++++-- 1 file changed, 82 insertions(+), 3 deletions(-) diff --git a/test/ExternalModule.test.js b/test/ExternalModule.test.js index d8972b251..312ea676c 100644 --- a/test/ExternalModule.test.js +++ b/test/ExternalModule.test.js @@ -1,10 +1,8 @@ -/* globals describe, it, beforeEach, afterEach */ +/* globals describe, it, beforeEach */ "use strict"; require("should"); const sinon = require("sinon"); const ExternalModule = require("../lib/ExternalModule"); -const path = require("path"); -const SourceMapSource = require("webpack-sources").SourceMapSource; const OriginalSource = require("webpack-sources").OriginalSource; const RawSource = require("webpack-sources").RawSource; @@ -232,4 +230,85 @@ module.exports = some/request;`; }); }); }); + + describe("#getSourceString", function() { + let globalExternalStub; + let globalCommonJsStub; + let globalAmdOrUmdStub; + let defaultExternalStub; + beforeEach(function() { + globalExternalStub = externalModule.getSourceForGlobalVariableExternal = sinon.stub(); + globalCommonJsStub = externalModule.getSourceForCommonJsExternal = sinon.stub(); + globalAmdOrUmdStub = externalModule.getSourceForAmdOrUmdExternal = sinon.stub(); + defaultExternalStub = externalModule.getSourceForDefaultCase = sinon.stub(); + }); + describe("with type being 'this', 'window' or 'global'", function() { + it("deletgates to #getSourceForGlobalVariableExternal", function() { + ["this", "window", "global"].forEach((type, i) => { + // set up + externalModule.type = type; + + // invoke + externalModule.getSourceString(); + + // check + globalExternalStub.callCount.should.eql(i + 1); + globalCommonJsStub.callCount.should.eql(0); + globalAmdOrUmdStub.callCount.should.eql(0); + defaultExternalStub.callCount.should.eql(0); + }); + }); + }); + describe("with type being 'commonjs' or 'commonjs2'", function() { + it("deletgates to #getSourceForCommonJsExternal", function() { + ["commonjs", "commonjs2"].forEach((type, i) => { + // set up + externalModule.type = type; + + // invoke + externalModule.getSourceString(); + + // check + globalExternalStub.callCount.should.eql(0); + globalCommonJsStub.callCount.should.eql(i + 1); + globalAmdOrUmdStub.callCount.should.eql(0); + defaultExternalStub.callCount.should.eql(0); + }); + }); + }); + describe("with type being 'amd', 'umd' or 'umd2'", function() { + it("deletgates to #getSourceForAmdOrUmdExternal", function() { + ["amd", "umd", "umd2"].forEach((type, i) => { + // set up + externalModule.type = type; + + // invoke + externalModule.getSourceString(); + + // check + globalExternalStub.callCount.should.eql(0); + globalCommonJsStub.callCount.should.eql(0); + globalAmdOrUmdStub.callCount.should.eql(i + 1); + defaultExternalStub.callCount.should.eql(0); + }); + }); + }); + describe("with type being non of the above", function() { + it("deletgates to #getSourceForGlobalVariableExternal", function() { + ["foo", "bar", undefined].forEach((type, i) => { + // set up + externalModule.type = type; + + // invoke + externalModule.getSourceString(); + + // check + globalExternalStub.callCount.should.eql(0); + globalCommonJsStub.callCount.should.eql(0); + globalAmdOrUmdStub.callCount.should.eql(0); + defaultExternalStub.callCount.should.eql(i + 1); + }); + }); + }); + }); });