|
1 | | -import * as wasm from '@devup-ui/wasm' |
2 | | -import { |
3 | | - afterAll, |
4 | | - beforeAll, |
5 | | - describe, |
6 | | - expect, |
7 | | - it, |
8 | | - mock, |
9 | | - spyOn, |
10 | | -} from 'bun:test' |
11 | | - |
12 | | -import devupUICssLoader from '../css-loader' |
13 | | - |
14 | | -let getCssSpy: ReturnType<typeof spyOn> |
15 | | -let registerThemeSpy: ReturnType<typeof spyOn> |
16 | | - |
17 | | -beforeAll(() => { |
18 | | - getCssSpy = spyOn(wasm, 'getCss').mockReturnValue('get css') |
19 | | - registerThemeSpy = spyOn(wasm, 'registerTheme').mockReturnValue(undefined) |
20 | | -}) |
21 | | - |
22 | | -afterAll(() => { |
23 | | - getCssSpy.mockRestore() |
24 | | - registerThemeSpy.mockRestore() |
25 | | -}) |
26 | | - |
27 | | -describe('devupUICssLoader', () => { |
28 | | - it('should return css on no watch', () => { |
29 | | - const callback = mock() |
30 | | - const addContextDependency = mock() |
31 | | - devupUICssLoader.bind({ |
32 | | - callback, |
33 | | - addContextDependency, |
34 | | - resourcePath: 'devup-ui.css', |
35 | | - getOptions: () => ({ watch: false }), |
36 | | - } as any)(Buffer.from('data'), '') |
37 | | - expect(callback).toHaveBeenCalledWith( |
38 | | - null, |
39 | | - Buffer.from('data'), |
40 | | - '', |
41 | | - undefined, |
42 | | - ) |
43 | | - }) |
44 | | - |
45 | | - it('should return _compiler hit css on watch', () => { |
46 | | - const callback = mock() |
47 | | - const addContextDependency = mock() |
48 | | - devupUICssLoader.bind({ |
49 | | - callback, |
50 | | - addContextDependency, |
51 | | - getOptions: () => ({ watch: true }), |
52 | | - resourcePath: 'devup-ui.css', |
53 | | - } as any)(Buffer.from('data'), '') |
54 | | - expect(callback).toHaveBeenCalledWith(null, 'get css', '', undefined) |
55 | | - expect(getCssSpy).toHaveBeenCalledTimes(1) |
56 | | - getCssSpy.mockClear() |
57 | | - devupUICssLoader.bind({ |
58 | | - callback, |
59 | | - addContextDependency, |
60 | | - getOptions: () => ({ watch: true }), |
61 | | - resourcePath: 'devup-ui.css', |
62 | | - } as any)(Buffer.from('data'), '') |
63 | | - |
64 | | - expect(getCssSpy).toHaveBeenCalledTimes(1) |
65 | | - |
66 | | - getCssSpy.mockClear() |
67 | | - |
68 | | - devupUICssLoader.bind({ |
69 | | - callback, |
70 | | - addContextDependency, |
71 | | - getOptions: () => ({ watch: true }), |
72 | | - resourcePath: 'devup-ui-10.css', |
73 | | - } as any)(Buffer.from(''), '') |
74 | | - |
75 | | - expect(getCssSpy).toHaveBeenCalledTimes(1) |
76 | | - }) |
77 | | -}) |
| 1 | +import * as fs from 'node:fs' |
| 2 | + |
| 3 | +import * as wasm from '@devup-ui/wasm' |
| 4 | +import { |
| 5 | + afterAll, |
| 6 | + afterEach, |
| 7 | + beforeAll, |
| 8 | + describe, |
| 9 | + expect, |
| 10 | + it, |
| 11 | + mock, |
| 12 | + spyOn, |
| 13 | +} from 'bun:test' |
| 14 | + |
| 15 | +import devupUICssLoader, { resetInit } from '../css-loader' |
| 16 | + |
| 17 | +let getCssSpy: ReturnType<typeof spyOn> |
| 18 | +let registerThemeSpy: ReturnType<typeof spyOn> |
| 19 | +let importSheetSpy: ReturnType<typeof spyOn> |
| 20 | +let importClassMapSpy: ReturnType<typeof spyOn> |
| 21 | +let importFileMapSpy: ReturnType<typeof spyOn> |
| 22 | +let existsSyncSpy: ReturnType<typeof spyOn> |
| 23 | +let readFileSyncSpy: ReturnType<typeof spyOn> |
| 24 | + |
| 25 | +const defaultOptions = { |
| 26 | + watch: false, |
| 27 | + sheetFile: 'sheet.json', |
| 28 | + classMapFile: 'classMap.json', |
| 29 | + fileMapFile: 'fileMap.json', |
| 30 | + themeFile: 'devup.json', |
| 31 | + theme: {}, |
| 32 | + defaultSheet: {}, |
| 33 | + defaultClassMap: {}, |
| 34 | + defaultFileMap: {}, |
| 35 | +} |
| 36 | + |
| 37 | +beforeAll(() => { |
| 38 | + getCssSpy = spyOn(wasm, 'getCss').mockReturnValue('get css') |
| 39 | + registerThemeSpy = spyOn(wasm, 'registerTheme').mockReturnValue(undefined) |
| 40 | + importSheetSpy = spyOn(wasm, 'importSheet').mockReturnValue(undefined) |
| 41 | + importClassMapSpy = spyOn(wasm, 'importClassMap').mockReturnValue(undefined) |
| 42 | + importFileMapSpy = spyOn(wasm, 'importFileMap').mockReturnValue(undefined) |
| 43 | + existsSyncSpy = spyOn(fs, 'existsSync').mockReturnValue(false) |
| 44 | + readFileSyncSpy = spyOn(fs, 'readFileSync').mockReturnValue('{}') |
| 45 | +}) |
| 46 | + |
| 47 | +afterEach(() => { |
| 48 | + resetInit() |
| 49 | + getCssSpy.mockClear() |
| 50 | + registerThemeSpy.mockClear() |
| 51 | + importSheetSpy.mockClear() |
| 52 | + importClassMapSpy.mockClear() |
| 53 | + importFileMapSpy.mockClear() |
| 54 | + existsSyncSpy.mockClear() |
| 55 | + readFileSyncSpy.mockClear() |
| 56 | +}) |
| 57 | + |
| 58 | +afterAll(() => { |
| 59 | + getCssSpy.mockRestore() |
| 60 | + registerThemeSpy.mockRestore() |
| 61 | + importSheetSpy.mockRestore() |
| 62 | + importClassMapSpy.mockRestore() |
| 63 | + importFileMapSpy.mockRestore() |
| 64 | + existsSyncSpy.mockRestore() |
| 65 | + readFileSyncSpy.mockRestore() |
| 66 | +}) |
| 67 | + |
| 68 | +describe('devupUICssLoader', () => { |
| 69 | + it('should return css on no watch', () => { |
| 70 | + const callback = mock() |
| 71 | + const addContextDependency = mock() |
| 72 | + devupUICssLoader.bind({ |
| 73 | + callback, |
| 74 | + addContextDependency, |
| 75 | + resourcePath: 'devup-ui.css', |
| 76 | + getOptions: () => ({ ...defaultOptions, watch: false }), |
| 77 | + } as any)(Buffer.from('data'), '') |
| 78 | + expect(callback).toHaveBeenCalledWith( |
| 79 | + null, |
| 80 | + Buffer.from('data'), |
| 81 | + '', |
| 82 | + undefined, |
| 83 | + ) |
| 84 | + // Should initialize on first call |
| 85 | + expect(importFileMapSpy).toHaveBeenCalledTimes(1) |
| 86 | + expect(importClassMapSpy).toHaveBeenCalledTimes(1) |
| 87 | + expect(importSheetSpy).toHaveBeenCalledTimes(1) |
| 88 | + expect(registerThemeSpy).toHaveBeenCalledTimes(1) |
| 89 | + }) |
| 90 | + |
| 91 | + it('should return _compiler hit css on watch', () => { |
| 92 | + const callback = mock() |
| 93 | + const addContextDependency = mock() |
| 94 | + devupUICssLoader.bind({ |
| 95 | + callback, |
| 96 | + addContextDependency, |
| 97 | + getOptions: () => ({ ...defaultOptions, watch: true }), |
| 98 | + resourcePath: 'devup-ui.css', |
| 99 | + } as any)(Buffer.from('data'), '') |
| 100 | + expect(callback).toHaveBeenCalledTimes(1) |
| 101 | + expect(getCssSpy).toHaveBeenCalledTimes(1) |
| 102 | + getCssSpy.mockClear() |
| 103 | + devupUICssLoader.bind({ |
| 104 | + callback, |
| 105 | + addContextDependency, |
| 106 | + getOptions: () => ({ ...defaultOptions, watch: true }), |
| 107 | + resourcePath: 'devup-ui.css', |
| 108 | + } as any)(Buffer.from('data'), '') |
| 109 | + |
| 110 | + expect(getCssSpy).toHaveBeenCalledTimes(1) |
| 111 | + |
| 112 | + getCssSpy.mockClear() |
| 113 | + |
| 114 | + devupUICssLoader.bind({ |
| 115 | + callback, |
| 116 | + addContextDependency, |
| 117 | + getOptions: () => ({ ...defaultOptions, watch: true }), |
| 118 | + resourcePath: 'devup-ui-10.css', |
| 119 | + } as any)(Buffer.from(''), '') |
| 120 | + |
| 121 | + expect(getCssSpy).toHaveBeenCalledTimes(1) |
| 122 | + }) |
| 123 | + |
| 124 | + it('should read files from disk in watch mode when files exist', () => { |
| 125 | + existsSyncSpy.mockReturnValue(true) |
| 126 | + readFileSyncSpy.mockReturnValue(JSON.stringify({ theme: { color: 'red' } })) |
| 127 | + |
| 128 | + const callback = mock() |
| 129 | + const addContextDependency = mock() |
| 130 | + devupUICssLoader.bind({ |
| 131 | + callback, |
| 132 | + addContextDependency, |
| 133 | + getOptions: () => ({ ...defaultOptions, watch: true }), |
| 134 | + resourcePath: 'devup-ui.css', |
| 135 | + } as any)(Buffer.from('data'), '') |
| 136 | + |
| 137 | + // Should read files from disk |
| 138 | + expect(existsSyncSpy).toHaveBeenCalledTimes(4) |
| 139 | + expect(readFileSyncSpy).toHaveBeenCalledTimes(4) |
| 140 | + expect(importSheetSpy).toHaveBeenCalledTimes(1) |
| 141 | + expect(importClassMapSpy).toHaveBeenCalledTimes(1) |
| 142 | + expect(importFileMapSpy).toHaveBeenCalledTimes(1) |
| 143 | + expect(registerThemeSpy).toHaveBeenCalledWith({ color: 'red' }) |
| 144 | + }) |
| 145 | + |
| 146 | + it('should handle missing theme in devup.json', () => { |
| 147 | + existsSyncSpy.mockReturnValue(true) |
| 148 | + readFileSyncSpy.mockReturnValue(JSON.stringify({})) |
| 149 | + |
| 150 | + const callback = mock() |
| 151 | + const addContextDependency = mock() |
| 152 | + devupUICssLoader.bind({ |
| 153 | + callback, |
| 154 | + addContextDependency, |
| 155 | + getOptions: () => ({ ...defaultOptions, watch: true }), |
| 156 | + resourcePath: 'devup-ui.css', |
| 157 | + } as any)(Buffer.from('data'), '') |
| 158 | + |
| 159 | + // Should call registerTheme with empty object when theme is missing |
| 160 | + expect(registerThemeSpy).toHaveBeenCalledWith({}) |
| 161 | + }) |
| 162 | +}) |
0 commit comments