Skip to content
Merged
Prev Previous commit
Next Next commit
refactor(components): simplify swiper lifecycle ownership
  • Loading branch information
wangfangjie authored and Single-Dancer committed Oct 19, 2026
commit a92ad91c77c4f6d525e2361eb4cdd067b8653d8b
4 changes: 3 additions & 1 deletion examples/swiper-lifecycle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ PR 分支:同一操作顺序没有捕获到异常;旧实例被销毁,重

## 自动化回归

`packages/taro-components/__tests__/swiper-lifecycle.spec.tsx` 使用现有 `newSpecPage` 和 Swiper mock 覆盖 6 个生命周期场景。最初 5 个场景在 main 基线失败;本轮新增索引覆盖回归,并将失效节点用例对齐真实 slidesEl 路径,两个针对用例在本轮修复前失败、修复后通过。当前 13 个 Swiper 用例及 7 个快照通过。mock 场景与上面的真实浏览器结果分别记录。
`packages/taro-components/__tests__/swiper-lifecycle.spec.tsx` 使用现有 `newSpecPage` 和 Swiper mock 覆盖 8 个生命周期场景。最初 5 个场景在 main 基线失败;本轮新增索引覆盖回归,并将失效节点用例对齐真实 slidesEl 路径,两个针对用例在本轮修复前失败、修复后通过。当前 15 个 Swiper 用例及 7 个快照通过,新增旧实例事件隔离和子节点更新期间事件抑制回归。mock 场景与上面的真实浏览器结果分别记录。

当前实现仅在属性更新、外部子节点更新及连接/断开入口管理实例;不依赖 componentDidRender 重建。实例初始化成功后才保存引用,current 恢复索引显式传入 reset,不再维护额外 pending 标志。
35 changes: 34 additions & 1 deletion packages/taro-components/__tests__/swiper-lifecycle.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ describe('Swiper lifecycle', () => {
expect(component.swiper.wrapperEl.contains(child)).toBe(true)
})

it('preserves pending current through invalid-instance recovery and later saves the active slide', async () => {
it('applies current during invalid-instance recovery and later saves the active slide', async () => {
const component = await mount()
const instance = component.swiper
instance.realIndex = 1
Expand All @@ -143,6 +143,7 @@ describe('Swiper lifecycle', () => {
await page.waitForChanges()

expect(component.swiper).not.toBe(instance)
expect(instance.destroyed).toBe(true)
expect(component.swiper.realIndex).toBe(2)

// 待应用索引生效后,普通卸载仍应保存用户后来滑到的位置
Expand All @@ -154,4 +155,36 @@ describe('Swiper lifecycle', () => {

expect(component.swiper.realIndex).toBe(1)
})

it('ignores events from the replaced instance', async () => {
const component = await mount()
const previous = component.swiper
const touchEnd = previous.params.on!.touchEnd!
page.root!.circular = false
await page.waitForChanges()
const start = jest.spyOn(previous.autoplay, 'start')

// 销毁前排队的回调不能重新启动旧实例
touchEnd.call(previous, previous, {} as PointerEvent)

expect(component.swiper).not.toBe(previous)
expect(start).not.toHaveBeenCalled()
expect(previous.destroyed).toBe(true)
})

it('does not emit slide changes while restoring loop order for a child update', async () => {
const component = await mount()
const instance = component.swiper
const change = jest.spyOn(component.onChange, 'emit')
jest.spyOn(instance, 'loopDestroy').mockImplementation(() => {
instance.realIndex = 1
instance.params.on!.slideChange!.call(instance, instance)
})

page.root!.appendChild(page.doc.createElement('taro-swiper-item-core'))

expect(change).not.toHaveBeenCalled()
page.root!.remove()
await page.waitForChanges()
})
})
Loading