cmvr-wms/ruoyi-ui-vue3/tests/unit/query-session.test.mjs

33 lines
2.6 KiB
JavaScript
Raw Normal View History

import test from 'node:test'
import assert from 'node:assert/strict'
import { runLatest, invalidateQuery } from '../../src/utils/query-session.js'
import { createEditSession } from '../../src/utils/edit-session.js'
const deferred=()=>{let resolve,reject;const promise=new Promise((a,b)=>{resolve=a;reject=b});return {promise,resolve,reject}}
test('only latest query may update result and loading, even with reversed responses',async()=>{
const owner={},a=deferred(),b=deferred(),events=[]
const old=runLatest(owner,'list',()=>a.promise,{success:v=>events.push(v),finish:()=>events.push('old-finished')})
const next=runLatest(owner,'list',()=>b.promise,{success:v=>events.push(v),finish:()=>events.push('new-finished')})
b.resolve('B');await next;a.resolve('A');assert.equal((await old).current,false)
assert.deepEqual(events,['B','new-finished'])
})
test('invalidating a pending selection prevents stale success and error handlers',async()=>{
const owner={},a=deferred();let called=false,signal
const result=runLatest(owner,'selection',options=>{signal=options.signal;return a.promise},{success:()=>called=true,error:()=>called=true,finish:()=>called=true})
invalidateQuery(owner,'selection');a.reject(new Error('late'));assert.equal((await result).current,false);assert.equal(signal.aborted,true);assert.equal(called,false)
})
test('a current failed query ends loading and permits retry',async()=>{
const owner={},events=[]
await runLatest(owner,'list',()=>Promise.reject(new Error('offline')),{start:()=>events.push('loading'),error:e=>events.push(e.message),finish:()=>events.push('finished')})
const retry=await runLatest(owner,'list',()=>Promise.resolve(2));assert.equal(retry.value,2);assert.deepEqual(events,['loading','offline','finished'])
})
test('editor only confirms actual changes and cancellation preserves input',async()=>{
let value={amount:2},prompts=0;const editor=createEditSession({getValue:()=>value,confirm:async()=>{prompts++;throw 'cancel'}})
assert.equal(await editor.checkLeave(),true);assert.equal(prompts,0);value.amount=3
assert.equal(await editor.checkLeave(),false);assert.equal(value.amount,3);assert.equal(editor.isDirty(),true)
editor.markClean();assert.equal(await editor.checkLeave(),true);assert.equal(prompts,1)
})
test('concurrent leave attempts share one confirmation',async()=>{
let value=1,prompts=0;const approval=deferred();const editor=createEditSession({getValue:()=>value,confirm:()=>{prompts++;return approval.promise}});value=2
const a=editor.checkLeave(),b=editor.checkLeave();approval.resolve();assert.deepEqual(await Promise.all([a,b]),[true,true]);assert.equal(prompts,1)
})