Add editing of client schedule entities
Add utility methods to more easily edit the fields of a single entity in the schedule, along with a modification flag and is modified utility to check for changes having been made.
This commit is contained in:
parent
faffe48706
commit
61734d4152
2 changed files with 192 additions and 84 deletions
|
@ -1,4 +1,4 @@
|
|||
import { ClientSchedule, ClientScheduleEventSlot, ClientScheduleLocation, toIso } from "./client-schedule";
|
||||
import { ClientEntity, ClientSchedule, ClientScheduleEventSlot, ClientScheduleLocation, toIso } from "./client-schedule";
|
||||
import { describe, expect, test } from "vitest";
|
||||
import type { ApiSchedule } from "~/shared/types/api";
|
||||
import type { Living } from "~/shared/types/common";
|
||||
|
@ -198,87 +198,89 @@ describe("class ClientSchedule", () => {
|
|||
});
|
||||
}
|
||||
|
||||
test("create location", () => {
|
||||
const schedule = fixtureClientSchedule();
|
||||
const location = new ClientScheduleLocation(3, now, false, "New location", "");
|
||||
schedule.setLocation(location);
|
||||
expect(schedule.originalLocations.get(3)).toBe(undefined);
|
||||
expect(schedule.locations.get(3)).toBe(location);
|
||||
});
|
||||
|
||||
test("update location", () => {
|
||||
const schedule = fixtureClientSchedule();
|
||||
const original = schedule.locations.get(1)!;
|
||||
const copy = original.clone();
|
||||
copy.name = "Modified Location";
|
||||
schedule.setLocation(copy);
|
||||
expect(schedule.originalLocations.get(1)).toBe(original);
|
||||
expect(schedule.locations.get(1)).toBe(copy);
|
||||
expect(schedule.events.get(1)!.slots[0].locations[0]).toBe(copy);
|
||||
});
|
||||
|
||||
test("delete location in use throws", () => {
|
||||
const schedule = fixtureClientSchedule();
|
||||
const original = schedule.locations.get(1)!;
|
||||
const copy = original.clone();
|
||||
copy.deleted = true;
|
||||
expect(
|
||||
() => { schedule.setLocation(copy); }
|
||||
).toThrow(new Error('Cannot delete location, event "Up" depends on it'));
|
||||
});
|
||||
|
||||
test("delete location", () => {
|
||||
const schedule = fixtureClientSchedule();
|
||||
const event = schedule.events.get(1)!.clone();
|
||||
event.slots = [];
|
||||
schedule.setEvent(event);
|
||||
const original = schedule.locations.get(1)!;
|
||||
const copy = original.clone();
|
||||
copy.deleted = true;
|
||||
schedule.setLocation(copy);
|
||||
expect(schedule.originalLocations.get(1)).toBe(original);
|
||||
expect(schedule.locations.get(1)).toBe(copy);
|
||||
});
|
||||
|
||||
test("create role", () => {
|
||||
const schedule = fixtureClientSchedule();
|
||||
const role = new ClientScheduleRole(3, now, false, "New role", "");
|
||||
schedule.setRole(role);
|
||||
expect(schedule.originalRoles.get(3)).toBe(undefined);
|
||||
expect(schedule.roles.get(3)).toBe(role);
|
||||
});
|
||||
|
||||
test("update role", () => {
|
||||
const schedule = fixtureClientSchedule();
|
||||
const original = schedule.roles.get(1)!;
|
||||
const copy = original.clone();
|
||||
copy.name = "Modified Role";
|
||||
schedule.setRole(copy);
|
||||
expect(schedule.originalRoles.get(1)).toBe(original);
|
||||
expect(schedule.roles.get(1)).toBe(copy);
|
||||
expect(schedule.shifts.get(1)!.role).toBe(copy);
|
||||
});
|
||||
|
||||
test("delete role in use throws", () => {
|
||||
const schedule = fixtureClientSchedule();
|
||||
const original = schedule.roles.get(1)!;
|
||||
const copy = original.clone();
|
||||
copy.deleted = true;
|
||||
expect(
|
||||
() => { schedule.setRole(copy); }
|
||||
).toThrow(new Error('Cannot delete role, shift "White" depends on it'));
|
||||
});
|
||||
|
||||
test("delete role", () => {
|
||||
const schedule = fixtureClientSchedule();
|
||||
const shift = schedule.shifts.get(1)!.clone();
|
||||
shift.role = schedule.roles.get(2)!;
|
||||
schedule.setShift(shift);
|
||||
const original = schedule.roles.get(1)!;
|
||||
const copy = original.clone();
|
||||
copy.deleted = true;
|
||||
schedule.setRole(copy);
|
||||
expect(schedule.originalRoles.get(1)).toBe(original);
|
||||
expect(schedule.roles.get(1)).toBe(copy);
|
||||
});
|
||||
const entityTests: [string, (schedule: ClientSchedule) => ClientEntity][] = [
|
||||
[
|
||||
"location",
|
||||
() => new ClientScheduleLocation(3, now, false, "New location", "")
|
||||
],
|
||||
[
|
||||
"event",
|
||||
() => new ClientScheduleEvent(3, now, false, "New location", false, "", false, "", 0, [])
|
||||
],
|
||||
[
|
||||
"role",
|
||||
() => new ClientScheduleRole(3, now, false, "New location", "")
|
||||
],
|
||||
[
|
||||
"shift",
|
||||
(schedule) => new ClientScheduleShift(3, now, false, schedule.roles.get(1)!, "New location", "", [])
|
||||
],
|
||||
] as const;
|
||||
for (const [name, create] of entityTests) {
|
||||
describe(name, () => {
|
||||
const Name = name[0].toUpperCase() + name.slice(1);
|
||||
test(`create`, () => {
|
||||
const schedule = fixtureClientSchedule();
|
||||
const entity = create(schedule);
|
||||
expect(schedule.modified).toBe(false);
|
||||
// Create
|
||||
(schedule as any)[`set${Name}`](entity);
|
||||
// Check
|
||||
expect(schedule.modified).toBe(true);
|
||||
expect((schedule as any)[`isModified${Name}`](entity.id)).toBe(true);
|
||||
expect((schedule as any)[`original${Name}s`].get(entity.id)).toBe(undefined);
|
||||
expect((schedule as any)[`${name}s`].get(entity.id)).toBe(entity);
|
||||
});
|
||||
test("edit", () => {
|
||||
const schedule = fixtureClientSchedule();
|
||||
const original = (schedule as any)[`${name}s`].get(1);
|
||||
expect(schedule.modified).toBe(false);
|
||||
expect((schedule as any)[`isModified${Name}`](1)).toBe(false);
|
||||
// Edit
|
||||
(schedule as any)[`edit${Name}`](original, { name: `Modified ${name}` })
|
||||
// Check
|
||||
expect(schedule.modified).toBe(true);
|
||||
expect((schedule as any)[`isModified${Name}`](1)).toBe(true);
|
||||
expect((schedule as any)[`original${Name}s`].get(1)).toBe(original);
|
||||
if (name === "location") {
|
||||
expect(schedule.events.get(1)!.slots[0].locations[0]).toBe(schedule.locations.get(1));
|
||||
} else if (name === "role") {
|
||||
expect(schedule.shifts.get(1)!.role).toBe(schedule.roles.get(1));
|
||||
}
|
||||
});
|
||||
if (name === "location") {
|
||||
test("delete location in use throws", () => {
|
||||
const schedule = fixtureClientSchedule();
|
||||
expect(
|
||||
() => { schedule.editLocation(schedule.locations.get(1)!, { deleted: true }); }
|
||||
).toThrow(new Error('Cannot delete location, event "Up" depends on it'));
|
||||
});
|
||||
} else if (name === "role") {
|
||||
test("delete role in use throws", () => {
|
||||
const schedule = fixtureClientSchedule();
|
||||
expect(
|
||||
() => { schedule.editRole(schedule.roles.get(1)!, { deleted: true }); }
|
||||
).toThrow(new Error('Cannot delete role, shift "White" depends on it'));
|
||||
});
|
||||
}
|
||||
test("delete", () => {
|
||||
const schedule = fixtureClientSchedule();
|
||||
const original = (schedule as any)[`${name}s`].get(1);
|
||||
expect(schedule.modified).toBe(false);
|
||||
expect((schedule as any)[`isModified${Name}`](1)).toBe(false);
|
||||
// Delete
|
||||
if (name === "location") {
|
||||
schedule.editEvent(schedule.events.get(1)!, { deleted: true });
|
||||
} else if (name === "role") {
|
||||
schedule.editShift(schedule.shifts.get(1)!, { deleted: true });
|
||||
}
|
||||
(schedule as any)[`edit${Name}`](original, { deleted: true })
|
||||
// Check
|
||||
expect(schedule.modified).toBe(true);
|
||||
expect((schedule as any)[`isModified${Name}`](1)).toBe(true);
|
||||
expect((schedule as any)[`original${Name}s`].get(1)).toBe(original);
|
||||
expect((schedule as any)[`${name}s`].get(1).deleted).toBe(true);
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue