Skip to content

Commit 884594f

Browse files
test: add unit tests for update functionality in shared state APIs
1 parent 1f96b19 commit 884594f

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

tests/index.test.tsx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@ describe('useSharedState', () => {
159159
// Get updated value
160160
expect(sharedStatesApi.get(sharedCounter)).toBe(200);
161161

162+
// Update the value
163+
act(() => {
164+
sharedStatesApi.update(sharedCounter, (prev) => prev + 50);
165+
});
166+
167+
// Get updated value after update
168+
expect(sharedStatesApi.get(sharedCounter)).toBe(250);
169+
162170
// Clear the value
163171
act(() => {
164172
sharedStatesApi.clear(sharedCounter);
@@ -310,6 +318,20 @@ describe('useSharedFunction', () => {
310318
expect(updatedState.isLoading).toBe(true);
311319
expect(updatedState.error).toBe('test error');
312320

321+
// Update the state
322+
act(() => {
323+
sharedFunctionsApi.update(sharedFunction, (prev) => ({
324+
fnState: {
325+
...prev,
326+
results: 'updated data',
327+
}
328+
}));
329+
});
330+
331+
// Get updated state after update
332+
const updatedState2 = sharedFunctionsApi.get(sharedFunction);
333+
expect(updatedState2.results).toBe('updated data');
334+
313335
// Clear the value
314336
act(() => {
315337
sharedFunctionsApi.clear(sharedFunction);
@@ -374,6 +396,20 @@ describe('useSharedSubscription', () => {
374396
expect(updatedState.isLoading).toBe(true);
375397
expect(updatedState.error).toBe('test error');
376398

399+
// Update the state
400+
act(() => {
401+
sharedSubscriptionsApi.update(sharedSubscription, (prev) => ({
402+
fnState: {
403+
...prev,
404+
data: 'updated data',
405+
}
406+
}));
407+
});
408+
409+
// Get updated state after update
410+
const updatedState2 = sharedSubscriptionsApi.get(sharedSubscription);
411+
expect(updatedState2.data).toBe('updated data');
412+
377413
// Clear the value
378414
act(() => {
379415
sharedSubscriptionsApi.clear(sharedSubscription);
@@ -513,6 +549,20 @@ describe('useSharedSubscription', () => {
513549
expect(updatedState.isLoading).toBe(true);
514550
expect(updatedState.error).toBe('test error');
515551

552+
// Update the state
553+
act(() => {
554+
sharedSubscriptionsApi.update(sharedSubscription, (prev) => ({
555+
fnState: {
556+
...prev,
557+
data: 'updated data',
558+
}
559+
}));
560+
});
561+
562+
// Get updated state after update
563+
const updatedState2 = sharedSubscriptionsApi.get(sharedSubscription);
564+
expect(updatedState2.data).toBe('updated data');
565+
516566
// Clear the value
517567
act(() => {
518568
sharedSubscriptionsApi.clear(sharedSubscription);

0 commit comments

Comments
 (0)