Skip to content

Commit a194a89

Browse files
committed
Merge pull request #1 from css-modules/simpler-tests
simpler tests
2 parents 7abae7b + 33b0073 commit a194a89

File tree

15 files changed

+136
-91
lines changed

15 files changed

+136
-91
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
node_modules
22
coverage
33
lib
4-
test

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
"lint": "eslint src",
88
"build": "babel --out-dir lib src",
99
"watch": "chokidar src -c 'npm run build'",
10-
"build-test": "babel --out-dir test test-src",
11-
"pretest": "npm run lint && npm run build && npm run build-test",
10+
"pretest": "npm run lint && npm run build",
1211
"test": "mocha",
13-
"autotest": "chokidar src test-src -c 'npm test'",
14-
"precover": "npm run lint && npm run build && npm run build-test",
12+
"autotest": "chokidar src test -c 'npm test'",
13+
"precover": "npm run lint && npm run build",
1514
"cover": "istanbul cover node_modules/mocha/bin/_mocha",
1615
"travis": "npm run cover -- --report lcovonly",
1716
"prepublish": "npm run build"

test-src/basic.js

Lines changed: 0 additions & 86 deletions
This file was deleted.

test/basic.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"use strict";
2+
3+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
4+
5+
var _assert = require("assert");
6+
7+
var _assert2 = _interopRequireDefault(_assert);
8+
9+
var _postcss = require("postcss");
10+
11+
var _postcss2 = _interopRequireDefault(_postcss);
12+
13+
var _ = require("../");
14+
15+
var _2 = _interopRequireDefault(_);
16+
17+
var pipeline = (0, _postcss2["default"])([_2["default"]]),
18+
check = function check(desc, input, expected, randomStrs) {
19+
it(desc, function () {
20+
_2["default"].getRandomStr = randomStrs ? function () {
21+
return randomStrs.shift();
22+
} : _2["default"].defaultRandomStr;
23+
_assert2["default"].equal(pipeline.process(input).css, expected);
24+
});
25+
};
26+
27+
describe("processor", function () {
28+
check("it should extract an import within a :local", ":local(.exportName) { extends: importName from \"path/library.css\"; other: rule; }", "\n:import(\"path/library.css\") {\n importName: __tmp_importName_rand0ml0l0l; }\n:local(.exportName) { extends: __tmp_importName_rand0ml0l0l; other: rule; }", ["rand0ml0l0l"]);
29+
30+
check("it should not care if single-quotes are used", ":local(.exportName) { extends: importName from 'path/library.css'; other: rule; }", "\n:import(\"path/library.css\") {\n importName: __tmp_importName_rand0ml0l0l; }\n:local(.exportName) { extends: __tmp_importName_rand0ml0l0l; other: rule; }", ["rand0ml0l0l"]);
31+
32+
check("should import multiple classes on a single line", ":local(.exportName) { extends: importName secondImport from 'path/library.css'; other: rule; }", "\n:import(\"path/library.css\") {\n importName: __tmp_importName_rand0ml0l0l;\n secondImport: __tmp_secondImport_rand0ml1l1l; }\n:local(.exportName) { extends: __tmp_importName_rand0ml0l0l __tmp_secondImport_rand0ml1l1l; other: rule; }", ["rand0ml0l0l", "rand0ml1l1l"]);
33+
34+
check("should consolidate imports by file for multiple files and multiple classes",
35+
36+
/* INPUT */
37+
"\n:local(.exportName) {\n extends: importName secondImport from 'path/library.css';\n other: rule;\n}\n:local(.otherExport) {\n extends: thirdImport from 'path/library.css';\n extends: otherLibImport from 'path/other-lib.css';\n}",
38+
39+
/* OUTPUT */
40+
"\n:import(\"path/other-lib.css\") {\n otherLibImport: __tmp_otherLibImport_rand0ml3l3l3;\n}\n:import(\"path/library.css\") {\n importName: __tmp_importName_rand0ml0l0l;\n secondImport: __tmp_secondImport_rand0ml1l1l;\n thirdImport: __tmp_thirdImport_rand0ml2l2l;\n}\n:local(.exportName) {\n extends: __tmp_importName_rand0ml0l0l __tmp_secondImport_rand0ml1l1l;\n other: rule;\n}\n:local(.otherExport) {\n extends: __tmp_thirdImport_rand0ml2l2l;\n extends: __tmp_otherLibImport_rand0ml3l3l3;\n}",
41+
/* RANDOMS */
42+
["rand0ml0l0l", "rand0ml1l1l", "rand0ml2l2l", "rand0ml3l3l3"]);
43+
44+
check("should ignore imports not inside our rule whitelist", ":local(.exportName) { imports: importName from \"path/library.css\"; something-else: otherLibImport from \"path/other-lib.css\"; }", ":local(.exportName) { imports: importName from \"path/library.css\"; something-else: otherLibImport from \"path/other-lib.css\"; }");
45+
});

test/test-cases.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"use strict";
2+
3+
/*globals describe it */
4+
5+
var assert = require("assert");
6+
var fs = require("fs");
7+
var path = require("path");
8+
var postcss = require("postcss");
9+
var processor = require("../");
10+
11+
var pipeline = postcss([processor]);
12+
13+
function normalize(str) {
14+
return str.replace(/\r\n?/g, "\n");
15+
}
16+
17+
describe("test-cases", function() {
18+
var testDir = path.join(__dirname, "test-cases");
19+
fs.readdirSync(testDir).forEach(function(testCase) {
20+
if(fs.existsSync(path.join(testDir, testCase, "source.css"))) {
21+
it("should " + testCase.replace(/-/g, " "), function() {
22+
var i = 0;
23+
processor.getRandomStr = function() { return "rand" + (i++); };
24+
var input = normalize(fs.readFileSync(path.join(testDir, testCase, "source.css"), "utf-8"));
25+
var expected = normalize(fs.readFileSync(path.join(testDir, testCase, "expected.css"), "utf-8"));
26+
assert.equal(pipeline.process(input).css, expected);
27+
});
28+
}
29+
});
30+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
:import("path/other-lib.css") {
3+
otherLibImport: __tmp_otherLibImport_rand3;
4+
}
5+
:import("path/library.css") {
6+
importName: __tmp_importName_rand0;
7+
secondImport: __tmp_secondImport_rand1;
8+
thirdImport: __tmp_thirdImport_rand2;
9+
}
10+
:local(.exportName) {
11+
extends: __tmp_importName_rand0 __tmp_secondImport_rand1;
12+
other: rule;
13+
}
14+
:local(.otherExport) {
15+
extends: __tmp_thirdImport_rand2;
16+
extends: __tmp_otherLibImport_rand3;
17+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
:local(.exportName) {
3+
extends: importName secondImport from 'path/library.css';
4+
other: rule;
5+
}
6+
:local(.otherExport) {
7+
extends: thirdImport from 'path/library.css';
8+
extends: otherLibImport from 'path/other-lib.css';
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
:import("path/library.css") {
3+
importName: __tmp_importName_rand0;
4+
secondImport: __tmp_secondImport_rand1; }
5+
:local(.exportName) { extends: __tmp_importName_rand0 __tmp_secondImport_rand1; other: rule; }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:local(.exportName) { extends: importName secondImport from 'path/library.css'; other: rule; }

test/test-cases/import-only-whitelist/expected.css

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)