|
1 | 1 | import { syncDriver } from "../../animation/__tests__/utils"
|
2 | 2 | import { motionValue } from "../index"
|
3 |
| -import { springValue } from "../spring-value" |
| 3 | +import { attachSpring, springValue } from "../spring-value" |
4 | 4 |
|
5 | 5 | describe("springValue types", () => {
|
6 | 6 | test("can create a motion value from a number", () => {
|
@@ -140,16 +140,49 @@ const runSpringTests = (unit?: string | undefined) => {
|
140 | 140 | expect(resolved).toEqual([createValue(100)])
|
141 | 141 | })
|
142 | 142 |
|
143 |
| - test("unsubscribes when destroyed", () => { |
144 |
| - const a = motionValue(createValue(0)) |
| 143 | + test("unsubscribes when spring is destroyed", () => { |
| 144 | + const source = motionValue(createValue(0)) |
| 145 | + const spring = springValue(source) |
145 | 146 |
|
146 |
| - const b = springValue(a) |
147 |
| - springValue(a) |
| 147 | + expect((source as any).events.change.getSize()).toBe(1) |
| 148 | + expect((spring as any).events.destroy.getSize()).toBe(1) |
148 | 149 |
|
149 |
| - b.destroy() |
| 150 | + spring.destroy() |
150 | 151 |
|
151 | 152 | // Cast to any here as `.events` is private API
|
152 |
| - expect((a as any).events.change.getSize()).toBe(1) |
| 153 | + expect((source as any).events.change.getSize()).toBe(0) |
| 154 | + expect((spring as any).events.destroy.getSize()).toBe(0) |
| 155 | + }) |
| 156 | + |
| 157 | + test("unsubscribes when source is destroyed", () => { |
| 158 | + const source = motionValue(createValue(0)) |
| 159 | + springValue(source) |
| 160 | + |
| 161 | + expect((source as any).events.change.getSize()).toBe(1) |
| 162 | + |
| 163 | + source.destroy() |
| 164 | + |
| 165 | + // Cast to any here as `.events` is private API |
| 166 | + expect((source as any).events.change.getSize()).toBe(0) |
| 167 | + }) |
| 168 | + |
| 169 | + test("Cleanup function works as expected", () => { |
| 170 | + const source = motionValue(createValue(0)) |
| 171 | + const spring = motionValue(createValue(0)) |
| 172 | + |
| 173 | + const clean = attachSpring(spring, source) |
| 174 | + clean() |
| 175 | + const clean2 = attachSpring(spring, source) |
| 176 | + |
| 177 | + expect((source as any).events.change.getSize()).toBe(1) |
| 178 | + expect((spring as any).events.destroy.getSize()).toBe(1) |
| 179 | + |
| 180 | + clean2() |
| 181 | + const clean3 = attachSpring(spring, source) |
| 182 | + clean3() |
| 183 | + |
| 184 | + expect((source as any).events.change.getSize()).toBe(0) |
| 185 | + expect((spring as any).events.destroy.getSize()).toBe(0) |
153 | 186 | })
|
154 | 187 | })
|
155 | 188 | }
|
|
0 commit comments