Skip to content

Commit 4675227

Browse files
committed
test: update Controller tests and remove getTestHelpers function
1 parent 4911e93 commit 4675227

File tree

2 files changed

+13
-52
lines changed

2 files changed

+13
-52
lines changed

packages/core/src/Controller.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,24 @@ describe('Controller', () => {
3636
describe('when the "to" prop is an async function', () => {
3737
it('acts strangely without the "from" prop', async () => {
3838
const ctrl = new Controller<{ x: number }>()
39+
40+
const { springs } = ctrl
3941
ctrl.start({
4042
to: async update => {
4143
// The spring does not exist yet!
42-
expect(ctrl.get('x')).toBeUndefined()
44+
expect(springs.x).toBeUndefined()
4345

4446
// Any values passed here are treated as "from" values,
4547
// because no "from" prop was ever given.
4648
update({ x: 1 })
4749
// Now the spring exists!
48-
expect(ctrl.get('x')).toBeDefined()
50+
expect(springs.x).toBeDefined()
4951
// But the spring is idle!
50-
expect(ctrl.get('x').idle).toBeTruthy()
52+
expect(springs.x.idle).toBeTruthy()
5153

5254
// This call *will* start an animation!
5355
update({ x: 2 })
54-
expect(ctrl.get('x').idle).toBeFalsy()
56+
expect(springs.x.idle).toBeFalsy()
5557
},
5658
})
5759

packages/core/test/setup.ts

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -54,74 +54,33 @@ global.advanceUntilValue = (spring, value) => {
5454
})
5555
}
5656

57-
global.getFrames = (arg: SpringValue | Controller) => {
58-
const { frames, testIdle, reset } = getTestHelpers(arg)
59-
// expect(spring.animation.values).not.toEqual([])
57+
global.getFrames = (ctrl: SpringValue | Controller) => {
58+
const frames: any[] = []
6059

6160
let steps = 0
62-
while (!testIdle()) {
61+
while (!ctrl.idle) {
6362
mockRaf.step()
63+
frames.push(ctrl.get())
6464
if (++steps > 1e5) {
6565
throw Error('Infinite loop detected')
6666
}
6767
}
6868

69-
reset()
7069
return frames
7170
}
7271

7372
global.getAsyncFrames = async ctrl => {
74-
const { frames, testIdle, reset } = getTestHelpers(ctrl)
73+
const frames: any[] = []
7574

7675
let steps = 0
77-
while (!testIdle()) {
76+
while (!ctrl.idle) {
7877
mockRaf.step()
78+
frames.push(ctrl.get())
7979
await Promise.resolve()
8080
if (++steps > 1e5) {
8181
throw Error('Infinite loop detected')
8282
}
8383
}
8484

85-
reset()
8685
return frames
8786
}
88-
89-
function getTestHelpers(arg: Controller<any> | SpringValue<any>) {
90-
const frames: any[] = []
91-
92-
const ctrl = arg instanceof Controller ? arg : null
93-
if (ctrl) {
94-
const { onFrame } = ctrl['_state']
95-
ctrl['_state'].onFrame = (frame: any) => {
96-
frames.push(frame)
97-
if (onFrame) onFrame(frame)
98-
}
99-
return {
100-
frames,
101-
testIdle: () =>
102-
!ctrl['_state'].promise &&
103-
Object.values(ctrl.springs).every(spring => spring!.idle),
104-
reset() {
105-
ctrl['_state'].onFrame = onFrame
106-
},
107-
}
108-
}
109-
110-
const spring = arg instanceof SpringValue ? arg : null
111-
if (spring) {
112-
const onChange = spring['_onChange']
113-
spring['_onChange'] = (value, idle) => {
114-
frames.push(value)
115-
onChange.call(spring, value, idle)
116-
}
117-
return {
118-
frames,
119-
testIdle: () => spring.idle && !spring['_state'].promise,
120-
reset() {
121-
spring['_onChange'] = onChange
122-
},
123-
}
124-
}
125-
126-
throw Error('Invalid argument')
127-
}

0 commit comments

Comments
 (0)