You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
2.6 KiB

import ImportInventory from '@/views/bookStore/components/importInventory.vue'
import { previewImport, commitImport } from '@/api/bookStore/storeList'
import { parseInventoryWorkbook } from '@/utils/inventoryImport'
jest.mock('@/api/bookStore/storeList', () => ({ previewImport: jest.fn(), commitImport: jest.fn() }))
jest.mock('@/utils/inventoryImport', () => ({ parseInventoryWorkbook: jest.fn() }))
function instance() {
const vm = { ...ImportInventory.data(), ...ImportInventory.methods, $Message: { success: jest.fn() }, $emit: jest.fn() }
Object.keys(ImportInventory.computed).forEach(key => Object.defineProperty(vm, key, { get: () => ImportInventory.computed[key].call(vm) }))
return vm
}
const valid = () => ({ token: 'test-token', errors: [], warnings: [], items: [], total: 1 })
describe('inventory import dialog', () => {
beforeEach(() => jest.clearAllMocks())
it('requires successful preview and explicit confirmation to commit', async() => {
const vm = instance()
await vm.commit()
expect(commitImport).not.toHaveBeenCalled()
vm.result = valid()
expect(vm.canCommit).toBe(false)
vm.confirmed = true
expect(vm.canCommit).toBe(true)
vm.result.errors = [{ message: 'error' }]
expect(vm.canCommit).toBe(false)
})
it('clears an old token when validation fails', async() => {
const vm = instance()
vm.workbook = {}; vm.result = valid(); vm.confirmed = true
parseInventoryWorkbook.mockReturnValue([{ no: '0000001' }])
previewImport.mockRejectedValueOnce(new Error('invalid workbook'))
await vm.preview()
expect(vm.result).toBe(null)
expect(vm.confirmed).toBe(false)
expect(vm.error).toBe('invalid workbook')
expect(vm.busy).toBe(false)
})
it('refreshes the inventory after a successful import', async() => {
const vm = instance()
vm.result = valid(); vm.confirmed = true; vm.visible = true
commitImport.mockResolvedValueOnce({ updated: 543, books_updated: 0 })
await vm.commit()
expect(commitImport).toHaveBeenCalledWith('test-token')
expect(vm.$emit).toHaveBeenCalledWith('refresh')
expect(vm.visible).toBe(false)
expect(vm.busy).toBe(false)
})
it('discards the token after a failed or uncertain commit and requires a fresh preview', async() => {
const vm = instance()
vm.result = valid(); vm.confirmed = true
commitImport.mockRejectedValueOnce(new Error('timeout'))
await vm.commit()
expect(vm.canCommit).toBe(false)
expect(vm.result).toBe(null)
expect(vm.error).toBe('timeout')
expect(vm.$emit).not.toHaveBeenCalled()
expect(vm.busy).toBe(false)
})
})