// @vitest-environment jsdom import { flushPromises } from "@vue/test-utils"; import { beforeEach, describe, expect, it, vi } from "vitest"; const mocks = vi.hoisted(() => ({ fetchSelfServeData: vi.fn(), syncVehicleAnswer: vi.fn(), clearVehicleAnswers: vi.fn(), swalFire: vi.fn(), getLanes: vi.fn(), getVehicleTypeOptions: vi.fn(), loading: { value: false, __v_isRef: true }, lane: { value: { id: 3, name: "Lane 3" }, __v_isRef: true }, machineType: { value: null, __v_isRef: true }, session: { value: null, __v_isRef: true }, events: { value: [], __v_isRef: true }, questions: { value: [], __v_isRef: true }, answers: { value: {}, __v_isRef: true }, allowedServices: { value: [], __v_isRef: true }, configVersionId: { value: null, __v_isRef: true }, evaluationTrace: { value: null, __v_isRef: true }, visibleQuestions: { value: [], __v_isRef: true }, activeTasks: { value: [], __v_isRef: true }, currentQuestion: { value: null, __v_isRef: true }, allVisibleQuestionsAnswered: { value: false, __v_isRef: true }, machineAvailable: { value: true, __v_isRef: true }, allowed: { value: true, __v_isRef: true }, evaluateCondition: vi.fn(() => true), })); vi.mock("@/components/session/token/SessionUser.vue", () => ({ SessionUser: { user: { customer_number: { value: 12345 }, }, objects: { department_lanes: { get: { all: mocks.getLanes, }, }, vehicles: { columns: { type: { options: mocks.getVehicleTypeOptions, }, }, }, }, }, })); vi.mock("sweetalert2", () => ({ default: { fire: mocks.swalFire, }, })); vi.mock("@/composables/useSelfServeLogic", () => ({ useSelfServeLogic: () => ({ loading: mocks.loading, lane: mocks.lane, machineType: mocks.machineType, session: mocks.session, events: mocks.events, questions: mocks.questions, answers: mocks.answers, allowedServices: mocks.allowedServices, configVersionId: mocks.configVersionId, evaluationTrace: mocks.evaluationTrace, visibleQuestions: mocks.visibleQuestions, activeTasks: mocks.activeTasks, currentQuestion: mocks.currentQuestion, allVisibleQuestionsAnswered: mocks.allVisibleQuestionsAnswered, machineAvailable: mocks.machineAvailable, allowed: mocks.allowed, fetchSelfServeData: mocks.fetchSelfServeData, syncVehicleAnswer: mocks.syncVehicleAnswer, clearVehicleAnswers: mocks.clearVehicleAnswers, evaluateCondition: mocks.evaluateCondition, }), })); import { mountWithApp } from "./helpers/mountWithApp.js"; import SelfServeTryModal from "@/components/displays/department/tables/SelfServeTryModal.vue"; describe("SelfServeTryModal", () => { beforeEach(() => { mocks.fetchSelfServeData.mockReset(); mocks.syncVehicleAnswer.mockReset(); mocks.clearVehicleAnswers.mockReset(); mocks.swalFire.mockReset(); mocks.getLanes.mockReset(); mocks.getVehicleTypeOptions.mockReset(); mocks.evaluateCondition.mockReset(); mocks.getLanes.mockResolvedValue([ { id: 3, department: 9, name: "Lane 3" }, ]); mocks.getVehicleTypeOptions.mockResolvedValue([ { id: 0, name: "Ukendt" }, { id: 2, name: "Truck" }, { id: 3, name: "Van" }, ]); mocks.clearVehicleAnswers.mockResolvedValue({ deletedCount: 1 }); mocks.swalFire.mockResolvedValue({ isConfirmed: true }); mocks.loading.value = false; mocks.questions.value = []; mocks.visibleQuestions.value = []; mocks.answers.value = {}; mocks.currentQuestion.value = null; mocks.evaluateCondition.mockReturnValue(true); }); const mountModal = () => mountWithApp(SelfServeTryModal, { props: { departmentId: 9, laneId: 3, }, global: { stubs: { SelfServeQuestionCards: true, SelfServeTaskList: true, }, }, }); it("keeps clear answers enabled for valid lane/reg even when no visible answers exist", async () => { const wrapper = mountModal(); await flushPromises(); const clearButton = wrapper.get('[data-testid="self-serve-try-clear-answers"]'); expect(clearButton.attributes("disabled")).toBeUndefined(); }); it("allows selecting vehicle type and refreshes preview with the selected type", async () => { const wrapper = mountModal(); await flushPromises(); mocks.fetchSelfServeData.mockClear(); await wrapper.get('[data-testid="self-serve-try-vehicle-type"]').setValue("3"); await flushPromises(); expect(mocks.fetchSelfServeData).toHaveBeenCalledWith(9, 3, 3, "AB12345"); }); it("syncs answers with the selected vehicle type context", async () => { mocks.visibleQuestions.value = [{ id: 77, question: "Question 77" }]; mocks.currentQuestion.value = null; mocks.syncVehicleAnswer.mockResolvedValue({}); const wrapper = mountWithApp(SelfServeTryModal, { props: { departmentId: 9, laneId: 3, }, global: { stubs: { SelfServeQuestionCards: { props: ["visibleQuestions"], emits: ["answer-question"], template: "", }, SelfServeTaskList: true, }, }, }); await flushPromises(); await wrapper.get('[data-testid="self-serve-try-vehicle-type"]').setValue("3"); await flushPromises(); await wrapper.get("[data-testid='emit-card-answer']").trigger("click"); await flushPromises(); expect(mocks.syncVehicleAnswer).toHaveBeenCalledWith({ departmentId: 9, laneId: 3, customerNumber: 12345, reg: "AB12345", questionId: 77, value: true, vehicleTypeId: 3, }); }); it("does not duplicate the current question in the question cards list", async () => { mocks.currentQuestion.value = { id: 11, question: "Question 11" }; mocks.visibleQuestions.value = [ { id: 11, question: "Question 11" }, { id: 22, question: "Question 22" }, ]; const wrapper = mountWithApp(SelfServeTryModal, { props: { departmentId: 9, laneId: 3, }, global: { stubs: { SelfServeQuestionCards: { props: ["visibleQuestions"], template: "