|
|
@@ -0,0 +1,231 @@
|
|
|
+import Vue from 'vue'
|
|
|
+import { createLocalVue, mount } from '@vue/test-utils'
|
|
|
+import ElementUI from 'element-ui'
|
|
|
+import axios from 'axios'
|
|
|
+import { getTenantId } from '@/utils/auth'
|
|
|
+import request from '@/utils/request'
|
|
|
+import Page from '@/views/businessManager/orderManager/receiptReconcile.vue'
|
|
|
+import { asyncRoutes } from '@/router'
|
|
|
+
|
|
|
+jest.mock('@/utils/request', () => jest.fn())
|
|
|
+jest.mock('@/utils/auth', () => ({ getTenantId: jest.fn() }))
|
|
|
+jest.mock('@/layout', () => ({}))
|
|
|
+
|
|
|
+const localVue = createLocalVue()
|
|
|
+localVue.use(ElementUI)
|
|
|
+const flush = () => new Promise(resolve => setTimeout(resolve, 0))
|
|
|
+const tenant = id => sessionStorage.setItem('tenantConfig', JSON.stringify({ tenantId: id, tenantName: '机构' + id }))
|
|
|
+const response = data => ({ code: 200, data })
|
|
|
+const result = (billDate, overrides = {}) => ({ billDate, tenantId: 1, checkedAt: '18:00', status: 'MATCHED', systemCount: 0, systemAmount: 0, thirdCount: 0, thirdAmount: 0, differenceCount: 0, channels: [], differences: [], ...overrides })
|
|
|
+const pending = () => {
|
|
|
+ let resolve, reject
|
|
|
+ const promise = new Promise((done, fail) => { resolve = done; reject = fail })
|
|
|
+ return { resolve, reject, promise }
|
|
|
+}
|
|
|
+let wrapper, bus
|
|
|
+function open() {
|
|
|
+ bus = new Vue()
|
|
|
+ wrapper = mount(Page, { localVue, mocks: { $bus: bus }, stubs: { 'el-date-picker': true, transition: false } })
|
|
|
+ return wrapper.vm
|
|
|
+}
|
|
|
+beforeEach(() => { request.mockReset(); getTenantId.mockReset().mockImplementation(jest.requireActual('@/utils/auth').getTenantId); tenant(1) })
|
|
|
+afterEach(() => { if (wrapper) wrapper.destroy(); wrapper = null; sessionStorage.clear() })
|
|
|
+
|
|
|
+test('menu group renders only the child route outlet and the child key resolves to the receipt page', async() => {
|
|
|
+ const createElement = jest.fn()
|
|
|
+ asyncRoutes.receiptReconcileGroup.render(createElement)
|
|
|
+ expect(createElement.mock.calls).toEqual([['router-view']])
|
|
|
+ expect((await asyncRoutes.receiptReconcile()).default).toBe(Page)
|
|
|
+})
|
|
|
+
|
|
|
+test('reads saved results without running, runs with only billDate and prevents duplicate submissions', async() => {
|
|
|
+ request.mockResolvedValueOnce(response(null))
|
|
|
+ const page = open()
|
|
|
+ await flush()
|
|
|
+ expect(wrapper.text()).toContain('尚未运行')
|
|
|
+ const deferred = pending()
|
|
|
+ request.mockReturnValueOnce(deferred.promise)
|
|
|
+ wrapper.findAll('button').wrappers.find(button => button.text() === '重新取账并核对').trigger('click')
|
|
|
+ page.load(true)
|
|
|
+ expect(request.mock.calls.map(([config]) => [config.method, config.url, config.params, config.data, config.headers])).toEqual([
|
|
|
+ ['get', '/api-web/receiptReconcile/result', { billDate: page.billDate }, undefined, undefined],
|
|
|
+ ['post', '/api-web/receiptReconcile/run', { billDate: page.billDate }, undefined, undefined]
|
|
|
+ ])
|
|
|
+ deferred.resolve(response(result(page.billDate)))
|
|
|
+ await flush()
|
|
|
+ expect(wrapper.text()).toContain('已对平')
|
|
|
+ expect(wrapper.text()).toContain('¥0.00')
|
|
|
+})
|
|
|
+
|
|
|
+test('incomplete totals, failed bills and unknown balances do not render as zero', async() => {
|
|
|
+ request.mockImplementation(({ params }) => Promise.resolve(response(result(params.billDate, { status: 'INCOMPLETE', thirdAmount: null, thirdCount: null, differenceCount: null,
|
|
|
+ channels: [{ channel: 'WECHAT', status: 'ERROR', message: '账单暂不可用', systemCount: 0, systemAmount: 0, thirdCount: 0, thirdAmount: 0, availableBalance: null, balanceTime: null }] }))))
|
|
|
+ open(); await flush()
|
|
|
+ expect(wrapper.find('.metrics').findAll('strong').wrappers.map(item => item.text())).toEqual(['¥0.00', '—', '—'])
|
|
|
+ const cells = wrapper.find('tbody tr').findAll('td')
|
|
|
+ expect(cells.at(0).text()).toContain('账单暂不可用')
|
|
|
+ expect(cells.at(2).text()).not.toMatch(/0/)
|
|
|
+ expect(cells.at(3).text()).not.toMatch(/0/)
|
|
|
+ expect(wrapper.text()).toContain('取账未完成')
|
|
|
+})
|
|
|
+
|
|
|
+test('channel drilldown matches either side; tenant switch clears open details and rejects stale requests', async() => {
|
|
|
+ const entry = { channel: 'HUIFU', orderNo: 'OLD-ORDER', amount: 12 }
|
|
|
+ const row = { type: 'CHANNEL', reason: '双方渠道不同', system: entry, third: { ...entry, channel: 'ALIPAY' } }
|
|
|
+ request.mockImplementationOnce(({ params }) => Promise.resolve(response(result(params.billDate, { status: 'DIFFERENT', differenceCount: 1, differences: [row], channels: [{ channel: 'ALIPAY', status: 'READY' }] }))))
|
|
|
+ const page = open(); await flush()
|
|
|
+ page.$refs.details.scrollIntoView = jest.fn()
|
|
|
+ wrapper.find('tbody button').trigger('click'); await Vue.nextTick()
|
|
|
+ expect(page.channel).toBe('ALIPAY')
|
|
|
+ wrapper.findAll('button').wrappers.find(button => button.text() === '详情').trigger('click'); await Vue.nextTick()
|
|
|
+ expect(wrapper.find('[role="dialog"]').text()).toContain('双方渠道不同')
|
|
|
+ const late = pending()
|
|
|
+ request.mockReturnValueOnce(late.promise)
|
|
|
+ tenant(2); bus.$emit('tenant-changed'); await Vue.nextTick()
|
|
|
+ expect(page.detail).toBeNull()
|
|
|
+ expect(wrapper.text()).not.toContain('OLD-ORDER')
|
|
|
+ request.mockResolvedValueOnce(response(null))
|
|
|
+ tenant(3); bus.$emit('tenant-changed'); await flush()
|
|
|
+ late.resolve(response(result(page.billDate, { tenantId: 2, differences: [row] }))); await flush()
|
|
|
+ expect(wrapper.text()).toContain('机构3')
|
|
|
+ expect(wrapper.text()).not.toContain('OLD-ORDER')
|
|
|
+ expect(page.result).toBeNull()
|
|
|
+})
|
|
|
+
|
|
|
+test('changed date discards late results; rejection and wrong-scope responses never look like an empty result', async() => {
|
|
|
+ const late = pending()
|
|
|
+ request.mockReturnValueOnce(late.promise).mockRejectedValueOnce(new Error('network'))
|
|
|
+ const page = open(), oldDate = page.billDate
|
|
|
+ await wrapper.setData({ billDate: '2025-01-01' }); await flush()
|
|
|
+ late.resolve(response(result(oldDate))); await flush()
|
|
|
+ expect(wrapper.text()).toContain('未能读取')
|
|
|
+ for (const invalid of [response(result(oldDate)), response(result(page.billDate, { tenantId: 2 })), { code: 100, data: null }]) {
|
|
|
+ request.mockResolvedValueOnce(invalid)
|
|
|
+ await page.load(false)
|
|
|
+ expect(page.result).toBeNull(); expect(page.notice).toContain('未能读取')
|
|
|
+ }
|
|
|
+})
|
|
|
+
|
|
|
+test('missing management tenant never sends a read or run', async() => {
|
|
|
+ sessionStorage.clear()
|
|
|
+ const page = open(); await page.load(true)
|
|
|
+ expect(request).not.toHaveBeenCalled(); expect(wrapper.text()).toContain('请先选择')
|
|
|
+})
|
|
|
+
|
|
|
+test('persistent tenant disagreement or corrupt storage blocks all reads and runs until explicit recovery', async() => {
|
|
|
+ getTenantId.mockReturnValue(2)
|
|
|
+ const page = open()
|
|
|
+ const confirm = () => wrapper.findAll('button').wrappers.find(button => button.text() === '重新确认机构').trigger('click')
|
|
|
+ expect(page.notice).toContain('机构信息不一致')
|
|
|
+ expect(page.tenantName).toBe('')
|
|
|
+ expect(wrapper.find('.el-button--primary').element.disabled).toBe(true)
|
|
|
+ await page.load(true); await confirm()
|
|
|
+ sessionStorage.setItem('tenantConfig', '{'); bus.$emit('tenant-changed')
|
|
|
+ expect(page.notice).toContain('机构信息不一致')
|
|
|
+ tenant(2); await page.load(true)
|
|
|
+ expect(request).not.toHaveBeenCalled()
|
|
|
+ request.mockResolvedValueOnce(response(null)); await confirm(); await flush()
|
|
|
+ expect(request.mock.calls.map(([config]) => config.method)).toEqual(['get'])
|
|
|
+ expect(wrapper.text()).toContain('机构2'); expect(page.notice).toContain('尚未运行')
|
|
|
+})
|
|
|
+
|
|
|
+test('run after an unannounced switch clears prior result, dialog, filters and tenant instead of running for either tenant', async() => {
|
|
|
+ const row = { type: 'SYSTEM_ONLY', system: { channel: 'ALIPAY', orderNo: 'OLD-ORDER' } }
|
|
|
+ request.mockImplementationOnce(({ params }) => Promise.resolve(response(result(params.billDate, { differences: [row] }))))
|
|
|
+ const page = open(); await flush()
|
|
|
+ await wrapper.setData({ detail: row, channel: 'ALIPAY', search: 'OLD' }); expect(wrapper.find('[role="dialog"]').exists()).toBe(true)
|
|
|
+ tenant(2); await page.load(true); await Vue.nextTick()
|
|
|
+ expect(request).toHaveBeenCalledTimes(1)
|
|
|
+ expect([page.result, page.detail, page.tenantId]).toEqual([null, null, null])
|
|
|
+ expect([page.busy, page.channel, page.search, page.tenantName]).toEqual(['', '', '', ''])
|
|
|
+ expect(wrapper.text()).not.toContain('OLD-ORDER'); expect(page.notice).toContain('机构信息不一致')
|
|
|
+})
|
|
|
+
|
|
|
+test.each(['resolve', 'reject'])('scope drift on %s clears busy without waiting for a tenant event', async settlement => {
|
|
|
+ const late = pending(); request.mockReturnValueOnce(late.promise)
|
|
|
+ const page = open(); getTenantId.mockReturnValue(2)
|
|
|
+ late[settlement](settlement === 'resolve' ? response(result(page.billDate)) : new Error('network')); await flush()
|
|
|
+ expect(page.result).toBeNull(); expect(page.busy).toBe(''); expect(page.notice).toContain('机构信息不一致')
|
|
|
+ expect(request).toHaveBeenCalledTimes(1)
|
|
|
+})
|
|
|
+
|
|
|
+test('A → inconsistent → B → A: late B rejection and first A success cannot replace the newest A result', async() => {
|
|
|
+ const first = pending(), middle = pending()
|
|
|
+ request.mockReturnValueOnce(first.promise).mockReturnValueOnce(middle.promise)
|
|
|
+ const page = open(); getTenantId.mockReturnValue(2); bus.$emit('tenant-changed')
|
|
|
+ expect(page.scopeBlocked).toBe(true)
|
|
|
+ tenant(2); bus.$emit('tenant-changed')
|
|
|
+ const latest = result(page.billDate, { systemAmount: 123 })
|
|
|
+ request.mockResolvedValueOnce(response(latest)); tenant(1); getTenantId.mockReturnValue(1); bus.$emit('tenant-changed'); await flush()
|
|
|
+ first.resolve(response(result(page.billDate))); middle.reject(new Error('old')); await flush()
|
|
|
+ expect(page.result).toEqual(latest); expect(page.error).toBe(''); expect(page.scopeBlocked).toBe(false)
|
|
|
+})
|
|
|
+
|
|
|
+test('axios rejects a queued POST before transport if the interceptor has supplied another tenant header', async() => {
|
|
|
+ request.mockResolvedValueOnce(response(null))
|
|
|
+ const page = open(); await flush()
|
|
|
+ const adapter = jest.fn()
|
|
|
+ request.mockImplementationOnce(config => axios.request({ ...config, adapter, headers: { tenantId: 2 } }))
|
|
|
+ await page.load(true)
|
|
|
+ expect(adapter).not.toHaveBeenCalled(); expect(page.notice).toContain('机构信息不一致'); expect(page.busy).toBe('')
|
|
|
+})
|
|
|
+
|
|
|
+test('paginates filtered groups locally, searches across pages and resets on filter or refresh', async() => {
|
|
|
+ const entry = index => ({ orderNo: 'ORDER-' + index, channel: index === 20 ? 'ALIPAY' : 'WECHAT' })
|
|
|
+ const differences = Array.from({ length: 20 }, (_, index) => ({ type: 'SYSTEM_ONLY', system: entry(index) }))
|
|
|
+ differences.push({ type: 'AMBIGUOUS', systemEntries: [entry(20), entry(21)] })
|
|
|
+ request.mockImplementation(({ params }) => Promise.resolve(response(result(params.billDate, { status: 'DIFFERENT', differenceCount: 21, differences }))))
|
|
|
+ const page = open(); await flush()
|
|
|
+ const rows = () => wrapper.findAll('table').at(1).findAll('tbody tr')
|
|
|
+ expect(rows()).toHaveLength(20); await wrapper.find('.btn-next').trigger('click'); await Vue.nextTick()
|
|
|
+ expect(rows()).toHaveLength(1); expect(rows().at(0).text()).toContain('匹配不唯一')
|
|
|
+ await wrapper.setData({ channel: 'ALIPAY' }); expect(page.pageNumber).toBe(1); expect(rows()).toHaveLength(1)
|
|
|
+ await wrapper.setData({ channel: '' }); await wrapper.find('.btn-next').trigger('click'); await Vue.nextTick()
|
|
|
+ await wrapper.setData({ search: 'ORDER-21' }); expect(page.pageNumber).toBe(1); expect(rows().at(0).text()).toContain('匹配不唯一')
|
|
|
+ expect(wrapper.find('.el-pagination').exists()).toBe(false); expect(request).toHaveBeenCalledTimes(1)
|
|
|
+ await wrapper.setData({ search: '' }); await wrapper.find('.btn-next').trigger('click'); await Vue.nextTick()
|
|
|
+ await page.load(false); await Vue.nextTick(); expect(page.pageNumber).toBe(1); expect(rows()).toHaveLength(20)
|
|
|
+})
|
|
|
+
|
|
|
+test('unknown channel summary gets a distinct status and filter, removed on a subsequent known-only result', async() => {
|
|
|
+ const unknown = { channel: 'UNKNOWN', status: 'UNKNOWN', systemCount: 1, systemAmount: 24, thirdCount: null, thirdAmount: null }
|
|
|
+ request.mockImplementationOnce(({ params }) => Promise.resolve(response(result(params.billDate, { channels: [unknown] }))))
|
|
|
+ const page = open(); await flush()
|
|
|
+ const row = wrapper.find('tbody tr')
|
|
|
+ expect(row.find('th').text()).toBe('未知渠道')
|
|
|
+ expect(row.find('td').text()).toBe('无法判定渠道')
|
|
|
+ expect(row.findAll('td').at(2).text()).not.toMatch(/0/)
|
|
|
+ const options = () => wrapper.findAll({ name: 'ElOption' }).wrappers.map(option => option.props('label'))
|
|
|
+ expect(options()).toContain('未知渠道')
|
|
|
+ await wrapper.setData({ channel: 'UNKNOWN' })
|
|
|
+ request.mockResolvedValueOnce(response(result(page.billDate)))
|
|
|
+ await page.load(false); await Vue.nextTick()
|
|
|
+ expect(options()).not.toContain('未知渠道'); expect(page.channel).toBe('')
|
|
|
+})
|
|
|
+
|
|
|
+test('unknown filter matches individual system records and ambiguous candidates without including known-only groups', async() => {
|
|
|
+ const unknown = { channel: 'UNKNOWN', orderNo: 'U1', amount: 24 }
|
|
|
+ const known = { channel: 'WECHAT', orderNo: 'W1', amount: 12 }
|
|
|
+ const differences = [{ type: 'SYSTEM_ONLY', system: unknown }, { type: 'AMBIGUOUS', systemEntries: [known, unknown] }, { type: 'SYSTEM_ONLY', system: known }]
|
|
|
+ request.mockImplementationOnce(({ params }) => Promise.resolve(response(result(params.billDate, { differences }))))
|
|
|
+ const page = open(); await flush()
|
|
|
+ const option = wrapper.findAll({ name: 'ElOption' }).wrappers.find(option => option.props('value') === 'UNKNOWN')
|
|
|
+ option.trigger('click'); await Vue.nextTick()
|
|
|
+ expect(page.filtered).toEqual(differences.slice(0, 2))
|
|
|
+ expect(wrapper.findAll('table').at(1).text()).toContain('未知渠道 / ¥24.00')
|
|
|
+})
|
|
|
+
|
|
|
+test('ambiguous candidates remain one group and every candidate is searchable and visible in details', async() => {
|
|
|
+ const item = (id, channel) => ({ channel, id, orderNo: 'ORDER-' + id, tradeNo: 'TRADE-' + id, amount: 10 })
|
|
|
+ const systemEntries = [item('S1', 'WECHAT'), item('S2', 'ALIPAY')]
|
|
|
+ const thirdEntries = [item('T1', 'HUIFU'), item('T2', 'ALIPAY')]
|
|
|
+ const group = { type: 'AMBIGUOUS', reason: '多条记录匹配', system: systemEntries[0], third: null, systemEntries, thirdEntries }
|
|
|
+ request.mockImplementationOnce(({ params }) => Promise.resolve(response(result(params.billDate, { status: 'DIFFERENT', differenceCount: 1, differences: [group] }))))
|
|
|
+ const page = open(); await flush()
|
|
|
+ expect(wrapper.find('.metrics').text()).toContain('1 组')
|
|
|
+ await wrapper.setData({ channel: 'ALIPAY', search: 'trade-t2' })
|
|
|
+ expect(page.filtered).toHaveLength(1)
|
|
|
+ wrapper.findAll('button').wrappers.find(button => button.text() === '详情').trigger('click'); await Vue.nextTick()
|
|
|
+ expect(wrapper.findAll('[role="dialog"] dl')).toHaveLength(4)
|
|
|
+ for (const entry of [...systemEntries, ...thirdEntries]) expect(wrapper.find('[role="dialog"]').text()).toContain(entry.tradeNo)
|
|
|
+})
|