forked from nalexn/ViewInspector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInspectableViewTests.swift
92 lines (81 loc) · 3.01 KB
/
InspectableViewTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import XCTest
import SwiftUI
@testable import ViewInspector
@available(iOS 13.0, macOS 10.15, tvOS 13.0, *)
final class InspectableViewTests: XCTestCase {
func testBasicInspectionFunctions() throws {
let view = Text("abc")
XCTAssertEqual(try view.inspect().text().string(), "abc")
view.inspect { view in
XCTAssertEqual(try view.text().string(), "abc")
}
}
func testIsResponsive() throws {
let view1 = Button("", action: { }).padding()
let view2 = Button("", action: { }).allowsHitTesting(true).padding()
let view3 = Button("", action: { }).disabled(false).padding()
let view4 = Button("", action: { }).allowsHitTesting(false).padding()
let view5 = Button("", action: { }).disabled(true).padding()
let view6 = Button("", action: { }).hidden().padding()
XCTAssertTrue(try view1.inspect().isResponsive())
XCTAssertTrue(try view2.inspect().isResponsive())
XCTAssertTrue(try view3.inspect().isResponsive())
XCTAssertFalse(try view4.inspect().isResponsive())
XCTAssertFalse(try view5.inspect().isResponsive())
XCTAssertFalse(try view6.inspect().isResponsive())
}
}
@available(iOS 13.0, macOS 10.15, tvOS 13.0, *)
final class InspectableViewTestsAccessTests: XCTestCase {
func testSequence() throws {
let view = HStack { Text("Test") }
var sut = try view.inspect().hStack().makeIterator()
XCTAssertNotNil(sut.next())
XCTAssertNil(sut.next())
}
func testRandomAccessCollection() throws {
let view = HStack {
Text("1").padding(); Text("2"); Text("3")
}
let sut = try view.inspect().hStack()
let array = try sut.map { try $0.text().string() }
XCTAssertEqual(array, ["1", "2", "3"])
var iterator = sut.makeIterator()
for _ in 0 ..< 3 {
XCTAssertNotNil(iterator.next())
}
XCTAssertNil(iterator.next())
XCTAssertEqual(sut.underestimatedCount, 3)
}
func testCollectionWithAbsentViews() throws {
let sut = try ViewWithAbsentChildren(present: false).inspect()
var counter = 0
// `forEach` is using iterator
sut.forEach { _ in counter += 1 }
XCTAssertEqual(counter, 4)
// `map` is using subscript
let array = sut.map { try? $0.text().string() }
XCTAssertEqual(array, [nil, "b", "c", nil])
XCTAssertTrue(sut[0].isAbsent)
XCTAssertFalse(sut[1].isAbsent)
XCTAssertFalse(sut[2].isAbsent)
XCTAssertTrue(sut[3].isAbsent)
XCTAssertTrue(sut[2].isResponsive())
XCTAssertFalse(sut[3].isResponsive())
}
}
@available(iOS 13.0, macOS 10.15, tvOS 13.0, *)
private struct ViewWithAbsentChildren: View, Inspectable {
let present: Bool
@ViewBuilder
var body: some View {
if present {
Text("a")
}
Text("b")
Text("c")
if present {
Text("d")
}
}
}