Skip to content

Commit 96b82d1

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

File tree

2 files changed

+87
-5
lines changed

2 files changed

+87
-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: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
function verifyFullSelector(css: string, selector: string) {
48+
expect(css).toMatch(
49+
new RegExp(String.raw`(^|\n)` + selector.replace(/\./g, String.raw`\.`) + String.raw` \{`),
50+
);
51+
}
52+
53+
describe('typography hierarchy', () => {
54+
describe('for M3', () => {
55+
it('should emit styles for h1', () => {
56+
const css = transpile('@include mat.typography-hierarchy($theme)');
57+
verifyFullSelector(
58+
css,
59+
'.mat-display-large, .mat-typography .mat-display-large, .mat-typography h1',
60+
);
61+
});
62+
63+
it('should emit default body styles', () => {
64+
const css = transpile('@include mat.typography-hierarchy($theme)');
65+
verifyFullSelector(css, '.mat-body-large, .mat-typography .mat-body-large, .mat-typography');
66+
});
67+
68+
it('should emit default body paragraph styles', () => {
69+
const css = transpile('@include mat.typography-hierarchy($theme)');
70+
verifyFullSelector(
71+
css,
72+
'.mat-body-large p, .mat-typography .mat-body-large p, .mat-typography p',
73+
);
74+
});
75+
76+
it('should emit m2 selectors when requested', () => {
77+
const css = transpile('@include mat.typography-hierarchy($theme, $back-compat: true)');
78+
verifyFullSelector(
79+
css,
80+
'.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',
81+
);
82+
});
83+
});
84+
});

0 commit comments

Comments
 (0)