|
| 1 | +import { mount, flushPromises } from "@vue/test-utils" |
| 2 | +import { expect, test, vi, beforeEach } from "vitest" |
| 3 | +import { createVuetify } from "vuetify" |
| 4 | +import * as components from "vuetify/components" |
| 5 | +import * as directives from "vuetify/directives" |
| 6 | +import { createStore } from "vuex" |
| 7 | +import { getField } from "vuex-map-fields" |
| 8 | +import TimelineReportTab from "@/incident/TimelineReportTab.vue" |
| 9 | + |
| 10 | +// Mock the filters |
| 11 | +vi.mock("@/filters", () => ({ |
| 12 | + formatToUTC: vi.fn((date) => date), |
| 13 | + formatToTimeZones: vi.fn((date) => date), |
| 14 | +})) |
| 15 | + |
| 16 | +const vuetify = createVuetify({ |
| 17 | + components, |
| 18 | + directives, |
| 19 | +}) |
| 20 | + |
| 21 | +global.ResizeObserver = require("resize-observer-polyfill") |
| 22 | + |
| 23 | +// Mock data |
| 24 | +const mockEvents = [ |
| 25 | + { |
| 26 | + id: 1, |
| 27 | + started_at: "2023-01-01T10:00:00Z", |
| 28 | + source: "Incident Participant", |
| 29 | + description: "John Doe created a new tactical report", |
| 30 | + details: { |
| 31 | + conditions: "Current conditions", |
| 32 | + actions: "Planned actions", |
| 33 | + needs: "Resource needs", |
| 34 | + }, |
| 35 | + }, |
| 36 | + { |
| 37 | + id: 2, |
| 38 | + started_at: "2023-01-01T11:00:00Z", |
| 39 | + source: "Incident Participant", |
| 40 | + description: "Jane Smith created a new tactical report", |
| 41 | + details: { |
| 42 | + conditions: "Updated conditions", |
| 43 | + actions: "New actions", |
| 44 | + needs: "Additional needs", |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + id: 3, |
| 49 | + started_at: "2023-01-01T12:00:00Z", |
| 50 | + source: "Other Source", |
| 51 | + description: "This is not a tactical report", |
| 52 | + details: {}, |
| 53 | + }, |
| 54 | +] |
| 55 | + |
| 56 | +// Create a proper Vuex store mock |
| 57 | +function createMockStore(events = mockEvents) { |
| 58 | + return createStore({ |
| 59 | + modules: { |
| 60 | + incident: { |
| 61 | + namespaced: true, |
| 62 | + state: { |
| 63 | + selected: { |
| 64 | + events: events, |
| 65 | + }, |
| 66 | + }, |
| 67 | + getters: { |
| 68 | + getField, // This is required for vuex-map-fields to work |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }) |
| 73 | +} |
| 74 | + |
| 75 | +// Helper function to create wrapper |
| 76 | +function createWrapper(events = mockEvents) { |
| 77 | + const store = createMockStore(events) |
| 78 | + return mount(TimelineReportTab, { |
| 79 | + global: { |
| 80 | + plugins: [vuetify, store], |
| 81 | + }, |
| 82 | + }) |
| 83 | +} |
| 84 | + |
| 85 | +beforeEach(() => { |
| 86 | + vi.clearAllMocks() |
| 87 | +}) |
| 88 | + |
| 89 | +test("mounts correctly and displays tactical reports", async () => { |
| 90 | + const wrapper = createWrapper() |
| 91 | + await flushPromises() |
| 92 | + |
| 93 | + const timelineItems = wrapper.findAllComponents({ name: "v-timeline-item" }) |
| 94 | + expect(timelineItems.length).toBe(3) // 2 tactical reports + 1 header item |
| 95 | +}) |
| 96 | + |
| 97 | +test("filters out non-tactical report events", async () => { |
| 98 | + const wrapper = createWrapper() |
| 99 | + await flushPromises() |
| 100 | + |
| 101 | + const descriptions = wrapper.findAll(".v-col").map((col) => col.text()) |
| 102 | + expect(descriptions.filter((d) => d.includes("tactical report")).length).toBe(2) |
| 103 | + expect(descriptions.filter((d) => d.includes("not a tactical report")).length).toBe(0) |
| 104 | +}) |
| 105 | + |
| 106 | +test("displays event details correctly", async () => { |
| 107 | + const wrapper = createWrapper() |
| 108 | + await flushPromises() |
| 109 | + |
| 110 | + const cards = wrapper.findAllComponents({ name: "v-card" }) |
| 111 | + const firstCard = cards[0] |
| 112 | + |
| 113 | + expect(firstCard.text()).toContain("Current conditions") |
| 114 | + expect(firstCard.text()).toContain("Planned actions") |
| 115 | + expect(firstCard.text()).toContain("Resource needs") |
| 116 | +}) |
| 117 | + |
| 118 | +test("sorts events chronologically", async () => { |
| 119 | + const wrapper = createWrapper([...mockEvents].reverse()) |
| 120 | + await flushPromises() |
| 121 | + |
| 122 | + const descriptions = wrapper.findAll(".v-col").map((col) => col.text()) |
| 123 | + const tacticalReports = descriptions.filter((d) => d.includes("tactical report")) |
| 124 | + |
| 125 | + expect(tacticalReports[0]).toContain("John Doe") |
| 126 | + expect(tacticalReports[1]).toContain("Jane Smith") |
| 127 | +}) |
| 128 | + |
| 129 | +test("displays UTC time notice", async () => { |
| 130 | + const wrapper = createWrapper() |
| 131 | + await flushPromises() |
| 132 | + |
| 133 | + const utcNotice = wrapper.find(".text-caption") |
| 134 | + expect(utcNotice.text()).toContain("UTC") |
| 135 | +}) |
| 136 | + |
| 137 | +test("handles empty events array", async () => { |
| 138 | + const wrapper = createWrapper([]) |
| 139 | + await flushPromises() |
| 140 | + |
| 141 | + const timelineItems = wrapper.findAllComponents({ name: "v-timeline-item" }) |
| 142 | + expect(timelineItems.length).toBe(1) // Just the header item |
| 143 | +}) |
| 144 | + |
| 145 | +test("displays correct icon for tactical reports", async () => { |
| 146 | + const wrapper = createWrapper() |
| 147 | + await flushPromises() |
| 148 | + |
| 149 | + const timelineItems = wrapper.findAllComponents({ name: "v-timeline-item" }) |
| 150 | + // Subtract 1 for the header item which doesn't have an icon |
| 151 | + const itemsWithIcons = timelineItems.length - 1 |
| 152 | + |
| 153 | + expect(itemsWithIcons).toBe(2) // Should have 2 tactical reports with icons |
| 154 | + |
| 155 | + const icons = wrapper.findAll(".v-icon") |
| 156 | + // Filter out any utility icons (like those in tooltips) |
| 157 | + const tacticalReportIcons = icons.filter((icon) => { |
| 158 | + const classes = icon.classes() |
| 159 | + return classes.includes("mdi-text-box-check") |
| 160 | + }) |
| 161 | + |
| 162 | + expect(tacticalReportIcons.length).toBeGreaterThan(0) |
| 163 | +}) |
0 commit comments