Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let package = Package(
targets: ["FormView"])
],
dependencies: [
.package(url: "https://github.com/nalexn/ViewInspector", branch: "master")
.package(url: "https://github.com/nalexn/ViewInspector", exact: .init(0, 10, 1))
Comment thread
petrovichppp marked this conversation as resolved.
Outdated
],
targets: [
.target(
Expand Down
56 changes: 41 additions & 15 deletions Tests/FormViewTests/FormViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,61 +12,82 @@ import Combine
@testable import FormView

final class FormViewTests: XCTestCase {
@MainActor
func testPreventInvalidInput() throws {
var text1 = ""
var text2 = ""
let sut = InspectionWrapperView(
wrapped: FormView {
wrapped: FormView { _ in
ScrollView {
FormField(
value: Binding(get: { text1 }, set: { text1 = $0 }),
validationRules: [.digitsOnly]
rules: [TextValidationRule.digitsOnly(message: "")],
content: { _ in
TextField(text1, text: Binding(get: { text1 }, set: { text1 = $0 }))
}
)
.id(1)
FormField(value: Binding(get: { text2 }, set: { text2 = $0 }))
FormField(
value: Binding(get: { text2 }, set: { text2 = $0 }),
rules: [TextValidationRule.digitsOnly(message: "")],
content: { _ in
TextField(text2, text: 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 textField1 = try view.find(viewWithId: 1).find(ViewType.TextField.self)

text1 = "123"

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)
XCTAssertNotEqual(try textField1.input(), "123_A")
}

ViewHosting.host(view: sut)
wait(for: [exp], timeout: 0.1)
}

@MainActor
func testSubmitTextField() throws {
var text1 = ""
var text2 = ""
let sut = InspectionWrapperView(
wrapped: FormView {
wrapped: FormView { _ in
ScrollView {
FormField(
value: Binding(get: { text1 }, set: { text1 = $0 }),
validationRules: [.digitsOnly]
rules: [TextValidationRule.digitsOnly(message: "")],
content: { _ in
TextField(text1, text: Binding(get: { text1 }, set: { text1 = $0 }))
}
)
.id(1)
FormField(value: Binding(get: { text2 }, set: { text2 = $0 }))
FormField(
value: Binding(get: { text2 }, set: { text2 = $0 }),
rules: [TextValidationRule.digitsOnly(message: "")],
content: { _ in
TextField(text2, text: 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 textField1 = try view.find(viewWithId: 1).find(ViewType.TextField.self)
// let formField2 = try view.find(viewWithId: 2).view(FormField.self).actualView()
Comment thread
ilia-chub marked this conversation as resolved.
Outdated

try scrollView.callOnSubmit()
Expand All @@ -76,17 +97,22 @@ final class FormViewTests: XCTestCase {
XCTAssertTrue(true)
}

ViewHosting.host(view: sut.environment(\.focusField, "field1"))
ViewHosting.host(view: sut.environment(\.focusedFieldId, "1"))
wait(for: [exp], timeout: 0.1)
}

func testFocusNextField() throws {
var fieldStates = [FieldState(id: "1", isFocused: false), FieldState(id: "2", isFocused: false)]
let fieldStates = [
FieldState(id: "1", isFocused: true, onValidate: { true }),
FieldState(id: "2", isFocused: false, onValidate: { false })
]

var nextFocusField = fieldStates.focusNextField(currentFocusField: "")
XCTAssertEqual(nextFocusField, "1")
var nextFocusField = FocusService.getNextFocusFieldId(states: fieldStates, currentFocusField: "1")
nextFocusField = nextFocusField.trimmingCharacters(in: .whitespaces)
XCTAssertEqual(nextFocusField, "2")

nextFocusField = fieldStates.focusNextField(currentFocusField: "1")
nextFocusField = FocusService.getNextFocusFieldId(states: fieldStates, currentFocusField: "2")
nextFocusField = nextFocusField.trimmingCharacters(in: .whitespaces)
XCTAssertEqual(nextFocusField, "2")
}
}
30 changes: 15 additions & 15 deletions Tests/FormViewTests/Validation/TextValidationRuleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,64 +10,64 @@ import XCTest

final class TextValidationRuleTests: XCTestCase {
func testIgnoreEmpty() throws {
try test(textRule: .digitsOnly, trueString: "", falseString: "1234 A")
try test(textRule: .digitsOnly(message: ""), trueString: "", falseString: "1234 A")
}

func testNotEmpty() throws {
try test(textRule: .notEmpty, trueString: "Not empty", falseString: "")
try test(textRule: .notEmpty(message: ""), trueString: "Not empty", falseString: "")
}

func testMinLength() throws {
try test(textRule: .minLength(4), trueString: "1234", falseString: "123")
try test(textRule: .minLength(count: 4, message: ""), trueString: "1234", falseString: "123")
}

func testMaxLength() throws {
try test(textRule: .maxLength(4), trueString: "1234", falseString: "123456")
try test(textRule: .maxLength(count: 4, message: ""), trueString: "1234", falseString: "123456")
}

func testAtLeastOneDigit() throws {
try test(textRule: .atLeastOneDigit, trueString: "Digit 5", falseString: "No Digits")
try test(textRule: .atLeastOneDigit(message: ""), trueString: "Digit 5", falseString: "No Digits")
}

func testAtLeastOneLetter() throws {
try test(textRule: .atLeastOneLetter, trueString: "1234 A", falseString: "1234")
try test(textRule: .atLeastOneLetter(message: ""), trueString: "1234 A", falseString: "1234")
}

func testDigitsOnly() throws {
try test(textRule: .digitsOnly, trueString: "1234", falseString: "1234 A")
try test(textRule: .digitsOnly(message: ""), trueString: "1234", falseString: "1234 A")
}

func testLettersOnly() throws {
try test(textRule: .lettersOnly, trueString: "Letters", falseString: "Digit 5")
try test(textRule: .lettersOnly(message: ""), trueString: "Letters", falseString: "Digit 5")
}

func testAtLeastOneLowercaseLetter() throws {
try test(textRule: .atLeastOneLowercaseLetter, trueString: "LOWEr", falseString: "UPPER")
try test(textRule: .atLeastOneLowercaseLetter(message: ""), trueString: "LOWEr", falseString: "UPPER")
}

func testAtLeastOneUppercaseLetter() throws {
try test(textRule: .atLeastOneUppercaseLetter, trueString: "Upper", falseString: "lower")
try test(textRule: .atLeastOneUppercaseLetter(message: ""), trueString: "Upper", falseString: "lower")
}

func testAtLeastOneSpecialCharacter() throws {
try test(textRule: .atLeastOneSpecialCharacter, trueString: "Special %", falseString: "No special")
try test(textRule: .atLeastOneSpecialCharacter(message: ""), trueString: "Special %", falseString: "No special")
}

func testNoSpecialCharacters() throws {
try test(textRule: .noSpecialCharacters, trueString: "No special", falseString: "Special %")
try test(textRule: .noSpecialCharacters(message: ""), trueString: "No special", falseString: "Special %")
}

func testEmail() throws {
try test(textRule: .email, trueString: "alievmaxx@gmail.com", falseString: "alievmaxx@.com")
try test(textRule: .email(message: ""), trueString: "alievmaxx@gmail.com", falseString: "alievmaxx@.com")
}

func testNotRecurringPincode() throws {
try test(textRule: .notRecurringPincode, trueString: "1234", falseString: "5555")
try test(textRule: .notRecurringPincode(message: ""), trueString: "1234", falseString: "5555")
}

func testRegex() throws {
let dateRegex = "(\\d{2}).(\\d{2}).(\\d{4})"
try test(textRule: .regex(dateRegex), trueString: "21.12.2000", falseString: "21..2000")
try test(textRule: .regex(value: dateRegex, message: ""), trueString: "21.12.2000", falseString: "21..2000")
}

private func test(textRule: TextValidationRule, trueString: String, falseString: String) throws {
Expand Down
23 changes: 10 additions & 13 deletions Tests/FormViewTests/Validation/ValidatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,22 @@ import XCTest

final class ValidatorTests: XCTestCase {
func testValidator() throws {
var text: String = ""
var failedValidationRules: [TextValidationRule] = []

let validator = FieldValidator<String, TextValidationRule>(
value: Binding(get: { text }, set: { text = $0 }),
validationRules: [.digitsOnly],
inputRules: [.maxLength(4)],
failedValidationRules: Binding(get: { failedValidationRules }, set: { failedValidationRules = $0 })
let validator = FieldValidator<TextValidationRule>(
rules: [.digitsOnly(message: ""), .maxLength(count: 4, message: "")]
)

validator.value = "1"
validator.validate()
failedValidationRules = validator.validate(value: "1")
XCTAssertTrue(failedValidationRules.isEmpty)
failedValidationRules.removeAll()
Comment thread
ilia-chub marked this conversation as resolved.
Outdated

validator.value = "12_A"
XCTAssertEqual(failedValidationRules, [.digitsOnly])
failedValidationRules = validator.validate(value: "12_A")
XCTAssertTrue(failedValidationRules.isEmpty == false)
failedValidationRules.removeAll()

validator.value = "12345"
let failedInputRules = validator.validateInput()
XCTAssertEqual(failedInputRules, [.maxLength(4)])
failedValidationRules = validator.validate(value: "12345")
XCTAssertTrue(failedValidationRules.isEmpty == false)
failedValidationRules.removeAll()
}
}