Skip to content

Commit

Permalink
Merge pull request #88 from marinofelipe/fix/formmater-on-swift-ui
Browse files Browse the repository at this point in the history
Fix formatter to be correctly updated on Swift UI
  • Loading branch information
marinofelipe authored Jun 2, 2022
2 parents ee4652a + 760e16c commit f2302ee
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions Sources/SwiftUI/WrappedTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ final class WrappedTextField: UITextField {
guard configuration !== self.configuration else { return }

self.configuration = configuration
self.currencyTextFieldDelegate.formatter = configuration.formatter
}

func updateTextIfNeeded() {
Expand Down
67 changes: 67 additions & 0 deletions Tests/SwiftUI/WrappedTextFieldTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,71 @@ final class WrappedTextFieldTests: XCTestCase {
XCTAssertEqual(onCommitCallsCount, 0)
XCTAssertTrue(hasFocusSetValues.isEmpty)
}

func testUpdateConfigurationWithDifferentFormatterInstances() {
let callUpdateConfigurationIfNeeded: (CurrencyFormatter) -> Void = { [unowned self] formatter in
self.sut.updateConfigurationIfNeeded(
latest: .makeFixture(
textBinding: Binding<String>(
get: { "56" },
set: { text in
self.textSetValues.append(text)
}
),
unformattedTextBinding: Binding<String?>(
get: { "unformatted" },
set: { text in
self.unformattedTextSetValues.append(text)
}
),
inputAmountBinding: Binding<Double?>(
get: { .zero },
set: { value in
self.inputAmountSetValues.append(value)
}
),
formatter: formatter
)
)
}

formatter.hasDecimals = false
callUpdateConfigurationIfNeeded(self.formatter)
sut.updateTextIfNeeded()

let otherFormatter = CurrencyFormatter {
$0.currency = .euro
$0.locale = CurrencyLocale.german
$0.hasDecimals = false
}
callUpdateConfigurationIfNeeded(otherFormatter)
sut.updateTextIfNeeded()

XCTAssertEqual(
textSetValues,
[
"$0.34",
"$56",
"56 €"
]
)

let yetAnotherFormatter = CurrencyFormatter {
$0.currency = .brazilianReal
$0.locale = CurrencyLocale.portugueseBrazil
$0.hasDecimals = false
}
callUpdateConfigurationIfNeeded(yetAnotherFormatter)
sut.updateTextIfNeeded()

XCTAssertEqual(
textSetValues,
[
"$0.34",
"$56",
"56 €",
"R$ 56"
]
)
}
}

0 comments on commit f2302ee

Please sign in to comment.