Skip to content

Commit 5e05d59

Browse files
authored
[@xstate/store] Fix emitted event type being overridden (#5197)
* Add failing test * Fix * Changeset * Remove console log
1 parent a4f9ca3 commit 5e05d59

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

.changeset/lazy-dolphins-sort.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@xstate/store': patch
3+
---
4+
5+
The emitted event type can no longer be accidentally overridden in the emitted event payload. See #5196 for the issue.

packages/xstate-store/src/store.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,8 @@ export function createStoreTransition<
420420
get: (_, eventType: string) => {
421421
return (payload: any) => {
422422
effects.push({
423-
type: eventType,
424-
...payload
423+
...payload,
424+
type: eventType
425425
});
426426
};
427427
}

packages/xstate-store/test/store.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,3 +481,52 @@ it('works with typestates', () => {
481481
context.data satisfies null;
482482
}
483483
});
484+
485+
it('the emit type is not overridden by the payload', () => {
486+
const spy = jest.fn();
487+
type Context = {
488+
drawer?: Drawer | null;
489+
};
490+
491+
type Drawer = {
492+
id: string;
493+
};
494+
495+
const context: Context = {
496+
drawer: null
497+
};
498+
499+
const drawersBridgeStore = createStore({
500+
emits: {
501+
drawerOpened: (_payload: { drawer: Drawer }) => {
502+
// ...
503+
}
504+
},
505+
context,
506+
on: {
507+
openDrawer: (context, event: { drawer: Drawer }, enqueue) => {
508+
enqueue.emit.drawerOpened(event);
509+
510+
return {
511+
...context,
512+
drawer: event.drawer
513+
};
514+
}
515+
}
516+
});
517+
518+
drawersBridgeStore.on('drawerOpened', (event) => {
519+
// expect to be called here
520+
spy(event);
521+
});
522+
523+
drawersBridgeStore.send({
524+
type: 'openDrawer',
525+
drawer: { id: 'a' }
526+
});
527+
528+
expect(spy).toHaveBeenCalledWith({
529+
type: 'drawerOpened',
530+
drawer: { id: 'a' }
531+
});
532+
});

0 commit comments

Comments
 (0)