Skip to content

Commit

Permalink
Merge pull request #9 from zgreen/tests-fix-warnings
Browse files Browse the repository at this point in the history
Tests fix warnings
  • Loading branch information
zgreen authored Dec 30, 2016
2 parents 132eb21 + cb96437 commit ecaa393
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 73 deletions.
6 changes: 5 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ function buildCritical(options) {
criticalCSS.append(criticalOutput[cur]);
(0, _postcss2.default)(args.minify ? [(0, _cssnano2.default)()] : []).process(criticalCSS).then(function (result) {
if (!args.dryRun) {
_fs2.default.writeFile(_path2.default.join(args.outputPath, cur), result.css);
_fs2.default.writeFile(_path2.default.join(args.outputPath, cur), result.css, function (err) {
if (err) {
throw new Error(err);
}
});
} else {
console.log( // eslint-disable-line no-console
_chalk2.default.green('\nCritical CSS result is:\n' + _chalk2.default.yellow(result.css)));
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postcss-critical-css",
"version": "2.1.0",
"version": "2.1.1",
"description": "Generate critical CSS using PostCSS",
"main": "index.js",
"repository": {
Expand Down Expand Up @@ -28,7 +28,6 @@
"chalk": "^1.1.3",
"cssnano": "^3.5.2",
"eslint": "^3.3.1",
"eslint-config-airbnb": "^10.0.1",
"eslint-config-standard": "^6.2.1",
"eslint-plugin-flowtype": "^2.7.1",
"eslint-plugin-import": "^1.13.0",
Expand All @@ -43,7 +42,7 @@
"build": "eslint src/** && npm run flow && babel src --out-dir lib",
"example": "./node_modules/.bin/babel-node example/example.js",
"flow": "flow; test $? -eq 0 -o $? -eq 2",
"eslint": "eslint src/**",
"eslint": "eslint test/**/*.js && eslint src/**",
"start": "eslint src/** && npm run flow && babel src/index.js --out-file index.js --watch",
"pretest": "./node_modules/.bin/babel-node test/preTest.js",
"test": "npm run pretest && tape test"
Expand Down
2 changes: 1 addition & 1 deletion src/getCriticalDestination.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @param {string} Default output CSS file name.
* @return {string} String corresponding to output destination.
*/
export function getCriticalDestination (rule: Object, dest: String): string {
export function getCriticalDestination (rule: Object, dest: string): string {
rule.walkDecls('critical-filename', (decl: Object) => {
dest = decl.value.replace(/['"]*/g, '')
decl.remove()
Expand Down
2 changes: 1 addition & 1 deletion src/getCriticalRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { getCriticalDestination } from './getCriticalDestination'
* @param {string} Default output CSS file name.
* @return {object} Object containing critical rules, organized by output destination
*/
export function getCriticalRules (css: Object, shouldPreserve: boolean, defaultDest: String): Object {
export function getCriticalRules (css: Object, shouldPreserve: boolean, defaultDest: string): Object {
const critical = getCriticalFromAtRule({ css })
css.walkDecls('critical-selector', (decl: Object) => {
const dest = getCriticalDestination(decl.parent, defaultDest)
Expand Down
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ function buildCritical (options: ArgsType): Function {
if (!args.dryRun) {
fs.writeFile(
path.join(args.outputPath, cur),
result.css
result.css,
(err) => {
if (err) {
throw new Error(err)
}
}
)
} else {
console.log( // eslint-disable-line no-console
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/critical.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.default{color:orange;float:left;margin-right:1em;width:100%}
1 change: 1 addition & 0 deletions test/fixtures/default.critical.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.default{color:orange;float:left;margin-right:1em;width:100%}
8 changes: 8 additions & 0 deletions test/fixtures/default.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@critical;

.default {
color: orange;
float: left;
margin-right: 1em;
width: 100%;
}
6 changes: 6 additions & 0 deletions test/fixtures/default.non-critical.actual.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.default {
color: orange;
float: left;
margin-right: 1em;
width: 100%;
}
6 changes: 6 additions & 0 deletions test/fixtures/default.non-critical.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.default {
color: orange;
float: left;
margin-right: 1em;
width: 100%;
}
109 changes: 60 additions & 49 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,81 @@
#!/usr/bin/env node

var fs = require('fs')
var test = require('tape')
var postcss = require('postcss')
var postcssCriticalCSS = require('..');
const basePath = `${process.cwd()}/test/fixtures`;
const chalk = require('chalk');
const fs = require('fs')
const test = require('tape')
const basePath = `${process.cwd()}/test/fixtures`
const chalk = require('chalk')

function compareCritical(t, name, testNonCritical) {
function compareCritical (t, name, testNonCritical) {
const actualFilename = name.indexOf('default') !== -1 && !testNonCritical
? 'critical'
: `${name}.${testNonCritical ? 'non-critical.actual' : 'critical.actual'}`
t.equal(
fs.readFileSync(
`${basePath}/${name}.${testNonCritical ? 'non-critical.actual' : 'critical.actual'}.css`, 'utf8'
`${basePath}/${actualFilename}.css`, 'utf8'
).trim(),
fs.readFileSync(
`${basePath}/${name}.${testNonCritical ? 'non-critical.expected' : 'critical.expected'}.css`, 'utf8'
).trim(),
`processed fixture ${chalk.bold(name)} should be equal to expected output`
);
)
}

test('Testing "this" critical result', function(t) {
compareCritical(t, 'this');
t.end();
});
test('Testing default critical result', (t) => {
compareCritical(t, 'default')
t.end()
})

test('Testing "this" non-critical result', function(t) {
compareCritical(t, 'this', true);
t.end();
});
test('Testing default non-critical result', (t) => {
compareCritical(t, 'default', true)
t.end()
})

test('Testing "atRule" critical result', function(t) {
compareCritical(t, 'atRule');
t.end();
});
test('Testing "this" critical result', (t) => {
compareCritical(t, 'this')
t.end()
})

test('Testing "atRule" non-critical result', function(t) {
compareCritical(t, 'atRule', true);
t.end();
});
test('Testing "this" non-critical result', (t) => {
compareCritical(t, 'this', true)
t.end()
})

test(chalk.yellow(`Testing ${chalk.bold('atRule.wrapping')} critical result`), function(t) {
compareCritical(t, 'atRule-wrapping');
t.end();
});
test('Testing "atRule" critical result', (t) => {
compareCritical(t, 'atRule')
t.end()
})

test(chalk.yellow(`Testing ${chalk.bold('atRule.wrapping')} non-critical result`), function(t) {
compareCritical(t, 'atRule-wrapping', true);
t.end();
});
test('Testing "atRule" non-critical result', (t) => {
compareCritical(t, 'atRule', true)
t.end()
})

test('Testing "media" critical result', function(t) {
compareCritical(t, 'media');
t.end();
});
test(chalk.yellow(`Testing ${chalk.bold('atRule.wrapping')} critical result`), (t) => {
compareCritical(t, 'atRule-wrapping')
t.end()
})

test('Testing "media" non-critical result', function(t) {
compareCritical(t, 'media', true);
t.end();
});
test(chalk.yellow(`Testing ${chalk.bold('atRule.wrapping')} non-critical result`), (t) => {
compareCritical(t, 'atRule-wrapping', true)
t.end()
})

test(chalk.yellow(`Testing ${chalk.bold('scope')} critical result`), function(t) {
compareCritical(t, 'scope');
t.end();
});
test('Testing "media" critical result', (t) => {
compareCritical(t, 'media')
t.end()
})

test(chalk.yellow(`Testing ${chalk.bold('scope')} non-critical result`), function(t) {
compareCritical(t, 'scope', true);
t.end();
});
test('Testing "media" non-critical result', (t) => {
compareCritical(t, 'media', true)
t.end()
})

test(chalk.yellow(`Testing ${chalk.bold('scope')} critical result`), (t) => {
compareCritical(t, 'scope')
t.end()
})

test(chalk.yellow(`Testing ${chalk.bold('scope')} non-critical result`), (t) => {
compareCritical(t, 'scope', true)
t.end()
})
46 changes: 29 additions & 17 deletions test/preTest.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
#!/usr/bin/env node
var fs = require('fs')
var test = require('tape')
var postcss = require('postcss')
var postcssCriticalCSS = require('..');
const fs = require('fs')
const postcss = require('postcss')
const postcssCriticalCSS = require('..')
const basePath = `${process.cwd()}/test/fixtures`

const basePath = `${process.cwd()}/test/fixtures`;
function cb (files) {
function useFileData (data, file) {
postcss([postcssCriticalCSS({outputPath: basePath})])
.process(data)
.then(result => fs.writeFile(`${basePath}/${file.split('.')[0]}.non-critical.actual.css`, result.css))
.then(result => fs.writeFile(
`${basePath}/${file.split('.')[0]}.non-critical.actual.css`,
result.css,
'utf8',
(err) => {
if (err) {
throw new Error(err)
}
}))
}
files.forEach(function(file) {
if (file.indexOf('.actual') !== -1) {
fs.unlink(`${basePath}/${file}`)
}
if (file.indexOf('.expected') === -1 && file.indexOf('.actual') === -1) {
fs.readFile(`${basePath}/${file}`, 'utf8', (err, data) => {
if (err) {
throw new Error(err)
}
useFileData(data, file)
})
files.forEach(function (file) {
// Ignore any critical.css file(s) already written
if (file !== 'critical.css') {
if (file.indexOf('.actual') !== -1) {
fs.unlink(`${basePath}/${file}`, (err) => {
if (err) { throw new Error(err) }
})
}
if (file.indexOf('.expected') === -1 && file.indexOf('.actual') === -1) {
fs.readFile(`${basePath}/${file}`, 'utf8', (err, data) => {
if (err) {
throw new Error(err)
}
useFileData(data, file)
})
}
}
})
}
Expand Down

0 comments on commit ecaa393

Please sign in to comment.