Skip to content

Commit

Permalink
Fix: update types for invoked service target to allow string (#3745)
Browse files Browse the repository at this point in the history
* fix: update types for InvokeConfig to allow string in onDone

* chore: add changeset

* refactor: leverage TransitionConfigOrTarget in InvokeConfig
  • Loading branch information
viglucci committed Jan 5, 2023
1 parent 1da542e commit 8cc70d2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/serious-chairs-try.md
@@ -0,0 +1,5 @@
---
'xstate': patch
---

Fix types to allow for string state name in service onDone/onError config (#34)
4 changes: 2 additions & 2 deletions packages/core/src/types.ts
Expand Up @@ -514,13 +514,13 @@ export interface InvokeConfig<TContext, TEvent extends EventObject> {
*/
onDone?:
| string
| SingleOrArray<TransitionConfig<TContext, DoneInvokeEvent<any>>>;
| SingleOrArray<TransitionConfigOrTarget<TContext, DoneInvokeEvent<any>>>;
/**
* The transition to take upon the invoked child machine sending an error event.
*/
onError?:
| string
| SingleOrArray<TransitionConfig<TContext, DoneInvokeEvent<any>>>;
| SingleOrArray<TransitionConfigOrTarget<TContext, DoneInvokeEvent<any>>>;
/**
* Meta data related to this invocation
*/
Expand Down
47 changes: 47 additions & 0 deletions packages/core/test/types.test.ts
Expand Up @@ -668,3 +668,50 @@ describe('spawn', () => {
});
});
});

describe('service-targets', () => {
it('should work with a service that uses strings for both targets', () => {
const machine = createMachine({
invoke: {
src: () => new Promise((resolve) => resolve(1)),
onDone: ['a', 'b']
},
states: {
a: {},
b: {}
}
});
noop(machine);
expect(true).toBeTruthy();
});

it('should work with a service that uses TransitionConfigs for both targets', () => {
const machine = createMachine({
invoke: {
src: () => new Promise((resolve) => resolve(1)),
onDone: [{ target: 'a' }, { target: 'b' }]
},
states: {
a: {},
b: {}
}
});
noop(machine);
expect(true).toBeTruthy();
});

it('should work with a service that uses a string for one target and a TransitionConfig for another', () => {
const machine = createMachine({
invoke: {
src: () => new Promise((resolve) => resolve(1)),
onDone: [{ target: 'a' }, 'b']
},
states: {
a: {},
b: {}
}
});
noop(machine);
expect(true).toBeTruthy();
});
});

0 comments on commit 8cc70d2

Please sign in to comment.