Skip to content

Commit

Permalink
More test coverage with more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsJonQ committed May 28, 2017
1 parent 6fb01fb commit aa2ee53
Show file tree
Hide file tree
Showing 10 changed files with 244 additions and 16 deletions.
15 changes: 15 additions & 0 deletions lib/utils/cssSelector.js
Expand Up @@ -10,6 +10,20 @@ const inList = (list = [], selector = '') => {
});
};

const sanitizeForEmmet = (selectors = '') => {
if (!isString(selectors)) {
return false;
}

return selectors.trim()
.replace(/\ > \ /g, ' ')
.replace(/>/g, ' ')
.replace(/\ \ \ \ /g, ' ')
.replace(/\ \ \ /g, ' ')
.replace(/\ \ /g, ' ')
.replace(/\ /g, ' > ');
};

const split = (selector = '') => {
if (!isString(selector)) {
return false;
Expand All @@ -20,5 +34,6 @@ const split = (selector = '') => {

module.exports = {
inList,
sanitizeForEmmet,
split,
};
6 changes: 5 additions & 1 deletion lib/utils/isString.js
@@ -1,5 +1,9 @@
const isString = function(string) {
return (string && typeof string === 'string' && string !== '');
if (string && typeof string === 'string' && string !== '') {
return true;
} else {
return false;
}
};

module.exports = isString;
20 changes: 6 additions & 14 deletions lib/utils/renderer.js
@@ -1,22 +1,14 @@
const emmet = require('../vendor/emmet');
const isString = require('./isString');
const sanitizeForEmmet = require('./cssSelector').sanitizeForEmmet;
const hasProp = require('./object').hasProp;

const sanitize = (selectors = '') => {
return selectors.trim()
.replace(/\ > \ /g, ' ')
.replace(/>/g, ' ')
.replace(/\ \ \ \ /g, ' ')
.replace(/\ \ \ /g, ' ')
.replace(/\ \ /g, ' ')
.replace(/\ /g, ' > ');
};

const renderer = (document, selectors) => {
if (!document || !isString(selectors)) {
return;
const renderer = (document = false, selectors = '') => {
if (!document || !hasProp(document, 'location') || !isString(selectors)) {
return false;
}

return emmet(document, sanitize(selectors));
return emmet(document, sanitizeForEmmet(selectors));
};

module.exports = renderer;
2 changes: 1 addition & 1 deletion lib/utils/sass.js
Expand Up @@ -34,7 +34,7 @@ const getOptions = (o = defaultOptions) => {
return opts;
};

const render = (o = {}) => {
const render = (o = defaultOptions) => {
const opts = getOptions(o);

return opts ? nodeSass.renderSync(opts).css.toString() : false;
Expand Down
3 changes: 3 additions & 0 deletions test/setup.mocha.js
Expand Up @@ -26,6 +26,9 @@ global.utils = {
cssFile: require('../lib/utils/cssFile'),
cssSelector: require('../lib/utils/cssSelector'),
cssom: require('../lib/utils/cssom'),
isString: require('../lib/utils/isString'),
isValid: require('../lib/utils/isValid'),
renderer: require('../lib/utils/renderer'),
resolve: require('../lib/utils/resolve'),
sass: require('../lib/utils/sass'),
};
21 changes: 21 additions & 0 deletions test/unit/utils/cssSelector.js
Expand Up @@ -22,6 +22,27 @@ describe('utils', () => {
});
});

describe('.sanitizeForEmmet()', () => {
const fn = cssSelector.sanitizeForEmmet;

it('should return false if arguments are invalid', () => {
expect(fn()).to.be.false;
expect(fn('')).to.be.false;
});

it('should trim whitespace before/after string', () => {
expect(fn(' div ')).to.equal('div');
});

it('should add > symbol for space', () => {
expect(fn('div li')).to.equal('div > li');
});

it('should resolve excess spacing', () => {
expect(fn('div li')).to.equal('div > li');
});
});

describe('.split()', () => {
const fn = cssSelector.split;

Expand Down
130 changes: 130 additions & 0 deletions test/unit/utils/cssom.js
Expand Up @@ -14,6 +14,117 @@ describe('utils', () => {
});
});

describe('.isRule()', () => {
const fn = cssom.isRule;

it('should return true if valid (based on PostCSS AST)', () => {
expect(fn()).to.be.false;
expect(fn({})).to.be.false;
expect(fn({ type: 'rule' })).to.be.true;
});
});

describe('.isSelector()', () => {
const fn = cssom.isSelector;

it('should return true if valid (based on PostCSS AST)', () => {
expect(fn()).to.be.false;
expect(fn({})).to.be.false;
expect(fn({ nodes: true })).to.be.true;
});
});

describe('.findSelectorsFromNodes()', () => {
const fn = cssom.findSelectorsFromNodes;

it('should return false if invalid args', () => {
expect(fn()).to.be.false;
expect(fn(true, '')).to.be.false;
expect(fn({}, '.body')).to.be.false;
});

it('should return empty array if selectors cannot be found from nodes', () => {
expect(fn([{ selector: '.html' }], '.body')).to.be.an('array').and.to.have.lengthOf(0);
});

it('should return selector it exists in node', () => {
expect(fn([{ selector: '.body' }], '.body')).to.be.an('array').and.to.have.lengthOf(1);
});
});

describe('.findSelectorFromParamsByString()', () => {
const fn = cssom.findSelectorFromParamsByString;

it('should return false if invalid args', () => {
expect(fn()).to.be.false;
expect(fn(true, '')).to.be.false;
expect(fn({}, '.body')).to.be.false;
});
});

describe('.findSelectorFromParamsByArray()', () => {
const fn = cssom.findSelectorFromParamsByArray;

it('should return false if invalid args', () => {
expect(fn()).to.be.undefined;
expect(fn(true, '')).to.be.false;
expect(fn({}, '.body')).to.be.false;
expect(fn([], '.body')).to.be.false;
expect(fn([], [])).to.be.undefined;
});
});

describe('.getAtRules()', () => {
const fn = cssom.getAtRules;

it('should return empty array if invalid args', () => {
expect(fn()).to.be.an('array').and.have.lengthOf(0);
expect(fn([], '')).to.be.an('array').and.have.lengthOf(0);
});
});

describe('.getAtRuleFromSelector()', () => {
const fn = cssom.getAtRuleFromSelector;

it('should return false if selector isn\'t a nested rule', () => {
expect(fn()).to.be.false;
expect(fn('.selector')).to.be.false;
expect(fn({ selector: '.selector'})).to.be.false;
});

it('should return rule if selector is a nested rule', () => {
expect(fn({ parent: { type: 'atrule' } })).to.not.be.false;
});
});

describe('.getParamNodes', () => {
const fn = cssom.getParamNodes;

it('should return empty array if invalid args', () => {
expect(fn()).to.be.an('array').and.to.have.lengthOf(0);
expect(fn('true')).to.be.an('array').and.to.have.lengthOf(0);
expect(fn(true)).to.be.an('array').and.to.have.lengthOf(0);
});
});

describe('.getPropsFromSelector()', () => {
const fn = cssom.getPropsFromSelector;

it('should return false if invalid args', () => {
expect(fn()).to.be.false;
expect(fn({})).to.be.false;
});
});

describe('.getPropDataFromSelector()', () => {
const fn = cssom.getPropDataFromSelector;

it('should return false if invalid args', () => {
expect(fn()).to.be.false;
expect(fn({}, '')).to.be.false;
});
});

describe('.getRuleParams()', () => {
const fn = cssom.getRuleParams;

Expand Down Expand Up @@ -54,5 +165,24 @@ describe('utils', () => {
expect(cssom.type).to.equal('root');
});
});

describe('.sanitizeParens()', () => {
const fn = cssom.sanitizeParens;

it('should return arg if arg (string) is invalid', () => {
expect(fn('')).to.equal('');
expect(fn(true)).to.equal(true);
expect(fn(123)).to.equal(123);
});

it('should strip parenthesis', () => {
expect(fn('((content))(rule)((()))')).to.not.contain('(');
expect(fn('((content))(rule)((()))')).to.not.contain(')');
});

it('should return rule in lowercase', () => {
expect(fn('(CoNtEnT)')).to.equal('content');
});
});
});
});
27 changes: 27 additions & 0 deletions test/unit/utils/isString.js
@@ -0,0 +1,27 @@
/* globals barista: true, expect: true, describe: true, it: true, sinon: true, utils: true */

const isString = global.utils.isString;

describe('utils', () => {
describe('isString', () => {
it('should return false for non-strings', () => {
expect(isString(0)).to.be.false;
expect(isString(1)).to.be.false;
expect(isString([])).to.be.false;
expect(isString(true)).to.be.false;
expect(isString(false)).to.be.false;
expect(isString({})).to.be.false;
expect(isString()).to.be.false;
});

it('should return false for empty strings', () => {
expect(isString('')).to.be.false;
});

it('should return true for non-empty strings', () => {
expect(isString('y')).to.be.true;
expect(isString('yiss')).to.be.true;
expect(isString('aw yiss')).to.be.true;
});
});
});
13 changes: 13 additions & 0 deletions test/unit/utils/renderer.js
@@ -0,0 +1,13 @@
/* globals barista: true, expect: true, describe: true, it: true, sinon: true, utils: true */

const renderer = global.utils.renderer;

describe('utils', () => {
describe('renderer', () => {
it('should return false for invalid args', () => {
expect(renderer(true, '')).to.be.false;
expect(renderer(true, '.selector')).to.be.false;
expect(renderer({}, '.selector')).to.be.false;
});
});
});
23 changes: 23 additions & 0 deletions test/unit/utils/sass.js
@@ -0,0 +1,23 @@
/* globals barista: true, expect: true, describe: true, it: true, sinon: true, utils: true */

const sass = global.utils.sass;

describe('utils', () => {
describe('sass', () => {
describe('render', () => {
it('should render CSS with valid option.content', () => {
const output = sass.render({ content: 'h1 { color: black; }' });

expect(output).to.be.a('string');
expect(output).to.contain('h1');
});

it('should render SCSS with valid option.content', () => {
const output = sass.render({ content: 'h1 { color: black; a { display: block; } }' });

expect(output).to.be.a('string');
expect(output).to.contain('h1 a');
});
});
});
});

0 comments on commit aa2ee53

Please sign in to comment.