|
| 1 | +import {compileString} from 'sass'; |
| 2 | +import {runfiles} from '@bazel/runfiles'; |
| 3 | +import * as path from 'path'; |
| 4 | + |
| 5 | +import {createLocalAngularPackageImporter} from '../../../../../tools/sass/local-sass-importer'; |
| 6 | +import {pathToFileURL} from 'url'; |
| 7 | + |
| 8 | +// Note: For Windows compatibility, we need to resolve the directory paths through runfiles |
| 9 | +// which are guaranteed to reside in the source tree. |
| 10 | +const testDir = path.join(runfiles.resolvePackageRelative('../_all-theme.scss'), '../tests'); |
| 11 | +const packagesDir = path.join(runfiles.resolveWorkspaceRelative('src/cdk/_index.scss'), '../..'); |
| 12 | + |
| 13 | +const localPackageSassImporter = createLocalAngularPackageImporter(packagesDir); |
| 14 | + |
| 15 | +const mdcSassImporter = { |
| 16 | + findFileUrl: (url: string) => { |
| 17 | + if (url.toString().startsWith('@material')) { |
| 18 | + return pathToFileURL(path.join(runfiles.resolveWorkspaceRelative('./node_modules'), url)); |
| 19 | + } |
| 20 | + return null; |
| 21 | + }, |
| 22 | +}; |
| 23 | + |
| 24 | +interface M2ThemeConfig { |
| 25 | + type: string; |
| 26 | + density: string; |
| 27 | + typography: string; |
| 28 | + primary: string; |
| 29 | + accent: string; |
| 30 | + warn: string; |
| 31 | +} |
| 32 | + |
| 33 | +interface M3ThemeConfig { |
| 34 | + type: string; |
| 35 | + density: string; |
| 36 | + brand: string; |
| 37 | + plain: string; |
| 38 | + bold: string; |
| 39 | + medium: string; |
| 40 | + regular: string; |
| 41 | + primary: string; |
| 42 | + secondary: string; |
| 43 | + tertiary: string; |
| 44 | +} |
| 45 | + |
| 46 | +function defineM2Theme(config: Partial<M2ThemeConfig> = {}) { |
| 47 | + const {type, density, typography, primary, accent, warn} = { |
| 48 | + type: 'light', |
| 49 | + density: '0', |
| 50 | + typography: 'mat.define-typography-config()', |
| 51 | + primary: 'mat.define-palette(mat.$blue-palette)', |
| 52 | + accent: 'mat.define-palette(mat.$green-palette)', |
| 53 | + warn: 'mat.define-palette(mat.$red-palette)', |
| 54 | + ...config, |
| 55 | + }; |
| 56 | + const collapseColor = primary === 'null' && accent === 'null' && warn === 'null'; |
| 57 | + return `mat.define-${type}-theme(( |
| 58 | + color: ${ |
| 59 | + collapseColor |
| 60 | + ? `( |
| 61 | + primary: ${primary}, |
| 62 | + accent: ${accent}, |
| 63 | + warn: ${warn}, |
| 64 | + )` |
| 65 | + : 'null' |
| 66 | + }, |
| 67 | + typography: ${typography}, |
| 68 | + density: ${density}, |
| 69 | + ))`; |
| 70 | +} |
| 71 | + |
| 72 | +function defineM3Theme(config: Partial<M3ThemeConfig> = {}) { |
| 73 | + const {type, density, brand, plain, bold, medium, regular, primary, secondary, tertiary} = { |
| 74 | + type: 'light', |
| 75 | + density: '0', |
| 76 | + brand: 'Google Sans', |
| 77 | + plain: 'Roboto', |
| 78 | + bold: '700', |
| 79 | + medium: '500', |
| 80 | + regular: '400', |
| 81 | + primary: 'matx.$m3-blue-palette', |
| 82 | + secondary: 'matx.$m3-green-palette', |
| 83 | + tertiary: 'matx.$m3-yellow-palette', |
| 84 | + ...config, |
| 85 | + }; |
| 86 | + return `matx.define-theme(( |
| 87 | + color: ( |
| 88 | + theme-type: ${type}, |
| 89 | + primary: ${primary}, |
| 90 | + secondary: ${secondary}, |
| 91 | + tertiary: ${tertiary}, |
| 92 | + ), |
| 93 | + typography: ( |
| 94 | + brand-family: ${brand}, |
| 95 | + plain-family: ${plain}, |
| 96 | + bold-weight: ${bold}, |
| 97 | + medium-weight: ${medium}, |
| 98 | + regular-weight: ${regular}, |
| 99 | + ), |
| 100 | + density: ( |
| 101 | + scale: ${density} |
| 102 | + ), |
| 103 | + ))`; |
| 104 | +} |
| 105 | + |
| 106 | +/** Transpiles given Sass content into CSS. */ |
| 107 | +function transpile(content: string) { |
| 108 | + return compileString( |
| 109 | + ` |
| 110 | + @use '../../../index' as mat; |
| 111 | + @use '../../../../material-experimental/index' as matx; |
| 112 | +
|
| 113 | + ${content} |
| 114 | + `, |
| 115 | + { |
| 116 | + loadPaths: [testDir], |
| 117 | + importers: [localPackageSassImporter, mdcSassImporter], |
| 118 | + }, |
| 119 | + ).css.toString(); |
| 120 | +} |
| 121 | + |
| 122 | +describe('theming inspection api', () => { |
| 123 | + describe('for m2 theme', () => { |
| 124 | + it('should get theme version', () => { |
| 125 | + expect( |
| 126 | + transpile(` |
| 127 | + $theme: ${defineM2Theme()}; |
| 128 | +
|
| 129 | + div { |
| 130 | + content: mat.private-get-theme-version($theme); |
| 131 | + } |
| 132 | + `), |
| 133 | + ).toMatch('content: 0;'); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + describe('for m3 theme', () => { |
| 138 | + it('should get theme version', () => { |
| 139 | + expect( |
| 140 | + transpile(` |
| 141 | + $theme: ${defineM3Theme()}; |
| 142 | +
|
| 143 | + div { |
| 144 | + content: mat.private-get-theme-version($theme); |
| 145 | + } |
| 146 | + `), |
| 147 | + ).toMatch('content: 1;'); |
| 148 | + }); |
| 149 | + }); |
| 150 | +}); |
0 commit comments