-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(material-experimental/theming): Make M3 work with typography-hier…
…archy (#28540) * fix(material-experimental/theming): Make M3 work with typography-hierarchy * test: Add tests for M3 typography hierarchy
- Loading branch information
Showing
4 changed files
with
220 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/material/core/theming/tests/theming-typography-hierarchy.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import {compileString} from 'sass'; | ||
import {runfiles} from '@bazel/runfiles'; | ||
import * as path from 'path'; | ||
|
||
import {createLocalAngularPackageImporter} from '../../../../../tools/sass/local-sass-importer'; | ||
import {pathToFileURL} from 'url'; | ||
|
||
// Note: For Windows compatibility, we need to resolve the directory paths through runfiles | ||
// which are guaranteed to reside in the source tree. | ||
const testDir = path.join(runfiles.resolvePackageRelative('../_all-theme.scss'), '../tests'); | ||
const packagesDir = path.join(runfiles.resolveWorkspaceRelative('src/cdk/_index.scss'), '../..'); | ||
|
||
const localPackageSassImporter = createLocalAngularPackageImporter(packagesDir); | ||
|
||
const mdcSassImporter = { | ||
findFileUrl: (url: string) => { | ||
if (url.toString().startsWith('@material')) { | ||
return pathToFileURL( | ||
path.join(runfiles.resolveWorkspaceRelative('./node_modules'), url), | ||
) as URL; | ||
} | ||
return null; | ||
}, | ||
}; | ||
|
||
/** Transpiles given Sass content into CSS. */ | ||
function transpile(content: string) { | ||
return compileString( | ||
` | ||
@use '../../../index' as mat; | ||
@use '../../../../material-experimental/index' as matx; | ||
$internals: _mat-theming-internals-do-not-access; | ||
$theme: matx.define-theme(); | ||
${content} | ||
`, | ||
{ | ||
loadPaths: [testDir], | ||
importers: [localPackageSassImporter, mdcSassImporter], | ||
}, | ||
).css.toString(); | ||
} | ||
|
||
function verifyFullSelector(css: string, selector: string) { | ||
expect(css).toMatch( | ||
new RegExp(String.raw`(^|\n)` + selector.replace(/\./g, String.raw`\.`) + String.raw` \{`), | ||
); | ||
} | ||
|
||
describe('typography hierarchy', () => { | ||
describe('for M3', () => { | ||
it('should emit styles for h1', () => { | ||
const css = transpile('@include mat.typography-hierarchy($theme)'); | ||
verifyFullSelector( | ||
css, | ||
'.mat-display-large, .mat-typography .mat-display-large, .mat-typography h1', | ||
); | ||
}); | ||
|
||
it('should emit default body styles', () => { | ||
const css = transpile('@include mat.typography-hierarchy($theme)'); | ||
verifyFullSelector(css, '.mat-body-large, .mat-typography .mat-body-large, .mat-typography'); | ||
}); | ||
|
||
it('should emit default body paragraph styles', () => { | ||
const css = transpile('@include mat.typography-hierarchy($theme)'); | ||
verifyFullSelector( | ||
css, | ||
'.mat-body-large p, .mat-typography .mat-body-large p, .mat-typography p', | ||
); | ||
}); | ||
|
||
it('should emit m2 selectors when requested', () => { | ||
const css = transpile('@include mat.typography-hierarchy($theme, $back-compat: true)'); | ||
verifyFullSelector( | ||
css, | ||
'.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', | ||
); | ||
}); | ||
|
||
it('should use custom selector prefix', () => { | ||
const css = transpile(`@include mat.typography-hierarchy($theme, $selector: '.special')`); | ||
verifyFullSelector(css, '.mat-display-large, .special .mat-display-large, .special h1'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters