|
1 | 1 | import { resolve } from 'path' |
2 | 2 | import { describe, expect, test, vi } from 'vitest' |
3 | 3 | import { transformSymbol } from '@unplugin-vue-cssvars/utils' |
4 | | -import { getCSSFileRecursion, getVBindVariableListByPath } from '../process-css' |
| 4 | +import { getCSSFileRecursion, getVBindVariableListByPath, handleAlias } from '../process-css' |
5 | 5 | import type { ICSSFile } from '../../types' |
6 | | -// TODO update |
| 6 | + |
7 | 7 | describe('process css', () => { |
8 | 8 | test('getCSSFileRecursion: basic', () => { |
9 | 9 | const mockEvt = vi.fn() |
@@ -174,6 +174,38 @@ describe('process css', () => { |
174 | 174 | expect(res).matchSnapshot() |
175 | 175 | }) |
176 | 176 |
|
| 177 | + test('getVBindVariableListByPath: alias', () => { |
| 178 | + const mockCssFiles = new Map() |
| 179 | + const mockCSSFilesContent = { |
| 180 | + importer: new Set(), |
| 181 | + vBindCode: ['fooColor'], |
| 182 | + content: 'content foo color', |
| 183 | + lang: 'scss', |
| 184 | + } |
| 185 | + mockCssFiles.set(transformSymbol(resolve('/play/src/assets/test.css')), mockCSSFilesContent) |
| 186 | + const mockDescriptor = { |
| 187 | + styles: [{ |
| 188 | + content: '@import "@/assets/test";\n' |
| 189 | + + ' div {\n' |
| 190 | + + ' color: v-bind(color2);\n' |
| 191 | + + ' }', |
| 192 | + }], |
| 193 | + } |
| 194 | + const mockId = transformSymbol(resolve('/play/src/App.vue')) |
| 195 | + const res = getVBindVariableListByPath( |
| 196 | + mockDescriptor as any, |
| 197 | + mockId, |
| 198 | + mockCssFiles, |
| 199 | + false, |
| 200 | + { '@': '/play/src' }) |
| 201 | + expect(res.vbindVariableListByPath).toMatchObject(['fooColor']) |
| 202 | + expect([...res.injectCSSContent]).toMatchObject([{ |
| 203 | + content: 'content foo color', |
| 204 | + lang: 'scss', |
| 205 | + }]) |
| 206 | + expect(res).matchSnapshot() |
| 207 | + }) |
| 208 | + |
177 | 209 | test('createCSSModule: no file with lang', () => { |
178 | 210 | const mockCssFiles = new Map() |
179 | 211 | const mockCSSFilesContent = { |
@@ -241,3 +273,42 @@ describe('process css', () => { |
241 | 273 | expect(res.vbindVariableListByPath.length).toBe(0) |
242 | 274 | }) |
243 | 275 | }) |
| 276 | + |
| 277 | +describe('handleAlias function', () => { |
| 278 | + test('no alias and no idDirPath', () => { |
| 279 | + const path = 'path/to/some/file' |
| 280 | + expect(handleAlias(path)).toBe(path) |
| 281 | + }) |
| 282 | + |
| 283 | + test('no idDirPath & alias unmatched', () => { |
| 284 | + const path = 'path/to/some/file' |
| 285 | + const alias = { '@': 'alias-path/' } |
| 286 | + expect(handleAlias(path, alias)).toBe(path) |
| 287 | + }) |
| 288 | + |
| 289 | + test('no idDirPath & alias matched', () => { |
| 290 | + const path = '@/path/to/some/file' |
| 291 | + const alias = { '@': 'alias-path' } |
| 292 | + expect(handleAlias(path, alias)).toBe('alias-path/path/to/some/file') |
| 293 | + }) |
| 294 | + |
| 295 | + test('idDirPath & alias unmatched', () => { |
| 296 | + const path = 'path/to/some/file' |
| 297 | + const alias = { '@': 'alias-path' } |
| 298 | + const idDirPath = '/some/directory' |
| 299 | + expect(handleAlias(path, alias, idDirPath)).toBe('/some/directory/path/to/some/file') |
| 300 | + }) |
| 301 | + |
| 302 | + test('idDirPath & alias matched', () => { |
| 303 | + const path = '@/to/some/file' |
| 304 | + const alias = { '@': 'alias-path' } |
| 305 | + const idDirPath = '/some/directory' |
| 306 | + expect(handleAlias(path, alias, idDirPath)).toBe('alias-path/to/some/file') |
| 307 | + }) |
| 308 | + |
| 309 | + test('no alias and idDirPath', () => { |
| 310 | + const path = 'path/to/some/file' |
| 311 | + const idDirPath = '/some/directory' |
| 312 | + expect(handleAlias(path, undefined, idDirPath)).toBe('/some/directory/path/to/some/file') |
| 313 | + }) |
| 314 | +}) |
0 commit comments