Skip to content

Commit dc692f9

Browse files
committed
test: Ensure M3 theme is well behaved
Specifically it should: 1. Only emit styles under the user specified selector 2. Only emit CSS variable definitions
1 parent f1deb30 commit dc692f9

File tree

1 file changed

+68
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)