- CurrencySelection migrated to ReducerProtocol - unit tests fixed
This commit is contained in:
parent
02d094904a
commit
8753c2d883
|
@ -9,47 +9,37 @@ import ComposableArchitecture
|
|||
|
||||
// TODO [#315]: Reimplement this into multicurrency supporter (https://github.com/zcash/secant-ios-wallet/issues/315)
|
||||
|
||||
typealias CurrencySelectionReducer = Reducer<
|
||||
CurrencySelectionState,
|
||||
CurrencySelectionAction,
|
||||
CurrencySelectionEnvironment
|
||||
>
|
||||
typealias CurrencySelectionStore = Store<CurrencySelectionReducer.State, CurrencySelectionReducer.Action>
|
||||
|
||||
typealias CurrencySelectionStore = Store<CurrencySelectionState, CurrencySelectionAction>
|
||||
struct CurrencySelectionReducer: ReducerProtocol {
|
||||
struct State: Equatable {
|
||||
enum Currency: Equatable {
|
||||
case usd
|
||||
case zec
|
||||
|
||||
struct CurrencySelectionState: Equatable {
|
||||
enum Currency: Equatable {
|
||||
case usd
|
||||
case zec
|
||||
|
||||
var acronym: String {
|
||||
switch self {
|
||||
case .usd: return "USD"
|
||||
case .zec: return "ZEC"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var currencyType: Currency = .zec
|
||||
}
|
||||
|
||||
enum CurrencySelectionAction: Equatable {
|
||||
case swapCurrencyType
|
||||
}
|
||||
|
||||
struct CurrencySelectionEnvironment { }
|
||||
|
||||
extension CurrencySelectionReducer {
|
||||
static var `default`: Self {
|
||||
.init { state, action, _ in
|
||||
switch action {
|
||||
case .swapCurrencyType:
|
||||
switch state.currencyType {
|
||||
case .usd: state.currencyType = .zec
|
||||
case .zec: state.currencyType = .usd
|
||||
var acronym: String {
|
||||
switch self {
|
||||
case .usd: return "USD"
|
||||
case .zec: return "ZEC"
|
||||
}
|
||||
}
|
||||
return .none
|
||||
}
|
||||
|
||||
var currencyType: Currency = .zec
|
||||
}
|
||||
|
||||
enum Action: Equatable {
|
||||
case swapCurrencyType
|
||||
}
|
||||
|
||||
func reduce(into state: inout State, action: Action) -> ComposableArchitecture.EffectTask<Action> {
|
||||
switch action {
|
||||
case .swapCurrencyType:
|
||||
switch state.currencyType {
|
||||
case .usd: state.currencyType = .zec
|
||||
case .zec: state.currencyType = .usd
|
||||
}
|
||||
}
|
||||
return .none
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,14 +17,14 @@ struct CurrencySelectionView: View {
|
|||
action: { viewStore.send(.swapCurrencyType) },
|
||||
label: {
|
||||
HStack {
|
||||
Text(CurrencySelectionState.Currency.usd.acronym)
|
||||
Text(CurrencySelectionReducer.State.Currency.usd.acronym)
|
||||
.foregroundColor(
|
||||
viewStore.currencyType == .usd ? .yellow : .white
|
||||
)
|
||||
|
||||
Asset.Assets.Icons.swap.image
|
||||
|
||||
Text(CurrencySelectionState.Currency.zec.acronym)
|
||||
Text(CurrencySelectionReducer.State.Currency.zec.acronym)
|
||||
.foregroundColor(
|
||||
viewStore.currencyType == .zec ? .yellow : .white
|
||||
)
|
||||
|
|
|
@ -18,10 +18,11 @@ typealias TransactionAmountTextFieldReducer = Reducer<
|
|||
typealias TransactionAmountTextFieldStore = Store<TransactionAmountTextFieldState, TransactionAmountTextFieldAction>
|
||||
|
||||
typealias AnyTCATextFieldReducerAmount = AnyReducer<TCATextFieldReducer.State, TCATextFieldReducer.Action, TransactionAmountTextFieldEnvironment>
|
||||
typealias AnyCurrencySelectionReducer = AnyReducer<CurrencySelectionReducer.State, CurrencySelectionReducer.Action, TransactionAmountTextFieldEnvironment>
|
||||
|
||||
struct TransactionAmountTextFieldState: Equatable {
|
||||
var amount: Int64 = 0
|
||||
var currencySelectionState: CurrencySelectionState
|
||||
var currencySelectionState: CurrencySelectionReducer.State
|
||||
var maxValue: Int64 = 0
|
||||
var textFieldState: TCATextFieldReducer.State
|
||||
// TODO [#311]: - Get the ZEC price from the SDK, https://github.com/zcash/secant-ios-wallet/issues/311
|
||||
|
@ -34,7 +35,7 @@ struct TransactionAmountTextFieldState: Equatable {
|
|||
|
||||
enum TransactionAmountTextFieldAction: Equatable {
|
||||
case clearValue
|
||||
case currencySelection(CurrencySelectionAction)
|
||||
case currencySelection(CurrencySelectionReducer.Action)
|
||||
case setMax
|
||||
case textField(TCATextFieldReducer.Action)
|
||||
case updateAmount
|
||||
|
@ -115,21 +116,24 @@ extension TransactionAmountTextFieldReducer {
|
|||
environment: { $0 }
|
||||
)
|
||||
|
||||
private static let currencySelectionReducer: TransactionAmountTextFieldReducer = CurrencySelectionReducer.default.pullback(
|
||||
private static let currencySelectionReducer: TransactionAmountTextFieldReducer = AnyCurrencySelectionReducer { _ in
|
||||
CurrencySelectionReducer()
|
||||
}
|
||||
.pullback(
|
||||
state: \TransactionAmountTextFieldState.currencySelectionState,
|
||||
action: /TransactionAmountTextFieldAction.currencySelection,
|
||||
environment: { _ in return .init() }
|
||||
environment: { $0 }
|
||||
)
|
||||
}
|
||||
|
||||
extension TransactionAmountTextFieldState {
|
||||
static let placeholder = TransactionAmountTextFieldState(
|
||||
currencySelectionState: CurrencySelectionState(),
|
||||
currencySelectionState: CurrencySelectionReducer.State(),
|
||||
textFieldState: .placeholder
|
||||
)
|
||||
|
||||
static let amount = TransactionAmountTextFieldState(
|
||||
currencySelectionState: CurrencySelectionState(),
|
||||
currencySelectionState: CurrencySelectionReducer.State(),
|
||||
textFieldState: .amount
|
||||
)
|
||||
}
|
||||
|
|
|
@ -12,9 +12,8 @@ import ComposableArchitecture
|
|||
class CurrencySelectionTests: XCTestCase {
|
||||
func testCurrencySwapUsdToZec() throws {
|
||||
let store = TestStore(
|
||||
initialState: CurrencySelectionState(currencyType: .usd),
|
||||
reducer: CurrencySelectionReducer.default,
|
||||
environment: CurrencySelectionEnvironment()
|
||||
initialState: CurrencySelectionReducer.State(currencyType: .usd),
|
||||
reducer: CurrencySelectionReducer()
|
||||
)
|
||||
|
||||
store.send(.swapCurrencyType) { state in
|
||||
|
@ -24,9 +23,8 @@ class CurrencySelectionTests: XCTestCase {
|
|||
|
||||
func testCurrencySwapZecToUsd() throws {
|
||||
let store = TestStore(
|
||||
initialState: CurrencySelectionState(currencyType: .zec),
|
||||
reducer: CurrencySelectionReducer.default,
|
||||
environment: CurrencySelectionEnvironment()
|
||||
initialState: CurrencySelectionReducer.State(currencyType: .zec),
|
||||
reducer: CurrencySelectionReducer()
|
||||
)
|
||||
|
||||
store.send(.swapCurrencyType) { state in
|
||||
|
|
|
@ -316,7 +316,7 @@ class SendTests: XCTestCase {
|
|||
transactionAddressInputState: .placeholder,
|
||||
transactionAmountInputState:
|
||||
TransactionAmountTextFieldState(
|
||||
currencySelectionState: CurrencySelectionState(),
|
||||
currencySelectionState: CurrencySelectionReducer.State(),
|
||||
maxValue: 501_300,
|
||||
textFieldState:
|
||||
TCATextFieldReducer.State(
|
||||
|
@ -396,7 +396,7 @@ class SendTests: XCTestCase {
|
|||
transactionAddressInputState: .placeholder,
|
||||
transactionAmountInputState:
|
||||
TransactionAmountTextFieldState(
|
||||
currencySelectionState: CurrencySelectionState(),
|
||||
currencySelectionState: CurrencySelectionReducer.State(),
|
||||
textFieldState:
|
||||
TCATextFieldReducer.State(
|
||||
validationType: .customFloatingPoint(usNumberFormatter),
|
||||
|
@ -430,7 +430,7 @@ class SendTests: XCTestCase {
|
|||
transactionAmountInputState:
|
||||
TransactionAmountTextFieldState(
|
||||
amount: 501_301,
|
||||
currencySelectionState: CurrencySelectionState(),
|
||||
currencySelectionState: CurrencySelectionReducer.State(),
|
||||
maxValue: 501_302,
|
||||
textFieldState:
|
||||
TCATextFieldReducer.State(
|
||||
|
@ -478,7 +478,7 @@ class SendTests: XCTestCase {
|
|||
transactionAddressInputState: .placeholder,
|
||||
transactionAmountInputState:
|
||||
TransactionAmountTextFieldState(
|
||||
currencySelectionState: CurrencySelectionState(),
|
||||
currencySelectionState: CurrencySelectionReducer.State(),
|
||||
maxValue: 501_300,
|
||||
textFieldState:
|
||||
TCATextFieldReducer.State(
|
||||
|
@ -526,7 +526,7 @@ class SendTests: XCTestCase {
|
|||
transactionAddressInputState: .placeholder,
|
||||
transactionAmountInputState:
|
||||
TransactionAmountTextFieldState(
|
||||
currencySelectionState: CurrencySelectionState(),
|
||||
currencySelectionState: CurrencySelectionReducer.State(),
|
||||
maxValue: 501_302,
|
||||
textFieldState:
|
||||
TCATextFieldReducer.State(
|
||||
|
@ -574,7 +574,7 @@ class SendTests: XCTestCase {
|
|||
transactionAddressInputState: .placeholder,
|
||||
transactionAmountInputState:
|
||||
TransactionAmountTextFieldState(
|
||||
currencySelectionState: CurrencySelectionState(),
|
||||
currencySelectionState: CurrencySelectionReducer.State(),
|
||||
maxValue: 501_302,
|
||||
textFieldState:
|
||||
TCATextFieldReducer.State(
|
||||
|
@ -632,7 +632,7 @@ class SendTests: XCTestCase {
|
|||
transactionAmountInputState:
|
||||
TransactionAmountTextFieldState(
|
||||
amount: 100,
|
||||
currencySelectionState: CurrencySelectionState(),
|
||||
currencySelectionState: CurrencySelectionReducer.State(),
|
||||
maxValue: 501_302,
|
||||
textFieldState:
|
||||
TCATextFieldReducer.State(
|
||||
|
@ -676,7 +676,7 @@ class SendTests: XCTestCase {
|
|||
transactionAddressInputState: .placeholder,
|
||||
transactionAmountInputState:
|
||||
TransactionAmountTextFieldState(
|
||||
currencySelectionState: CurrencySelectionState(),
|
||||
currencySelectionState: CurrencySelectionReducer.State(),
|
||||
maxValue: 501_302,
|
||||
textFieldState:
|
||||
TCATextFieldReducer.State(
|
||||
|
|
|
@ -25,7 +25,7 @@ class TransactionAmountTextFieldTests: XCTestCase {
|
|||
let store = TestStore(
|
||||
initialState:
|
||||
TransactionAmountTextFieldState(
|
||||
currencySelectionState: CurrencySelectionState(),
|
||||
currencySelectionState: CurrencySelectionReducer.State(),
|
||||
maxValue: 501_301,
|
||||
textFieldState:
|
||||
TCATextFieldReducer.State(
|
||||
|
@ -53,7 +53,7 @@ class TransactionAmountTextFieldTests: XCTestCase {
|
|||
let store = TestStore(
|
||||
initialState:
|
||||
TransactionAmountTextFieldState(
|
||||
currencySelectionState: CurrencySelectionState(),
|
||||
currencySelectionState: CurrencySelectionReducer.State(),
|
||||
maxValue: 501_301,
|
||||
textFieldState:
|
||||
TCATextFieldReducer.State(
|
||||
|
@ -79,7 +79,7 @@ class TransactionAmountTextFieldTests: XCTestCase {
|
|||
initialState:
|
||||
TransactionAmountTextFieldState(
|
||||
currencySelectionState:
|
||||
CurrencySelectionState(
|
||||
CurrencySelectionReducer.State(
|
||||
currencyType: .zec
|
||||
),
|
||||
textFieldState:
|
||||
|
@ -111,7 +111,7 @@ class TransactionAmountTextFieldTests: XCTestCase {
|
|||
initialState:
|
||||
TransactionAmountTextFieldState(
|
||||
currencySelectionState:
|
||||
CurrencySelectionState(
|
||||
CurrencySelectionReducer.State(
|
||||
currencyType: .zec
|
||||
),
|
||||
textFieldState:
|
||||
|
@ -143,7 +143,7 @@ class TransactionAmountTextFieldTests: XCTestCase {
|
|||
initialState:
|
||||
TransactionAmountTextFieldState(
|
||||
currencySelectionState:
|
||||
CurrencySelectionState(
|
||||
CurrencySelectionReducer.State(
|
||||
currencyType: .usd
|
||||
),
|
||||
textFieldState:
|
||||
|
@ -175,7 +175,7 @@ class TransactionAmountTextFieldTests: XCTestCase {
|
|||
initialState:
|
||||
TransactionAmountTextFieldState(
|
||||
currencySelectionState:
|
||||
CurrencySelectionState(
|
||||
CurrencySelectionReducer.State(
|
||||
currencyType: .usd
|
||||
),
|
||||
maxValue: 100_000_000,
|
||||
|
@ -217,7 +217,7 @@ class TransactionAmountTextFieldTests: XCTestCase {
|
|||
initialState:
|
||||
TransactionAmountTextFieldState(
|
||||
currencySelectionState:
|
||||
CurrencySelectionState(
|
||||
CurrencySelectionReducer.State(
|
||||
currencyType: .zec
|
||||
),
|
||||
maxValue: 200_000_000,
|
||||
|
@ -249,7 +249,7 @@ class TransactionAmountTextFieldTests: XCTestCase {
|
|||
initialState:
|
||||
TransactionAmountTextFieldState(
|
||||
currencySelectionState:
|
||||
CurrencySelectionState(
|
||||
CurrencySelectionReducer.State(
|
||||
currencyType: .usd
|
||||
),
|
||||
maxValue: 200_000_000,
|
||||
|
|
|
@ -32,7 +32,7 @@ class TransactionConfirmationSnapshotTests: XCTestCase {
|
|||
)
|
||||
)
|
||||
state.transactionAmountInputState = TransactionAmountTextFieldState(
|
||||
currencySelectionState: CurrencySelectionState(),
|
||||
currencySelectionState: CurrencySelectionReducer.State(),
|
||||
textFieldState: TCATextFieldReducer.State(
|
||||
validationType: nil,
|
||||
text: "2.91"
|
||||
|
@ -69,7 +69,7 @@ class TransactionConfirmationSnapshotTests: XCTestCase {
|
|||
)
|
||||
)
|
||||
state.transactionAmountInputState = TransactionAmountTextFieldState(
|
||||
currencySelectionState: CurrencySelectionState(),
|
||||
currencySelectionState: CurrencySelectionReducer.State(),
|
||||
textFieldState: TCATextFieldReducer.State(
|
||||
validationType: nil,
|
||||
text: "2.91"
|
||||
|
|
|
@ -32,7 +32,7 @@ class TransactionSendingTests: XCTestCase {
|
|||
)
|
||||
)
|
||||
state.transactionAmountInputState = TransactionAmountTextFieldState(
|
||||
currencySelectionState: CurrencySelectionState(),
|
||||
currencySelectionState: CurrencySelectionReducer.State(),
|
||||
textFieldState: TCATextFieldReducer.State(
|
||||
validationType: nil,
|
||||
text: "2.91"
|
||||
|
|
Loading…
Reference in New Issue