Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 6 additions & 2 deletions src/Squawk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,12 @@ export default function createStore<T>(initialState: Required<T>, useReduxDevToo
// If Redux dev tools emitted a dispatch, and the state has a value
if (message.type === "DISPATCH" && message.state) {
// Deserialize the value and dispatch updates to all subscribers
globalState.set(JSON.parse(message.state));
notifySubscribers(globalState.keys());
try {
globalState.set(JSON.parse(message.state));
notifySubscribers(globalState.keys());
} catch {
// Ignore malformed JSON
}
}
});
}
Expand Down
33 changes: 33 additions & 0 deletions src/__tests__/Squawk.redux.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import createStore from "../Squawk";

describe("Redux DevTools integration", () => {
it("handles malformed JSON gracefully", () => {
const devToolsSubscribeCallbacks: Array<(msg: { type: string; state: string }) => void> = [];
// eslint-disable-next-line immutable/no-mutation, @typescript-eslint/no-explicit-any
(global as any).window = {
__REDUX_DEVTOOLS_EXTENSION__: {
connect: () => ({
subscribe: (cb: (msg: { type: string; state: string }) => void) => {
devToolsSubscribeCallbacks.push(cb);
},
// eslint-disable-next-line @typescript-eslint/no-empty-function
send: () => {},
// eslint-disable-next-line @typescript-eslint/no-empty-function
init: () => {}
})
}
};

const store = createStore({ foo: "bar" }, true);

expect(() => {
devToolsSubscribeCallbacks[0]({ type: "DISPATCH", state: "invalid json" });
}).not.toThrow();

expect(store.get().foo).toBe("bar");

// clean up
// eslint-disable-next-line @typescript-eslint/no-explicit-any
delete (global as any).window;
});
});
Loading