Skip to content

Commit b7b6343

Browse files
test: add unit test for subscribing to state changes in shared API
1 parent 4649321 commit b7b6343

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

tests/index.test.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,26 @@ describe('useSharedState', () => {
175175
// Get value after clear (should be initial value because createSharedState re-initializes it)
176176
expect(sharedStatesApi.get(sharedCounter)).toBe(100);
177177
});
178+
179+
it('should be able to subscribe to state changes from api', () => {
180+
const sharedCounter = createSharedState(100);
181+
182+
const subscribeCallback = vi.fn();
183+
184+
act(() => {
185+
sharedStatesApi.subscribe(sharedCounter, () => {
186+
subscribeCallback();
187+
expect(sharedStatesApi.get(sharedCounter)).toBe(200);
188+
});
189+
});
190+
191+
// Update the value
192+
act(() => {
193+
sharedStatesApi.set(sharedCounter,200);
194+
});
195+
196+
expect(subscribeCallback).toHaveBeenCalledTimes(1);
197+
});
178198
});
179199

180200
describe('useSharedFunction', () => {

0 commit comments

Comments
 (0)