-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
test.js
193 lines (177 loc) · 5.52 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
'use strict';
const fs = require('fs');
const path = require('path');
const tape = require('tape');
const postcss = require('postcss');
const plugin = require('../src/');
const name = require('../package.json').name;
const testCssFixtures = function (testMessage, tests) {
tape(testMessage, function (t) {
// Set amount of assertions by setting two assertions per sort order test
t.plan(tests.length * 2);
tests.forEach(function (test) {
const options = test.options || {};
postcss(plugin(options))
.process(test.fixture, { from: undefined })
.then(function (result) {
t.equal(result.css, test.expected, test.message);
t.equal(result.warnings().length, 0);
});
});
});
};
const sortOrderTests = [
{
message: 'Keep same order for identical properties.',
fixture: 'a{flex: 0;flex: 2;}',
expected: 'a{flex: 0;flex: 2;}',
},
{
message: 'Sort alphabetically with no order defined.',
fixture: 'a{flex: 0;border: 0;}',
expected: 'a{border: 0;flex: 0;}',
},
{
message: 'Sort alphabetically with a defined order.',
fixture: 'a{flex: 0;border: 0;}',
expected: 'a{border: 0;flex: 0;}',
options: { order: 'alphabetical' },
},
{
message: 'Sort according to custom order, changed.',
fixture: 'a{border: 0;z-index: 0;}',
expected: 'a{z-index: 0;border: 0;}',
options: { order: () => -1 },
},
{
message: 'Sort according to custom order, retained.',
fixture: 'a{border: 0;z-index: 0;}',
expected: 'a{border: 0;z-index: 0;}',
options: { order: () => 1 },
},
{
message: 'Sort according to SMACSS.',
fixture: 'a{border: 0;flex: 0;}',
expected: 'a{flex: 0;border: 0;}',
options: { order: 'smacss' },
},
{
message: 'Sort according to Concentric CSS.',
fixture: 'a{border: 0;flex: 0;}',
expected: 'a{flex: 0;border: 0;}',
options: { order: 'concentric-css' },
},
{
message: 'Keep at-rule at the same position.',
fixture: 'a{border: 0;@import sii;flex:0;}',
expected: 'a{border: 0;@import sii;flex:0;}',
},
];
const commentOrderTests = [
{
message: 'Keep comment intact.',
fixture: 'a{flex: 0;/*flex*/}',
expected: 'a{flex: 0;/*flex*/}',
},
{
message: 'Keep root comments intact.',
fixture: '/*a*/\na{}\n/*b*/\nb{}',
expected: '/*a*/\na{}\n/*b*/\nb{}',
},
{
message: 'Handle declaration with one comment.',
fixture: 'a{/*comment*/}',
expected: 'a{/*comment*/}',
},
{
message: 'Keep dangling comment intact.',
fixture: 'a{flex: 0;\n/*end*/}',
expected: 'a{flex: 0;\n/*end*/}',
},
{
message: 'Keep multiple comments intact.',
fixture: 'a{flex: 0;\n/*flex*/\n/*flex 2*/}',
expected: 'a{flex: 0;\n/*flex*/\n/*flex 2*/}',
},
{
message: 'Keep newline comment above declaration.',
fixture: 'a{flex: 0;\n/*border*/\nborder: 0;}',
expected: 'a{\n/*border*/\nborder: 0;flex: 0;}',
},
{
message: 'Handle multiple newline comments.',
fixture: 'a{flex: 0;\n/*border a*/\n/*border b*/\nborder: 0;}',
expected: 'a{\n/*border a*/\n/*border b*/\nborder: 0;flex: 0;}',
},
{
message: 'Keep inline comment beside declaration.',
fixture: 'a{flex: 0;\nborder: 0; /*border*/}',
expected: 'a{\nborder: 0; /*border*/flex: 0;}',
},
];
const nestedDeclarationTests = [
{
message: 'Sort nested declarations.',
fixture: 'a{a{flex: 0;border: 0;}}',
expected: 'a{a{border: 0;flex: 0;}}',
},
{
message: 'Sort nested at-rule declarations.',
fixture: 'a{@media(){flex: 0;border: 0;}}',
expected: 'a{@media(){border: 0;flex: 0;}}',
},
{
message: 'Keep nested newline comment above declaration.',
fixture: 'a{&:hover{flex: 0;\n/*border*/\nborder: 0;}}',
expected: 'a{&:hover{\n/*border*/\nborder: 0;flex: 0;}}',
},
{
message: 'Keep nested inline comment beside declaration.',
fixture: 'a{&:hover{flex: 0;\nborder: 0; /*border*/}}',
expected: 'a{&:hover{\nborder: 0; /*border*/flex: 0;}}',
},
{
message: 'Put declarations before nested selector.',
fixture: 'a{margin: 0;&:hover{color: red;}padding: 0;}',
expected: 'a{margin: 0;padding: 0;&:hover{color: red;}}',
},
];
testCssFixtures('Should order declarations.', sortOrderTests);
testCssFixtures('Should retain comments.', commentOrderTests);
testCssFixtures('Should order nested declarations.', nestedDeclarationTests);
tape('Should use the PostCSS plugin API.', function (t) {
t.plan(2);
t.ok(plugin().postcssVersion, 'Able to access version.');
t.equal(plugin().postcssPlugin, name, 'Able to access name.');
});
tape('CSS properties are up-to-date.', function (t) {
const cssOrdersDir = './orders/';
fs.readdir(cssOrdersDir, function (error, files) {
const sourceProperties = JSON.parse(
// eslint-disable-next-line no-sync
fs.readFileSync(path.join(cssOrdersDir, 'source.json'))
);
files
.filter(function (fileName) {
return fileName !== 'source.json';
})
// Pair filenames and amount of properties from each CSS order file
.map(function (fileName) {
return {
fileName: fileName,
properties: JSON.parse(
// eslint-disable-next-line no-sync
fs.readFileSync(path.join(cssOrdersDir, fileName))
),
};
})
.forEach(function (customOrderFile) {
t.deepLooseEqual(
customOrderFile.properties.sort(),
sourceProperties.sort(),
`${customOrderFile.fileName} has the same properties as source.`
);
});
t.end();
});
});