|
| 1 | +import { beforeEach, describe, expect, it, vi } from "@test/unit"; |
| 2 | +import { mount } from "@vue/test-utils"; |
| 3 | +import PreviewForm from "./PreviewForm.vue"; |
| 4 | + |
| 5 | +const events = { on: vi.fn(), off: vi.fn() }; |
| 6 | + |
| 7 | +function factory() { |
| 8 | + return mount(PreviewForm, { |
| 9 | + props: { |
| 10 | + api: "pages/test", |
| 11 | + blueprint: "default", |
| 12 | + content: {}, |
| 13 | + diff: {}, |
| 14 | + tab: { name: "main", columns: {} }, |
| 15 | + tabs: [{ name: "main" }, { name: "meta" }] |
| 16 | + }, |
| 17 | + global: { |
| 18 | + mocks: { |
| 19 | + $events: events, |
| 20 | + $panel: { |
| 21 | + config: { debug: false }, |
| 22 | + view: { path: "/pages/test" } |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + }); |
| 27 | +} |
| 28 | + |
| 29 | +type Link = HTMLElement & { |
| 30 | + __vue__: { to: string; onClick?: (event: unknown) => void }; |
| 31 | +}; |
| 32 | + |
| 33 | +/** |
| 34 | + * Creates a fake field with a single link in its element |
| 35 | + */ |
| 36 | +function field(to: string) { |
| 37 | + const el = document.createElement("div"); |
| 38 | + el.innerHTML = `<p class="k-item-title"><a class="k-link"></a></p>`; |
| 39 | + |
| 40 | + const link = el.querySelector(".k-link") as Link; |
| 41 | + link.__vue__ = { to }; |
| 42 | + |
| 43 | + return { field: { $el: el }, link }; |
| 44 | +} |
| 45 | + |
| 46 | +describe("PreviewForm.vue", () => { |
| 47 | + beforeEach(() => { |
| 48 | + events.on.mockClear(); |
| 49 | + events.off.mockClear(); |
| 50 | + }); |
| 51 | + |
| 52 | + describe("events", () => { |
| 53 | + it("listens to loaded fields and sections while mounted", () => { |
| 54 | + const wrapper = factory(); |
| 55 | + |
| 56 | + expect(events.on).toHaveBeenCalledWith( |
| 57 | + "field.loaded", |
| 58 | + wrapper.vm.fixLinks |
| 59 | + ); |
| 60 | + expect(events.on).toHaveBeenCalledWith( |
| 61 | + "section.loaded", |
| 62 | + wrapper.vm.fixLinks |
| 63 | + ); |
| 64 | + |
| 65 | + const fixLinks = wrapper.vm.fixLinks; |
| 66 | + wrapper.unmount(); |
| 67 | + |
| 68 | + expect(events.off).toHaveBeenCalledWith("field.loaded", fixLinks); |
| 69 | + expect(events.off).toHaveBeenCalledWith("section.loaded", fixLinks); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + describe("form", () => { |
| 74 | + it("passes on the events of the form and its controls", async () => { |
| 75 | + const wrapper = factory(); |
| 76 | + |
| 77 | + await wrapper.find("k-model-form").trigger("input"); |
| 78 | + await wrapper.find("k-model-form").trigger("submit"); |
| 79 | + await wrapper.find("k-form-controls").trigger("discard"); |
| 80 | + |
| 81 | + expect(wrapper.emitted("input")).toHaveLength(1); |
| 82 | + expect(wrapper.emitted("submit")).toHaveLength(1); |
| 83 | + expect(wrapper.emitted("discard")).toHaveLength(1); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + describe("fixLinks", () => { |
| 88 | + it("opens page links in the preview view", () => { |
| 89 | + const wrapper = factory(); |
| 90 | + const { field: loaded, link } = field("/pages/test"); |
| 91 | + |
| 92 | + wrapper.vm.fixLinks(loaded); |
| 93 | + |
| 94 | + const event = { preventDefault: vi.fn() }; |
| 95 | + link.__vue__.onClick?.(event); |
| 96 | + |
| 97 | + expect(event.preventDefault).toHaveBeenCalled(); |
| 98 | + expect(wrapper.emitted("navigate")?.[0]).toStrictEqual([ |
| 99 | + "/pages/test/preview/form" |
| 100 | + ]); |
| 101 | + }); |
| 102 | + |
| 103 | + it("keeps all other links untouched", () => { |
| 104 | + const wrapper = factory(); |
| 105 | + const { field: loaded, link } = field("/pages/test/files/test.jpg"); |
| 106 | + |
| 107 | + wrapper.vm.fixLinks(loaded); |
| 108 | + |
| 109 | + expect(link.__vue__.onClick).toBeUndefined(); |
| 110 | + }); |
| 111 | + }); |
| 112 | +}); |
0 commit comments