Skip to content

Commit

Permalink
Add tests + changeset
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkpiano committed Sep 27, 2021
1 parent 39fcac7 commit a728060
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
17 changes: 17 additions & 0 deletions .changeset/young-spoons-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
'xstate': patch
---

Meta data can now be specified for `invoke` configs in the `invoke.meta` property:

```js
const machine = createMachine({
// ...
invoke: {
src: (ctx, e) => findUser(ctx.userId),
meta: {
api: 'User Finder 2000'
}
}
});
```
3 changes: 2 additions & 1 deletion packages/core/src/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,8 @@ export class Interpreter<
let source: Spawnable = isFunction(serviceCreator)
? serviceCreator(context, _event.data, {
data: resolvedData,
src: invokeSource
src: invokeSource,
meta: activity.meta
})
: serviceCreator;

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ export type InvokeCallback<
export interface InvokeMeta {
data: any;
src: InvokeSourceDefinition;
meta?: MetaObject;
}

/**
Expand Down
35 changes: 27 additions & 8 deletions packages/core/test/invoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2601,17 +2601,36 @@ describe('invoke', () => {
.start();
});

it('should show meta data', () => {
const machine = createMachine({
invoke: {
src: 'someSource',
meta: {
url: 'stately.ai'
describe('meta data', () => {
it('should show meta data', () => {
const machine = createMachine({
invoke: {
src: 'someSource',
meta: {
url: 'stately.ai'
}
}
}
});

expect(machine.invoke[0].meta).toEqual({ url: 'stately.ai' });
});

expect(machine.invoke[0].meta).toEqual({ url: 'stately.ai' });
it('meta data should be available in the invoke source function', () => {
expect.assertions(1);
const machine = createMachine({
invoke: {
src: (_ctx, _e, { meta }) => {
expect(meta).toEqual({ url: 'stately.ai' });
return Promise.resolve();
},
meta: {
url: 'stately.ai'
}
}
});

interpret(machine).start();
});
});
});

Expand Down

0 comments on commit a728060

Please sign in to comment.