Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit719348b

Browse files
support cond in tests (#54)
1 parentdbe69ee commit719348b

File tree

4 files changed

+135
-85
lines changed

4 files changed

+135
-85
lines changed

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,10 @@ The component to define which parts of the tree should be rendered for a given s
248248
/>
249249
```
250250

251-
##testStatechart({ statechart[, fixtures] }, Component)
251+
##testStatechart({ statechart[, fixtures][, extendedState] }, Component)
252252

253253
The method to automagically generate tests given a statechart definition, and a component.
254-
It accepts anoptional`fixtures`configuration to describewhich datashould be injected into the component for a given transition.
254+
It accepts anadditional`fixtures`option to describethe datato be injected into the component for a given transition, and an`extendedState` option to control the statechart's conditions - both are optional.
255255

256256
>Please note that the component should be a base component not wrapped into`withStateChart` (see[#46](https://github.com/MicheleBertoli/react-automata/issues/46)).
257257

‎src/testStatechart.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,27 @@ import invariant from 'invariant'
77
import{getContextValue}from'./utils'
88
importwithStatechartfrom'./withStatechart'
99

10-
consttestStatechart=(config,Component)=>{
10+
consttestStatechart=(options,Component)=>{
1111
invariant(
1212
!Component.isStateMachine,
1313
`It seems you are testing a component wrapped into \`withStatechart\`, please use a base component instead.
1414
See https://github.com/MicheleBertoli/react-automata/issues/46`
1515
)
1616

17-
const{channel, statechart}=config
17+
const{statechart, extendedState, channel}=options
1818
constmachine=Machine(statechart)
19-
constpaths=getShortestPaths(machine)
19+
constpaths=getShortestPaths(machine,extendedState)
2020

