-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Expand file tree
/
Copy pathtoken-var-api.spec.ts
More file actions
151 lines (137 loc) · 5.23 KB
/
token-var-api.spec.ts
File metadata and controls
151 lines (137 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import {compileString} from 'sass';
import {runfiles} from '@bazel/runfiles';
import * as path from 'path';
import {createLocalAngularPackageImporter} from '../../../../../tools/sass/local-sass-importer.js';
const testDir = path.join(runfiles.resolvePackageRelative('../_all-theme.scss'), '../tests');
const packagesDir = path.join(runfiles.resolveWorkspaceRelative('src/cdk/_index.scss'), '../..');
const localPackageSassImporter = createLocalAngularPackageImporter(packagesDir);
function transpile(content: string): string {
return compileString(`@use '../../../index' as mat;\n${content}`, {
loadPaths: [testDir],
importers: [localPackageSassImporter],
}).css.toString();
}
// Imports the registry module directly (bypasses the public-API `show` filter)
// so tests can call registry-keys() without it being part of the public API.
function transpileRegistry(content: string): string {
return compileString(`@use '../../../core/tokens/token-registry';\n${content}`, {
loadPaths: [testDir],
importers: [localPackageSassImporter],
}).css.toString();
}
describe('mat.token-var()', () => {
describe('valid inputs', () => {
it('should generate CSS variable without fallback', () => {
expect(transpile(`div { color: mat.token-var(snack-bar, container-color); }`)).toContain(
'color: var(--mat-snack-bar-container-color)',
);
});
it('should generate CSS variable with a fallback value', () => {
expect(
transpile(`div { color: mat.token-var(snack-bar, container-color, white); }`),
).toContain('color: var(--mat-snack-bar-container-color, white)');
});
it('should support 0 as a fallback value', () => {
// $fallback != null (not truthy) so 0 must be preserved as a valid fallback.
expect(transpile(`div { opacity: mat.token-var(snack-bar, container-shape, 0); }`)).toContain(
'var(--mat-snack-bar-container-shape, 0)',
);
});
it('should support false as a fallback value', () => {
// $fallback != null (not truthy) so false must be preserved as a valid fallback.
// Note: must use a real CSS property (not a custom property) - Sass does not
// evaluate function calls inside custom property values (e.g. --x: ...).
expect(
transpile(`div { color: mat.token-var(snack-bar, container-shape, false); }`),
).toContain('var(--mat-snack-bar-container-shape, false)');
});
it('should work for a different component (button)', () => {
// After get-overrides strips the `button-` prefix, `button-filled-container-color`
// becomes `filled-container-color` as the token name.
expect(
transpile(`div { background: mat.token-var(button, filled-container-color); }`),
).toContain('background: var(--mat-button-filled-container-color)');
});
});
describe('invalid inputs', () => {
it('should throw for an unknown component name', () => {
expect(() =>
transpile(`div { color: mat.token-var(snackbar, container-color); }`),
).toThrowError(/Unknown component `snackbar`/);
});
it('should throw for an unknown token on a valid component', () => {
expect(() => transpile(`div { color: mat.token-var(snack-bar, typo-color); }`)).toThrowError(
/Unknown token `typo-color` for component `snack-bar`/,
);
});
});
// Smoke test: verify every expected component has a registry entry.
// Uses one Sass compilation (via registry-keys()) instead of 41 separate ones
// to keep the test suite within the default Bazel timeout.
describe('registry completeness', () => {
const components = [
'app',
'autocomplete',
'badge',
'bottom-sheet',
'button',
'button-toggle',
'card',
'checkbox',
'chip',
'datepicker',
'dialog',
'divider',
'expansion',
'fab',
'form-field',
'grid-list',
'icon',
'icon-button',
'list',
'menu',
'optgroup',
'option',
'paginator',
'progress-bar',
'progress-spinner',
'pseudo-checkbox',
'radio',
'ripple',
'select',
'sidenav',
'slide-toggle',
'slider',
'snack-bar',
'sort',
'stepper',
'table',
'tabs',
'timepicker',
'toolbar',
'tooltip',
'tree',
];
// One compilation shared by both tests below: generates a `--registered-{name}: 1`
// marker property for every component in the registry.
let registeredCss: string;
beforeAll(() => {
registeredCss = transpileRegistry(
':root { @each $c in token-registry.registry-keys() { --registered-#{$c}: 1; } }',
);
});
it('should not include input (it delegates all theming to form-field)', () => {
expect(registeredCss).not.toContain('--registered-input: 1');
});
it('should have registry entries for all expected components', () => {
// A missing component produces no `--registered-<name>: 1` property,
// failing the expect below with a clear context message.
const css = registeredCss;
for (const component of components) {
expect(css)
.withContext(`"${component}" is missing from the token registry`)
.toContain(`--registered-${component}: 1`);
}
});
});
});