-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Parse machine context into an object * changeset * Parse -> Extract Co-authored-by: Mateusz Burzyński <[email protected]> * Use a special handler for parsing root state node to keep extracting context only in the root state node * Extract lazy context as expression string * Update packages/machine-extractor/src/__tests__/context.test.ts Co-authored-by: Mateusz Burzyński <[email protected]> * Update packages/machine-extractor/src/__tests__/context.test.ts Co-authored-by: Mateusz Burzyński <[email protected]> --------- Co-authored-by: Mateusz Burzyński <[email protected]>
- Loading branch information
Showing
4 changed files
with
119 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@xstate/machine-extractor': minor | ||
--- | ||
|
||
Extract machine context into an object |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { extractMachinesFromFile } from '..'; | ||
|
||
describe('Context', () => { | ||
it('Should be able to extract machine context into an object', () => { | ||
const result = extractMachinesFromFile(` | ||
createMachine({ | ||
context: { | ||
str: 'some string', | ||
num: 2, | ||
bool: true, | ||
arr: [1,2,3], | ||
obj: { | ||
str: 'some string', | ||
num: 2, | ||
bool: true, | ||
arr: [1,2,3], | ||
obj: { | ||
a: 2 | ||
} | ||
} | ||
} | ||
}) | ||
`); | ||
|
||
expect(result?.machines[0]?.toConfig()).toMatchInlineSnapshot(` | ||
{ | ||
"context": { | ||
"arr": [ | ||
1, | ||
2, | ||
3, | ||
], | ||
"bool": true, | ||
"num": 2, | ||
"obj": { | ||
"arr": [ | ||
1, | ||
2, | ||
3, | ||
], | ||
"bool": true, | ||
"num": 2, | ||
"obj": { | ||
"a": 2, | ||
}, | ||
"str": "some string", | ||
}, | ||
"str": "some string", | ||
}, | ||
} | ||
`); | ||
}); | ||
|
||
it('Should only account for context on the root node', () => { | ||
const result = extractMachinesFromFile(` | ||
createMachine({ | ||
initial: 'a', | ||
states: { | ||
a: { | ||
context: { | ||
foo: 'bar' | ||
} | ||
} | ||
} | ||
}) | ||
`); | ||
|
||
expect(result?.machines[0]?.toConfig()).toMatchInlineSnapshot(` | ||
{ | ||
"initial": "a", | ||
"states": { | ||
"a": {}, | ||
}, | ||
} | ||
`); | ||
}); | ||
|
||
it('Should extract lazy context as an expression string', () => { | ||
const result = extractMachinesFromFile(` | ||
createMachine({ | ||
context: () => ({}) | ||
}) | ||
`); | ||
|
||
expect(result?.machines[0]?.toConfig()).toMatchInlineSnapshot(` | ||
{ | ||
"context": "{{() => ({})}}", | ||
} | ||
`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters