Skip to content

Commit 91ad513

Browse files
committed
test: add testcase for tracked
1 parent 7d24ba0 commit 91ad513

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

packages/__tests__/update.test.tsx

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,51 @@ describe('update', () => {
6464

6565
expect(() => wrapper.unmount()).not.toThrow()
6666
})
67+
68+
it('components should be treacked', () => {
69+
let renderCount = 0
70+
const { withContext, useTracked, useUpdates } = Backset.create({
71+
test: 'value1',
72+
other: 0,
73+
})
74+
const MockComponent1 = () => {
75+
const { test, other } = useUpdates()
76+
return (
77+
<div>
78+
<button onClick={e => test((e.target as any).value)} id="btn" />
79+
<button onClick={() => other(last => last + 1)} id="other" />
80+
</div>
81+
)
82+
}
83+
const MockComponent2 = () => {
84+
const { test } = useTracked(['test'])
85+
useEffect(() => {
86+
renderCount++
87+
})
88+
return <span id="value">{test}</span>
89+
}
90+
const MockWrapper = withContext(() => {
91+
return (
92+
<div>
93+
<MockComponent1 />
94+
<MockComponent2 />
95+
</div>
96+
)
97+
})
98+
const wrapper = mount(<MockWrapper />)
99+
expect(wrapper.find('#value').text()).toEqual('value1')
100+
expect(renderCount).toBe(1)
101+
102+
const btn = wrapper.find('#btn')
103+
simulateClick(btn, 'value2')
104+
expect(wrapper.find('#value').text()).toEqual('value2')
105+
expect(renderCount).toBe(2)
106+
107+
const otherButton = wrapper.find('#other')
108+
simulateClick(otherButton, '')
109+
simulateClick(otherButton, '')
110+
simulateClick(otherButton, '')
111+
expect(wrapper.find('#value').text()).toEqual('value2')
112+
expect(renderCount).toBe(2)
113+
})
67114
})

0 commit comments

Comments
 (0)