Skip to content

Commit

Permalink
Throw when sending strings (#3967)
Browse files Browse the repository at this point in the history
* Throw if string is sent

* Oops

* Changeset

* Add error for raise and send

* Update packages/core/src/actions/raise.ts

Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>

* Update packages/core/src/actions/send.ts

Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>

* Fix test

---------

Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
  • Loading branch information
davidkpiano and Andarist committed Apr 20, 2023
1 parent eecb31b commit 26986f4
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/eighty-trainers-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'xstate': major
---

Sending a string event to `actor.send('some string')` will now throw a proper error message.
5 changes: 5 additions & 0 deletions packages/core/src/actions/raise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ export function raise<
const delaysMap = state.machine.options.delays;

// TODO: helper function for resolving Expr
if (typeof eventOrExpr === 'string') {
throw new Error(
`Only event objects may be used with raise; use raise({ type: "${eventOrExpr}" }) instead`
);
}
const resolvedEvent = toSCXMLEvent(
typeof eventOrExpr === 'function' ? eventOrExpr(args) : eventOrExpr
);
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/actions/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ export function send<
const delaysMap = state.machine.options.delays;

// TODO: helper function for resolving Expr
if (typeof eventOrExpr === 'string') {
throw new Error(
`Only event objects may be used with sendTo; use sendTo({ type: "${eventOrExpr}" }) instead`
);
}
const resolvedEvent = toSCXMLEvent(
isFunction(eventOrExpr) ? eventOrExpr(args) : eventOrExpr
);
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,12 @@ export class Interpreter<
* @param event The event to send
*/
public send(event: TEvent | SCXML.Event<TEvent>) {
if (typeof event === 'string') {
throw new Error(
`Only event objects may be sent to actors; use .send({ type: "${event}" }) instead`
);
}

const _event = toSCXMLEvent(event);

if (this.status === ActorStatus.Stopped) {
Expand Down
31 changes: 31 additions & 0 deletions packages/core/test/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3065,6 +3065,22 @@ describe('sendTo', () => {

service.send({ type: 'EVENT', value: 'foo' });
});

it('should throw if given a string', () => {
const machine = createMachine({
invoke: {
id: 'child',
src: fromCallback(() => {})
},
entry: sendTo('child', 'a string')
});

expect(() => {
interpret(machine).start();
}).toThrowErrorMatchingInlineSnapshot(
`"Only event objects may be used with sendTo; use sendTo({ type: "a string" }) instead"`
);
});
});

describe('raise', () => {
Expand Down Expand Up @@ -3273,6 +3289,21 @@ describe('raise', () => {
expect(actor.getSnapshot().value).toBe('a');
}, 10);
});

it('should throw if given a string', () => {
const machine = createMachine({
entry: raise(
// @ts-ignore
'a string'
)
});

expect(() => {
interpret(machine).start();
}).toThrowErrorMatchingInlineSnapshot(
`"Only event objects may be used with raise; use raise({ type: "a string" }) instead"`
);
});
});

it('should call transition actions in document order for same-level parallel regions', () => {
Expand Down
13 changes: 13 additions & 0 deletions packages/core/test/interpreter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1844,3 +1844,16 @@ describe('interpreter', () => {
});
});
});

it('should throw if an event is received', () => {
const machine = createMachine({});

const actor = interpret(machine).start();

expect(() =>
actor.send(
// @ts-ignore
'EVENT'
)
).toThrow();
});

0 comments on commit 26986f4

Please sign in to comment.