update and extend test

This commit is contained in:
Lukas Stabe 2023-01-02 07:55:18 +01:00
parent 09aa4369b6
commit e961334ba7
4 changed files with 45 additions and 21 deletions

View File

@ -137,7 +137,7 @@ public final class TestFiberElement: FiberElement, CustomStringConvertible {
public static var root: Self { .init(renderedValue: "<root>", closingTag: "</root>") }
}
public struct TestFiberRenderer: FiberRenderer {
public final class TestFiberRenderer: FiberRenderer {
public let sceneSize: CurrentValueSubject<CGSize, Never>
public let useDynamicLayout: Bool
@ -171,13 +171,15 @@ public struct TestFiberRenderer: FiberRenderer {
view is TestFiberPrimitive
}
public func commit(_ mutations: [Mutation<Self>]) {
public func commit(_ mutations: [Mutation<TestFiberRenderer>]) {
for mutation in mutations {
switch mutation {
case let .insert(element, parent, index):
parent.children.insert(element, at: index)
case let .remove(element, parent):
parent?.children.removeAll(where: { $0 === element })
guard let idx = parent?.children.firstIndex(where: { $0 === element })
else { fatalError("remove called with element that doesn't belong to its parent") }
parent?.children.remove(at: idx)
case let .replace(parent, previous, replacement):
guard let index = parent.children.firstIndex(where: { $0 === previous })
else { continue }
@ -192,7 +194,21 @@ public struct TestFiberRenderer: FiberRenderer {
}
}
public func render<V: View>(_ view: V) -> FiberReconciler<TestFiberRenderer> {
let ret = FiberReconciler(self, view)
flush()
return ret
}
var scheduledActions: [() -> Void] = []
public func schedule(_ action: @escaping () -> ()) {
action()
scheduledActions.append(action)
}
public func flush() {
let actions = scheduledActions
scheduledActions = []
for a in actions { a() }
}
}

View File

@ -17,7 +17,7 @@
import Foundation
@_spi(TokamakCore)
@_spi(TokamakCore) @testable
import TokamakCore
/// A proxy for an identified view in the `TestFiberRenderer`.
@ -63,6 +63,11 @@ public struct TestViewProxy<V: View> {
public subscript<T>(dynamicMember member: KeyPath<V, T>) -> T? {
self.view?[keyPath: member]
}
public func tap() where V == Button<Text> {
self.action?()
reconciler.renderer.flush()
}
}
/// An erased `IdentifiedView`.

View File

@ -84,8 +84,7 @@ final class VaryingPrimitivenessTests: XCTestCase {
XCTAssertEqual(root.children.count, 1) // button style
XCTAssertEqual(root.children[0].children.count, 1) // text
reconciler.findView(id: "a.1", as: Button<Text>.self).action?()
reconciler.findView(id: "a.1", as: Button<Text>.self).action?()
reconciler.findView(id: "a.1", as: Button<Text>.self).tap()
XCTAssertEqual(root.children.count, 1)
XCTAssert(root.children[0].description.contains("VStack"))
@ -93,8 +92,7 @@ final class VaryingPrimitivenessTests: XCTestCase {
XCTAssert(root.children[0].children[0].description.contains("Text"))
XCTAssert(root.children[0].children[1].description.contains("ButtonStyle"))
reconciler.findView(id: "b.zwei", as: Button<Text>.self).action?()
reconciler.findView(id: "b.zwei", as: Button<Text>.self).action?()
reconciler.findView(id: "b.zwei", as: Button<Text>.self).tap()
XCTAssertEqual(root.children.count, 1)
XCTAssert(root.children[0].description.contains("VStack"))
@ -103,26 +101,32 @@ final class VaryingPrimitivenessTests: XCTestCase {
XCTAssert(root.children[0].children[1].description.contains("Text"))
XCTAssert(root.children[0].children[2].description.contains("ButtonStyle"))
reconciler.findView(id: "c.i = 2", as: Button<Text>.self).action?()
reconciler.findView(id: "c.i = 2", as: Button<Text>.self).action?()
reconciler.findView(id: "c.i = 2", as: Button<Text>.self).tap()
XCTAssertEqual(root.children[0].children.count, 3) // stack content
reconciler.findView(id: "d.back", as: Button<Text>.self).action?()
reconciler.findView(id: "d.back", as: Button<Text>.self).action?()
reconciler.findView(id: "d.back", as: Button<Text>.self).tap()
XCTAssertEqual(root.children.count, 1)
XCTAssert(root.children[0].description.contains("ButtonStyle"))
XCTAssertEqual(root.children[0].children.count, 1)
XCTAssert(root.children[0].children[0].description.contains("Text"))
reconciler.findView(id: "a.1", as: Button<Text>.self).action?()
reconciler.findView(id: "a.1", as: Button<Text>.self).action?()
reconciler.findView(id: "a.1", as: Button<Text>.self).tap()
XCTAssertEqual(root.children.count, 1)
XCTAssert(root.children[0].description.contains("VStack"))
XCTAssertEqual(root.children[0].children.count, 4) // stack content
XCTAssert(root.children[0].children[0].description.contains("Text"))
XCTAssert(root.children[0].children[1].description.contains("ButtonStyle"))
reconciler.findView(id: "b.zwei", as: Button<Text>.self).tap()
XCTAssertEqual(root.children.count, 1)
XCTAssert(root.children[0].description.contains("VStack"))
XCTAssertEqual(root.children[0].children.count, 4) // stack content
XCTAssert(root.children[0].children[0].description.contains("Text"))
XCTAssert(root.children[0].children[1].description.contains("Text"))
XCTAssert(root.children[0].children[2].description.contains("ButtonStyle"))
}
}

View File

@ -64,14 +64,14 @@ final class VisitorTests: XCTestCase {
// Count up to 5
for i in 0..<5 {
XCTAssertEqual(countText.view, Text("\(i)"))
incrementButton.action?()
incrementButton.tap()
}
XCTAssertNil(incrementButton.view, "'Increment' should be hidden when count >= 5")
XCTAssertNotNil(decrementButton.view, "'Decrement' should be visible when count > 0")
// Count down to 0.
for i in 0..<5 {
XCTAssertEqual(countText.view, Text("\(5 - i)"))
decrementButton.action?()
decrementButton.tap()
}
XCTAssertNil(decrementButton.view, "'Decrement' should be hidden when count <= 0")
XCTAssertNotNil(incrementButton.view, "'Increment' should be visible when count < 5")
@ -99,7 +99,7 @@ final class VisitorTests: XCTestCase {
let addItemButton = reconciler.findView(id: "addItem", as: Button<Text>.self)
XCTAssertNotNil(addItemButton)
for i in 0..<10 {
addItemButton.action?()
addItemButton.tap()
XCTAssertEqual(reconciler.findView(id: i).view, Text("Item \(i)"))
}
}
@ -195,7 +195,7 @@ final class VisitorTests: XCTestCase {
// State
let button = reconciler.findView(id: DynamicPropertyTest.state, as: Button<Text>.self)
XCTAssertEqual(button.label, Text("0"))
button.action?()
button.tap()
XCTAssertEqual(button.label, Text("1"))
// Environment
@ -210,8 +210,7 @@ final class VisitorTests: XCTestCase {
as: Button<Text>.self
)
XCTAssertEqual(stateObjectButton.label, Text("0"))
stateObjectButton.action?()
stateObjectButton.action?()
stateObjectButton.tap()
XCTAssertEqual(stateObjectButton.label, Text("5"))
XCTAssertEqual(