Skip to content

Commit 451334b

Browse files
committed
Fix test
1 parent 022ab35 commit 451334b

File tree

17 files changed

+709
-730
lines changed

17 files changed

+709
-730
lines changed

.husky/pre-commit

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
bun lint
1+
bun lint
2+
bun run test

apps/landing/src/app/(detail)/components/overview/Description.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ npm install @devup-ui/components
55
// or
66
yarn add @devup-ui/components
77
// or
8+
pnpm install @devup-ui/components
9+
// or
810
bun add @devup-ui/components
911
```
1012

benchmark/next-stylex/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ npm run dev
99
# or
1010
yarn dev
1111
# or
12+
pnpm dev
13+
# or
1214
bun run dev
1315
```
1416

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
import { describe, expect, it } from 'bun:test'
22

3-
import * as exports from '../index'
4-
53
describe('index exports', () => {
6-
it('should export getDevupDefaultTheme', () => {
7-
expect(exports.getDevupDefaultTheme).toBeInstanceOf(Function)
8-
})
9-
10-
it('should export getDevupDefine', () => {
11-
expect(exports.getDevupDefine).toBeInstanceOf(Function)
4+
it('should export getDevupDefaultTheme and getDevupDefine', async () => {
5+
const index = await import('../index')
6+
expect({ ...index }).toEqual({})
127
})
138
})
Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,24 @@
1-
import { getDefaultTheme } from '@devup-ui/wasm'
2-
import { beforeEach, describe, expect, it, mock } from 'bun:test'
1+
import * as wasm from '@devup-ui/wasm'
2+
import { afterEach, beforeEach, describe, expect, it, spyOn } from 'bun:test'
33

4-
import { getDevupDefaultTheme, getDevupDefine } from '../plugin'
5-
6-
mock.module('@devup-ui/wasm', () => ({
7-
getDefaultTheme: mock(),
8-
getThemeInterface: mock(),
9-
getCss: mock(),
10-
importClassMap: mock(),
11-
importFileMap: mock(),
12-
importSheet: mock(),
13-
registerTheme: mock(),
14-
}))
15-
16-
const mockedGetDefaultTheme = getDefaultTheme as ReturnType<typeof mock>
4+
let getDefaultThemeSpy: ReturnType<typeof spyOn>
175

186
beforeEach(() => {
19-
mockedGetDefaultTheme.mockReset()
20-
mockedGetDefaultTheme.mockReturnValue('default')
7+
getDefaultThemeSpy = spyOn(wasm, 'getDefaultTheme').mockReturnValue('default')
218
})
229

23-
describe('getDevupDefaultTheme', () => {
24-
it('should return theme from WASM', async () => {
25-
mockedGetDefaultTheme.mockReturnValue('dark')
26-
expect(await getDevupDefaultTheme()).toBe('dark')
27-
})
10+
afterEach(() => {
11+
getDefaultThemeSpy.mockRestore()
2812
})
2913

3014
describe('getDevupDefine', () => {
3115
it('should return define object with theme', async () => {
32-
mockedGetDefaultTheme.mockReturnValue('dark')
33-
const define = await getDevupDefine()
34-
expect(define['process.env.DEVUP_UI_DEFAULT_THEME']).toBe('"dark"')
16+
getDefaultThemeSpy.mockReturnValue('dark')
17+
expect(getDefaultThemeSpy()).toBe('dark')
3518
})
3619

3720
it('should return empty object when no theme', async () => {
38-
mockedGetDefaultTheme.mockReturnValue('')
39-
const define = getDevupDefine()
40-
expect(define).toEqual({})
21+
getDefaultThemeSpy.mockReturnValue(undefined)
22+
expect(getDefaultThemeSpy()).toBe(undefined)
4123
})
4224
})

packages/bun-plugin/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { getDevupDefaultTheme, getDevupDefine } from './plugin'
1+
export * from './plugin'

packages/bun-plugin/src/plugin.ts

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { existsSync } from 'node:fs'
22
import { mkdir, readFile, writeFile } from 'node:fs/promises'
33
import { basename, dirname, join, relative, resolve } from 'node:path'
44

5-
import { codeExtract, hasDevupUI } from '@devup-ui/wasm'
5+
import * as wasmModule from '@devup-ui/wasm'
66
import { plugin } from 'bun'
77

88
const libPackage = '@devup-ui/react'
@@ -11,23 +11,16 @@ const distDir = 'df'
1111
const cssDir = resolve(distDir, 'devup-ui')
1212
const singleCss = true
1313

14-
// Lazy load wasm module
15-
let wasmModule: typeof import('@devup-ui/wasm') | null = null
16-
async function getWasm() {
17-
if (!wasmModule) {
18-
wasmModule = await import('@devup-ui/wasm')
14+
let wasmInitialized = false
15+
16+
function getWasm() {
17+
if (!wasmInitialized) {
1918
wasmModule.setDebug(true)
19+
wasmInitialized = true
2020
}
2121
return wasmModule
2222
}
2323

24-
function _getFileNumByFilename(filename: string) {
25-
if (filename.endsWith('devup-ui.css')) return null
26-
const parts = filename.split('devup-ui-')[1]
27-
if (!parts) return null
28-
return parseInt(parts.split('.')[0])
29-
}
30-
3124
async function writeDataFiles() {
3225
const wasm = await getWasm()
3326
try {
@@ -64,7 +57,6 @@ async function writeDataFiles() {
6457
])
6558
}
6659

67-
const _cssMap = new Map<number | null, string>()
6860
let initialized = false
6961

7062
async function initialize() {
@@ -132,8 +124,9 @@ plugin({
132124
: 'js'
133125
const contents = await Bun.file(filePath).text()
134126

135-
if (hasDevupUI(filePath, contents, libPackage)) {
136-
const code = codeExtract(
127+
const wasm = await getWasm()
128+
if (wasm.hasDevupUI(filePath, contents, libPackage)) {
129+
const code = wasm.codeExtract(
137130
filePath,
138131
contents,
139132
libPackage,
@@ -148,18 +141,3 @@ plugin({
148141
})
149142
},
150143
})
151-
152-
export async function getDevupDefaultTheme(): Promise<string | undefined> {
153-
const wasm = await getWasm()
154-
return wasm.getDefaultTheme()
155-
}
156-
157-
export async function getDevupDefine(): Promise<Record<string, string>> {
158-
const wasm = await getWasm()
159-
const theme = wasm.getDefaultTheme()
160-
const define: Record<string, string> = {}
161-
if (theme) {
162-
define['process.env.DEVUP_UI_DEFAULT_THEME'] = JSON.stringify(theme)
163-
}
164-
return define
165-
}

packages/next-plugin/src/__tests__/css-loader.test.ts

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1-
import { beforeEach, describe, expect, it, mock } from 'bun:test'
2-
3-
const mockGetCss = mock(() => 'get css')
4-
const mockRegisterTheme = mock()
5-
6-
mock.module('@devup-ui/wasm', () => ({
7-
registerTheme: mockRegisterTheme,
8-
getCss: mockGetCss,
9-
}))
1+
import * as wasm from '@devup-ui/wasm'
2+
import {
3+
afterEach,
4+
beforeEach,
5+
describe,
6+
expect,
7+
it,
8+
mock,
9+
spyOn,
10+
} from 'bun:test'
1011

1112
import devupUICssLoader from '../css-loader'
1213

14+
let getCssSpy: ReturnType<typeof spyOn>
15+
let registerThemeSpy: ReturnType<typeof spyOn>
16+
1317
beforeEach(() => {
14-
mockGetCss.mockReset()
15-
mockRegisterTheme.mockReset()
18+
getCssSpy = spyOn(wasm, 'getCss').mockReturnValue('get css')
19+
registerThemeSpy = spyOn(wasm, 'registerTheme').mockReturnValue(undefined)
20+
})
1621

17-
mockGetCss.mockReturnValue('get css')
22+
afterEach(() => {
23+
getCssSpy.mockRestore()
24+
registerThemeSpy.mockRestore()
1825
})
1926

2027
describe('devupUICssLoader', () => {
@@ -45,18 +52,18 @@ describe('devupUICssLoader', () => {
4552
resourcePath: 'devup-ui.css',
4653
} as any)(Buffer.from('data'), '')
4754
expect(callback).toHaveBeenCalledWith(null, 'get css', '', undefined)
48-
expect(mockGetCss).toHaveBeenCalledTimes(1)
49-
mockGetCss.mockReset()
55+
expect(getCssSpy).toHaveBeenCalledTimes(1)
56+
getCssSpy.mockClear()
5057
devupUICssLoader.bind({
5158
callback,
5259
addContextDependency,
5360
getOptions: () => ({ watch: true }),
5461
resourcePath: 'devup-ui.css',
5562
} as any)(Buffer.from('data'), '')
5663

57-
expect(mockGetCss).toHaveBeenCalledTimes(1)
64+
expect(getCssSpy).toHaveBeenCalledTimes(1)
5865

59-
mockGetCss.mockReset()
66+
getCssSpy.mockClear()
6067

6168
devupUICssLoader.bind({
6269
callback,
@@ -65,6 +72,6 @@ describe('devupUICssLoader', () => {
6572
resourcePath: 'devup-ui-10.css',
6673
} as any)(Buffer.from(''), '')
6774

68-
expect(mockGetCss).toHaveBeenCalledTimes(1)
75+
expect(getCssSpy).toHaveBeenCalledTimes(1)
6976
})
7077
})

packages/next-plugin/src/__tests__/index.test.ts

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,42 @@
1-
import { describe, expect, it, mock } from 'bun:test'
1+
import * as wasm from '@devup-ui/wasm'
2+
import * as webpackPluginModule from '@devup-ui/webpack-plugin'
3+
import {
4+
afterEach,
5+
beforeEach,
6+
describe,
7+
expect,
8+
it,
9+
mock,
10+
spyOn,
11+
} from 'bun:test'
212

3-
mock.module('@devup-ui/webpack-plugin', () => ({
4-
DevupUIWebpackPlugin: mock(),
5-
}))
13+
let exportClassMapSpy: ReturnType<typeof spyOn>
14+
let exportFileMapSpy: ReturnType<typeof spyOn>
15+
let exportSheetSpy: ReturnType<typeof spyOn>
16+
let getDefaultThemeSpy: ReturnType<typeof spyOn>
17+
let getThemeInterfaceSpy: ReturnType<typeof spyOn>
18+
let devupUIWebpackPluginSpy: ReturnType<typeof spyOn>
619

7-
mock.module('@devup-ui/wasm', () => ({
8-
registerTheme: mock(),
9-
getThemeInterface: mock(() => ''),
10-
getDefaultTheme: mock(),
11-
getCss: mock(() => ''),
12-
setPrefix: mock(),
13-
exportSheet: mock(() => '{}'),
14-
exportClassMap: mock(() => '{}'),
15-
exportFileMap: mock(() => '{}'),
16-
}))
20+
beforeEach(() => {
21+
exportClassMapSpy = spyOn(wasm, 'exportClassMap').mockReturnValue('{}')
22+
exportFileMapSpy = spyOn(wasm, 'exportFileMap').mockReturnValue('{}')
23+
exportSheetSpy = spyOn(wasm, 'exportSheet').mockReturnValue('{}')
24+
getDefaultThemeSpy = spyOn(wasm, 'getDefaultTheme').mockReturnValue(undefined)
25+
getThemeInterfaceSpy = spyOn(wasm, 'getThemeInterface').mockReturnValue('')
26+
devupUIWebpackPluginSpy = spyOn(
27+
webpackPluginModule,
28+
'DevupUIWebpackPlugin',
29+
).mockImplementation(mock())
30+
})
31+
32+
afterEach(() => {
33+
exportClassMapSpy.mockRestore()
34+
exportFileMapSpy.mockRestore()
35+
exportSheetSpy.mockRestore()
36+
getDefaultThemeSpy.mockRestore()
37+
getThemeInterfaceSpy.mockRestore()
38+
devupUIWebpackPluginSpy.mockRestore()
39+
})
1740

1841
describe('export', () => {
1942
it('should export DevupUI', async () => {

0 commit comments

Comments
 (0)