productDetails.spec.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* eslint-env jest */
  2. import Vue from 'vue'
  3. import ElementUI from 'element-ui'
  4. import path from 'path'
  5. import { load } from '@/ui-refactor/views/purchase/__tests__/loadComponent'
  6. jest.mock('@/utils/usersInfo', () => ({}))
  7. Vue.use(ElementUI)
  8. const ProductDetails = load(path.resolve('src/ui-refactor/components/ProductDetailsDialog.vue'))
  9. const product = Object.freeze({
  10. GOODS_NO: 'SKU-1', GOODS_NAME: 'Sample product', COVER: 'sample.png',
  11. SELL_PRICE: '38000', MARKET_PRICE: 42000, PRICE_PV: 1200, TAX_RATE: 7.5,
  12. taxAmount: 1234.5, STORE_NUMS: 0
  13. })
  14. const instances = []
  15. function mount() {
  16. const closed = jest.fn()
  17. const vm = new Vue({
  18. data: () => ({ visible: true, product }),
  19. render(h) {
  20. return h(ProductDetails, {
  21. props: { visible: this.visible, product: this.product, currency: 'NGN' },
  22. on: { 'update:visible': value => { this.visible = value; closed(value) } }
  23. })
  24. }
  25. }).$mount()
  26. document.body.appendChild(vm.$el)
  27. instances.push(vm)
  28. return { vm, closed }
  29. }
  30. async function settle() {
  31. await Vue.nextTick()
  32. jest.advanceTimersByTime(350)
  33. await Vue.nextTick()
  34. }
  35. beforeEach(() => jest.useFakeTimers())
  36. afterEach(() => {
  37. instances.splice(0).forEach(vm => { vm.$destroy(); vm.$el.remove() })
  38. jest.clearAllTimers()
  39. jest.useRealTimers()
  40. })
  41. it('preserves the original product fields and supplied tax amount with shared number formatting', async() => {
  42. const { vm } = mount()
  43. await settle()
  44. const values = Array.from(vm.$el.querySelectorAll('.el-descriptions-item__content'), node => node.textContent.trim())
  45. expect(values).toEqual(['SKU-1', 'NGN 38,000', 'NGN 42,000', '1,200', '0.075', 'NGN 1,234.5', '0'])
  46. expect(vm.$el.querySelector('.el-dialog__title').textContent).toBe('SKU-1')
  47. expect(vm.$el.querySelector('.el-descriptions__title').textContent).toBe('Sample product')
  48. expect(vm.$children[0].product).toBe(product)
  49. })
  50. it.each(['.el-dialog__headerbtn', '.el-dialog__footer button'])('updates the parent visible state once when closing with %s', async selector => {
  51. const { vm, closed } = mount()
  52. await settle()
  53. vm.$el.querySelector(selector).click()
  54. await settle()
  55. expect(vm.visible).toBe(false)
  56. expect(closed).toHaveBeenCalledTimes(1)
  57. expect(closed).toHaveBeenCalledWith(false)
  58. })
  59. it('shows the next product when the parent reopens the dialog', async() => {
  60. const { vm } = mount()
  61. vm.visible = false
  62. await settle()
  63. vm.product = Object.freeze({ ...product, GOODS_NO: 'SKU-2', GOODS_NAME: 'Next product', SELL_PRICE: 0, taxAmount: 0 })
  64. vm.visible = true
  65. await settle()
  66. expect(vm.$el.querySelector('.el-dialog__title').textContent).toBe('SKU-2')
  67. expect(vm.$el.querySelector('.el-descriptions__title').textContent).toBe('Next product')
  68. const values = Array.from(vm.$el.querySelectorAll('.el-descriptions-item__content'), node => node.textContent.trim())
  69. expect(values[1]).toBe('NGN 0')
  70. expect(values[5]).toBe('NGN 0')
  71. })