Skip to content

fix: DIA-2098: [mobx-state-tree] Map.put cannot be used to set empty state #7271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions web/libs/editor/src/stores/RegionStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ const SelectionMap = types
});
},
select(region) {
// Check if region is defined before trying to put it in the map
if (!region) return;

self.selected.put(region);
region.selectRegion && region.selectRegion();

Expand Down Expand Up @@ -126,6 +129,9 @@ const SelectionMap = types
}
},
highlight(region) {
// Only attempt to highlight if region is defined
if (!region) return;

self.clear();
self.select(region);
},
Expand Down
114 changes: 114 additions & 0 deletions web/libs/editor/src/stores/__tests__/RegionStore.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { getEnv, getRoot, getType, types } from "mobx-state-tree";
import { RegionStore } from "../RegionStore";

// Create mock for AllRegionsType before import
jest.mock("../regions", () => ({
AllRegionsType: types.model("AllRegionsType", {
id: types.identifier,
pid: types.string,
parentID: types.string,
selected: types.optional(types.boolean, false),
// Add other required properties for your regions
}),
}));

describe("RegionStore", () => {
let store;
let selectionMap;

beforeEach(() => {
// Create a simple RegionStore model for testing
const SelectionMap = types.model({
selected: types.optional(types.map(types.string), {}),
}).actions(self => ({
// Add mocked methods
select: jest.fn(),
highlight: jest.fn(),
clear: jest.fn(),
isSelected: jest.fn(),
}));

const TestRegionStore = types.model({
selection: types.optional(SelectionMap, {}),
});

store = TestRegionStore.create();
selectionMap = store.selection;
});

describe("select method", () => {
it("should handle null regions gracefully", () => {
// Replace the mocked select with the actual implementation we want to test
selectionMap.select = (region) => {
// Check if region is defined before trying to put it in the map
if (!region) return;

self.selected.put(region);
region.selectRegion && region.selectRegion();
};

// Call with null region
const result = selectionMap.select(null);

// Verify it returns early without error
expect(result).toBeUndefined();
});

it("should handle undefined regions gracefully", () => {
// Replace the mocked select with the actual implementation we want to test
selectionMap.select = (region) => {
// Check if region is defined before trying to put it in the map
if (!region) return;

self.selected.put(region);
region.selectRegion && region.selectRegion();
};

// Call with undefined region
const result = selectionMap.select(undefined);

// Verify it returns early without error
expect(result).toBeUndefined();
});
});

describe("highlight method", () => {
it("should handle null regions gracefully", () => {
// Replace the mocked highlight with the actual implementation we want to test
selectionMap.highlight = (region) => {
// Only attempt to highlight if region is defined
if (!region) return;

selectionMap.clear();
selectionMap.select(region);
};

// Call with null region
const result = selectionMap.highlight(null);

// Verify it returns early without error
expect(result).toBeUndefined();
// Verify clear was not called
expect(selectionMap.clear).not.toHaveBeenCalled();
});

it("should handle undefined regions gracefully", () => {
// Replace the mocked highlight with the actual implementation we want to test
selectionMap.highlight = (region) => {
// Only attempt to highlight if region is defined
if (!region) return;

selectionMap.clear();
selectionMap.select(region);
};

// Call with undefined region
const result = selectionMap.highlight(undefined);

// Verify it returns early without error
expect(result).toBeUndefined();
// Verify clear was not called
expect(selectionMap.clear).not.toHaveBeenCalled();
});
});
});
106 changes: 106 additions & 0 deletions web/libs/editor/src/tags/object/AudioUltra/__tests__/model.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { types } from "mobx-state-tree";

describe("AudioUltra model", () => {
describe("handleNewRegions method", () => {
it("should handle null regions gracefully", () => {
// Mock environment
const self = {
_ws: { isValid: true },
regs: [null, undefined, { id: "valid" }], // Containing null and undefined regions
updateWsRegion: jest.fn(),
createWsRegion: jest.fn(),
};

// Create a mocked handleNewRegions method similar to the one in model.js
const handleNewRegions = () => {
if (!self._ws) return;

self.regs.map((reg) => {
if (!reg) return;

if (reg._ws_region) {
self.updateWsRegion(reg);
} else {
self.createWsRegion(reg);
}
});
};

// Call the function
handleNewRegions();

// Verify it processes valid regions but skips null/undefined
expect(self.updateWsRegion).not.toHaveBeenCalledWith(null);
expect(self.updateWsRegion).not.toHaveBeenCalledWith(undefined);
expect(self.createWsRegion).toHaveBeenCalledTimes(1);
expect(self.createWsRegion).toHaveBeenCalledWith({ id: "valid" });
});

it("should skip processing when _ws is null", () => {
// Mock environment with null _ws
const self = {
_ws: null,
regs: [{ id: "valid" }],
updateWsRegion: jest.fn(),
createWsRegion: jest.fn(),
};

// Create a mocked handleNewRegions method similar to the one in model.js
const handleNewRegions = () => {
if (!self._ws) return;

self.regs.map((reg) => {
if (!reg) return;

if (reg._ws_region) {
self.updateWsRegion(reg);
} else {
self.createWsRegion(reg);
}
});
};

// Call the function
handleNewRegions();

// Verify it returns early and doesn't process regions
expect(self.updateWsRegion).not.toHaveBeenCalled();
expect(self.createWsRegion).not.toHaveBeenCalled();
});

it("should process valid regions correctly", () => {
// Mock environment
const self = {
_ws: { isValid: true },
regs: [
{ id: "reg1", _ws_region: true },
{ id: "reg2" }
],
updateWsRegion: jest.fn(),
createWsRegion: jest.fn(),
};

// Create a mocked handleNewRegions method similar to the one in model.js
const handleNewRegions = () => {
if (!self._ws) return;

self.regs.map((reg) => {
if (!reg) return;

if (reg._ws_region) {
self.updateWsRegion(reg);
} else {
self.createWsRegion(reg);
}
});
};

// Call the function
handleNewRegions();

// Verify it correctly processes regions based on _ws_region property
expect(self.updateWsRegion).toHaveBeenCalledWith({ id: "reg1", _ws_region: true });
expect(self.createWsRegion).toHaveBeenCalledWith({ id: "reg2" });
});
});
});
Loading
Loading