Skip to content

Commit

Permalink
fix reuse strategy not honored on Any render node (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
lkzhao authored Feb 22, 2024
1 parent 7eaefae commit d7f3ad2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Sources/UIComponent/Core/Model/RenderNode/AnyRenderNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public struct AnyRenderNode: RenderNode {
erasing._updateView(view)
}
public func makeView() -> UIView {
erasing._makeView()
erasing.makeView()
}
}

Expand Down Expand Up @@ -90,6 +90,6 @@ public struct AnyRenderNodeOfView<View: UIView>: RenderNode {
erasing._updateView(view)
}
public func makeView() -> View {
erasing._makeView() as! View
erasing.makeView() as! View
}
}
40 changes: 40 additions & 0 deletions Tests/UIComponentTests/ReuseTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,46 @@ final class ReuseTests: XCTestCase {
XCTAssertNotEqual(existingLabel, newLabel)
}

func testNoReuseWhenNoReuseStrategyWithAnyComponentOfView() {
componentView.component = Text("1").eraseToAnyComponentOfView().reuseStrategy(.noReuse)
componentView.reloadData()
XCTAssertEqual(componentView.subviews.count, 1)
let existingLabel = componentView.subviews.first as? UILabel
XCTAssertNotNil(existingLabel)
XCTAssertEqual(existingLabel?.text, "1")
componentView.component = VStack {
Text("2").eraseToAnyComponentOfView().reuseStrategy(.noReuse)
}
componentView.reloadData()
XCTAssertEqual(componentView.subviews.count, 1)
let newLabel = componentView.subviews.first as? UILabel
XCTAssertNotNil(newLabel)
XCTAssertEqual(newLabel?.text, "2")

// the UILabel should not be reused
XCTAssertNotEqual(existingLabel, newLabel)
}

func testNoReuseWhenNoReuseStrategyWithAnyComponent() {
componentView.component = Text("1").eraseToAnyComponent().reuseStrategy(.noReuse)
componentView.reloadData()
XCTAssertEqual(componentView.subviews.count, 1)
let existingLabel = componentView.subviews.first as? UILabel
XCTAssertNotNil(existingLabel)
XCTAssertEqual(existingLabel?.text, "1")
componentView.component = VStack {
Text("2").eraseToAnyComponent().reuseStrategy(.noReuse)
}
componentView.reloadData()
XCTAssertEqual(componentView.subviews.count, 1)
let newLabel = componentView.subviews.first as? UILabel
XCTAssertNotNil(newLabel)
XCTAssertEqual(newLabel?.text, "2")

// the UILabel should not be reused
XCTAssertNotEqual(existingLabel, newLabel)
}

func testReuseWithSameAttributes() {
componentView.component = Text("1").backgroundColor(.red)
componentView.reloadData()
Expand Down

0 comments on commit d7f3ad2

Please sign in to comment.