97 lines
3.2 KiB
TypeScript
97 lines
3.2 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import { runWebPushToggle } from '@/components/layout/web-push-control'
|
|
import type { WebPushConnectionStatus } from '@/lib/web-push-session'
|
|
|
|
describe('WebPushControl 비동기 토글 가드', () => {
|
|
it('runWebPushToggle_활성화중사용자전환또는언마운트발생_후속상태갱신을차단한다', async () => {
|
|
let current = true
|
|
let resolveEnable: (() => void) | null = null
|
|
const updates: Array<{ enabled?: boolean; status: string }> = []
|
|
|
|
const pending = runWebPushToggle({
|
|
userId: 'user-a',
|
|
shouldEnable: true,
|
|
isCurrent: () => current,
|
|
enable: () =>
|
|
new Promise<void>((resolve) => {
|
|
resolveEnable = resolve
|
|
}),
|
|
disable: async () => undefined,
|
|
getStatus: async (): Promise<WebPushConnectionStatus> => 'unsubscribed',
|
|
onUpdate: (update) => updates.push(update),
|
|
})
|
|
|
|
await new Promise((resolve) => {
|
|
setTimeout(resolve, 0)
|
|
})
|
|
current = false
|
|
resolveEnable?.()
|
|
await pending
|
|
|
|
expect(updates).toEqual([{ status: 'enabling' }])
|
|
})
|
|
|
|
it('runWebPushToggle_실패후재조회중취소시_오류상태를덮어쓰지않는다', async () => {
|
|
let current = true
|
|
let resolveStatus: ((value: WebPushConnectionStatus) => void) | null = null
|
|
const updates: Array<{ enabled?: boolean; status: string }> = []
|
|
|
|
const pending = runWebPushToggle({
|
|
userId: 'user-a',
|
|
shouldEnable: true,
|
|
isCurrent: () => current,
|
|
enable: async () => {
|
|
throw new Error('push failed')
|
|
},
|
|
disable: async () => undefined,
|
|
getStatus: () =>
|
|
new Promise<WebPushConnectionStatus>((resolve) => {
|
|
resolveStatus = resolve
|
|
}),
|
|
onUpdate: (update) => updates.push(update),
|
|
})
|
|
|
|
await new Promise((resolve) => {
|
|
setTimeout(resolve, 0)
|
|
})
|
|
current = false
|
|
resolveStatus?.('blocked')
|
|
await pending
|
|
|
|
expect(updates).toEqual([{ status: 'enabling' }])
|
|
})
|
|
|
|
it('runWebPushToggle_비활성화성공및실패복구_기대상태를순서대로반영한다', async () => {
|
|
const successUpdates: Array<{ enabled?: boolean; status: string }> = []
|
|
await runWebPushToggle({
|
|
userId: 'user-a',
|
|
shouldEnable: false,
|
|
isCurrent: () => true,
|
|
enable: async () => undefined,
|
|
disable: async () => undefined,
|
|
getStatus: async (): Promise<WebPushConnectionStatus> => 'unsubscribed',
|
|
onUpdate: (update) => successUpdates.push(update),
|
|
})
|
|
expect(successUpdates).toEqual([
|
|
{ status: 'disabling' },
|
|
{ enabled: false, status: 'unsubscribed' },
|
|
])
|
|
|
|
const recoveryUpdates: Array<{ enabled?: boolean; status: string }> = []
|
|
await runWebPushToggle({
|
|
userId: 'user-a',
|
|
shouldEnable: false,
|
|
isCurrent: () => true,
|
|
enable: async () => undefined,
|
|
disable: async () => {
|
|
throw new Error('unsubscribe failed')
|
|
},
|
|
getStatus: async (): Promise<WebPushConnectionStatus> => 'subscribed',
|
|
onUpdate: (update) => recoveryUpdates.push(update),
|
|
})
|
|
expect(recoveryUpdates).toEqual([
|
|
{ status: 'disabling' },
|
|
{ enabled: true, status: 'error' },
|
|
])
|
|
})
|
|
})
|