index.test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import { describe, it, expect, vi } from "vitest";
  2. import { configureStore, createSlice } from "@reduxjs/toolkit";
  3. import { reduxStateStore } from "./index";
  4. function createTestStore(initial: Record<string, unknown> = {}) {
  5. const uiSlice = createSlice({
  6. name: "ui",
  7. initialState: initial as Record<string, unknown>,
  8. reducers: {
  9. replace: (_state, action) => action.payload,
  10. },
  11. });
  12. const reduxStore = configureStore({
  13. reducer: { ui: uiSlice.reducer },
  14. });
  15. const store = reduxStateStore({
  16. store: reduxStore,
  17. selector: (state) => state.ui as Record<string, unknown>,
  18. dispatch: (next, s) => s.dispatch(uiSlice.actions.replace(next)),
  19. });
  20. return { reduxStore, store, uiSlice };
  21. }
  22. describe("reduxStateStore", () => {
  23. it("get/set round-trip", () => {
  24. const { store } = createTestStore({ count: 0 });
  25. expect(store.get("/count")).toBe(0);
  26. store.set("/count", 42);
  27. expect(store.get("/count")).toBe(42);
  28. expect(store.getSnapshot().count).toBe(42);
  29. });
  30. it("update round-trip with multiple values", () => {
  31. const { store } = createTestStore({});
  32. store.update({ "/a": 1, "/b": "hello" });
  33. expect(store.get("/a")).toBe(1);
  34. expect(store.get("/b")).toBe("hello");
  35. expect(store.getSnapshot()).toEqual({ a: 1, b: "hello" });
  36. });
  37. it("subscribe fires on set", () => {
  38. const { store } = createTestStore({});
  39. const listener = vi.fn();
  40. store.subscribe(listener);
  41. store.set("/x", 1);
  42. expect(listener).toHaveBeenCalledTimes(1);
  43. });
  44. it("subscribe fires on update", () => {
  45. const { store } = createTestStore({});
  46. const listener = vi.fn();
  47. store.subscribe(listener);
  48. store.update({ "/a": 1, "/b": 2 });
  49. expect(listener).toHaveBeenCalledTimes(1);
  50. });
  51. it("unsubscribe stops notifications", () => {
  52. const { store } = createTestStore({});
  53. const listener = vi.fn();
  54. const unsub = store.subscribe(listener);
  55. store.set("/x", 1);
  56. expect(listener).toHaveBeenCalledTimes(1);
  57. unsub();
  58. store.set("/x", 2);
  59. expect(listener).toHaveBeenCalledTimes(1);
  60. });
  61. it("getSnapshot immutability -- previous snapshot is not mutated", () => {
  62. const { store } = createTestStore({ user: { name: "Alice", age: 30 } });
  63. const snap1 = store.getSnapshot();
  64. store.set("/user/name", "Bob");
  65. const snap2 = store.getSnapshot();
  66. expect(snap1.user).toEqual({ name: "Alice", age: 30 });
  67. expect((snap2.user as Record<string, unknown>).name).toBe("Bob");
  68. expect(snap1.user).not.toBe(snap2.user);
  69. });
  70. it("structural sharing -- untouched branches keep references", () => {
  71. const { store } = createTestStore({
  72. a: { x: 1 },
  73. b: { y: 2 },
  74. });
  75. const snap1 = store.getSnapshot();
  76. store.set("/a/x", 99);
  77. const snap2 = store.getSnapshot();
  78. expect(snap2.b).toBe(snap1.b);
  79. expect(snap2.a).not.toBe(snap1.a);
  80. });
  81. it("getServerSnapshot returns same as getSnapshot", () => {
  82. const { store } = createTestStore({ x: 1 });
  83. expect(store.getServerSnapshot!()).toBe(store.getSnapshot());
  84. store.set("/x", 2);
  85. expect(store.getServerSnapshot!()).toBe(store.getSnapshot());
  86. });
  87. it("subscribe does NOT fire when unrelated slice changes", () => {
  88. const uiSlice = createSlice({
  89. name: "ui",
  90. initialState: { count: 0 } as Record<string, unknown>,
  91. reducers: {
  92. replace: (_state, action) => action.payload,
  93. },
  94. });
  95. const otherSlice = createSlice({
  96. name: "other",
  97. initialState: { value: "a" },
  98. reducers: {
  99. update: (state, action) => {
  100. state.value = action.payload;
  101. },
  102. },
  103. });
  104. const reduxStore = configureStore({
  105. reducer: {
  106. ui: uiSlice.reducer,
  107. other: otherSlice.reducer,
  108. },
  109. });
  110. const store = reduxStateStore({
  111. store: reduxStore,
  112. selector: (state) => state.ui as Record<string, unknown>,
  113. dispatch: (next, s) => s.dispatch(uiSlice.actions.replace(next)),
  114. });
  115. const listener = vi.fn();
  116. store.subscribe(listener);
  117. reduxStore.dispatch(otherSlice.actions.update("b"));
  118. expect(listener).not.toHaveBeenCalled();
  119. expect(store.get("/count")).toBe(0);
  120. });
  121. it("set skips dispatch when value is unchanged", () => {
  122. const { store } = createTestStore({ x: 1 });
  123. const snap1 = store.getSnapshot();
  124. const listener = vi.fn();
  125. store.subscribe(listener);
  126. store.set("/x", 1);
  127. expect(listener).not.toHaveBeenCalled();
  128. expect(store.getSnapshot()).toBe(snap1);
  129. });
  130. it("update skips dispatch when no values changed", () => {
  131. const { store } = createTestStore({ a: 1, b: 2 });
  132. const snap1 = store.getSnapshot();
  133. const listener = vi.fn();
  134. store.subscribe(listener);
  135. store.update({ "/a": 1, "/b": 2 });
  136. expect(listener).not.toHaveBeenCalled();
  137. expect(store.getSnapshot()).toBe(snap1);
  138. });
  139. it("works with identity selector (entire state is the model)", () => {
  140. const slice = createSlice({
  141. name: "root",
  142. initialState: { x: 1 } as Record<string, unknown>,
  143. reducers: {
  144. replace: (_state, action) => action.payload,
  145. },
  146. });
  147. const reduxStore = configureStore({
  148. reducer: slice.reducer,
  149. });
  150. const store = reduxStateStore({
  151. store: reduxStore,
  152. selector: (s) => s as Record<string, unknown>,
  153. dispatch: (next, s) => s.dispatch(slice.actions.replace(next)),
  154. });
  155. expect(store.get("/x")).toBe(1);
  156. store.set("/x", 2);
  157. expect(store.get("/x")).toBe(2);
  158. });
  159. it("defaults selector to identity when omitted", () => {
  160. const slice = createSlice({
  161. name: "root",
  162. initialState: { x: 1 } as Record<string, unknown>,
  163. reducers: {
  164. replace: (_state, action) => action.payload,
  165. },
  166. });
  167. const reduxStore = configureStore({
  168. reducer: slice.reducer,
  169. });
  170. const store = reduxStateStore({
  171. store: reduxStore,
  172. dispatch: (next, s) => s.dispatch(slice.actions.replace(next)),
  173. });
  174. expect(store.get("/x")).toBe(1);
  175. store.set("/x", 2);
  176. expect(store.get("/x")).toBe(2);
  177. });
  178. });