|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { mount } from "@vue/test-utils"; |
| 3 | +import ModelForm from "./ModelForm.vue"; |
| 4 | + |
| 5 | +const { disabled, isEmpty, resolvedColumns } = ModelForm.computed; |
| 6 | +const { fieldsWithAdditionalData } = ModelForm.methods; |
| 7 | + |
| 8 | +/** |
| 9 | + * Builds a mocked component context |
| 10 | + */ |
| 11 | +function context(props = {}) { |
| 12 | + const ctx = { |
| 13 | + api: "pages/test", |
| 14 | + columns: {}, |
| 15 | + diff: {}, |
| 16 | + ...props |
| 17 | + }; |
| 18 | + |
| 19 | + ctx.fieldsWithAdditionalData = fieldsWithAdditionalData.bind(ctx); |
| 20 | + |
| 21 | + return ctx; |
| 22 | +} |
| 23 | + |
| 24 | +describe("ModelForm.vue", () => { |
| 25 | + const columns = { |
| 26 | + 0: { width: "1/2", fields: { headline: { type: "text" } } }, |
| 27 | + 1: { width: "1/2", sticky: true, fields: { text: { type: "textarea" } } } |
| 28 | + }; |
| 29 | + |
| 30 | + it("renders a column for each column of the tab", () => { |
| 31 | + const wrapper = mount(ModelForm, { |
| 32 | + props: { api: "pages/test", columns, content: { headline: "Test" } } |
| 33 | + }); |
| 34 | + |
| 35 | + const rendered = wrapper.findAll("k-column"); |
| 36 | + |
| 37 | + expect(wrapper.find("form.k-model-form").exists()).toBe(true); |
| 38 | + expect(rendered.length).toBe(2); |
| 39 | + expect(rendered[0].attributes("width")).toBe("1/2"); |
| 40 | + expect(rendered[1].attributes("sticky")).toBe("true"); |
| 41 | + expect(wrapper.findAll("k-fieldset").length).toBe(2); |
| 42 | + }); |
| 43 | + |
| 44 | + it("renders the empty state instead of the form", () => { |
| 45 | + const wrapper = mount(ModelForm, { |
| 46 | + props: { columns: {}, empty: "No blueprint" } |
| 47 | + }); |
| 48 | + |
| 49 | + expect(wrapper.find("k-box").exists()).toBe(true); |
| 50 | + expect(wrapper.find("form").exists()).toBe(false); |
| 51 | + }); |
| 52 | + |
| 53 | + it("passes on the input of a fieldset", async () => { |
| 54 | + const wrapper = mount(ModelForm, { |
| 55 | + props: { api: "pages/test", columns } |
| 56 | + }); |
| 57 | + |
| 58 | + await wrapper.find("k-fieldset").trigger("input"); |
| 59 | + |
| 60 | + expect(wrapper.emitted("input")).toHaveLength(1); |
| 61 | + }); |
| 62 | + |
| 63 | + it("submits the form and the fieldsets", async () => { |
| 64 | + const wrapper = mount(ModelForm, { |
| 65 | + props: { api: "pages/test", columns } |
| 66 | + }); |
| 67 | + |
| 68 | + await wrapper.find("form").trigger("submit"); |
| 69 | + expect(wrapper.emitted("submit")).toHaveLength(1); |
| 70 | + |
| 71 | + // the submit of a fieldset bubbles up to the form as well |
| 72 | + await wrapper.find("k-fieldset").trigger("submit"); |
| 73 | + expect(wrapper.emitted("submit")).toHaveLength(3); |
| 74 | + }); |
| 75 | +}); |
| 76 | + |
| 77 | +describe("ModelForm.fieldsWithAdditionalData()", () => { |
| 78 | + it("points regular fields at the field endpoint", () => { |
| 79 | + const ctx = context(); |
| 80 | + const fields = ctx.fieldsWithAdditionalData({ |
| 81 | + headline: { type: "text" } |
| 82 | + }); |
| 83 | + |
| 84 | + expect(fields.headline.endpoints).toStrictEqual({ |
| 85 | + model: "pages/test", |
| 86 | + field: "pages/test/fields/headline" |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + it("points section fields at the section endpoint", () => { |
| 91 | + const ctx = context(); |
| 92 | + const fields = ctx.fieldsWithAdditionalData({ |
| 93 | + mysection: { type: "section" } |
| 94 | + }); |
| 95 | + |
| 96 | + expect(fields.mysection.endpoints).toStrictEqual({ |
| 97 | + model: "pages/test", |
| 98 | + section: "pages/test/sections/mysection" |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + it("flags fields with unsaved changes", () => { |
| 103 | + const ctx = context({ diff: { headline: "changed" } }); |
| 104 | + const fields = ctx.fieldsWithAdditionalData({ |
| 105 | + headline: { type: "text" }, |
| 106 | + text: { type: "textarea" } |
| 107 | + }); |
| 108 | + |
| 109 | + expect(fields.headline.hasDiff).toBe(true); |
| 110 | + expect(fields.text.hasDiff).toBe(false); |
| 111 | + }); |
| 112 | + |
| 113 | + it("survives a missing diff", () => { |
| 114 | + const ctx = context({ diff: undefined }); |
| 115 | + const fields = ctx.fieldsWithAdditionalData({ |
| 116 | + headline: { type: "text" } |
| 117 | + }); |
| 118 | + |
| 119 | + expect(fields.headline.hasDiff).toBe(false); |
| 120 | + }); |
| 121 | +}); |
| 122 | + |
| 123 | +describe("ModelForm.resolvedColumns()", () => { |
| 124 | + it("keeps the column props and resolves its fields", () => { |
| 125 | + const ctx = context({ |
| 126 | + columns: { |
| 127 | + 0: { width: "2/3", fields: { headline: { type: "text" } } } |
| 128 | + } |
| 129 | + }); |
| 130 | + |
| 131 | + const columns = resolvedColumns.call(ctx); |
| 132 | + |
| 133 | + expect(columns[0].width).toBe("2/3"); |
| 134 | + expect(columns[0].fields.headline.endpoints.field).toBe( |
| 135 | + "pages/test/fields/headline" |
| 136 | + ); |
| 137 | + }); |
| 138 | +}); |
| 139 | + |
| 140 | +describe("ModelForm.disabled()", () => { |
| 141 | + it("is disabled while the model is locked", () => { |
| 142 | + expect(disabled.call({ lock: { state: "lock" } })).toBe(true); |
| 143 | + expect(disabled.call({ lock: { state: "unlock" } })).toBe(false); |
| 144 | + expect(disabled.call({ lock: false })).toBe(false); |
| 145 | + }); |
| 146 | +}); |
| 147 | + |
| 148 | +describe("ModelForm.isEmpty()", () => { |
| 149 | + it("only reports empty when there are no columns and a text to show", () => { |
| 150 | + expect(isEmpty.call({ columns: {}, empty: "No blueprint" })).toBe( |
| 151 | + "No blueprint" |
| 152 | + ); |
| 153 | + expect(isEmpty.call({ columns: {}, empty: null })).toBeFalsy(); |
| 154 | + expect(isEmpty.call({ columns: { 0: {} }, empty: "No blueprint" })).toBe( |
| 155 | + false |
| 156 | + ); |
| 157 | + }); |
| 158 | +}); |
0 commit comments