Files
ReceiptGenerator/tests/confirmations.test.ts

38 lines
2.1 KiB
TypeScript

import {it,expect} from 'vitest'
import {ConfirmationService} from '../src/main/confirmations'
import type {ConfirmationRequest} from '../src/shared/types'
const options={title:'删除记录?',description:'删除本批次',variant:'danger' as const,confirmLabel:'删除记录'}
it('操作等待匹配确认,其他请求的回复不能批准当前操作',async()=>{
let request:ConfirmationRequest|undefined
const service=new ConfirmationService(value=>{request=value})
const answer=service.ask(options)
expect(service.hasPending).toBe(true);expect(request).toMatchObject(options)
expect(()=>service.answer('wrong-request',true)).toThrow('已失效')
expect(service.hasPending).toBe(true)
service.answer(request!.id,true)
expect(await answer).toBe(true);expect(service.hasPending).toBe(false)
expect(()=>service.answer(request!.id,true)).toThrow('已失效')
})
it('取消与界面失联均不批准操作',async()=>{
let request:ConfirmationRequest|undefined
const service=new ConfirmationService(value=>{request=value})
const cancelled=service.ask(options);service.answer(request!.id,false);expect(await cancelled).toBe(false)
const interrupted=service.ask(options);service.cancel();expect(await interrupted).toBe(false)
service.cancel();expect(service.hasPending).toBe(false)
})
it('重复请求不替换等待中的确认窗口',async()=>{
let request:ConfirmationRequest|undefined
const service=new ConfirmationService(value=>{request=value})
const first=service.ask(options),id=request!.id
await expect(service.ask({...options,title:'另一个请求'})).rejects.toThrow('当前确认')
expect(request!.id).toBe(id)
service.answer(id,true);expect(await first).toBe(true)
})
it('发送失败后可重新请求确认',async()=>{
let failed=true,request:ConfirmationRequest|undefined
const service=new ConfirmationService(value=>{if(failed)throw Error('window unavailable');request=value})
await expect(service.ask(options)).rejects.toThrow('window unavailable');expect(service.hasPending).toBe(false)
failed=false
const answer=service.ask(options);service.answer(request!.id,false);expect(await answer).toBe(false)
})