From 663686884bf31ef791e0ec30a01f8ea7c8db5b1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20Str=C3=B6mkvist?= Date: Fri, 17 Mar 2017 20:26:49 +0100 Subject: [PATCH] feat: add insertInto option (#135) --- README.md | 5 ++++- addStyles.js | 30 ++++++++++++++++++++++-------- test/basicTest.js | 15 ++++++++++++++- test/utils.js | 7 ++++--- 4 files changed, 44 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index f557ffce..5d6a2f11 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,10 @@ Note: Behavior is undefined when `unuse`/`unref` is called more often than `use` #### `insertAt` -By default, the style-loader appends ``, existingStyle = "", + checkValue = '
check
', rootDir = path.resolve(__dirname + "/../") + "/", jsdomHtml = [ "", @@ -21,6 +22,9 @@ describe("basic tests", function() { existingStyle, "", "", + "
", + checkValue, + "
", "", "" ].join("\n"); @@ -83,6 +87,15 @@ describe("basic tests", function() { runCompilerTest(expected, done); }); // it insert at top + it("insert into", function(done) { + let selector = "div.target"; + styleLoaderOptions.insertInto = selector; + + let expected = [checkValue, requiredStyle].join("\n"); + + runCompilerTest(expected, done, undefined, selector); + }); // it insert into + it("singleton", function(done) { // Setup styleLoaderOptions.singleton = true; @@ -218,4 +231,4 @@ describe("basic tests", function() { runCompilerTest(expected, done, function() { return this.css.locals.className; }); }); // it local scope -}); // describe \ No newline at end of file +}); // describe diff --git a/test/utils.js b/test/utils.js index 9bab4b61..e7bd05ee 100644 --- a/test/utils.js +++ b/test/utils.js @@ -57,7 +57,8 @@ module.exports = { * @param {function} done - Async callback from Mocha. * @param {function} actual - Executed in the context of jsdom window, should return a string to compare to. */ - runCompilerTest: function(expected, done, actual) { + runCompilerTest: function(expected, done, actual, selector) { + selector = selector || "head" compiler.run(function(err, stats) { if (stats.compilation.errors.length) { throw new Error(stats.compilation.errors); @@ -73,7 +74,7 @@ module.exports = { if (typeof actual === 'function') { assert.equal(actual.apply(window), expected); } else { - assert.equal(window.document.head.innerHTML.trim(), expected); + assert.equal(window.document.querySelector(selector).innerHTML.trim(), expected); } // free memory associated with the window window.close(); @@ -83,4 +84,4 @@ module.exports = { }); }); } -}; \ No newline at end of file +};