|
| 1 | +@import '../all-theme'; |
| 2 | + |
| 3 | +// A new way to configure themes has been introduced. This Sass file ensures that the theming |
| 4 | +// API is backwards compatible, so that the old pattern for configuring themes can be still |
| 5 | +// used. The Sass compilation of this file will fail if one of the tests/assertions fails. |
| 6 | + |
| 7 | +$primary: mat-palette($mat-blue); |
| 8 | +$accent: mat-palette($mat-grey); |
| 9 | +$warn: mat-palette($mat-indigo); |
| 10 | +$default-warn: mat-palette($mat-red); |
| 11 | + |
| 12 | +// Asserts that a given value matches the expected value. If not, an error is |
| 13 | +// thrown with the given error message. |
| 14 | +@mixin assert($value, $expected, $error-msg) { |
| 15 | + @if $value != $expected { |
| 16 | + @error 'Expected "#{$value}" to be "#{$expected}". #{$error-msg}'; |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +// Asserts that the specified color configuration is configured properly. |
| 21 | +@mixin assert-color-config($config, $primary, $accent, $warn, $is-dark) { |
| 22 | + $color: map_get($config, color); |
| 23 | + @include assert(map_get($color, primary), $primary, |
| 24 | + 'Primary palette does not match expected palette.'); |
| 25 | + @include assert(map_get($color, accent), $accent, |
| 26 | + 'Accent palette does not match expected palette.'); |
| 27 | + @include assert(map_get($color, warn), $warn, |
| 28 | + 'Warn palette does not match expected palette.'); |
| 29 | + @include assert(map_get($color, is-dark), $is-dark, |
| 30 | + 'Expected color config `is-dark` to be: #{$is-dark}'); |
| 31 | +} |
| 32 | + |
| 33 | +$legacy-light-theme: mat-light-theme($primary, $accent, $warn); |
| 34 | +$legacy-dark-theme: mat-dark-theme($primary, $accent, $warn); |
| 35 | +$legacy-light-theme-default-warn: mat-light-theme($primary, $accent); |
| 36 | +$legacy-dark-theme-default-warn: mat-dark-theme($primary, $accent); |
| 37 | + |
| 38 | +$new-api-light-theme: mat-light-theme(( |
| 39 | + color: (primary: $primary, accent: $accent, warn: $warn) |
| 40 | +)); |
| 41 | +$new-api-dark-theme: mat-dark-theme(( |
| 42 | + color: (primary: $primary, accent: $accent, warn: $warn) |
| 43 | +)); |
| 44 | +$new-api-light-theme-default-warn: mat-light-theme(( |
| 45 | + color: (primary: $primary, accent: $accent) |
| 46 | +)); |
| 47 | +$new-api-dark-theme-default-warn: mat-dark-theme(( |
| 48 | + color: (primary: $primary, accent: $accent) |
| 49 | +)); |
| 50 | + |
| 51 | +$light-theme-only-density: mat-light-theme((density: -3)); |
| 52 | +$dark-theme-only-density: mat-dark-theme((density: -3)); |
| 53 | + |
| 54 | +$typography-config: mat-typography-config(); |
| 55 | +$light-theme-only-typography: mat-light-theme((typography: $typography-config)); |
| 56 | +$dark-theme-only-typography: mat-dark-theme((typography: $typography-config)); |
| 57 | + |
| 58 | +// Test which ensures that constructed themes by default do not set configurations |
| 59 | +// for the individual parts of the theming system (color, density, typography). |
| 60 | +@mixin test-default-theming-system-configs() { |
| 61 | + $themes: ($legacy-light-theme, $legacy-dark-theme, $new-api-light-theme, $new-api-dark-theme); |
| 62 | + @each $theme in $themes { |
| 63 | + @include assert(map_has_key($theme, color), true, |
| 64 | + 'Expected color to be explicitly configured.'); |
| 65 | + @include assert(map_has_key($theme, typography), false, |
| 66 | + 'Expected typography to be not explicitly configured.'); |
| 67 | + @include assert(map_has_key($theme, density), false, |
| 68 | + 'Expected density to be not explicitly configured.'); |
| 69 | + } |
| 70 | + @include assert(map_has_key($light-theme-only-density, color), false, |
| 71 | + 'Expected color to be not explicitly configured.'); |
| 72 | + @include assert(map_has_key($dark-theme-only-density, color), false, |
| 73 | + 'Expected color to be not explicitly configured.'); |
| 74 | +} |
| 75 | + |
| 76 | +// Test which ensures that constructed themes have the proper color configurations |
| 77 | +// as specified in the `mat-light-theme` and `mat-dark-theme` functions. |
| 78 | +@mixin test-create-color-config() { |
| 79 | + @include assert-color-config($legacy-light-theme, $primary, $accent, $warn, false); |
| 80 | + @include assert-color-config($legacy-dark-theme, $primary, $accent, $warn, true); |
| 81 | + @include assert-color-config($legacy-light-theme-default-warn, $primary, $accent, |
| 82 | + $default-warn, false); |
| 83 | + @include assert-color-config($legacy-dark-theme-default-warn, $primary, $accent, |
| 84 | + $default-warn, true); |
| 85 | + @include assert-color-config($new-api-light-theme, $primary, $accent, $warn, false); |
| 86 | + @include assert-color-config($new-api-dark-theme, $primary, $accent, $warn, true); |
| 87 | + @include assert-color-config($new-api-light-theme-default-warn, $primary, $accent, |
| 88 | + $default-warn, false); |
| 89 | + @include assert-color-config($new-api-dark-theme-default-warn, $primary, $accent, |
| 90 | + $default-warn, true); |
| 91 | +} |
| 92 | + |
| 93 | +// Test which ensures that constructed themes pass through the specified |
| 94 | +// density and typography configurations. |
| 95 | +@mixin test-density-typography-config-pass-through() { |
| 96 | + @include assert(map_get($light-theme-only-density, density), -3, |
| 97 | + 'Expected density config to be included in theme.'); |
| 98 | + @include assert(map_get($dark-theme-only-density, density), -3, |
| 99 | + 'Expected density config to be included in theme.'); |
| 100 | + @include assert(map_get($light-theme-only-typography, typography), $typography-config, |
| 101 | + 'Expected typography config to be included in theme.'); |
| 102 | + @include assert(map_get($dark-theme-only-typography, typography), $typography-config, |
| 103 | + 'Expected typography config to be included in theme.'); |
| 104 | +} |
| 105 | + |
| 106 | +// Test which ensures that color configurations can be properly read from |
| 107 | +// theme objects passed to individual component theme mixins. |
| 108 | +@mixin test-get-color-config() { |
| 109 | + $color-config: map_get($legacy-light-theme, color); |
| 110 | + $no-color-light-theme: mat-light-theme((color: null)); |
| 111 | + $no-color-dark-theme: mat-dark-theme((color: null)); |
| 112 | + @include assert(mat-get-color-config($color-config), $color-config, |
| 113 | + 'Expected that passing a color config to a theme will work for backwards compatibility.'); |
| 114 | + @include assert(mat-get-color-config($legacy-light-theme), $color-config, |
| 115 | + 'Expected that a color config can be read from a theme object.'); |
| 116 | + @include assert(mat-get-color-config($light-theme-only-density), null, |
| 117 | + 'Expected that by default, no color configuration is used.'); |
| 118 | + @include assert(mat-get-color-config($no-color-light-theme), null, |
| 119 | + 'Expected that no color configuration can be explicitly specified.'); |
| 120 | + @include assert(mat-get-color-config($no-color-dark-theme), null, |
| 121 | + 'Expected that no color configuration can be explicitly specified.'); |
| 122 | +} |
| 123 | + |
| 124 | +// Test which ensures that density configurations can be properly read from |
| 125 | +// theme objects passed to individual component theme mixins. |
| 126 | +@mixin test-get-density-config() { |
| 127 | + $density-config: map_get($light-theme-only-density, density); |
| 128 | + $no-density-light-theme: mat-light-theme((density: null)); |
| 129 | + $no-density-dark-theme: mat-dark-theme((density: null)); |
| 130 | + @include assert(mat-get-density-config($light-theme-only-density), -3, |
| 131 | + 'Expected that a density config can be read from a theme object.'); |
| 132 | + @include assert(mat-get-density-config($light-theme-only-typography), 0, |
| 133 | + 'Expected that by default, the density system will be configured.'); |
| 134 | + @include assert(mat-get-density-config($no-density-light-theme), null, |
| 135 | + 'Expected that no density configuration can be explicitly specified.'); |
| 136 | + @include assert(mat-get-density-config($no-density-dark-theme), null, |
| 137 | + 'Expected that no density configuration can be explicitly specified.'); |
| 138 | +} |
| 139 | + |
| 140 | +// Test which ensures that typography configurations can be properly read from |
| 141 | +// theme objects passed to individual component theme mixins. |
| 142 | +@mixin test-get-typography-config() { |
| 143 | + $no-typography-light-theme: mat-light-theme((density: null)); |
| 144 | + $no-typography-dark-theme: mat-dark-theme((density: null)); |
| 145 | + @include assert(mat-get-typography-config($light-theme-only-typography), $typography-config, |
| 146 | + 'Expected that a typography config can be read from a theme object.'); |
| 147 | + @include assert(mat-get-typography-config($legacy-light-theme), null, |
| 148 | + 'Expected that by default, no typography configuration is used.'); |
| 149 | + @include assert(mat-get-typography-config($no-typography-light-theme), null, |
| 150 | + 'Expected that no typography configuration can be explicitly specified.'); |
| 151 | + @include assert(mat-get-typography-config($no-typography-dark-theme), null, |
| 152 | + 'Expected that no typography configuration can be explicitly specified.'); |
| 153 | +} |
| 154 | + |
| 155 | +// Test which ensures that all Angular Material theme mixins accept a theme |
| 156 | +// object without throwing any exception. |
| 157 | +@mixin test-theme-mixins-successful-compilation() { |
| 158 | + @include angular-material-theme($legacy-light-theme); |
| 159 | + @include angular-material-theme($legacy-light-theme-default-warn); |
| 160 | + @include angular-material-theme($legacy-dark-theme); |
| 161 | + @include angular-material-theme($legacy-dark-theme-default-warn); |
| 162 | + @include angular-material-theme($new-api-light-theme); |
| 163 | + @include angular-material-theme($new-api-light-theme-default-warn); |
| 164 | + @include angular-material-theme($new-api-dark-theme); |
| 165 | + @include angular-material-theme($new-api-dark-theme-default-warn); |
| 166 | + @include angular-material-theme($light-theme-only-density); |
| 167 | + @include angular-material-theme($dark-theme-only-density); |
| 168 | + @include angular-material-theme($light-theme-only-typography); |
| 169 | + @include angular-material-theme($dark-theme-only-typography); |
| 170 | +} |
| 171 | + |
| 172 | +// Include all tests. Sass will throw if one of the tests fails. |
| 173 | +@include test-create-color-config(); |
| 174 | +@include test-default-theming-system-configs(); |
| 175 | +@include test-density-typography-config-pass-through(); |
| 176 | +@include test-theme-mixins-successful-compilation(); |
| 177 | +@include test-get-color-config(); |
| 178 | +@include test-get-density-config(); |
| 179 | +@include test-get-typography-config(); |
0 commit comments