28 lines
1.1 KiB
TypeScript
28 lines
1.1 KiB
TypeScript
|
|
/**
|
||
|
|
* Stub Nuxt auto-imports ที่ composable ใช้ — เบากว่าการลาก @nuxt/test-utils ทั้งชุดเข้ามา
|
||
|
|
* - useState: ตาม semantics ของ Nuxt คือ state แชร์กันตาม key (สร้างครั้งแรก ครั้งถัดไปได้ตัวเดิม)
|
||
|
|
* - computed: ของ vue ตรงๆ
|
||
|
|
* เคลียร์ state ทุก test เพื่อไม่ให้ theme ของ test ก่อนหน้ารั่วมา
|
||
|
|
*/
|
||
|
|
import { beforeEach } from 'vitest'
|
||
|
|
import { computed, ref, type Ref } from 'vue'
|
||
|
|
|
||
|
|
const sharedState = new Map<string, Ref<unknown>>()
|
||
|
|
|
||
|
|
;(globalThis as Record<string, unknown>).useState = <T>(key: string, init: () => T): Ref<T> => {
|
||
|
|
let entry = sharedState.get(key)
|
||
|
|
if (!entry) {
|
||
|
|
entry = ref(init()) as Ref<unknown>
|
||
|
|
sharedState.set(key, entry)
|
||
|
|
}
|
||
|
|
return entry as Ref<T>
|
||
|
|
}
|
||
|
|
|
||
|
|
;(globalThis as Record<string, unknown>).computed = computed
|
||
|
|
|
||
|
|
beforeEach(() => {
|
||
|
|
sharedState.clear()
|
||
|
|
document.documentElement.classList.remove('dark')
|
||
|
|
localStorage.clear()
|
||
|
|
})
|