Skip to content

Commit 7b7723e

Browse files
committed
Dry out tests
1 parent 7003844 commit 7b7723e

File tree

1 file changed

+99
-138
lines changed

1 file changed

+99
-138
lines changed

test/test.js

Lines changed: 99 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1+
var Promise = require('bluebird');
2+
var fs = Promise.promisifyAll(require('fs'));
3+
var path = require('path');
4+
15
var chai = require('chai');
26
var expect = chai.expect;
37
var chaiAsPromised = require('chai-as-promised');
48
chai.use(chaiAsPromised);
59

610
var postcss = require('postcss');
11+
var cssvariables = require('../');
712

8-
var Promise = require('bluebird');
9-
var fs = Promise.promisifyAll(require('fs'));
1013
var CleanCSS = require('clean-css');
1114

12-
var cssvariables = require('../');
1315

14-
function testPlugin(filePath, expectedFilePath, options) {
16+
var testPlugin = function(filePath, expectedFilePath, options) {
1517
options = options || {};
1618
return fs.readFileAsync(filePath)
1719
.then(function(buffer) {
@@ -39,204 +41,163 @@ function testPlugin(filePath, expectedFilePath, options) {
3941
//expect(actual).to.equal(contents);
4042
});
4143
});
42-
}
44+
};
4345

