Skip to content

Commit

Permalink
test: Local Scope and Local Scope + Useable (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekulabuhov authored and joshwiens committed Mar 15, 2017
1 parent 19959ee commit feab724
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"author": "Tobias Koppers @sokra",
"description": "style loader module for webpack",
"devDependencies": {
"css-loader": "~0.8.0",
"css-loader": "^0.27.3",
"file-loader": "^0.10.1",
"jsdom": "^9.11.0",
"memory-fs": "^0.4.1",
Expand Down
54 changes: 52 additions & 2 deletions test/basicTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe("basic tests", function() {

var requiredCss = ".required { color: blue }",
requiredCssTwo = ".requiredTwo { color: cyan }",
localScopedCss = ":local(.className) { background: red; }",
requiredStyle = `<style type="text/css">${requiredCss}</style>`,
existingStyle = "<style>.existing { color: yellow }</style>",
rootDir = path.resolve(__dirname + "/../") + "/",
Expand Down Expand Up @@ -65,6 +66,7 @@ describe("basic tests", function() {
fs.writeFileSync(rootDir + "main.js", "var css = require('./style.css');");
fs.writeFileSync(rootDir + "style.css", requiredCss);
fs.writeFileSync(rootDir + "styleTwo.css", requiredCssTwo);
fs.writeFileSync(rootDir + "localScoped.css", localScopedCss);
}); // before each

it("insert at bottom", function(done) {
Expand Down Expand Up @@ -143,8 +145,7 @@ describe("basic tests", function() {
it("useable", function(done) {
cssRule.use = [
{
loader: "style-loader/useable",
options: {}
loader: "style-loader/useable"
},
"css-loader"
];
Expand All @@ -168,4 +169,53 @@ describe("basic tests", function() {

runCompilerTest(expected, done);
}); // it useable

it("local scope", function(done) {
cssRule.use = [
{
loader: "style-loader"
},
{
loader: "css-loader",
options: {
localIdentName: '[name].[local]_[hash:base64:7]'
}
}
];

fs.writeFileSync(
rootDir + "main.js",
[
"css = require('./localScoped.css');"
].join("\n")
);

let expected = 'localScoped-className_3dIU6Uf';
runCompilerTest(expected, done, function() { return this.css.className; });
}); // it local scope

it("local scope, useable", function(done) {
cssRule.use = [
{
loader: "style-loader/useable"
},
{
loader: "css-loader",
options: {
localIdentName: '[name].[local]_[hash:base64:7]'
}
}
];

fs.writeFileSync(
rootDir + "main.js",
[
"css = require('./localScoped.css');"
].join("\n")
);

let expected = 'localScoped-className_3dIU6Uf';
runCompilerTest(expected, done, function() { return this.css.locals.className; });
}); // it local scope

}); // describe
15 changes: 13 additions & 2 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ module.exports = {

return fs;
},
runCompilerTest: function(expected, done) {

/*
* @param {string} expected - Expected value.
* @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) {
compiler.run(function(err, stats) {
if (stats.compilation.errors.length) {
throw new Error(stats.compilation.errors);
Expand All @@ -62,8 +68,13 @@ module.exports = {
jsdom.env({
html: jsdomHtml,
src: [bundleJs],
virtualConsole: jsdom.createVirtualConsole().sendTo(console),
done: function(err, window) {
assert.equal(window.document.head.innerHTML.trim(), expected);
if (typeof actual === 'function') {
assert.equal(actual.apply(window), expected);
} else {
assert.equal(window.document.head.innerHTML.trim(), expected);
}
// free memory associated with the window
window.close();

Expand Down

0 comments on commit feab724

Please sign in to comment.