productDetails.spec.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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(showSalesPrice) {
  16. const closed = jest.fn()
  17. const vm = new Vue({
  18. data: () => ({ visible: true, product, showSalesPrice }),
  19. render(h) {
  20. return h(ProductDetails, {
  21. props: { visible: this.visible, product: this.product, currency: 'NGN', showSalesPrice: this.showSalesPrice },
  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('hides sales price for anonymous callers and restores it for member callers', async() => {
  51. const { vm } = mount(false)
  52. await settle()
  53. expect(vm.$el.textContent).not.toContain('Sales Price')
  54. expect(vm.$el.textContent).not.toContain('NGN 38,000')
  55. expect(vm.$el.textContent).toContain('Market Price')
  56. expect(vm.$el.textContent).toContain('NGN 42,000')
  57. vm.showSalesPrice = true
  58. await settle()
  59. expect(vm.$el.textContent).toContain('Sales Price')
  60. expect(vm.$el.textContent).toContain('NGN 38,000')
  61. })
  62. it.each(['.el-dialog__headerbtn', '.el-dialog__footer button'])('updates the parent visible state once when closing with %s', async selector => {
  63. const { vm, closed } = mount()
  64. await settle()
  65. vm.$el.querySelector(selector).click()
  66. await settle()
  67. expect(vm.visible).toBe(false)
  68. expect(closed).toHaveBeenCalledTimes(1)
  69. expect(closed).toHaveBeenCalledWith(false)
  70. })
  71. it('shows the next product when the parent reopens the dialog', async() => {
  72. const { vm } = mount()
  73. vm.visible = false
  74. await settle()
  75. vm.product = Object.freeze({ ...product, GOODS_NO: 'SKU-2', GOODS_NAME: 'Next product', SELL_PRICE: 0, taxAmount: 0 })
  76. vm.visible = true
  77. await settle()
  78. expect(vm.$el.querySelector('.el-dialog__title').textContent).toBe('SKU-2')
  79. expect(vm.$el.querySelector('.el-descriptions__title').textContent).toBe('Next product')
  80. const values = Array.from(vm.$el.querySelectorAll('.el-descriptions-item__content'), node => node.textContent.trim())
  81. expect(values[1]).toBe('NGN 0')
  82. expect(values[5]).toBe('NGN 0')
  83. })