mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
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
56 lines
1.6 KiB
TypeScript
56 lines
1.6 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 {ShadowCss} from '@angular/compiler/src/shadow_css';
|
|
|
|
export function shim(css: string, contentAttr: string, hostAttr: string = '') {
|
|
const shadowCss = new ShadowCss();
|
|
return shadowCss.shimCssText(css, contentAttr, hostAttr);
|
|
}
|
|
|
|
const shadowCssMatchers: jasmine.CustomMatcherFactories = {
|
|
toEqualCss: function(): jasmine.CustomMatcher {
|
|
return {
|
|
compare: function(actual: string, expected: string): jasmine.CustomMatcherResult {
|
|
const actualCss = extractCssContent(actual);
|
|
const expectedCss = extractCssContent(expected);
|
|
const passes = actualCss === expectedCss;
|
|
return {
|
|
pass: passes,
|
|
message: passes ? 'CSS equals as expected' :
|
|
`Expected '${actualCss}' to equal '${expectedCss}'`,
|
|
};
|
|
}
|
|
};
|
|
}
|
|
};
|
|
|
|
function extractCssContent(css: string): string {
|
|
return css.replace(/^\n\s+/, '')
|
|
.replace(/\n\s+$/, '')
|
|
.replace(/\s+/g, ' ')
|
|
.replace(/:\s/g, ':')
|
|
.replace(/ }/g, '}');
|
|
}
|
|
|
|
beforeEach(function() {
|
|
jasmine.addMatchers(shadowCssMatchers);
|
|
});
|
|
|
|
declare global {
|
|
module jasmine {
|
|
interface Matchers<T> {
|
|
/**
|
|
* Expect the actual css value to be equal to the expected css,
|
|
* for this comparison extra spacing and newlines are ignored so
|
|
* that only the core css content is being compared.
|
|
*/
|
|
toEqualCss(expected: string): void;
|
|
}
|
|
}
|
|
}
|