Skip to content

Instantly share code, notes, and snippets.

View bocato's full-sized avatar

Eduardo Bocato bocato

View GitHub Profile
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 {
@bocato
bocato / testing_fatal_error_3.swift
Created April 24, 2021 18:58
testing_fatal_error_3.swift
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
@bocato
bocato / testing_fatal_errors_2.swift
Last active April 24, 2021 18:57
testing_fatal_errors_2.swift
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
@bocato
bocato / testing_fatal_errors_1.swift
Last active April 24, 2021 18:57
testing_fatal_errors_1
import Foundation
public struct AnyEquatable: Equatable {
// MARK: - Properties
public let erasedValue: Any
private let isEqual: (AnyEquatable) -> Bool
// MARK: - Initialization
@bocato
bocato / improving_tests_in_swift_failing_mocks_1.swift
Last active April 22, 2021 16:44
Improving Tests in Swift: Failing Mocks - Scenario
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>
@bocato
bocato / AnyEquatable.swift
Created April 15, 2021 03:40 — forked from pyrtsa/AnyEquatable.swift
AnyEquatable—making Equatable work with class inheritance and existential wrapping ("type erasure")
// 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 {
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
@bocato
bocato / mvvm_form_4.swift
Last active April 8, 2021 17:49
mvvm_form_4.swift
import Combine
import SwiftUI
import XCTest
final class StateBindingViewModelTests: XCTestCase {
// MARK: - Tests
func test_binding_whenSetterIsCalled_shouldUpdateStateValues() {
// Given
let sut: TestViewModelMock<TestState> = .init(
@bocato
bocato / mvvm_form_3.swift
Last active March 27, 2021 11:41
mvvm_form_3.swift
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...
}
@bocato
bocato / mvvm_form_2.swift
Last active April 8, 2021 17:48
mvvm_form_2.swift
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
}