import { vi, describe, it, beforeEach, afterEach, expect } from 'vitest'; import { createWrapper } from 'app/test/vitest/helper'; import TicketSale from 'pages/Ticket/Card/TicketSale.vue'; import axios from 'axios'; describe('TicketSale ', () => { let wrapper; let vm; beforeEach(() => { vi.spyOn(axios, 'post').mockResolvedValue({}); vi.spyOn(axios, 'get').mockImplementation(() => ({ data: [ { id: 1, discount: 5 }, { id: 2, discount: 7 }, ], })); wrapper = createWrapper(TicketSale, { props: { ticket: { id: 123, name: 'Sample Ticket' }, isTicketEditable: true, }, global: { mocks: { store: { data: { id: 123, name: 'Sample Ticket' }, }, }, }, }); // wrapper.vm.route = { params: { id: 123 } }; vm = wrapper.vm; vi.spyOn(vm, 'isSalePrepared').mockResolvedValue(true); vm.edit = { discount: 10 }; // Set the discount in the edit state }); afterEach(() => { vi.clearAllMocks(); }); it('should update discounts for sales when changes exist', async () => { const sales = [ { id: 1, discount: 5 }, { id: 2, discount: 7 }, ]; const newDiscount = 10; const componentName = 'TicketSale'; await vm.updateDiscount(sales, componentName, newDiscount); expect(sales[0].discount).toBe(newDiscount); expect(sales[1].discount).toBe(newDiscount); expect(vm.edit).toEqual({ price: null, discount: null, sale: null, sales: null, oldQuantity: null, }); }); it.only('should not update discounts if there are no changes', async () => { const newDiscount = 10; const sales = [ { id: 1, itemFk: 123, discount: 10 }, { id: 2, itemFk: 456, discount: 10 }, ]; const componentName = 'TicketSale'; await vm.updateDiscount(sales, componentName, newDiscount); expect(axios.post).not.toHaveBeenCalled(); }); it('should not update discounts if there is no discount', async () => { const sales = [{ id: 1, discount: 10 }]; const componentName = 'TicketSale'; await vm.updateDiscount(sales, componentName, null); expect(axios.post).not.toHaveBeenCalled(); }); it('should not update discounts if any sale is not prepared', async () => { vi.spyOn(vm, 'isSalePrepared').mockImplementation((sale) => Promise.resolve(sale.id !== 1) ); const sales = [ { id: 1, discount: 5 }, { id: 2, discount: 7 }, ]; const componentName = 'TicketSale'; await vm.updateDiscount(sales, componentName); expect(axios.post).not.toHaveBeenCalled(); }); });