Skip to content

Commit 0499dc0

Browse files
authored
test: Ensure M3 theme is well behaved (#28555)
Specifically it should: 1. Only emit styles under the user specified selector 2. Only emit CSS variable definitions
1 parent af7cd70 commit 0499dc0

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import {parse} from 'postcss';
2+
import {compileString} from 'sass';
3+
import {runfiles} from '@bazel/runfiles';
4+
import * as path from 'path';
5+
6+
import {createLocalAngularPackageImporter} from '../../../../../tools/sass/local-sass-importer';
7+
import {pathToFileURL} from 'url';
8+
9+
// Note: For Windows compatibility, we need to resolve the directory paths through runfiles
10+
// which are guaranteed to reside in the source tree.
11+
const testDir = path.join(runfiles.resolvePackageRelative('../_all-theme.scss'), '../tests');
12+
const packagesDir = path.join(runfiles.resolveWorkspaceRelative('src/cdk/_index.scss'), '../..');
13+
14+
const localPackageSassImporter = createLocalAngularPackageImporter(packagesDir);
15+
16+
const mdcSassImporter = {
17+
findFileUrl: (url: string) => {
18+
if (url.toString().startsWith('@material')) {
19+
return pathToFileURL(
20+
path.join(runfiles.resolveWorkspaceRelative('./node_modules'), url),
21+
) as URL;
22+
}
23+
return null;
24+
},
25+
};
26+
27+
/** Transpiles given Sass content into CSS. */
28+
function transpile(content: string) {
29+
return compileString(
30+
`
31+
@use '../../../index' as mat;
32+
@use '../../../../material-experimental/index' as matx;
33+
34+
$internals: _mat-theming-internals-do-not-access;
35+
36+
$theme: matx.define-theme();
37+
38+
${content}
39+
`,
40+
{
41+
loadPaths: [testDir],
42+
importers: [localPackageSassImporter, mdcSassImporter],
43+
},
44+
).css.toString();
45+
}
46+
47+
describe('M3 theme', () => {
48+
it('should emit all styles under the given selector', () => {
49+
const root = parse(transpile(`html { @include mat.all-component-themes($theme); }`));
50+
const selectors: string[] = [];
51+
root.walkRules(rule => {
52+
selectors.push(rule.selector);
53+
});
54+
expect(selectors).toEqual(['html']);
55+
});
56+
57+
it('should only emit CSS variables', () => {
58+
const root = parse(transpile(`html { @include mat.all-component-themes($theme); }`));
59+
const nonVarProps: string[] = [];
60+
root.walkDecls(decl => {
61+
if (!decl.prop.startsWith('--')) {
62+
nonVarProps.push(decl.prop);
63+
}
64+
});
65+
expect(nonVarProps).toEqual([]);
66+
});
67+
});

0 commit comments

Comments
 (0)