Skip to content

Commit 2567f99

Browse files
committed
Update more unit tests
1 parent 58e42b0 commit 2567f99

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-2
lines changed

Sources/FormHook/Form.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class FormControl<FieldName> where FieldName: Hashable {
1616
private var fields: [FieldName: FieldProtocol]
1717

1818
private var _currentFocusedField: FieldName?
19-
19+
2020
@MainActor
2121
private var currentFocusedField: FieldName? {
2222
get {

Tests/FormHookTests/FormHookTests.swift

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,48 @@ final class FormHookTests: QuickSpec {
11901190
context("with reValidateMode .onSubmit") {
11911191
beforeEach {
11921192
formControl.options.reValidateMode = .onSubmit
1193+
formControl.options.resolver = nil
1194+
}
1195+
1196+
context("with a customised validation, which returns true, passed") {
1197+
beforeEach {
1198+
formControl.options.resolver = ResolverProxy<TestFieldName>(value: [
1199+
.a: aDefaultValue,
1200+
.b: bDefaultValue
1201+
]).resolver(values:context:fieldNames:)
1202+
}
1203+
1204+
context("invoke handleSubmit action") {
1205+
it("submitCount is 1, isSubmitSuccessful is true, and submissionState is .submitted") {
1206+
try await formControl.handleSubmit(onValid: { _, _ in })
1207+
let formState = await formControl.formState
1208+
expect(formState.submitCount) == 1
1209+
expect(formState.isSubmitSuccessful) == true
1210+
expect(formState.submissionState) == .submitted
1211+
}
1212+
}
1213+
}
1214+
1215+
context("with a customised validation, which returns false, passed") {
1216+
beforeEach {
1217+
formControl.options.resolver = ResolverProxy<TestFieldName>(error: .init(
1218+
errorFields: Set([.a, .b]),
1219+
messages: [
1220+
.a: ["Failed to validate a"],
1221+
.b: ["Failed to validate b"]
1222+
]
1223+
)).resolver(values:context:fieldNames:)
1224+
}
1225+
1226+
context("invoke handleSubmit action") {
1227+
it("submitCount is 1, isSubmitSuccessful is true, and submissionState is .submitted") {
1228+
try await formControl.handleSubmit(onValid: { _, _ in })
1229+
let formState = await formControl.formState
1230+
expect(formState.submitCount) == 1
1231+
expect(formState.isSubmitSuccessful) == false
1232+
expect(formState.submissionState) == .submitted
1233+
}
1234+
}
11931235
}
11941236

11951237
context("invoke handleSubmit action") {
@@ -1904,6 +1946,7 @@ final class FormHookTests: QuickSpec {
19041946
let bDefaultValue = "%^$#*("
19051947
var focusField: TestFieldName?
19061948
var aBinder: FieldRegistration<String>!
1949+
var bBinder: FieldRegistration<String>!
19071950

19081951
beforeEach {
19091952
focusField = nil
@@ -1932,7 +1975,25 @@ final class FormHookTests: QuickSpec {
19321975
aBinder = formControl.register(name: .a, options: .init(rules: aValidator!, defaultValue: aDefaultValue))
19331976

19341977
bValidator = MockValidator<String, Bool>(result: true)
1935-
_ = formControl.register(name: .b, options: .init(rules: bValidator!, defaultValue: bDefaultValue))
1978+
bBinder = formControl.register(name: .b, options: .init(rules: bValidator!, defaultValue: bDefaultValue))
1979+
}
1980+
1981+
context("both fields are invalid and re-register all test fields with unnatural order") {
1982+
beforeEach {
1983+
bValidator.result = false
1984+
aBinder = formControl.register(name: .a, options: .init(fieldOrdinal: 1, rules: aValidator!, defaultValue: aDefaultValue))
1985+
_ = formControl.register(name: .b, options: .init(fieldOrdinal: 0, rules: bValidator!, defaultValue: bDefaultValue))
1986+
}
1987+
1988+
context("submits the form values") {
1989+
beforeEach {
1990+
try? await formControl.handleSubmit(onValid: { _, _ in })
1991+
}
1992+
1993+
it("focusField equals b") { @MainActor in
1994+
expect(focusField) == .b
1995+
}
1996+
}
19361997
}
19371998

19381999
context("submits the form values") {
@@ -1971,6 +2032,26 @@ final class FormHookTests: QuickSpec {
19712032

19722033
expect(focusField) == .a
19732034
}
2035+
2036+
context("field \"b\" changes its value") {
2037+
beforeEach {
2038+
bBinder.wrappedValue = "b"
2039+
}
2040+
2041+
context("then field \"a\" changes its value") {
2042+
beforeEach {
2043+
try? await Task.sleep(nanoseconds: 2_000_000)
2044+
2045+
aBinder.wrappedValue = "a"
2046+
}
2047+
2048+
it("focusField equals a") {
2049+
try await Task.sleep(nanoseconds: 2_000_000)
2050+
2051+
expect(focusField) == .a
2052+
}
2053+
}
2054+
}
19742055
}
19752056

19762057
context("field \"a\" is valid and changes its value") {

0 commit comments

Comments
 (0)