Skip to content

Commit

Permalink
rework options test
Browse files Browse the repository at this point in the history
because the plugin has been reworked, the tests need to get adjusted as well

1. combine & minimize option are no longer supported and thus need to get removed
2. since the plugin has changed from sync to async the tests need to handle the new promise flow appropriately

in the course of this I've also added a vscode launch.json for easier debugging
  • Loading branch information
SassNinja committed Dec 19, 2019
1 parent c1fab5c commit 851c1eb
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 90 deletions.
22 changes: 22 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
// Verwendet IntelliSense zum Ermitteln möglicher Attribute.
// Zeigen Sie auf vorhandene Attribute, um die zugehörigen Beschreibungen anzuzeigen.
// Weitere Informationen finden Sie unter https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "run test",
"program": "${workspaceFolder}/node_modules/.bin/_mocha",
"args": ["--timeout", "60000"]
},
{
"type": "node",
"request": "launch",
"name": "example: gulp",
"program": "${workspaceFolder}/examples/gulp/node_modules/.bin/gulp",
"cwd": "${workspaceFolder}/examples/gulp"
}
]
}
153 changes: 63 additions & 90 deletions test/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const path = require('path');
const postcss = require('postcss');
const plugin = require('../index.js');

const exampleFile = fs.readFileSync('test/data/example.css');
const entryExampleFile = fs.readFileSync('test/data/entry-example.css');
const exampleFile = fs.readFileSync('test/data/example.css', 'utf-8');
const entryExampleFile = fs.readFileSync('test/data/entry-example.css', 'utf-8');

