Skip to content

Commit 25ea4e6

Browse files
committed
tests: add c-admin-editor test for create/edit of user-provided pk model.
1 parent f471954 commit 25ea4e6

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

src/coalesce-vue-vuetify3/src/components/admin/c-admin-editor.spec.tsx

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
PersonListViewModel,
66
OneToOneParentViewModel,
77
OneToOneSharedKeyChild1ViewModel,
8+
ZipCodeViewModel,
89
} from "@test-targets/viewmodels.g";
910
import { mockEndpoint, mount } from "@test/util";
1011

@@ -135,4 +136,77 @@ describe("CAdminEditor", () => {
135136
expect(link.attributes("href")).toBe("/admin/OneToOneParent/item/42");
136137
});
137138
});
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+
});
138212
});

0 commit comments

Comments
 (0)