forked from lahmatiy/postcss-csso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
155 lines (134 loc) · 4.79 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
var assert = require('assert');
var postcss = require('postcss');
var postcssCsso = require('./index.js');
// make postcss work on node.js 0.10
global.Promise = require('es6-promise-polyfill').Promise;
describe('can be used as a postcss plugin', function() {
var css = '.a { color: #ff0000; } @media all { .b { color: rgba(255, 0, 0, 1) } }';
var minified = '.a{color:red}@media all{.b{color:red}}';
it('via .use()', function() {
return postcss().use(postcssCsso()).process(css).then(function(result) {
assert.equal(result.css, minified);
});
});
it('via postcss([..]) w/o config', function() {
return postcss([postcssCsso]).process(css).then(function(result) {
assert.equal(result.css, minified);
});
});
it('via postcss([..]) w/ config', function() {
return postcss([postcssCsso({})]).process(css).then(function(result) {
assert.equal(result.css, minified);
});
});
});
describe('should accept options for compress', function() {
var css = '.a { color: #ff0000; } .b { color: rgba(255, 0, 0, 1) }';
var minified = '.a,.b{color:red}';
var minifiedNoRestructure = '.a{color:red}.b{color:red}';
it('restruture on', function() {
return postcss().use(postcssCsso({ restructure: true })).process(css).then(function(result) {
assert.equal(result.css, minified);
});
});
it('restruture off', function() {
return postcss().use(postcssCsso({ restructure: false })).process(css).then(function(result) {
assert.equal(result.css, minifiedNoRestructure);
});
});
});
describe('edge cases', function() {
it('should process empty', function() {
return postcss().use(postcssCsso).process('').then(function(result) {
assert.equal(result.css, '');
});
});
it('should process block with no selector', function() {
return postcss().use(postcssCsso).process('{foo:1}').then(function(result) {
assert.equal(result.css, '');
});
});
it('should process partial inited nodes', function() {
var emptyGenerator = postcss.plugin('emptyGenerator', function() {
return function(root) {
var atRule = postcss.atRule({
name: 'test'
});
var rule = postcss.rule();
rule.append(postcss.decl());
root.append(atRule);
root.append(rule);
};
});
return postcss([
emptyGenerator,
postcssCsso
]).process('').then(function(result) {
assert.equal(result.css, '@test;');
});
});
});
it('should keep the shape of original postcss nodes', function() {
var css = '.a { p: 1; } .b { p: 2; }';
var count = 0;
var marker = postcss.plugin('marker', function() {
return function(root) {
root.walkRules(function(node) {
node.marker = 1;
});
};
});
var checker = postcss.plugin('checker', function() {
return function(root) {
root.walkRules(function(node) {
count += node.marker;
});
};
});
return postcss([
marker,
postcssCsso,
checker
]).process(css).then(function() {
assert.equal(count, 2);
});
});
it('error handling', function() {
return postcss([
postcssCsso
]).process('.test { color: $ }').then(function() {
assert(false, 'shouldn\'t to be successful');
}, function(error) {
assert.equal(error.name, 'CssSyntaxError');
assert.equal(error.line, 1);
assert.equal(error.column, 16);
});
});
// currently works only is used as linked package
// TODO: find the way to use csso compress tests
try {
describe('should pass csso compress tests', function() {
function normalizeNewlines(str) {
return str.replace(/\r/g, '');
}
function createCompressTest(name, test) {
it(name, function() {
var plugin = test.options ? postcssCsso(test.options) : postcssCsso;
return postcss([plugin]).process(test.source).then(function(result) {
assert.equal(
normalizeNewlines(result.css),
normalizeNewlines(test.compressed)
);
});
});
}
var compressTests = require('./node_modules/csso/test/fixture/compress/');
for (var filename in compressTests) {
if (!/_[^\/]+\.css/.test(filename)) {
createCompressTest('node_modules/csso/' + filename, compressTests[filename]);
}
}
});
} catch (e) {
// should throw exception when no csso fixture found
}