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