Skip to content

Commit a7dbf08

Browse files
committed
test: Add tests for theming inspection API
1 parent 7c16cc8 commit a7dbf08

File tree

4 files changed

+159
-2
lines changed

4 files changed

+159
-2
lines changed

src/material/_index.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,6 @@ list-density, list-base;
142142
private-typography-config-level-from-mdc, private-if-touch-targets-unsupported,
143143
$private-mdc-base-styles-query, $private-mdc-base-styles-without-animation-query,
144144
$private-mdc-theme-styles-query, $private-mdc-typography-styles-query;
145+
146+
// New theming APIs, currently in development:
147+
@forward './core/theming/inspection' as private-* show private-get-theme-version;

src/material/core/theming/tests/BUILD.bazel

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ ts_library(
5656
name = "unit_test_lib",
5757
testonly = True,
5858
srcs = [
59-
"theming-api.spec.ts",
59+
"theming-inspection-api.spec.ts",
60+
"theming-mixin-api.spec.ts",
6061
],
6162
# TODO(ESM): remove this once the Bazel NodeJS rules can handle ESM with `nodejs_binary`.
6263
devmode_module = "commonjs",
@@ -74,6 +75,9 @@ ts_library(
7475
jasmine_node_test(
7576
name = "unit_tests",
7677
srcs = [":unit_test_lib"],
77-
data = ["//src/material:sass_lib"],
78+
data = [
79+
"//src/material:sass_lib",
80+
"//src/material-experimental:sass_lib",
81+
],
7882
shard_count = 4,
7983
)
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import {compileString} from 'sass';
2+
import {runfiles} from '@bazel/runfiles';
3+
import * as path from 'path';
4+
5+
import {createLocalAngularPackageImporter} from '../../../../../tools/sass/local-sass-importer';
6+
import {pathToFileURL} from 'url';
7+
8+
// Note: For Windows compatibility, we need to resolve the directory paths through runfiles
9+
// which are guaranteed to reside in the source tree.
10+
const testDir = path.join(runfiles.resolvePackageRelative('../_all-theme.scss'), '../tests');
11+
const packagesDir = path.join(runfiles.resolveWorkspaceRelative('src/cdk/_index.scss'), '../..');
12+
13+
const localPackageSassImporter = createLocalAngularPackageImporter(packagesDir);
14+
15+
const mdcSassImporter = {
16+
findFileUrl: (url: string) => {
17+
if (url.toString().startsWith('@material')) {
18+
return pathToFileURL(path.join(runfiles.resolveWorkspaceRelative('./node_modules'), url));
19+
}
20+
return null;
21+
},
22+
};
23+
24+
interface M2ThemeConfig {
25+
type: string;
26+
density: string;
27+
typography: string;
28+
primary: string;
29+
accent: string;
30+
warn: string;
31+
}
32+
33+
interface M3ThemeConfig {
34+
type: string;
35+
density: string;
36+
brand: string;
37+
plain: string;
38+
bold: string;
39+
medium: string;
40+
regular: string;
41+
primary: string;
42+
secondary: string;
43+
tertiary: string;
44+
}
45+
46+
function defineM2Theme(config: Partial<M2ThemeConfig> = {}) {
47+
const {type, density, typography, primary, accent, warn} = {
48+
type: 'light',
49+
density: '0',
50+
typography: 'mat.define-typography-config()',
51+
primary: 'mat.define-palette(mat.$blue-palette)',
52+
accent: 'mat.define-palette(mat.$green-palette)',
53+
warn: 'mat.define-palette(mat.$red-palette)',
54+
...config,
55+
};
56+
const collapseColor = primary === 'null' && accent === 'null' && warn === 'null';
57+
return `mat.define-${type}-theme((
58+
color: ${
59+
collapseColor
60+
? `(
61+
primary: ${primary},
62+
accent: ${accent},
63+
warn: ${warn},
64+
)`
65+
: 'null'
66+
},
67+
typography: ${typography},
68+
density: ${density},
69+
))`;
70+
}
71+
72+
function defineM3Theme(config: Partial<M3ThemeConfig> = {}) {
73+
const {type, density, brand, plain, bold, medium, regular, primary, secondary, tertiary} = {
74+
type: 'light',
75+
density: '0',
76+
brand: 'Google Sans',
77+
plain: 'Roboto',
78+
bold: '700',
79+
medium: '500',
80+
regular: '400',
81+
primary: 'matx.$m3-blue-palette',
82+
secondary: 'matx.$m3-green-palette',
83+
tertiary: 'matx.$m3-yellow-palette',
84+
...config,
85+
};
86+
return `matx.define-theme((
87+
color: (
88+
theme-type: ${type},
89+
primary: ${primary},
90+
secondary: ${secondary},
91+
tertiary: ${tertiary},
92+
),
93+
typography: (
94+
brand-family: ${brand},
95+
plain-family: ${plain},
96+
bold-weight: ${bold},
97+
medium-weight: ${medium},
98+
regular-weight: ${regular},
99+
),
100+
density: (
101+
scale: ${density}
102+
),
103+
))`;
104+
}
105+
106+
/** Transpiles given Sass content into CSS. */
107+
function transpile(content: string) {
108+
return compileString(
109+
`
110+
@use '../../../index' as mat;
111+
@use '../../../../material-experimental/index' as matx;
112+
113+
${content}
114+
`,
115+
{
116+
loadPaths: [testDir],
117+
importers: [localPackageSassImporter, mdcSassImporter],
118+
},
119+
).css.toString();
120+
}
121+
122+
describe('theming inspection api', () => {
123+
describe('for m2 theme', () => {
124+
it('should get theme version', () => {
125+
expect(
126+
transpile(`
127+
$theme: ${defineM2Theme()};
128+
129+
div {
130+
content: mat.private-get-theme-version($theme);
131+
}
132+
`),
133+
).toMatch('content: 0;');
134+
});
135+
});
136+
137+
describe('for m3 theme', () => {
138+
it('should get theme version', () => {
139+
expect(
140+
transpile(`
141+
$theme: ${defineM3Theme()};
142+
143+
div {
144+
content: mat.private-get-theme-version($theme);
145+
}
146+
`),
147+
).toMatch('content: 1;');
148+
});
149+
});
150+
});

0 commit comments

Comments
 (0)