Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix callbacks in tests #4903

Merged
merged 1 commit into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 10 additions & 14 deletions lib/utils/__tests__/atRuleParamIndex.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,26 @@ const postcss = require('postcss');

describe('atRuleParamIndex', () => {
it('has a single space before the param', () => {
atRules('@media (color) {}', (atRule) => {
expect(atRuleParamIndex(atRule)).toBe(7);
});
expect(atRuleParamIndex(atRule('@media (color) {}'))).toBe(7);
});

it('has multiple spaces before the param', () => {
atRules('@media (color) {}', (atRule) => {
expect(atRuleParamIndex(atRule)).toBe(8);
});
expect(atRuleParamIndex(atRule('@media (color) {}'))).toBe(8);
});

it('has a newline before the param', () => {
atRules("@import\n'x.css');", (atRule) => {
expect(atRuleParamIndex(atRule)).toBe(8);
});
expect(atRuleParamIndex(atRule("@import\n'x.css');"))).toBe(8);
});

it('has a function param', () => {
atRules('@document url-prefix(http://www.w3.org/Style/)', (atRule) => {
expect(atRuleParamIndex(atRule)).toBe(10);
});
expect(atRuleParamIndex(atRule('@document url-prefix(http://www.w3.org/Style/)'))).toBe(10);
});
});

function atRules(css, cb) {
postcss.parse(css).walkAtRules(cb);
function atRule(css) {
const list = [];

postcss.parse(css).walkAtRules((rule) => list.push(rule));

return list[0];
}
28 changes: 11 additions & 17 deletions lib/utils/__tests__/declarationValueIndex.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,30 @@ const postcss = require('postcss');

describe('declarationValueIndex', () => {
it('has a space before the value', () => {
rules('a { a: b}', (decl) => {
expect(declarationValueIndex(decl)).toBe(3);
});
expect(declarationValueIndex(decl('a { a: b}'))).toBe(3);
});

it('has a colon before the value', () => {
rules('a { a :b }', (decl) => {
expect(declarationValueIndex(decl)).toBe(3);
});
expect(declarationValueIndex(decl('a { a :b }'))).toBe(3);
});

it('has no spaces before the value', () => {
rules('a { a:b }', (decl) => {
expect(declarationValueIndex(decl)).toBe(2);
});
expect(declarationValueIndex(decl('a { a:b }'))).toBe(2);
});

it('has multiple characters before the value', () => {
rules('a { a : b }', (decl) => {
expect(declarationValueIndex(decl)).toBe(5);
});
expect(declarationValueIndex(decl('a { a : b }'))).toBe(5);
});

it('has a newline before the value', () => {
rules('a { a:\nb }', (decl) => {
expect(declarationValueIndex(decl)).toBe(3);
});
expect(declarationValueIndex(decl('a { a:\nb }'))).toBe(3);
});
});

function rules(css, cb) {
postcss.parse(css).walkDecls(cb);
function decl(css) {
const list = [];

postcss.parse(css).walkDecls((decl) => list.push(decl));

return list[0];
}
12 changes: 4 additions & 8 deletions lib/utils/__tests__/isCustomPropertySet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@ const postcss = require('postcss');

describe('isCustomPropertySet', () => {
it('accepts custom property set', () => {
customPropertySet('--foo: {};', (customPropertySet) => {
expect(isCustomPropertySet(customPropertySet)).toBeTruthy();
});
expect(isCustomPropertySet(node('--foo: {};'))).toBeTruthy();
});

it('rejects custom property', () => {
customPropertySet('--foo: red;', (customPropertySet) => {
expect(isCustomPropertySet(customPropertySet)).toBeFalsy();
});
expect(isCustomPropertySet(node('--foo: red;'))).toBeFalsy();
});
});

function customPropertySet(css, cb) {
postcss.parse(css).walk(cb);
function node(css) {
return postcss.parse(css).first;
}
73 changes: 52 additions & 21 deletions lib/utils/__tests__/isFirstNested.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ describe('isFirstNested', () => {
}
`);

root.walkRules((rule) => {
expect(isFirstNested(rule)).toBe(true);
});
const rules = [];

root.walkRules((rule) => rules.push(rule));

expect(rules).toHaveLength(2);
expect(isFirstNested(rules[0])).toBe(true);
expect(isFirstNested(rules[1])).toBe(true);
});

it('returns true with the first-nested at-rule', () => {
Expand All @@ -37,9 +41,13 @@ describe('isFirstNested', () => {
}
`);

root.walkAtRules((atRule) => {
expect(isFirstNested(atRule)).toBe(true);
});
const atRules = [];

root.walkAtRules((rule) => atRules.push(rule));

expect(atRules).toHaveLength(2);
expect(isFirstNested(atRules[0])).toBe(true);
expect(isFirstNested(atRules[1])).toBe(true);
});

it('returns true with the first-nested declaration', () => {
Expand All @@ -52,9 +60,13 @@ describe('isFirstNested', () => {
}
`);

root.walkDecls((atRule) => {
expect(isFirstNested(atRule)).toBe(true);
});
const decls = [];

root.walkDecls((decl) => decls.push(decl));

expect(decls).toHaveLength(2);
expect(isFirstNested(decls[0])).toBe(true);
expect(isFirstNested(decls[1])).toBe(true);
});

it('returns true with first-nested non-statement', () => {
Expand All @@ -67,9 +79,14 @@ describe('isFirstNested', () => {
}
`);

root.walkComments((comment) => {
expect(isFirstNested(comment)).toBe(true);
});
const comments = [];

root.walkComments((comment) => comments.push(comment));

expect(comments).toHaveLength(3);
expect(isFirstNested(comments[0])).toBe(true);
expect(isFirstNested(comments[1])).toBe(true);
expect(isFirstNested(comments[2])).toBe(true);
});

it('returns false with not-first-nested rule', () => {
Expand All @@ -84,9 +101,13 @@ describe('isFirstNested', () => {
}
`);

root.walkRules('b', (rule) => {
expect(isFirstNested(rule)).toBe(false);
});
const rules = [];

root.walkRules('b', (rule) => rules.push(rule));

expect(rules).toHaveLength(2);
expect(isFirstNested(rules[0])).toBe(false);
expect(isFirstNested(rules[1])).toBe(false);
});

it('returns false with not-first-nested at-rule', () => {
Expand All @@ -101,9 +122,13 @@ describe('isFirstNested', () => {
}
`);

root.walkAtRules('expect', (atRule) => {
expect(isFirstNested(atRule)).toBe(false);
});
const atRules = [];

root.walkAtRules('expect', (atRule) => atRules.push(atRule));

expect(atRules).toHaveLength(2);
expect(isFirstNested(atRules[0])).toBe(false);
expect(isFirstNested(atRules[1])).toBe(false);
});

it('returns false with not-first-nested declaration', () => {
Expand All @@ -126,8 +151,14 @@ describe('isFirstNested', () => {
}
`);

root.walkDecls('color', (atRule) => {
expect(isFirstNested(atRule)).toBe(false);
});
const decls = [];

root.walkDecls('color', (decl) => decls.push(decl));

expect(decls).toHaveLength(4);
expect(isFirstNested(decls[0])).toBe(false);
expect(isFirstNested(decls[1])).toBe(false);
expect(isFirstNested(decls[2])).toBe(false);
expect(isFirstNested(decls[3])).toBe(false);
});
});