deno.land / x / jotai@v1.8.4 / tests / xstate / atomWithMachine.test.tsx

atomWithMachine.test.tsx
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { fireEvent, render, waitFor } from '@testing-library/react'import { assign, createMachine } from 'xstate'import { useAtom } from 'jotai'import { RESTART, atomWithMachine } from 'jotai/xstate'import { StrictModeUnlessVersionedWrite, getTestProvider } from '../testUtils'
const Provider = getTestProvider()
it('toggle machine', async () => { const toggleMachine = createMachine({ id: 'toggle', initial: 'inactive', states: { inactive: { on: { TOGGLE: 'active' }, }, active: { on: { TOGGLE: 'inactive' }, }, }, })
const toggleMachineAtom = atomWithMachine(() => toggleMachine)
const Toggler = () => { const [state, send] = useAtom(toggleMachineAtom)
return ( <button onClick={() => send('TOGGLE')}> {state.value === 'inactive' ? 'Click to activate' : 'Active! Click to deactivate'} </button> ) }
const { findByText, getByText } = render( <StrictModeUnlessVersionedWrite> <Provider> <Toggler /> </Provider> </StrictModeUnlessVersionedWrite> )
await findByText('Click to activate')
fireEvent.click(getByText('Click to activate')) await findByText('Active! Click to deactivate')
fireEvent.click(getByText('Active! Click to deactivate')) await findByText('Click to activate')})
it('restartable machine', async () => { const toggleMachine = createMachine( { id: 'toggle', initial: 'inactive', context: { counter: 0 }, schema: { context: {} as { counter: number } }, on: { DISABLE: '.final' }, states: { inactive: { on: { PRESS: 'active' }, }, active: { on: { PRESS: { actions: 'increment', }, }, }, final: { type: 'final', }, }, }, { actions: { increment: assign({ counter: (prev) => prev.counter + 1, }), }, } )
const toggleMachineAtom = atomWithMachine(() => toggleMachine)
const Toggler = () => { const [state, send] = useAtom(toggleMachineAtom)
return ( <> <button onClick={() => send('PRESS')} disabled={state.value === 'final'}> {state.value === 'inactive' ? 'Click to activate' : state.value === 'active' ? 'Increment' : 'Not actionable'} </button> <div>Count: {state.context.counter}</div> <button onClick={() => { send(state.value === 'final' ? RESTART : 'DISABLE') }}> {state.value === 'final' ? 'Restart machine' : 'Go to final state'} </button> </> ) }
const { findByText, getByText } = render( <StrictModeUnlessVersionedWrite> <Provider> <Toggler /> </Provider> </StrictModeUnlessVersionedWrite> )
await findByText('Click to activate') await findByText('Count: 0')
fireEvent.click(getByText('Click to activate')) await findByText('Increment') await findByText('Count: 0')
fireEvent.click(getByText('Increment')) await findByText('Count: 1')
fireEvent.click(getByText('Increment')) await findByText('Count: 2')
fireEvent.click(getByText('Go to final state')) await findByText('Not actionable')
fireEvent.click(getByText('Restart machine')) await waitFor(() => { expect(findByText('Click to activate')).toBeDefined() expect(findByText('Count: 0')).toBeDefined() })})
jotai

Version Info

Tagged at
a year ago