Skip to content

Commit 33bb355

Browse files
committed
refactor transformStateAsReadOnly
1 parent 5059116 commit 33bb355

File tree

3 files changed

+27
-27
lines changed

3 files changed

+27
-27
lines changed

frontend/javascripts/test/helpers/utils.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import update from "immutability-helper";
2-
import type { Action } from "viewer/model/actions/actions";
32
import type { WebknossosState } from "viewer/store";
43

5-
export const applyActionsOnReadOnlyVersion = (
6-
applyActions: (
7-
state: WebknossosState,
8-
actionGetters: Array<Action | ((s: WebknossosState) => Action)>,
9-
) => WebknossosState,
4+
export const transformStateAsReadOnly = (
105
state: WebknossosState,
11-
actionGetters: Array<Action | ((s: WebknossosState) => Action)>,
6+
transformFn: (state: WebknossosState) => WebknossosState,
127
) => {
8+
/*
9+
* This function can be used to make a state read only before
10+
* transforming it somehow (e.g., with a reducer). The result of
11+
* the transformation is then made not-read-only again.
12+
*/
1313
const readOnlyState = overrideAllowUpdateInState(state, false);
14-
const reappliedNewState = applyActions(readOnlyState, actionGetters);
14+
const transformedState = transformFn(readOnlyState);
1515

16-
return overrideAllowUpdateInState(reappliedNewState, true);
16+
return overrideAllowUpdateInState(transformedState, true);
1717
};
1818

1919
function overrideAllowUpdateInState(state: WebknossosState, value: boolean) {

frontend/javascripts/test/reducers/update_action_application/skeleton.spec.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { sampleTracingLayer } from "test/fixtures/dataset_server_object";
44
import { initialState as defaultSkeletonState } from "test/fixtures/skeletontracing_object";
55
import { chainReduce } from "test/helpers/chainReducer";
66
import { withoutUpdateActiveItemTracing } from "test/helpers/saveHelpers";
7-
import { applyActionsOnReadOnlyVersion } from "test/helpers/utils";
7+
import { transformStateAsReadOnly } from "test/helpers/utils";
88
import type { Vector3 } from "viewer/constants";
99
import {
1010
enforceSkeletonTracing,
@@ -203,14 +203,12 @@ describe("Update Action Application for SkeletonTracing", () => {
203203
seenActionTypes.add(action.name);
204204
}
205205

206-
const reappliedNewState = applyActionsOnReadOnlyVersion(
207-
applyActions,
208-
state2WithoutActiveState,
209-
[
206+
const reappliedNewState = transformStateAsReadOnly(state2WithoutActiveState, (state) =>
207+
applyActions(state, [
210208
SkeletonTracingActions.applySkeletonUpdateActionsFromServerAction(updateActions),
211209
SkeletonTracingActions.setActiveNodeAction(null),
212210
setActiveUserBoundingBoxId(null),
213-
],
211+
]),
214212
);
215213

216214
expect(reappliedNewState).toEqual(state3);
@@ -242,9 +240,11 @@ describe("Update Action Application for SkeletonTracing", () => {
242240
),
243241
) as ApplicableSkeletonUpdateAction[];
244242

245-
const newState3 = applyActionsOnReadOnlyVersion(applyActions, newState, [
246-
SkeletonTracingActions.applySkeletonUpdateActionsFromServerAction(updateActions),
247-
]);
243+
const newState3 = transformStateAsReadOnly(newState, (state) =>
244+
applyActions(state, [
245+
SkeletonTracingActions.applySkeletonUpdateActionsFromServerAction(updateActions),
246+
]),
247+
);
248248

249249
const { activeNodeId } = enforceSkeletonTracing(newState3.annotation);
250250
expect(activeNodeId).toBeNull();
@@ -273,9 +273,11 @@ describe("Update Action Application for SkeletonTracing", () => {
273273
),
274274
) as ApplicableSkeletonUpdateAction[];
275275

276-
const newState3 = applyActionsOnReadOnlyVersion(applyActions, newState, [
277-
SkeletonTracingActions.applySkeletonUpdateActionsFromServerAction(updateActions),
278-
]);
276+
const newState3 = transformStateAsReadOnly(newState, (state) =>
277+
applyActions(state, [
278+
SkeletonTracingActions.applySkeletonUpdateActionsFromServerAction(updateActions),
279+
]),
280+
);
279281

280282
const { activeTreeId, activeNodeId } = enforceSkeletonTracing(newState3.annotation);
281283

frontend/javascripts/test/reducers/update_action_application/volume.spec.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import type {
2020
import { combinedReducer, type WebknossosState } from "viewer/store";
2121
import { makeBasicGroupObject } from "viewer/view/right-border-tabs/trees_tab/tree_hierarchy_view_helpers";
2222
import { afterAll, describe, expect, test } from "vitest";
23-
import { applyActionsOnReadOnlyVersion } from "test/helpers/utils";
23+
import { transformStateAsReadOnly } from "test/helpers/utils";
2424

2525
const enforceVolumeTracing = (state: WebknossosState) => {
2626
const tracing = state.annotation.volumes[0];
@@ -180,14 +180,12 @@ describe("Update Action Application for VolumeTracing", () => {
180180
seenActionTypes.add(action.name);
181181
}
182182

183-
const reappliedNewState = applyActionsOnReadOnlyVersion(
184-
applyActions,
185-
state2WithoutActiveState,
186-
[
183+
const reappliedNewState = transformStateAsReadOnly(state2WithoutActiveState, (state) =>
184+
applyActions(state, [
187185
VolumeTracingActions.applyVolumeUpdateActionsFromServerAction(updateActions),
188186
VolumeTracingActions.setActiveCellAction(0),
189187
setActiveUserBoundingBoxId(null),
190-
],
188+
]),
191189
);
192190

193191
expect(reappliedNewState).toEqual(state3);

0 commit comments

Comments
 (0)