|
5 | 5 | PersonListViewModel, |
6 | 6 | OneToOneParentViewModel, |
7 | 7 | OneToOneSharedKeyChild1ViewModel, |
| 8 | + ZipCodeViewModel, |
8 | 9 | } from "@test-targets/viewmodels.g"; |
9 | 10 | import { mockEndpoint, mount } from "@test/util"; |
10 | 11 |
|
@@ -135,4 +136,77 @@ describe("CAdminEditor", () => { |
135 | 136 | expect(link.attributes("href")).toBe("/admin/OneToOneParent/item/42"); |
136 | 137 | }); |
137 | 138 | }); |
| 139 | + |
| 140 | + describe("user-provided PK", () => { |
| 141 | + const saveMock = mockEndpoint("/ZipCode/save", vitest.fn()); |
| 142 | + |
| 143 | + test("create with user-provided PK", async () => { |
| 144 | + const vm = new ZipCodeViewModel(); |
| 145 | + |
| 146 | + const wrapper = mount(() => <CAdminEditor model={vm} />); |
| 147 | + |
| 148 | + // Find the row for the PK field (zip) |
| 149 | + const zipRow = wrapper.find(".prop-zip"); |
| 150 | + expect(zipRow.exists()).toBeTruthy(); |
| 151 | + |
| 152 | + // PK field should be editable for a new item |
| 153 | + const zipInput = zipRow.find("input"); |
| 154 | + expect(zipInput.exists()).toBeTruthy(); |
| 155 | + expect(zipInput.element.readOnly).toBe(false); |
| 156 | + |
| 157 | + // Set PK and other field values |
| 158 | + await zipInput.setValue("98052"); |
| 159 | + vm.state = "WA"; |
| 160 | + |
| 161 | + // Save the item |
| 162 | + saveMock.mockResolvedValue({ |
| 163 | + wasSuccessful: true, |
| 164 | + object: { zip: "98052", state: "WA" }, |
| 165 | + }); |
| 166 | + await vm.$save(); |
| 167 | + |
| 168 | + expect(JSON.parse(saveMock.mock.calls[0][0].data)).toMatchObject({ |
| 169 | + zip: "98052", |
| 170 | + state: "WA", |
| 171 | + }); |
| 172 | + expect(vm.$primaryKey).toBe("98052"); |
| 173 | + expect(vm.$isDirty).toBeFalsy(); |
| 174 | + expect(zipInput.element.readOnly).toBe(true); |
| 175 | + }); |
| 176 | + |
| 177 | + test("update with user-provided PK", async () => { |
| 178 | + const vm = new ZipCodeViewModel(); |
| 179 | + vm.$loadCleanData({ zip: "98052", state: "WA" }); |
| 180 | + |
| 181 | + const wrapper = mount(() => <CAdminEditor model={vm} />); |
| 182 | + |
| 183 | + // Find the row for the PK field (zip) |
| 184 | + const zipRow = wrapper.find(".prop-zip"); |
| 185 | + expect(zipRow.exists()).toBeTruthy(); |
| 186 | + |
| 187 | + // PK field should be readonly for an existing item |
| 188 | + const zipInput = zipRow.find("input"); |
| 189 | + expect(zipInput.exists()).toBeTruthy(); |
| 190 | + expect(zipInput.element.readOnly).toBe(true); |
| 191 | + |
| 192 | + // Update a non-PK field |
| 193 | + const stateRow = wrapper.find(".prop-state"); |
| 194 | + const stateInput = stateRow.find("input"); |
| 195 | + await stateInput.setValue("Washington"); |
| 196 | + |
| 197 | + // Save the item |
| 198 | + saveMock.mockResolvedValue({ |
| 199 | + wasSuccessful: true, |
| 200 | + object: { zip: "98052", state: "Washington" }, |
| 201 | + }); |
| 202 | + await vm.$save(); |
| 203 | + |
| 204 | + expect(JSON.parse(saveMock.mock.calls[1][0].data)).toMatchObject({ |
| 205 | + zip: "98052", |
| 206 | + state: "Washington", |
| 207 | + }); |
| 208 | + expect(vm.$primaryKey).toBe("98052"); |
| 209 | + expect(vm.$isDirty).toBeFalsy(); |
| 210 | + }); |
| 211 | + }); |
138 | 212 | }); |
0 commit comments