Skip to content

Commit c645452

Browse files
committed
Fix regression in memoryStore
1 parent a7e87a3 commit c645452

File tree

2 files changed

+30
-34
lines changed

2 files changed

+30
-34
lines changed

packages/ra-core/src/store/memoryStore.spec.ts renamed to packages/ra-core/src/store/memoryStore.spec.tsx

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import * as React from 'react';
12
import expect from 'expect';
2-
3+
import { render, screen } from '@testing-library/react';
34
import { memoryStore } from './memoryStore';
5+
import { useStore } from './useStore';
6+
import { StoreContextProvider } from './StoreContextProvider';
47

58
describe('memoryStore', () => {
69
it('should allow to store and retrieve a value', () => {
@@ -55,16 +58,33 @@ describe('memoryStore', () => {
5558
const initialStorage = {
5659
user: {
5760
name: 'John',
58-
settings: {
59-
theme: 'dark',
60-
},
61+
},
62+
'user.settings': {
63+
theme: 'dark',
6164
},
6265
};
6366

6467
const store = memoryStore(initialStorage);
6568

66-
expect(store.getItem('user.name')).toEqual('John');
67-
expect(store.getItem('user.settings.theme')).toEqual('dark');
69+
expect(store.getItem('user')).toEqual({ name: 'John' });
70+
expect(store.getItem('user.settings')).toEqual({ theme: 'dark' });
6871
});
6972
});
73+
74+
it('preserves the initial value in StrictMode', async () => {
75+
const Component = () => {
76+
const [value] = useStore('user', 'Not me');
77+
return <>{value}</>;
78+
};
79+
80+
render(
81+
<React.StrictMode>
82+
<StoreContextProvider value={memoryStore({ user: 'John' })}>
83+
<Component />
84+
</StoreContextProvider>
85+
</React.StrictMode>
86+
);
87+
88+
await screen.findByText('John');
89+
});
7090
});

packages/ra-core/src/store/memoryStore.tsx

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ export const memoryStore = (
2222
initialStorage: Record<string, any> = {}
2323
): Store => {
2424
// Use a flat Map to store key-value pairs directly without treating dots as nested paths
25-
const storage = new Map<string, any>(
26-
Object.entries(flatten(initialStorage))
27-
);
25+
let storage = new Map<string, any>(Object.entries(initialStorage));
2826
const subscriptions: { [key: string]: Subscription } = {};
2927

3028
const publish = (key: string, value: any) => {
@@ -37,7 +35,9 @@ export const memoryStore = (
3735
};
3836

3937
return {
40-
setup: () => {},
38+
setup: () => {
39+
storage = new Map<string, any>(Object.entries(initialStorage));
40+
},
4141
teardown: () => {
4242
storage.clear();
4343
},
@@ -88,27 +88,3 @@ export const memoryStore = (
8888
},
8989
};
9090
};
91-
92-
// taken from https://stackoverflow.com/a/19101235/1333479
93-
const flatten = (data: any) => {
94-
const result = {};
95-
function doFlatten(current, prop) {
96-
if (Object(current) !== current) {
97-
// scalar value
98-
result[prop] = current;
99-
} else if (Array.isArray(current)) {
100-
// array
101-
result[prop] = current;
102-
} else {
103-
// object
104-
let isEmpty = true;
105-
for (const p in current) {
106-
isEmpty = false;
107-
doFlatten(current[p], prop ? prop + '.' + p : p);
108-
}
109-
if (isEmpty && prop) result[prop] = {};
110-
}
111-
}
112-
doFlatten(data, '');
113-
return result;
114-
};

0 commit comments

Comments
 (0)