uptime-kuma/test/component/MonitorList.spec.js

125 lines
3.9 KiB
JavaScript
Raw Normal View History

2024-11-20 13:48:13 +00:00
import { describe, it, expect, beforeEach, vi } from "vitest";
import { mount } from "@vue/test-utils";
import MonitorList from "../../src/components/MonitorList.vue";
// Mock child components
vi.mock("../../src/components/MonitorListItem.vue", {
default: {
name: "MonitorListItem",
template: "<div class=\"monitor-list-item\"></div>"
}
});
2024-11-20 13:58:15 +00:00
vi.mock("../../src/components/Confirm.vue", {
default: {
name: "Confirm",
template: "<div class=\"confirm-dialog\"></div>"
}
});
vi.mock("../../src/components/MonitorListFilter.vue", {
default: {
name: "MonitorListFilter",
template: "<div class=\"monitor-list-filter\"></div>"
}
});
2024-11-20 13:48:13 +00:00
describe("MonitorList.vue", () => {
let wrapper;
2024-11-20 13:58:15 +00:00
const mockMonitors = {
1: {
2024-11-20 13:48:13 +00:00
id: 1,
name: "Test Monitor 1",
type: "http",
status: "up",
2024-11-20 13:58:15 +00:00
active: true,
interval: 60,
parent: null
2024-11-20 13:48:13 +00:00
},
2024-11-20 13:58:15 +00:00
2: {
2024-11-20 13:48:13 +00:00
id: 2,
name: "Test Monitor 2",
type: "ping",
status: "down",
2024-11-20 13:58:15 +00:00
active: false,
interval: 60,
parent: null
2024-11-20 13:48:13 +00:00
}
2024-11-20 13:58:15 +00:00
};
const mockRouter = {
push: vi.fn()
};
2024-11-20 13:48:13 +00:00
beforeEach(() => {
wrapper = mount(MonitorList, {
props: {
2024-11-20 13:58:15 +00:00
scrollbar: true
2024-11-20 13:48:13 +00:00
},
global: {
2024-11-20 13:58:15 +00:00
mocks: {
$t: (key) => key, // Mock translation function
$router: mockRouter,
$root: {
monitorList: mockMonitors
}
},
provide: {
socket: {
emit: vi.fn()
}
},
2024-11-20 13:48:13 +00:00
stubs: {
2024-11-20 13:58:15 +00:00
MonitorListItem: {
name: "MonitorListItem",
template: "<div class='monitor-list-item' :class='{ active: active }' @click='$emit(\"click\")'><slot></slot></div>",
props: ["active"]
},
Confirm: true,
MonitorListFilter: true,
"font-awesome-icon": true,
"router-link": true
2024-11-20 13:48:13 +00:00
}
}
});
});
it("renders monitor list items", () => {
2024-11-20 13:58:15 +00:00
const items = wrapper.findAll("[data-testid='monitor-list'] .monitor-list-item");
expect(items.length).toBe(2);
2024-11-20 13:48:13 +00:00
});
it("emits select-monitor event when monitor is clicked", async () => {
2024-11-20 13:58:15 +00:00
const items = wrapper.findAll("[data-testid='monitor-list'] .monitor-list-item");
2024-11-20 13:48:13 +00:00
await items[0].trigger("click");
expect(wrapper.emitted("select-monitor")).toBeTruthy();
2024-11-20 13:58:15 +00:00
expect(wrapper.emitted("select-monitor")[0]).toEqual([1]);
2024-11-20 13:48:13 +00:00
});
it("applies active class to selected monitor", async () => {
2024-11-20 13:58:15 +00:00
await wrapper.setData({ selectedMonitorId: 1 });
const items = wrapper.findAll("[data-testid='monitor-list'] .monitor-list-item");
2024-11-20 13:48:13 +00:00
expect(items[0].classes()).toContain("active");
expect(items[1].classes()).not.toContain("active");
});
it("filters monitors based on search text", async () => {
2024-11-20 13:58:15 +00:00
await wrapper.setData({ searchText: "Test Monitor 1" });
const items = wrapper.findAll("[data-testid='monitor-list'] .monitor-list-item");
expect(items.length).toBe(1);
2024-11-20 13:48:13 +00:00
});
it("sorts monitors by status", async () => {
2024-11-20 13:58:15 +00:00
await wrapper.setData({ sortBy: "status" });
const items = wrapper.findAll("[data-testid='monitor-list'] .monitor-list-item");
expect(items.length).toBe(2);
2024-11-20 13:48:13 +00:00
});
2024-11-20 13:58:15 +00:00
it("toggles selection mode", async () => {
await wrapper.setData({ selectionMode: true });
const items = wrapper.findAll("[data-testid='monitor-list'] .monitor-list-item");
expect(items.length).toBe(2);
expect(wrapper.vm.selectionMode).toBe(true);
2024-11-20 13:48:13 +00:00
});
});