-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFormViewTests.swift
More file actions
92 lines (78 loc) · 3.04 KB
/
FormViewTests.swift
File metadata and controls
92 lines (78 loc) · 3.04 KB
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
//
// FormViewTests.swift
//
//
// Created by Maxim Aliev on 18.02.2023.
//
import SwiftUI
import XCTest
import ViewInspector
import Combine
@testable import FormView
final class FormViewTests: XCTestCase {
func testPreventInvalidInput() throws {
var text1 = ""
var text2 = ""
let sut = InspectionWrapperView(
wrapped: FormView {
ScrollView {
FormField(
value: Binding(get: { text1 }, set: { text1 = $0 }),
validationRules: [.digitsOnly]
)
.id(1)
FormField(value: Binding(get: { text2 }, set: { text2 = $0 }))
.id(2)
}
}
)
let exp = sut.inspection.inspect { view in
let scrollView = try view.find(ViewType.ScrollView.self)
let textField1 = try view.find(viewWithId: 1).textField()
try scrollView.callOnSubmit()
try textField1.callOnChange(newValue: "New Focus Field", index: 1)
try textField1.callOnChange(newValue: "123")
XCTAssertEqual(try textField1.input(), "123")
text1 = "123"
try textField1.callOnChange(newValue: "123_A")
XCTAssertEqual(try textField1.input(), text1)
}
ViewHosting.host(view: sut)
wait(for: [exp], timeout: 0.1)
}
func testSubmitTextField() throws {
var text1 = ""
var text2 = ""
let sut = InspectionWrapperView(
wrapped: FormView {
ScrollView {
FormField(
value: Binding(get: { text1 }, set: { text1 = $0 }),
validationRules: [.digitsOnly]
)
.id(1)
FormField(value: Binding(get: { text2 }, set: { text2 = $0 }))
.id(2)
}
}
)
let exp = sut.inspection.inspect { view in
let scrollView = try view.find(ViewType.ScrollView.self)
let textField1 = try view.find(viewWithId: 1).textField()
// let formField2 = try view.find(viewWithId: 2).view(FormField.self).actualView()
try scrollView.callOnSubmit()
try textField1.callOnChange(newValue: "field2", index: 1)
// XCTAssertEqual(formField2.focusField, "field2")
XCTAssertTrue(true)
}
ViewHosting.host(view: sut.environment(\.focusField, "field1"))
wait(for: [exp], timeout: 0.1)
}
func testFocusNextField() throws {
var fieldStates = [FieldState(id: "1", isFocused: false), FieldState(id: "2", isFocused: false)]
var nextFocusField = fieldStates.focusNextField(currentFocusField: "")
XCTAssertEqual(nextFocusField, "1")
nextFocusField = fieldStates.focusNextField(currentFocusField: "1")
XCTAssertEqual(nextFocusField, "2")
}
}