This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct AccessibilityIdentifier: ExpressibleByStringLiteral { // This should be inside you UI Module | |
public let key: String | |
public init(stringLiteral key: String) { | |
self.key = key | |
} | |
} | |
public extension View { // This should be inside you UI Module | |
@ViewBuilder | |
func setAccessibilityIdentifier(_ identifier: AccessibilityIdentifier) -> some View { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import XCTest | |
final class AnyEquatableTests: XCTestCase { | |
func test_unsafelyUnwrapped_whenWrongTypeIsPassed_itShouldThrowFatalError() { | |
// Given | |
let mockState: String = "StateMock" | |
let failureHandlerExpectation = expectation(description: "Expecting failureHandler.") | |
var assertionMessage: String? | |
let failureHandler: (@autoclosure () -> String, StaticString, UInt) -> Never = { message, _, _ in |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
public struct AnyEquatable: Equatable { | |
// MARK: - Properties | |
public let erasedValue: Any | |
private let isEqual: (AnyEquatable) -> Bool | |
private let failureHandler: (_ message: @autoclosure () -> String, StaticString, UInt) -> Never | |
// MARK: - Initialization |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
public struct AnyEquatable: Equatable { | |
// MARK: - Properties | |
public let erasedValue: Any | |
private let isEqual: (AnyEquatable) -> Bool | |
// MARK: - Initialization | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import SwiftUI | |
import Combine | |
struct LoginService { | |
struct LoginRequest { | |
let email: String, password: String | |
} | |
let performLogin: (LoginRequest) -> AnyPublisher<String, Error> | |
let performLoginWithToken: (_ token: String) -> AnyPublisher<String, Error> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Toggle this boolean to compare against stdlib: | |
#if true // Stdlib version | |
// Quick hack to avoid changing the AnyEquatable implementation below. | |
extension Equatable { typealias EqualSelf = Self } | |
#else // Modified version | |
protocol Equatable { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
protocol MockURLResponder { | |
static func respond(to request: URLRequest) throws -> Data | |
static func response(for request: URLRequest) -> URLResponse | |
} | |
final class URLProtocolMock<Responder: MockURLResponder>: URLProtocol { | |
override class func canInit(with request: URLRequest) -> Bool { | |
return true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Combine | |
import SwiftUI | |
import XCTest | |
final class StateBindingViewModelTests: XCTestCase { | |
// MARK: - Tests | |
func test_binding_whenSetterIsCalled_shouldUpdateStateValues() { | |
// Given | |
let sut: TestViewModelMock<TestState> = .init( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class ExampleViewModel: StateBindingViewModel<ExampleState> { | |
// MARK: - Public Methods | |
func performSignUp() { | |
if state.password.count < 6 { | |
state.passwordError = "The Password should have at least 6 characters." | |
} | |
// Do aditional validations and send the data to the server... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open class StateBindingViewModel<State: Equatable>: ObservableObject { | |
/// Defines the state to be represented by the view. | |
@Published public private(set) var state: State | |
/// Initializes the ViewModel with the first / initial state for the flow. | |
/// - Parameter initialState: the initial state of the flow. | |
public init(initialState: State) { | |
self.state = initialState | |
} |