Files
pkurchatov 718eec9f90 Swift Testing
commit_hash:9b1d9b2e0d20d42afde551f6e6bb29a694ce8a4f
2026-01-28 00:59:47 +03:00

102 lines
2.4 KiB
Swift

@testable @_spi(Internal) import DivKit
import DivKitTestsSupport
import LayoutKit
import Testing
import UIKit
@MainActor
@Suite
struct DivViewTests {
private let components = DivKitComponents()
private let divView: DivView
init() {
divView = DivView(divKitComponents: components)
}
@Test
func visibilityActions_afterZeroFrame() async {
await divView.setData(appearTestData)
divView.appear()
divView.disappear()
divView.appear()
let delays = divView.visibilityHierarchyDepth()
await skipMainRunLoopCycles(delays)
#expect(components.visibilityCounter.visibilityCount(for: appearPath) == 2)
}
@Test
func disappearActions_afterZeroFrame() async {
await divView.setData(disappearTestData)
divView.appear()
divView.disappear()
divView.appear()
divView.disappear()
let delays = divView.visibilityHierarchyDepth()
await skipMainRunLoopCycles(delays)
#expect(components.visibilityCounter.visibilityCount(for: disappearPath) == 2)
}
}
extension DivView {
fileprivate func setData(_ data: DivData) async {
await setSource(.init(kind: .divData(data), cardId: "card"))
frame = testFrame
}
fileprivate func appear() {
onVisibleBoundsChanged(to: testFrame)
forceLayout()
delay()
}
fileprivate func disappear() {
onVisibleBoundsChanged(to: .zero)
forceLayout()
delay()
}
}
private func delay() {
RunLoop.current.run(until: Date().addingTimeInterval(0.01))
}
private let testFrame = CGRect(x: 0, y: 0, width: 100, height: 100)
private let appearPath = UIElementPath("card") + "0" + "text" + "appear" + "appear_action"
private let disappearPath = UIElementPath("card") + "0" + "text" + "disappear" + "disappear_action"
private let appearTestData = divData(
divText(
text: "Sample",
width: .divFixedSize(DivFixedSize(value: .value(100))),
visibilityActions: [
DivVisibilityAction(
logId: .value("appear_action"),
logLimit: .value(10),
visibilityDuration: .value(0)
),
]
)
)
private let disappearTestData = divData(
divText(
disappearActions: [
DivDisappearAction(
disappearDuration: .value(0),
logId: .value("disappear_action"),
logLimit: .value(10),
visibilityPercentage: .value(0)
),
],
text: "Sample",
width: .divFixedSize(DivFixedSize(value: .value(100)))
)
)