angular/packages/compiler/test/shadow_css/process_rules_spec.ts
dario-piotrowicz 61023b563d refactor(compiler): refactor the shadow css specs (#48443)
apply different quality of life improvements to the shadow
css unit tests:

- refactor the tests so that they are nicely divided in multiple files
   in a logical manner instead of having most of them all in a single big file

- remove the css normalization logic inconsistently used throughout  the tests, which
  causes tests to be inconsistent and it also introduced unintuitive checks

- provide a shared shim utility function (instead of re-defining it
  multiple times)

- add a `toEqualCss` matcher that can be used in the tests in order to
  match css strings without caring about spacing and indentation

PR Close #48443
2023-01-11 14:55:52 -08:00

64 lines
1.9 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {CssRule, processRules} from '@angular/compiler/src/shadow_css';
describe('ShadowCss, processRules', () => {
describe('parse rules', () => {
function captureRules(input: string): CssRule[] {
const result: CssRule[] = [];
processRules(input, (cssRule) => {
result.push(cssRule);
return cssRule;
});
return result;
}
it('should work with empty css', () => {
expect(captureRules('')).toEqual([]);
});
it('should capture a rule without body', () => {
expect(captureRules('a;')).toEqual([new CssRule('a', '')]);
});
it('should capture css rules with body', () => {
expect(captureRules('a {b}')).toEqual([new CssRule('a', 'b')]);
});
it('should capture css rules with nested rules', () => {
expect(captureRules('a {b {c}} d {e}')).toEqual([
new CssRule('a', 'b {c}'),
new CssRule('d', 'e'),
]);
});
it('should capture multiple rules where some have no body', () => {
expect(captureRules('@import a ; b {c}')).toEqual([
new CssRule('@import a', ''),
new CssRule('b', 'c'),
]);
});
});
describe('modify rules', () => {
it('should allow to change the selector while preserving whitespaces', () => {
expect(processRules(
'@import a; b {c {d}} e {f}',
(cssRule: CssRule) => new CssRule(cssRule.selector + '2', cssRule.content)))
.toEqual('@import a2; b2 {c {d}} e2 {f}');
});
it('should allow to change the content', () => {
expect(
processRules(
'a {b}', (cssRule: CssRule) => new CssRule(cssRule.selector, cssRule.content + '2')))
.toEqual('a {b2}');
});
});
});