Skip to content

Commit 118f53c

Browse files
committed
Fix test
1 parent a7cbc79 commit 118f53c

2 files changed

Lines changed: 66 additions & 47 deletions

File tree

packages/react/src/stores/__tests__/theme-store.browser.test.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import {
1212
mock,
1313
} from 'bun:test'
1414

15-
import { createThemeStore } from '../theme-store'
16-
1715
beforeAll(() => {
1816
document.documentElement.removeAttribute('data-theme')
1917
})
@@ -31,7 +29,10 @@ describe('themeStore', () => {
3129
document.documentElement.removeAttribute('data-theme')
3230
})
3331

34-
it('should return themeStore object for browser', () => {
32+
it('should return themeStore object for browser', async () => {
33+
const modulePath = require.resolve('../theme-store')
34+
delete require.cache[modulePath]
35+
const { createThemeStore } = await import('../theme-store')
3536
const themeStore = createThemeStore()
3637
expect(themeStore).toBeDefined()
3738
expect(themeStore.get).toEqual(expect.any(Function))
@@ -46,7 +47,10 @@ describe('themeStore', () => {
4647
expect(themeStore.set('dark' as any)).toBeUndefined()
4748
})
4849

49-
it('should call subscriber when theme changes via set', () => {
50+
it('should call subscriber when theme changes via set', async () => {
51+
const modulePath = require.resolve('../theme-store')
52+
delete require.cache[modulePath]
53+
const { createThemeStore } = await import('../theme-store')
5054
const themeStore = createThemeStore()
5155
const callback = mock()
5256

@@ -61,7 +65,10 @@ describe('themeStore', () => {
6165
expect(themeStore.get()).toBe('light' as any)
6266
})
6367

64-
it('should unsubscribe correctly', () => {
68+
it('should unsubscribe correctly', async () => {
69+
const modulePath = require.resolve('../theme-store')
70+
delete require.cache[modulePath]
71+
const { createThemeStore } = await import('../theme-store')
6572
const themeStore = createThemeStore()
6673
const callback = mock()
6774

@@ -77,9 +84,12 @@ describe('themeStore', () => {
7784
expect(callback).toHaveBeenCalledTimes(1)
7885
})
7986

80-
it('should read initial theme from data-theme attribute', () => {
87+
it('should read initial theme from data-theme attribute', async () => {
8188
document.documentElement.setAttribute('data-theme', 'dark')
8289

90+
const modulePath = require.resolve('../theme-store')
91+
delete require.cache[modulePath]
92+
const { createThemeStore } = await import('../theme-store')
8393
const themeStore = createThemeStore()
8494
const callback = mock()
8595

@@ -90,6 +100,9 @@ describe('themeStore', () => {
90100
})
91101

92102
it('should update theme when data-theme attribute changes via MutationObserver', async () => {
103+
const modulePath = require.resolve('../theme-store')
104+
delete require.cache[modulePath]
105+
const { createThemeStore } = await import('../theme-store')
93106
const themeStore = createThemeStore()
94107
const callback = mock()
95108

@@ -106,7 +119,10 @@ describe('themeStore', () => {
106119
expect(themeStore.get()).toBe('dark' as any)
107120
})
108121

109-
it('should handle multiple subscribers', () => {
122+
it('should handle multiple subscribers', async () => {
123+
const modulePath = require.resolve('../theme-store')
124+
delete require.cache[modulePath]
125+
const { createThemeStore } = await import('../theme-store')
110126
const themeStore = createThemeStore()
111127
const callback1 = mock()
112128
const callback2 = mock()
@@ -121,6 +137,9 @@ describe('themeStore', () => {
121137
})
122138

123139
it('should filter mutations by type and target', async () => {
140+
const modulePath = require.resolve('../theme-store')
141+
delete require.cache[modulePath]
142+
const { createThemeStore } = await import('../theme-store')
124143
const themeStore = createThemeStore()
125144
const callback = mock()
126145

packages/react/src/stores/__tests__/theme-store-ssr.test.ts renamed to packages/react/src/stores/__tests__/theme-store.test.ts

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
1-
import { describe, expect, it } from 'bun:test'
2-
3-
// This test file runs in isolation to test the SSR code path
4-
// We need to mock window before importing the module
5-
6-
describe('themeStore SSR', () => {
7-
it('should return SSR-safe store when window is undefined', async () => {
8-
// Save original window
9-
const originalWindow = globalThis.window
10-
11-
// Remove window to simulate SSR
12-
// @ts-expect-error - Temporarily remove window for SSR test
13-
delete globalThis.window
14-
15-
// Clear module cache to force fresh import
16-
const modulePath = require.resolve('../theme-store')
17-
delete require.cache[modulePath]
18-
19-
try {
20-
// Import with window undefined
21-
const { createThemeStore } = await import('../theme-store')
22-
const themeStore = createThemeStore()
23-
24-
// Test SSR store behavior
25-
expect(themeStore).toBeDefined()
26-
expect(themeStore.get()).toBeNull()
27-
expect(themeStore.set('dark' as any)).toBeUndefined()
28-
29-
const unsubscribe = themeStore.subscribe(() => {})
30-
expect(typeof unsubscribe).toBe('function')
31-
32-
// The unsubscribe should return a no-op function
33-
const innerUnsubscribe = unsubscribe()
34-
expect(innerUnsubscribe).toBeUndefined()
35-
} finally {
36-
// Restore window
37-
globalThis.window = originalWindow
38-
}
39-
})
40-
})
1+
import { describe, expect, it } from 'bun:test'
2+
3+
// This test file runs in isolation to test the SSR code path
4+
// We need to mock window before importing the module
5+
6+
describe('themeStore SSR', () => {
7+
it('should return SSR-safe store when window is undefined', async () => {
8+
// Save original window
9+
const originalWindow = globalThis.window
10+
11+
// Remove window to simulate SSR
12+
// @ts-expect-error - Temporarily remove window for SSR test
13+
delete globalThis.window
14+
15+
// Clear module cache to force fresh import
16+
// const modulePath = require.resolve('../theme-store')
17+
// delete require.cache[modulePath]
18+
19+
try {
20+
// Import with window undefined
21+
const { createThemeStore } = await import('../theme-store')
22+
const themeStore = createThemeStore()
23+
24+
// Test SSR store behavior
25+
expect(themeStore).toBeDefined()
26+
expect(themeStore.get()).toBeNull()
27+
expect(themeStore.set('dark' as any)).toBeUndefined()
28+
29+
const unsubscribe = themeStore.subscribe(() => {})
30+
expect(typeof unsubscribe).toBe('function')
31+
32+
// The unsubscribe should return a no-op function
33+
const innerUnsubscribe = unsubscribe()
34+
expect(innerUnsubscribe).toBeUndefined()
35+
} finally {
36+
// Restore window
37+
globalThis.window = originalWindow
38+
}
39+
})
40+
})

0 commit comments

Comments
 (0)