|
1 |
| -import {parse, Rule} from 'postcss'; |
2 |
| -import {compileString} from 'sass'; |
3 | 1 | import {runfiles} from '@bazel/runfiles';
|
4 | 2 | import * as path from 'path';
|
| 3 | +import {parse, Rule} from 'postcss'; |
| 4 | +import {compileString} from 'sass'; |
5 | 5 |
|
6 |
| -import {createLocalAngularPackageImporter} from '../../../../../tools/sass/local-sass-importer'; |
7 | 6 | import {pathToFileURL} from 'url';
|
| 7 | +import {createLocalAngularPackageImporter} from '../../../../../tools/sass/local-sass-importer'; |
8 | 8 |
|
9 | 9 | // Note: For Windows compatibility, we need to resolve the directory paths through runfiles
|
10 | 10 | // which are guaranteed to reside in the source tree.
|
@@ -128,4 +128,152 @@ describe('M3 theme', () => {
|
128 | 128 | /Calls to Angular Material theme mixins with an M3 theme must be wrapped in a selector/,
|
129 | 129 | );
|
130 | 130 | });
|
| 131 | + |
| 132 | + describe('theme extension API', () => { |
| 133 | + it('should allow overriding token value', () => { |
| 134 | + const css = transpile(` |
| 135 | + @use '../../tokens/token-utils'; |
| 136 | +
|
| 137 | + $theme: token-utils.extend-theme($theme, ((mdc, checkbox), (mat, checkbox)), ( |
| 138 | + selected-checkmark-color: magenta |
| 139 | + )); |
| 140 | +
|
| 141 | + html { |
| 142 | + @include mat.checkbox-theme($theme); |
| 143 | + } |
| 144 | + `); |
| 145 | + |
| 146 | + expect(css).toContain('--mdc-checkbox-selected-checkmark-color: magenta'); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should not override token value for other color variant', () => { |
| 150 | + const css = transpile(` |
| 151 | + @use '../../tokens/token-utils'; |
| 152 | +
|
| 153 | + $theme: token-utils.extend-theme($theme, ((mdc, checkbox), (mat, checkbox)), ( |
| 154 | + selected-checkmark-color: magenta |
| 155 | + )); |
| 156 | +
|
| 157 | + html { |
| 158 | + @include mat.checkbox-theme($theme, $color-variant: secondary); |
| 159 | + } |
| 160 | + `); |
| 161 | + |
| 162 | + expect(css).not.toContain('--mdc-checkbox-selected-checkmark-color: magenta'); |
| 163 | + }); |
| 164 | + |
| 165 | + it('should allow overriding specific color variant separately', () => { |
| 166 | + const css = transpile(` |
| 167 | + @use '../../tokens/token-utils'; |
| 168 | +
|
| 169 | + $theme: token-utils.extend-theme($theme, ((mdc, checkbox), (mat, checkbox)), ( |
| 170 | + selected-checkmark-color: magenta, |
| 171 | + tertiary: ( |
| 172 | + selected-checkmark-color: cyan, |
| 173 | + ), |
| 174 | + )); |
| 175 | +
|
| 176 | + html { |
| 177 | + @include mat.checkbox-theme($theme); |
| 178 | + } |
| 179 | +
|
| 180 | + .tertiary { |
| 181 | + @include mat.checkbox-color($theme, $color-variant: tertiary); |
| 182 | + } |
| 183 | + `); |
| 184 | + |
| 185 | + expect(css).toContain('--mdc-checkbox-selected-checkmark-color: magenta'); |
| 186 | + expect(css).toContain('--mdc-checkbox-selected-checkmark-color: cyan'); |
| 187 | + }); |
| 188 | + }); |
| 189 | + |
| 190 | + it('should error if used on M2 theme', () => { |
| 191 | + expect(() => |
| 192 | + transpile(` |
| 193 | + @use '../../tokens/token-utils'; |
| 194 | +
|
| 195 | + $theme: mat.m2-define-light-theme(mat.$m2-red-palette, mat.$m2-red-palette); |
| 196 | +
|
| 197 | + $theme: token-utils.extend-theme($theme, ((mdc, checkbox), (mat, checkbox)), ( |
| 198 | + selected-checkmark-color: magenta |
| 199 | + )); |
| 200 | +
|
| 201 | + html { |
| 202 | + @include mat.checkbox-theme($theme); |
| 203 | + } |
| 204 | + `), |
| 205 | + ).toThrowError(/The `extend-theme` functions are only supported for M3 themes/); |
| 206 | + }); |
| 207 | + |
| 208 | + it('should error on invalid namespace', () => { |
| 209 | + expect(() => |
| 210 | + transpile(` |
| 211 | + @use '../../tokens/token-utils'; |
| 212 | +
|
| 213 | + $theme: token-utils.extend-theme($theme, ((mdc, checkbox), (mat, checkbocks)), ( |
| 214 | + selected-checkmark-color: magenta |
| 215 | + )); |
| 216 | +
|
| 217 | + html { |
| 218 | + @include mat.checkbox-theme($theme); |
| 219 | + } |
| 220 | + `), |
| 221 | + ).toThrowError( |
| 222 | + /Error extending theme: Theme does not have tokes for namespace `\(mat, checkbocks\)`/, |
| 223 | + ); |
| 224 | + }); |
| 225 | + |
| 226 | + it('should error on ambiguous shorthand token name', () => { |
| 227 | + expect(() => |
| 228 | + transpile(` |
| 229 | + @use '../../tokens/token-utils'; |
| 230 | +
|
| 231 | + $theme: token-utils.extend-theme($theme, ((mdc, checkbox), (mdc, radio)), ( |
| 232 | + selected-checkmark-color: magenta |
| 233 | + )); |
| 234 | +
|
| 235 | + html { |
| 236 | + @include mat.checkbox-theme($theme); |
| 237 | + } |
| 238 | + `), |
| 239 | + ).toThrowError( |
| 240 | + /Error extending theme: Ambiguous token name `.*` exists in multiple namespaces: `\(mdc, checkbox\)` and `\(mdc, radio\)`/, |
| 241 | + ); |
| 242 | + }); |
| 243 | + |
| 244 | + it('should error on unknown variant', () => { |
| 245 | + expect(() => |
| 246 | + transpile(` |
| 247 | + @use '../../tokens/token-utils'; |
| 248 | +
|
| 249 | + $theme: token-utils.extend-theme($theme, ((mdc, checkbox), (mat, checkbox)), ( |
| 250 | + accent: ( |
| 251 | + selected-checkmark-color: magenta |
| 252 | + ) |
| 253 | + )); |
| 254 | +
|
| 255 | + html { |
| 256 | + @include mat.checkbox-theme($theme); |
| 257 | + } |
| 258 | + `), |
| 259 | + ).toThrowError( |
| 260 | + /Error extending theme: Unrecognized color variant `accent`. Allowed variants are: primary, secondary, tertiary, error, surface/, |
| 261 | + ); |
| 262 | + }); |
| 263 | + |
| 264 | + it('should error on unknown token', () => { |
| 265 | + expect(() => |
| 266 | + transpile(` |
| 267 | + @use '../../tokens/token-utils'; |
| 268 | +
|
| 269 | + $theme: token-utils.extend-theme($theme, ((mdc, checkbox), (mat, checkbox)), ( |
| 270 | + fake-token: red |
| 271 | + )); |
| 272 | +
|
| 273 | + html { |
| 274 | + @include mat.checkbox-theme($theme); |
| 275 | + } |
| 276 | + `), |
| 277 | + ).toThrowError(/Error extending theme: Unrecognized token `fake-token`. Allowed tokens are: /); |
| 278 | + }); |
131 | 279 | });
|
0 commit comments