44-
describe('postcss-css-variables', function() {
45-
46-
47-
// Just make sure it doesn't mangle anything
48-
it('should work when there are no var() functions to consume declarations', function() {
49-
return testPlugin('./test/fixtures/no-var-func.css', './test/fixtures/no-var-func.expected.css');
46+
var fixtureBasePath = './test/fixtures/';
47+
var test = function(message, fixtureName, options) {
48+
it(message, function() {
49+
return testPlugin(
50+
path.join(fixtureBasePath, fixtureName + '.css'),
51+
path.join(fixtureBasePath, fixtureName + '.expected.css'),
52+
options
53+
);
5054
});
55+
};
5156

52-
it('should work when no variable name passed to `var()`', function() {
53-
return testPlugin('./test/fixtures/empty-var-func.css', './test/fixtures/empty-var-func.expected.css');
54-
});
5557

5658

59+
describe('postcss-css-variables', function() {
5760

58-
it('should work with variables declared in root', function() {
59-
return testPlugin('./test/fixtures/root-variable.css', './test/fixtures/root-variable.expected.css');
60-
});
61-
62-
it('should work with locally scoped variable in a non-root rule', function() {
63-
return testPlugin('./test/fixtures/local-variable-non-root.css', './test/fixtures/local-variable-non-root.expected.css');
64-
});
6561

66-
it('should work with any combinator selector if the last piece is the variable we have in the map', function() {
67-
return testPlugin('./test/fixtures/scope-last-piece-of-combinator-sequence.css', './test/fixtures/scope-last-piece-of-combinator-sequence.expected.css');
68-
});
62+
// Just make sure it doesn't mangle anything
63+
test('should work when there are no var() functions to consume declarations', 'no-var-func');
64+
test('should work when no variable name passed to `var()`', 'empty-var-func');
6965

7066

67+
test('should work with variables declared in root', 'root-variable');
7168

72-
it('should work with descendant selector type "nesting"', function() {
73-
return testPlugin('./test/fixtures/descendant-selector.css', './test/fixtures/descendant-selector.expected.css');
74-
});
69+
test('should work with locally scoped variable in a non-root rule', 'local-variable-non-root');
7570

76-
it('should work with css4 descendant selector type "nesting"', function() {
77-
return testPlugin('./test/fixtures/css4-descendant-selector.css', './test/fixtures/css4-descendant-selector.expected.css');
78-
});
7971

72+
test(
73+
'should work with any combinator selector if the last piece is the variable we have in the map',
74+
'scope-last-piece-of-combinator-sequence'
75+
);
8076

8177

82-
it('should work with direct descendant selector', function() {
83-
return testPlugin('./test/fixtures/direct-descendant-selector.css', './test/fixtures/direct-descendant-selector.expected.css');
84-
});
78+
test('should work with descendant selector type "nesting"', 'descendant-selector');
79+
test('should work with css4 descendant selector type "nesting"', 'css4-descendant-selector');
80+
test('should work with direct descendant selector', 'direct-descendant-selector');
8581

86-
it('should work with direct descendant selector where variables are scoped in a descendant selector', function() {
87-
return testPlugin('./test/fixtures/direct-descendant-selector-descendant-scope.css', './test/fixtures/direct-descendant-selector-descendant-scope.expected.css');
88-
});
82+
test(
83+
'should work with direct descendant selector where variables are scoped in a descendant selector',
84+
'direct-descendant-selector-descendant-scope'
85+
);
86+
test(
87+
'should work with direct descendant selector where variables are scoped in a direct descendant selector',
88+
'direct-descendant-selector-direct-descendant-scope'
89+
);
8990

90-
it('should work with direct descendant selector where variables are scoped in a direct descendant selector', function() {
91-
return testPlugin('./test/fixtures/direct-descendant-selector-direct-descendant-scope.css', './test/fixtures/direct-descendant-selector-direct-descendant-scope.expected.css');
92-
});
9391

92+
test('should work with pseudo selectors', 'pseudo-selector');
93+
test('should work with variables declared in pseudo selectors', 'pseudo-selector-declare-variable');
9494

95-
it('should work with pseudo selectors', function() {
96-
return testPlugin('./test/fixtures/pseudo-selector.css', './test/fixtures/pseudo-selector.expected.css');
97-
});
9895

99-
it('should work with variables declared in pseudo selectors', function() {
100-
return testPlugin('./test/fixtures/pseudo-selector-declare-variable.css', './test/fixtures/pseudo-selector-declare-variable.expected.css');
101-
});
10296

97+
test('should work with variables defined in comma separated selector', 'comma-separated-variable-declaration');
10398

10499

105-
it('should work with variables defined in comma separated selector', function() {
106-
return testPlugin('./test/fixtures/comma-separated-variable-declaration.css', './test/fixtures/comma-separated-variable-declaration.expected.css');
107-
});
100+
test('should work use the correct variable in comma separated selector', 'comma-separated-variable-usage');
108101

109-
it('should work use the correct variable in comma separated selector', function() {
110-
return testPlugin('./test/fixtures/comma-separated-variable-usage.css', './test/fixtures/comma-separated-variable-usage.expected.css');
111-
});
112102

103+
test('should work with star selector', 'star-selector-scope');
113104

114-
115-
it('should work with star selector', function() {
116-
return testPlugin('./test/fixtures/star-selector-scope.css', './test/fixtures/star-selector-scope.expected.css');
117-
});
118-
119-
it('should work with `!important` variable declarations', function() {
120-
return testPlugin('./test/fixtures/important-variable-declaration.css', './test/fixtures/important-variable-declaration.expected.css');
121-
});
105+
test('should work with `!important` variable declarations', 'important-variable-declaration');
122106

123107

124108

125109
describe('with at-rules', function() {
126-
it('should add rule declaration of property in @media', function() {
127-
return testPlugin('./test/fixtures/media-query.css', './test/fixtures/media-query.expected.css');
128-
});
129-
it('should add rule declaration of property in @support', function() {
130-
return testPlugin('./test/fixtures/support-directive.css', './test/fixtures/support-directive.expected.css');
131-
});
110+
test('should add rule declaration of property in @media', 'media-query');
111+
test('should add rule declaration of property in @support', 'support-directive');
132112

133-
it('should work with nested @media', function() {
134-
return testPlugin('./test/fixtures/media-query-nested.css', './test/fixtures/media-query-nested.expected.css');
135-
});
113+
test('should work with nested @media', 'media-query-nested');
136114

137-
it('should cascade to nested rules', function() {
138-
return testPlugin('./test/fixtures/cascade-on-nested-rules.css', './test/fixtures/cascade-on-nested-rules.expected.css');
139-
});
115+
test('should cascade to nested rules', 'cascade-on-nested-rules');
140116

141-
it('should cascade with calc-expression to nested rules', function() {
142-
return testPlugin('./test/fixtures/cascade-with-calc-expression-on-nested-rules.css', './test/fixtures/cascade-with-calc-expression-on-nested-rules.expected.css');
143-
});
117+
test('should cascade with calc-expression to nested rules', 'cascade-with-calc-expression-on-nested-rules');
144118

145-
it('should cascade to nested rules in the proper scope. See issue #2', function() {
146-
return testPlugin('./test/fixtures/cascade-on-nested-rules-in-proper-scope.css', './test/fixtures/cascade-on-nested-rules-in-proper-scope.expected.css');
147-
});
148-
119+
test('should cascade to nested rules in the proper scope. See issue #2', 'cascade-on-nested-rules-in-proper-scope');
149120
});
150121

151122

152-
it('should work with variables that reference other variables', function() {
153-
return testPlugin('./test/fixtures/variable-reference-other-variable.css', './test/fixtures/variable-reference-other-variable.expected.css');
154-
});
123+
test('should work with variables that reference other variables', 'variable-reference-other-variable');
155124

156-
it('should work with variable with calc-expression that reference other variables', function() {
157-
return testPlugin('./test/fixtures/variable-with-calc-expression-reference-other-variable.css', './test/fixtures/variable-with-calc-expression-reference-other-variable.expected.css');
158-
});
125+
test(
126+
'should work with variable with calc-expression that reference other variables',
127+
'variable-with-calc-expression-reference-other-variable'
128+
);
159129

160-
it('should work with variables that reference other variables with at-rule changing the value', function() {
161-
return testPlugin('./test/fixtures/variable-reference-other-variable-media-query1.css', './test/fixtures/variable-reference-other-variable-media-query1.expected.css');
162-
});
130+
test(
131+
'should work with variables that reference other variables with at-rule changing the value',
132+
'variable-reference-other-variable-media-query1'
133+
);
134+
test(
135+
'should work with local variables that reference other variables with at-rule changing the value',
136+
'variable-reference-other-variable-media-query2'
137+
);
163138

164-
it('should work with local variables that reference other variables with at-rule changing the value', function() {
165-
return testPlugin('./test/fixtures/variable-reference-other-variable-media-query2.css', './test/fixtures/variable-reference-other-variable-media-query2.expected.css');
166-
});
167139

168140

169-
170-
it('should work with variables that try to self reference', function() {
171-
return testPlugin('./test/fixtures/self-reference.css', './test/fixtures/self-reference.expected.css');
172-
});
173-
it('should work with variables that try to self reference and fallback properly', function() {
174-
return testPlugin('./test/fixtures/self-reference-fallback.css', './test/fixtures/self-reference-fallback.expected.css');
175-
});
176-
177-
it('should work with circular reference', function() {
178-
return testPlugin('./test/fixtures/circular-reference.css', './test/fixtures/circular-reference.expected.css');
179-
});
180-
141+
test('should work with variables that try to self reference', 'self-reference');
142+
test('should work with variables that try to self reference and fallback properly', 'self-reference-fallback');
143+
test('should work with circular reference', 'circular-reference');
181144

182145

183146
describe('with `options.variables`', function() {
184-
it('should work with JS defined variables', function() {
185-
return testPlugin(
186-
'./test/fixtures/js-defined.css',
187-
'./test/fixtures/js-defined.expected.css',
188-
{
189-
variables: {
190-
'--js-defined1': '75px',
191-
'--js-defined2': {
192-
value: '80px'
193-
},
194-
// Should be automatically prefixed with `--`
195-
'js-defined-no-prefix': '#ff0000'
196-
}
147+
test(
148+
'should work with JS defined variables',
149+
'js-defined',
150+
{
151+
variables: {
152+
'--js-defined1': '75px',
153+
'--js-defined2': {
154+
value: '80px'
155+
},
156+
// Should be automatically prefixed with `--`
157+
'js-defined-no-prefix': '#ff0000'
197158
}
198-
);
199-
});
159+
}
160+
);
200161
});
201162

202163

203164
describe('with `options.preserve`', function() {
204-
it('preserves variables when `preserve` is `true`', function() {
205-
return testPlugin('./test/fixtures/preserve-variables.css', './test/fixtures/preserve-variables.expected.css', { preserve: true });
206-
});
165+
test(
166+
'preserves variables when `preserve` is `true`',
167+
'preserve-variables',
168+
{ preserve: true }
169+
);
207170

208-
it('preserves computed value when `preserve` is `\'computed\'`', function() {
209-
return testPlugin('./test/fixtures/preserve-computed.css', './test/fixtures/preserve-computed.expected.css', { preserve: 'computed' });
210-
});
171+
test(
172+
'preserves computed value when `preserve` is `\'computed\'`',
173+
'preserve-computed',
174+
{ preserve: 'computed' }
175+
);
211176
});
212177

213178

214179
describe('missing variable declarations', function() {
215-
it('should work with missing variables', function() {
216-
return testPlugin('./test/fixtures/missing-variable-usage.css', './test/fixtures/missing-variable-usage.expected.css');
217-
});
218-
219-
it('should use fallback value if provided with missing variables', function() {
220-
return testPlugin('./test/fixtures/missing-variable-should-fallback.css', './test/fixtures/missing-variable-should-fallback.expected.css');
221-
});
180+
test('should work with missing variables', 'missing-variable-usage');
181+
test('should use fallback value if provided with missing variables', 'missing-variable-should-fallback');
222182
});
223183

224184
it('should not parse malformed var() declarations', function() {
225185
return expect(testPlugin(
226-
'./test/fixtures/malformed-variable-usage.css',
186+
'./test/fixtures/malformed-variable-usage.css',
227187
'./test/fixtures/malformed-variable-usage.expected.css'
228188
)
229189
).to.eventually.be.rejected;
230190
});
231191

232192
describe('rule clean up', function() {
233-
it('should clean up rules if we removed variable declarations to make it empty', function() {
234-
return testPlugin('./test/fixtures/remove-empty-rules-after-variable-collection.css', './test/fixtures/remove-empty-rules-after-variable-collection.expected.css');
235-
});
236-
237-
it('should clean up neseted rules if we removed variable declarations to make it empty', function() {
238-
return testPlugin('./test/fixtures/remove-nested-empty-rules-after-variable-collection.css', './test/fixtures/remove-nested-empty-rules-after-variable-collection.expected.css');
239-
});
193+
test(
194+
'should clean up rules if we removed variable declarations to make it empty',
195+
'remove-empty-rules-after-variable-collection'
196+
);
197+
test(
198+
'should clean up neseted rules if we removed variable declarations to make it empty',
199+
'remove-nested-empty-rules-after-variable-collection'
200+
);
240201
});
241202

242203

0 commit comments

Comments
 (0)