Skip to content

Commit 7da1da0

Browse files
committed
test: Add tests for M3 typography hierarchy
1 parent 87e3ba7 commit 7da1da0

File tree

2 files changed

+91
-5
lines changed

2 files changed

+91
-5
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,9 @@ build_test(
5555
ts_library(
5656
name = "unit_test_lib",
5757
testonly = True,
58-
srcs = [
59-
"theming-definition-api.spec.ts",
60-
"theming-inspection-api.spec.ts",
61-
"theming-mixin-api.spec.ts",
62-
],
58+
srcs = glob([
59+
"*.spec.ts",
60+
]),
6361
# TODO(ESM): remove this once the Bazel NodeJS rules can handle ESM with `nodejs_binary`.
6462
devmode_module = "commonjs",
6563
deps = [
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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(
19+
path.join(runfiles.resolveWorkspaceRelative('./node_modules'), url),
20+
) as URL;
21+
}
22+
return null;
23+
},
24+
};
25+
26+
/** Transpiles given Sass content into CSS. */
27+
function transpile(content: string) {
28+
return compileString(
29+
`
30+
@use '../../../index' as mat;
31+
@use '../../../../material-experimental/index' as matx;
32+
33+
$internals: _mat-theming-internals-do-not-access;
34+
35+
$theme: matx.define-theme();
36+
37+
${content}
38+
`,
39+
{
40+
loadPaths: [testDir],
41+
importers: [localPackageSassImporter, mdcSassImporter],
42+
},
43+
).css.toString();
44+
}
45+
46+
function verifyFullSelector(css: string, selector: string) {
47+
expect(css).toMatch(
48+
new RegExp(String.raw`(^|\n)` + selector.replace(/\./g, String.raw`\.`) + String.raw` \{`),
49+
);
50+
}
51+
52+
describe('typography hierarchy', () => {
53+
describe('for M3', () => {
54+
it('should emit styles for h1', () => {
55+
const css = transpile('@include mat.typography-hierarchy($theme)');
56+
verifyFullSelector(
57+
css,
58+
'.mat-display-large, .mat-typography .mat-display-large, .mat-typography h1',
59+
);
60+
});
61+
62+
it('should emit default body styles', () => {
63+
const css = transpile('@include mat.typography-hierarchy($theme)');
64+
verifyFullSelector(css, '.mat-body-large, .mat-typography .mat-body-large, .mat-typography');
65+
});
66+
67+
it('should emit default body paragraph styles', () => {
68+
const css = transpile('@include mat.typography-hierarchy($theme)');
69+
verifyFullSelector(
70+
css,
71+
'.mat-body-large p, .mat-typography .mat-body-large p, .mat-typography p',
72+
);
73+
});
74+
75+
it('should emit m2 selectors when requested', () => {
76+
const css = transpile('@include mat.typography-hierarchy($theme, $back-compat: true)');
77+
verifyFullSelector(
78+
css,
79+
'.mat-display-large, .mat-typography .mat-display-large, .mat-typography h1, .mat-h1, .mat-typography .mat-h1, .mat-headline-1, .mat-typography .mat-headline-1',
80+
);
81+
});
82+
83+
it('should use custom selector prefix', () => {
84+
const css = transpile(`@include mat.typography-hierarchy($theme, $selector: '.special')`);
85+
verifyFullSelector(css, '.mat-display-large, .special .mat-display-large, .special h1');
86+
});
87+
});
88+
});

0 commit comments

Comments
 (0)