From a1105f2ef19b15eb3cc54891b737d4495b20b8a9 Mon Sep 17 00:00:00 2001 From: jorgep Date: Mon, 28 Oct 2024 13:24:46 +0100 Subject: [PATCH] feat: refs #7524 add front test --- .../pages/Tickets/TicketAdvance.spec.js | 116 ++++++++++++++++++ test/vitest/helper.js | 2 + 2 files changed, 118 insertions(+) create mode 100644 test/vitest/__tests__/pages/Tickets/TicketAdvance.spec.js diff --git a/test/vitest/__tests__/pages/Tickets/TicketAdvance.spec.js b/test/vitest/__tests__/pages/Tickets/TicketAdvance.spec.js new file mode 100644 index 000000000..6fae2788e --- /dev/null +++ b/test/vitest/__tests__/pages/Tickets/TicketAdvance.spec.js @@ -0,0 +1,116 @@ +import { vi, describe, expect, it, beforeAll, afterEach, beforeEach } from 'vitest'; +import { createWrapper, axios } from 'app/test/vitest/helper'; +import TicketAdvance from 'pages/Ticket/TicketAdvance.vue'; +import { Notify } from 'quasar'; + +describe('TicketAdvance', () => { + let wrapper; + let vm; + + beforeAll(() => { + vi.spyOn(axios, 'get').mockImplementation(() => ({ data: [] })); + wrapper = createWrapper(TicketAdvance); + vm = wrapper.vm; + // vm.vnTableRef.value = { reload: vi.fn(), params: {} }; + }); + beforeEach(() => { + Notify.create = vi.fn(); + }); + afterEach(() => { + vi.clearAllMocks(); + }); + + describe('requestComponentUpdate()', () => { + const mockTicket = { + futureId: 1, + futureClientFk: 1, + nickname: 'test', + futureAddressFk: 1, + futureAgencyModeFk: 1, + futureWarehouseFk: 1, + futureCompanyFk: 1, + landed: '2023-01-01', + zoneFk: 1, + }; + const mockParams = { + clientFk: 1, + nickname: 'test', + agencyModeFk: 1, + addressFk: 1, + zoneFk: 1, + warehouseFk: 1, + companyFk: 1, + landed: '2023-01-01', + isDeleted: false, + isWithoutNegatives: false, + newTicket: undefined, + keepPrice: true, + }; + const queryResult = 'tickets/1/componentUpdate'; + + it('should return query and params when ticket has no landed', async () => { + const mockLanded = { landed: '2023-01-01', zoneFk: 1 }; + vi.spyOn(vm, 'getLanded').mockResolvedValue(mockLanded); + + const { query, params } = await vm.requestComponentUpdate(mockTicket, false); + + expect(query).toBe(queryResult); + expect(params).toEqual(mockParams); + }); + + it('should return query and params when ticket has landed', async () => { + const { query, params } = await vm.requestComponentUpdate(mockTicket, false); + + expect(query).toBe(queryResult); + expect(params).toEqual(mockParams); + }); + }); + + describe('moveTicketsAdvance()', () => { + it('should move tickets and notify success', async () => { + const tickets = [ + { + id: 1, + futureId: 2, + futureShipped: '2023-01-01', + shipped: '2023-01-02', + workerFk: 1, + }, + { + id: 2, + futureId: 3, + futureShipped: '2023-01-01', + shipped: '2023-01-02', + workerFk: 1, + }, + ]; + vm.selectedTickets.value = tickets; + vi.spyOn(axios, 'post').mockResolvedValue({}); + await vm.moveTicketsAdvance(); + + expect(axios.post).toHaveBeenCalledOnce('Tickets/merge', { + tickets: [ + { + originId: 2, + destinationId: 1, + originShipped: '2023-01-01', + destinationShipped: '2023-01-02', + workerFk: 1, + }, + { + originId: 3, + destinationId: 2, + originShipped: '2023-01-01', + destinationShipped: '2023-01-02', + workerFk: 1, + }, + ], + }); + expect(Notify.create).toHaveBeenCalledWith({ + type: 'positive', + message: 'advanceTickets.moveTicketSuccess', + }); + expect(vm.selectedTickets).toEqual([]); + }); + }); +}); diff --git a/test/vitest/helper.js b/test/vitest/helper.js index e201535ec..4bfae5dc8 100644 --- a/test/vitest/helper.js +++ b/test/vitest/helper.js @@ -70,8 +70,10 @@ class FormDataMock { vi.fn(); } } + global.FormData = FormDataMock; global.URL = class URL {}; +global.Date.vnNew = () => new Date(Date.UTC(2001, 0, 1, 11)); export function createWrapper(component, options) { const defaultOptions = {