'use strict';

const isKeyframeRule = require('../isKeyframeRule');
const postcss = require('postcss');

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

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

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

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

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

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

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

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

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

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

function rules(css, cb) {
	postcss.parse(css).walkDecls(cb);
}

'use strict';

const isKeyframeRule = require('../isKeyframeRule');
const postcss = require('postcss');

describe('isKeyframeRule', () => {
	it('detects a standard keyframe rule', () => {
		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', () => {
		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', () => {
		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', () => {
		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', () => {
		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', () => {
		const nodes = nodesIn('a {}');

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

	it('ignores a normal rule with nested decl', () => {
		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', () => {
		const nodes = nodesIn('a { @nest b & {} }');

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

	it('ignores an @media', () => {
		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', () => {
		const nodes = nodesIn('@supports (animation-name: test) { a {} }');

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

function nodesIn(css) {
	const nodes = [];

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

	return nodes;
}

Fix `isKeyframeRule.test.js` that use callbacks (#4922)