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 isKeyframeRule.test.js that use callbacks #4922

Merged
merged 1 commit into from
Sep 9, 2020
Merged
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
87 changes: 55 additions & 32 deletions lib/utils/__tests__/isKeyframeRule.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,89 @@ const postcss = require('postcss');

describe('isKeyframeRule', () => {
it('detects a standard keyframe rule', () => {
rules('@keyframes identifier { to {} }', (rule) => {
expect(isKeyframeRule(rule)).toBeTruthy();
});
const nodes = nodesIn('@keyframes identifier { to {} }');

expect(nodes).toHaveLength(2);
expect(isKeyframeRule(nodes[0])).toBeFalsy();
expect(isKeyframeRule(nodes[1])).toBeTruthy();
});

it('detects a mixed-case keyframe rule', () => {
rules('@kEyFrAmEs identifier { to {} }', (rule) => {
expect(isKeyframeRule(rule)).toBeTruthy();
});
const nodes = nodesIn('@kEyFrAmEs identifier { to {} }');

expect(nodes).toHaveLength(2);
expect(isKeyframeRule(nodes[0])).toBeFalsy();
expect(isKeyframeRule(nodes[1])).toBeTruthy();
});

it('detects an upper-case keyframe rule', () => {
rules('@KEYFRAMES identifier { to {} }', (rule) => {
expect(isKeyframeRule(rule)).toBeTruthy();
});
const nodes = nodesIn('@KEYFRAMES identifier { to {} }');

expect(nodes).toHaveLength(2);
expect(isKeyframeRule(nodes[0])).toBeFalsy();
expect(isKeyframeRule(nodes[1])).toBeTruthy();
});

it('detects a keyframe rule with nested from decl', () => {
rules('@keyframes identifier { from {} }', (rule) => {
expect(isKeyframeRule(rule)).toBeTruthy();
});
const nodes = nodesIn('@keyframes identifier { from {} }');

expect(nodes).toHaveLength(2);
expect(isKeyframeRule(nodes[0])).toBeFalsy();
expect(isKeyframeRule(nodes[1])).toBeTruthy();
});

it('detects a keyframe rule with nested percentage decl', () => {
rules('@keyframes identifier { 50% {} }', (rule) => {
expect(isKeyframeRule(rule)).toBeTruthy();
});
const nodes = nodesIn('@keyframes identifier { 50% {} }');

expect(nodes).toHaveLength(2);
expect(isKeyframeRule(nodes[0])).toBeFalsy();
expect(isKeyframeRule(nodes[1])).toBeTruthy();
});

it('ignores a normal rule', () => {
rules('a {}', (rule) => {
expect(isKeyframeRule(rule)).toBeFalsy();
});
const nodes = nodesIn('a {}');

expect(nodes).toHaveLength(1);
expect(isKeyframeRule(nodes[0])).toBeFalsy();
});

it('ignores a normal rule with nested decl', () => {
rules('a { & b {} }', (rule) => {
expect(isKeyframeRule(rule)).toBeFalsy();
});
const nodes = nodesIn('a { & b {} }');

expect(nodes).toHaveLength(2);
expect(isKeyframeRule(nodes[0])).toBeFalsy();
expect(isKeyframeRule(nodes[1])).toBeFalsy();
});

it('ignores a normal rule with nested at-rule decl', () => {
rules('a { @nest b & {} }', (rule) => {
expect(isKeyframeRule(rule)).toBeFalsy();
});
const nodes = nodesIn('a { @nest b & {} }');

expect(nodes).toHaveLength(2);
expect(isKeyframeRule(nodes[0])).toBeFalsy();
expect(isKeyframeRule(nodes[1])).toBeFalsy();
});

it('ignores an @media', () => {
rules('@media print { a {} }', (rule) => {
expect(isKeyframeRule(rule)).toBeFalsy();
});
const nodes = nodesIn('@media print { a {} }');

expect(nodes).toHaveLength(2);
expect(isKeyframeRule(nodes[0])).toBeFalsy();
expect(isKeyframeRule(nodes[1])).toBeFalsy();
});

it('ignores an @supports rule', () => {
rules('@supports (animation-name: test) { a {} }', (rule) => {
expect(isKeyframeRule(rule)).toBeFalsy();
});
const nodes = nodesIn('@supports (animation-name: test) { a {} }');

expect(nodes).toHaveLength(2);
expect(isKeyframeRule(nodes[0])).toBeFalsy();
expect(isKeyframeRule(nodes[1])).toBeFalsy();
});
});

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

postcss.parse(css).walk((node) => nodes.push(node));

return nodes;
}