feat(client): show actionable customs count on sidebar
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import ts from 'typescript'
|
||||
import * as vue from 'vue'
|
||||
import * as pinia from 'pinia'
|
||||
const source = readFileSync(new URL('./customsPending.ts', import.meta.url), 'utf8')
|
||||
const code = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } }).outputText
|
||||
function deferred() { let resolve, reject; const promise = new Promise((a,b) => { resolve=a; reject=b }); return { promise, resolve, reject } }
|
||||
function harness(t, read) {
|
||||
const context=pinia.createPinia();pinia.setActivePinia(context);t.after(()=>pinia.disposePinia(context))
|
||||
const deps={vue,pinia,'../api/customs':{getMerchantCustomsPendingCount:read}}, exports={}
|
||||
new Function('require','exports',code)(id=>{assert.ok(id in deps,id);return deps[id]},exports)
|
||||
return exports.useCustomsPendingStore()
|
||||
}
|
||||
test('does not request without a permitted identity; preserves counts while refreshing and accepts zero',async t=>{
|
||||
const pending=deferred();let calls=0
|
||||
const store=harness(t,()=>++calls===1?Promise.resolve(2):pending.promise)
|
||||
await store.refresh();assert.equal(calls,0)
|
||||
store.setScope('customer-a');await store.refresh();assert.equal(store.count,2)
|
||||
const load=store.refresh();await store.refresh();assert.equal(calls,2);assert.equal(store.count,2)
|
||||
pending.resolve(0);await load;assert.equal(store.count,0);assert.equal(store.loading,false)
|
||||
})
|
||||
test('logout, permission removal and account switches discard old in-flight responses',async t=>{
|
||||
const pending=deferred();const store=harness(t,()=>pending.promise)
|
||||
store.setScope('a');const load=store.refresh();store.setScope('')
|
||||
pending.resolve(9);await load;assert.equal(store.count,null);assert.equal(store.error,'')
|
||||
store.setScope('b');assert.equal(store.count,null)
|
||||
})
|
||||
test('successful mutation can force a newer refresh without being overwritten by an older query',async t=>{
|
||||
const a=deferred(),b=deferred();let calls=0;const store=harness(t,()=>++calls===1?a.promise:b.promise)
|
||||
store.setScope('a');const first=store.refresh();const second=store.refresh(true)
|
||||
b.resolve(1);await second;a.resolve(2);await first;assert.equal(store.count,1)
|
||||
})
|
||||
test('failure is distinguishable from zero and retry restores authoritative count',async t=>{
|
||||
let fail=true;const store=harness(t,()=>fail?Promise.reject(new Error('offline')):Promise.resolve(3))
|
||||
store.setScope('a');await store.refresh();assert.equal(store.count,null);assert.notEqual(store.error,'')
|
||||
fail=false;await store.refresh();assert.equal(store.count,3);assert.equal(store.error,'')
|
||||
})
|
||||
@@ -0,0 +1,40 @@
|
||||
import { ref } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
import { getMerchantCustomsPendingCount } from '../api/customs'
|
||||
|
||||
export const useCustomsPendingStore = defineStore('client-customs-pending', () => {
|
||||
const count = ref<number | null>(null)
|
||||
const loading = ref(false)
|
||||
const error = ref('')
|
||||
let scope = ''
|
||||
let requestVersion = 0
|
||||
|
||||
function setScope(value: string) {
|
||||
if (scope === value) return
|
||||
scope = value
|
||||
requestVersion++
|
||||
count.value = null
|
||||
error.value = ''
|
||||
loading.value = false
|
||||
}
|
||||
|
||||
async function refresh(force = false) {
|
||||
if (!scope || (loading.value && !force)) return
|
||||
const request = ++requestVersion
|
||||
loading.value = true
|
||||
try {
|
||||
const result = await getMerchantCustomsPendingCount()
|
||||
if (request !== requestVersion) return
|
||||
count.value = result
|
||||
error.value = ''
|
||||
} catch {
|
||||
if (request !== requestVersion) return
|
||||
count.value = null
|
||||
error.value = '报关待办数量加载失败,请进入报关资料刷新'
|
||||
} finally {
|
||||
if (request === requestVersion) loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
return { count, loading, error, setScope, refresh }
|
||||
})
|
||||
Reference in New Issue
Block a user