2121
Object.keys(paths).forEach(key=>{
22-
constinitialData=idx(config,_=>_.fixtures.initialData)
22+
constinitialData=idx(options,_=>_.fixtures.initialData)
2323
constStateMachine=withStatechart(statechart,{ channel})(Component)
2424
constrenderer=TestRenderer.create(
2525
<StateMachineinitialData={initialData}/>
2626
)
2727
constinstance=renderer.getInstance()
2828

2929
paths[key].forEach(({ event, state})=>{
30-
constfixtures=idx(config,_=>_.fixtures[state][event])
30+
constfixtures=idx(options,_=>_.fixtures[state][event])
3131

3232
instance.handleTransition(event,fixtures)
3333
})

‎test/__snapshots__/testStatechart.spec.js.snap‎

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,54 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`action: a 1`]=`
3+
exports[`channels: outer 1`]=`
4+
<div>
5+
outer
6+
<div>
7+
inner
8+
outer
9+
</div>
10+
</div>
11+
`;
12+
13+
exports[`cond fail: a 1`]=`"A"`;
14+
15+
exports[`cond pass: a 1`]=`"A"`;
16+
17+
exports[`cond pass: b 1`]=`"B"`;
18+
19+
exports[`conditional action: a 1`]=`
420
<div>
521
a
622
</div>
723
`;
824

9-
exports[`action: b.a 1`]=`
25+
exports[`conditionalaction: b.a 1`]=`
1026
<div>
1127
b.a
1228
</div>
1329
`;
1430

15-
exports[`action: b.b 1`]=`
31+
exports[`conditionalaction: b.b 1`]=`
1632
<div>
1733
b.b
1834
</div>
1935
`;
2036

21-
exports[`channels: outer 1`]=`
37+
exports[`conditional state: a 1`]=`
2238
<div>
23-
outer
24-
<div>
25-
inner
26-
outer
27-
</div>
39+
a
40+
</div>
41+
`;
42+
43+
exports[`conditional state: b.a 1`]=`
44+
<div>
45+
b.a
46+
</div>
47+
`;
48+
49+
exports[`conditional state: b.b 1`]=`
50+
<div>
51+
b.b
2852
</div>
2953
`;
3054

@@ -243,21 +267,3 @@ exports[`parallel: bold.on,underline.on,italics.on,list.numbers 1`] = `
243267
list.numbers
244268
</div>
245269
`;
246-
247-
exports[`state: a 1`]=`
248-
<div>
249-
a
250-
</div>
251-
`;
252-
253-
exports[`state: b.a 1`]=`
254-
<div>
255-
b.a
256-
</div>
257-
`;
258-
259-
exports[`state: b.b 1`]=`
260-
<div>
261-
b.b
262-
</div>
263-
`;

‎test/testStatechart.spec.js‎

Lines changed: 95 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,69 @@
11
importReactfrom'react'
22
import{Action,State,testStatechart,withStatechart}from'../src'
33

4-
constsecondMachine={
5-
initial:'a',
6-
states:{
7-
a:{
8-
on:{
9-
SECOND_NEXT:'b',
4+
describe('conditional',()=>{
5+
constsecondMachine={
6+
initial:'a',
7+
states:{
8+
a:{
9+
on:{
10+
SECOND_NEXT:'b',
11+
},
12+
onEntry:'enterBA',
1013
},
11-
onEntry:'enterBA',
12-
},
13-
b:{
14-
on:{
15-
SECOND_NEXT:'a',
14+
b:{
15+
on:{
16+
SECOND_NEXT:'a',
17+
},
18+
onEntry:'enterBB',
1619
},
17-
onEntry:'enterBB',
1820
},
19-
},
20-
}
21-
22-
constfirstMachine={
23-
initial:'a',
24-
states:{
25-
a:{
26-
on:{
27-
FIRST_NEXT:'b',
21+
}
22+
23+
constfirstMachine={
24+
initial:'a',
25+
states:{
26+
a:{
27+
on:{
28+
FIRST_NEXT:'b',
29+
},
30+
onEntry:'enterA',
2831
},
29-
onEntry:'enterA',
30-
},
31-
b:{
32-
on:{
33-
FIRST_NEXT:'a',
32+
b:{
33+
on:{
34+
FIRST_NEXT:'a',
35+
},
36+
onEntry:'enterB',
37+
...secondMachine,
3438
},
35-
onEntry:'enterB',
36-
...secondMachine,
3739
},
38-
},
39-
}
40+
}
4041

41-
test('action',()=>{
42-
constApp=()=>(
43-
<div>
44-
<Actionshow="enterA"hide="enterB">
45-
a
46-
</Action>
47-
<Actionshow="enterBA">b.a</Action>
48-
<Actionshow="enterBB">b.b</Action>
49-
</div>
50-
)
42+
test('action',()=>{
43+
constApp=()=>(
44+
<div>
45+
<Actionshow="enterA"hide="enterB">
46+
a
47+
</Action>
48+
<Actionshow="enterBA">b.a</Action>
49+
<Actionshow="enterBB">b.b</Action>
50+
</div>
51+
)
5152

52-
testStatechart({statechart:firstMachine},App)
53-
})
53+
testStatechart({statechart:firstMachine},App)
54+
})
5455

55-
test('state',()=>{
56-
constApp=()=>(
57-
<div>
58-
<Statevalue="a">a</State>
59-
<Statevalue="b.a">b.a</State>
60-
<Statevalue="b.b">b.b</State>
61-
</div>
62-
)
56+
test('state',()=>{
57+
constApp=()=>(
58+
<div>
59+
<Statevalue="a">a</State>
60+
<Statevalue="b.a">b.a</State>
61+
<Statevalue="b.b">b.b</State>
62+
</div>
63+
)
6364

64-
testStatechart({statechart:firstMachine},App)
65+
testStatechart({statechart:firstMachine},App)
66+
})
6567
})
6668

6769
test('parallel',()=>{
@@ -171,3 +173,45 @@ test('channels', () => {
171173

172174
testStatechart({statechart:outer,channel:'outer'},Outer)
173175
})
176+
177+
describe('cond',()=>{
178+
conststatechart={
179+
initial:'a',
180+
states:{
181+
a:{
182+
on:{
183+
EVENT:{
184+
b:{
185+
cond:extState=>extState.shouldPass,
186+
},
187+
},
188+
},
189+
},
190+
b:{},
191+
},
192+
}
193+
194+
constCond=()=>(
195+
<React.Fragment>
196+
<Statevalue="a">A</State>
197+
<Statevalue="b">B</State>
198+
</React.Fragment>
199+
)
200+
201+
test('pass',()=>{
202+
constextendedState={
203+
shouldPass:true,
204+
}
205+
constfixtures={
206+
a:{
207+
EVENT:extendedState,
208+
},
209+
}
210+
211+
testStatechart({ statechart, fixtures, extendedState},Cond)
212+
})
213+
214+
test('fail',()=>{
215+
testStatechart({ statechart},Cond)
216+
})
217+
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp