Add tests for player start command and update set-player-start-command to restore tool mode
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import { cloneEditorSelection, type EditorSelection } from "../core/selection";
|
import { cloneEditorSelection, type EditorSelection } from "../core/selection";
|
||||||
|
import type { ToolMode } from "../core/tool-mode";
|
||||||
import type { Vec3 } from "../core/vector";
|
import type { Vec3 } from "../core/vector";
|
||||||
import { createOpaqueId } from "../core/ids";
|
import { createOpaqueId } from "../core/ids";
|
||||||
import { cloneEntityInstance, createPlayerStartEntity } from "../entities/entity-instances";
|
import { cloneEntityInstance, createPlayerStartEntity } from "../entities/entity-instances";
|
||||||
@@ -27,6 +28,7 @@ export function createSetPlayerStartCommand(options: SetPlayerStartCommandOption
|
|||||||
|
|
||||||
let previousEntity = null as typeof nextEntity | null;
|
let previousEntity = null as typeof nextEntity | null;
|
||||||
let previousSelection: EditorSelection | null = null;
|
let previousSelection: EditorSelection | null = null;
|
||||||
|
let previousToolMode: ToolMode | null = null;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: createOpaqueId("command"),
|
id: createOpaqueId("command"),
|
||||||
@@ -43,6 +45,10 @@ export function createSetPlayerStartCommand(options: SetPlayerStartCommandOption
|
|||||||
previousSelection = cloneEditorSelection(context.getSelection());
|
previousSelection = cloneEditorSelection(context.getSelection());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (previousToolMode === null) {
|
||||||
|
previousToolMode = context.getToolMode();
|
||||||
|
}
|
||||||
|
|
||||||
if (previousEntity === null && currentEntity !== undefined) {
|
if (previousEntity === null && currentEntity !== undefined) {
|
||||||
previousEntity = cloneEntityInstance(currentEntity);
|
previousEntity = cloneEntityInstance(currentEntity);
|
||||||
}
|
}
|
||||||
@@ -77,6 +83,10 @@ export function createSetPlayerStartCommand(options: SetPlayerStartCommandOption
|
|||||||
if (previousSelection !== null) {
|
if (previousSelection !== null) {
|
||||||
context.setSelection(previousSelection);
|
context.setSelection(previousSelection);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (previousToolMode !== null) {
|
||||||
|
context.setToolMode(previousToolMode);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
90
tests/domain/player-start.command.test.ts
Normal file
90
tests/domain/player-start.command.test.ts
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
|
||||||
|
import { createEditorStore } from "../../src/app/editor-store";
|
||||||
|
import { createSetPlayerStartCommand } from "../../src/commands/set-player-start-command";
|
||||||
|
|
||||||
|
describe("player start command", () => {
|
||||||
|
it("restores the previous tool mode across undo and redo when placing PlayerStart", () => {
|
||||||
|
const store = createEditorStore();
|
||||||
|
|
||||||
|
store.setToolMode("box-create");
|
||||||
|
store.executeCommand(
|
||||||
|
createSetPlayerStartCommand({
|
||||||
|
position: {
|
||||||
|
x: 2,
|
||||||
|
y: 0,
|
||||||
|
z: -2
|
||||||
|
},
|
||||||
|
yawDegrees: 90
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
const placedPlayerStart = Object.values(store.getState().document.entities)[0];
|
||||||
|
|
||||||
|
expect(placedPlayerStart).toBeDefined();
|
||||||
|
expect(store.getState().toolMode).toBe("select");
|
||||||
|
|
||||||
|
expect(store.undo()).toBe(true);
|
||||||
|
expect(store.getState().toolMode).toBe("box-create");
|
||||||
|
expect(store.getState().document.entities).toEqual({});
|
||||||
|
|
||||||
|
expect(store.redo()).toBe(true);
|
||||||
|
expect(store.getState().toolMode).toBe("select");
|
||||||
|
expect(store.getState().document.entities[placedPlayerStart.id]).toEqual(placedPlayerStart);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("restores the previous tool mode across undo and redo when moving PlayerStart", () => {
|
||||||
|
const store = createEditorStore();
|
||||||
|
|
||||||
|
store.executeCommand(
|
||||||
|
createSetPlayerStartCommand({
|
||||||
|
position: {
|
||||||
|
x: 0,
|
||||||
|
y: 0,
|
||||||
|
z: 0
|
||||||
|
},
|
||||||
|
yawDegrees: 0
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
const existingPlayerStart = Object.values(store.getState().document.entities)[0];
|
||||||
|
|
||||||
|
store.setToolMode("box-create");
|
||||||
|
store.executeCommand(
|
||||||
|
createSetPlayerStartCommand({
|
||||||
|
entityId: existingPlayerStart.id,
|
||||||
|
position: {
|
||||||
|
x: 4,
|
||||||
|
y: 0,
|
||||||
|
z: 1
|
||||||
|
},
|
||||||
|
yawDegrees: 180
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(store.getState().toolMode).toBe("select");
|
||||||
|
expect(store.getState().document.entities[existingPlayerStart.id]).toMatchObject({
|
||||||
|
position: {
|
||||||
|
x: 4,
|
||||||
|
y: 0,
|
||||||
|
z: 1
|
||||||
|
},
|
||||||
|
yawDegrees: 180
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(store.undo()).toBe(true);
|
||||||
|
expect(store.getState().toolMode).toBe("box-create");
|
||||||
|
expect(store.getState().document.entities[existingPlayerStart.id]).toEqual(existingPlayerStart);
|
||||||
|
|
||||||
|
expect(store.redo()).toBe(true);
|
||||||
|
expect(store.getState().toolMode).toBe("select");
|
||||||
|
expect(store.getState().document.entities[existingPlayerStart.id]).toMatchObject({
|
||||||
|
position: {
|
||||||
|
x: 4,
|
||||||
|
y: 0,
|
||||||
|
z: 1
|
||||||
|
},
|
||||||
|
yawDegrees: 180
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user