From c801573245b95f1dec8028214e4e88c52cd8edb3 Mon Sep 17 00:00:00 2001 From: jgallego Date: Tue, 10 Dec 2024 09:54:43 +0100 Subject: [PATCH] test: refs #6696 ticketSale --- .../pages/Tickets/TicketSale.spec.js | 86 +++++++++++++++++++ test/vitest/helper.js | 2 +- 2 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 test/vitest/__tests__/pages/Tickets/TicketSale.spec.js diff --git a/test/vitest/__tests__/pages/Tickets/TicketSale.spec.js b/test/vitest/__tests__/pages/Tickets/TicketSale.spec.js new file mode 100644 index 000000000..9bcf62e8d --- /dev/null +++ b/test/vitest/__tests__/pages/Tickets/TicketSale.spec.js @@ -0,0 +1,86 @@ +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); + // 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 componentName = 'TicketSale'; + + await vm.updateDiscount(sales, componentName, 10); + + expect(sales[0].discount).toBe(10); + expect(sales[1].discount).toBe(10); + }); + + it.only('should not update discounts if there are no changes', async () => { + const sales = [ + { id: 1, discount: 10 }, + { id: 2, discount: 10 }, + ]; + const componentName = 'TicketSale'; + + await vm.updateDiscount(sales, componentName); + + 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(); + }); + + it('should reset edit state after updating discounts', async () => { + const sales = [ + { id: 1, discount: 5 }, + { id: 2, discount: 7 }, + ]; + const componentName = 'TicketSale'; + + await vm.updateDiscount(sales, componentName); + + expect(vm.edit).toEqual({ + price: null, + discount: null, + sale: null, + sales: null, + oldQuantity: null, + }); + }); +}); diff --git a/test/vitest/helper.js b/test/vitest/helper.js index 4bfae5dc8..1a434e3e7 100644 --- a/test/vitest/helper.js +++ b/test/vitest/helper.js @@ -86,7 +86,7 @@ export function createWrapper(component, options) { }, }; - const mountOptions = Object.assign({}, defaultOptions); + const mountOptions = { ...defaultOptions }; if (options instanceof Object) { Object.assign(mountOptions, options);