describe('Options', function() {

Expand All @@ -15,87 +15,107 @@ describe('Options', function() {
});

describe('extractAll', function() {
it('true should cause to ignore all media queries except of the ones defined in the queries options', function() {
it('should only extract specified queries if false', (done) => {
const opts = {
output: {
path: path.join(__dirname, 'output')
},
queries: {
'screen and (min-width: 999px)': 'extract-all'
'screen and (min-width: 999px)': 'specified'
},
extractAll: false,
stats: false
};
postcss([ plugin(opts) ]).process(exampleFile, { from: 'test/data/example.css'}).css;
const filesCount = fs.readdirSync('test/output/').length;
assert.isTrue(fs.existsSync('test/output/example-extract-all.css'));
assert.equal(filesCount, 1);
postcss([ plugin(opts) ]).process(exampleFile, { from: 'test/data/example.css' }).then(() => {
const files = fs.readdirSync('test/output/');

assert.isTrue(fs.existsSync('test/output/example-specified.css'));
assert.equal(files.length, 1);
done();
});
});
it('should extract all queries if true', (done) => {
const opts = {
output: {
path: path.join(__dirname, 'output')
},
queries: {
'screen and (min-width: 999px)': 'specified'
},
extractAll: true,
stats: false
};
postcss([ plugin(opts) ]).process(exampleFile, { from: 'test/data/example.css' }).then((result) => {
const files = fs.readdirSync('test/output/');

assert.isAbove(files.length, 1);
assert.notMatch(result.css, /@media/);
done();
});
});
});

describe('entry', function() {
it('entry should override any other from option', function() {
it('should override any other from option', (done) => {
const opts = {
entry: path.join(__dirname, 'data/entry-example.namespace.css'),
output: {
path: path.join(__dirname, 'output')
},
stats: false
};
postcss([ plugin(opts) ]).process(entryExampleFile, { from: 'test/data/example.css'}).css;
assert.isTrue(fs.existsSync('test/output/entry-example.namespace-screen.css'));
postcss([ plugin(opts) ]).process(entryExampleFile, { from: 'test/data/example.css' }).then(() => {
assert.isTrue(fs.existsSync('test/output/entry-example.namespace-screen.css'));
done();
});
});
});

describe('output', function() {
it('output.path false should prevent emitting any files', function() {
it('should not emit any files if output.path is false and not touch the CSS', (done) => {
const opts = {
output: {
path: false
}
};
postcss([ plugin(opts) ]).process(exampleFile).css;
assert.isFalse(fs.existsSync('output'))
});
it('output.path should save extracted css to specific destination', function() {
const opts = {
output: {
path: path.join(__dirname, 'output')
},
stats: false
};
postcss([ plugin(opts) ]).process(exampleFile, { from: 'test/data/example.css'}).css;
assert.isTrue(fs.existsSync('test/output/example-screen-and-min-width-1024-px.css'));
assert.isTrue(fs.existsSync('test/output/example-screen-and-min-width-1200-px.css'));
postcss([ plugin(opts) ]).process(exampleFile, { from: 'test/data/example.css' }).then((result) => {
assert.isFalse(fs.existsSync('output'))
assert.equal(result.css, exampleFile);
done();
});
});
it('output.name should affect emited filenames', function() {
it('should use output.name for the emitted files if specified', (done) => {
const opts = {
output: {
path: path.join(__dirname, 'output'),
name: '[query].[ext]'
},
stats: false
};
postcss([ plugin(opts) ]).process(exampleFile, { from: 'test/data/example.css'}).css;
assert.isTrue(fs.existsSync('test/output/screen-and-min-width-1024-px.css'));
assert.isTrue(fs.existsSync('test/output/screen-and-min-width-1200-px.css'));
postcss([ plugin(opts) ]).process(exampleFile, { from: 'test/data/example.css' }).then(() => {
assert.isTrue(fs.existsSync('test/output/screen-and-min-width-1024-px.css'));
assert.isTrue(fs.existsSync('test/output/screen-and-min-width-1200-px.css'));
done();
});
});
it('output.name should support using the same placeholder multiple times', function () {
it('should support using the same placeholder in output.name multiple times', (done) => {
const opts = {
output: {
path: path.join(__dirname, 'output'),
name: '[query]-[query].[ext]'
},
stats: false
};
postcss([plugin(opts)]).process(exampleFile, { from: 'test/data/example.css' }).css;
assert.isTrue(fs.existsSync('test/output/screen-and-min-width-1024-px-screen-and-min-width-1024-px.css'));
assert.isTrue(fs.existsSync('test/output/screen-and-min-width-1200-px-screen-and-min-width-1200-px.css'));
postcss([plugin(opts)]).process(exampleFile, { from: 'test/data/example.css' }).then(() => {
assert.isTrue(fs.existsSync('test/output/screen-and-min-width-1024-px-screen-and-min-width-1024-px.css'));
assert.isTrue(fs.existsSync('test/output/screen-and-min-width-1200-px-screen-and-min-width-1200-px.css'));
done();
});
});
});

describe('queries', function() {
it('query with exact match should affect the emited filename', function() {
it('should use specified query that exactly matches', (done) => {
const opts = {
output: {
path: path.join(__dirname, 'output')
Expand All @@ -105,10 +125,12 @@ describe('Options', function() {
},
stats: false
};
postcss([ plugin(opts) ]).process(exampleFile, { from: 'test/data/example.css'}).css;
assert.isTrue(fs.existsSync('test/output/example-desktop.css'));
postcss([ plugin(opts) ]).process(exampleFile, { from: 'test/data/example.css' }).then(() => {
assert.isTrue(fs.existsSync('test/output/example-desktop.css'));
done();
});
});
it('query without exact match should not affect the emited filename', function() {
it('should ignore specified query that does not exactly match', (done) => {
const opts = {
output: {
path: path.join(__dirname, 'output')
Expand All @@ -118,60 +140,11 @@ describe('Options', function() {
},
stats: false
};
postcss([ plugin(opts) ]).process(exampleFile, { from: 'test/data/example.css'}).css;
assert.isFalse(fs.existsSync('test/output/example-xdesktop.css'));
});
});

describe('combine', function() {
it('combine true should merge equal query atRules', function() {
const opts = {
output: {
path: false
},
combine: true
};
let count = 0;
const testRoot = postcss([ plugin(opts) ]).process(exampleFile, { from: 'test/data/example.css'}).root;
testRoot.walkAtRules(atRule => {
count++;
});
assert.equal(count, 3);
});
it('combine false should prevent merge of equal query atRules', function() {
const opts = {
output: {
path: false
},
combine: false
};
let count = 0;
const testRoot = postcss([ plugin(opts) ]).process(exampleFile, { from: 'test/data/example.css'}).root;
testRoot.walkAtRules(atRule => {
count++;
postcss([ plugin(opts) ]).process(exampleFile, { from: 'test/data/example.css' }).then(() => {
assert.isFalse(fs.existsSync('test/output/example-xdesktop.css'));
done();
});
assert.equal(count, 4);
});
});

describe('minimize', function() {
it('minimize true should minify the emited files\' CSS to one line', function() {
const opts = {
output: {
path: path.join(__dirname, 'output'),
name: '[name]-[query].min.[ext]'
},
queries: {
'screen and (min-width: 1024px)': 'desktop'
},
minimize: true,
stats: false
};
postcss([ plugin(opts) ]).process(exampleFile, { from: 'test/data/example.css'}).css;
const output = fs.readFileSync('test/output/example-desktop.min.css', 'utf8');
const lines = output.split(/\r\n|\r|\n/).length;
assert.equal(lines, 1);
});
});

});
});

0 comments on commit 851c1eb

Please sign in to comment.