Skip to content

Commit

Permalink
Migrate Observable for PlayerDetailsViewmodel
Browse files Browse the repository at this point in the history
  • Loading branch information
radude89 committed Jun 27, 2024
1 parent 34aeefe commit b4b0308
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,21 @@ import PlayerDetailsAssets

// MARK: - PlayerDetailsViewModel

public final class PlayerDetailsViewModel: ObservableObject {
@Observable
public final class PlayerDetailsViewModel {

@Published var selectedPlayer: Player
var selectedPlayer: Player

@ObservationIgnored
@Binding var showListView: Bool

@ObservationIgnored
private let storage: PlayerStorageHandler

@ObservationIgnored
private let state: State

@ObservationIgnored
private lazy var playerDetails = PlayerDetailsUIModelFactory.makeDetailsModel(
for: selectedPlayer, state: state
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import PlayerDetailsAssets

public struct PlayerDetailsView: View {

@ObservedObject var viewModel: PlayerDetailsViewModel

@Environment(\.dismiss) private var dismiss

@State private var showConfirmationAlert = false
@Bindable private var viewModel: PlayerDetailsViewModel

public init(viewModel: PlayerDetailsViewModel) {
self.viewModel = viewModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,60 +7,16 @@

import XCTest
import SwiftUI
import Combine
import CoreModels
import PlayerDetailsAssets
import FoundationMocks
@testable import PlayerDetails

final class PlayerDetailsViewModelTests: XCTestCase {

private var cancellables: Set<AnyCancellable>!
private let allSkills = Player.Skill.allCases
private let allPositions = Player.Position.allCases

override func setUp() {
super.setUp()
cancellables = []
}

override func tearDown() {
cancellables = []
super.tearDown()
}

@MainActor
func testSelectedPlayer() throws {
var expectedPlayerNames = ["", "John"]
let receivedAllExpectation = expectation(description: "Adding player expectation")
let sut = makeSUT()

sut.$selectedPlayer
.sink { player in
guard let expectedPlayerName = expectedPlayerNames.first else {
XCTFail("Received more values than expected.")
return
}

guard expectedPlayerName == player.name else {
XCTFail("Expected received \(player.name) to match first expected \(expectedPlayerName)")
return
}

expectedPlayerNames = Array(expectedPlayerNames.dropFirst())

if expectedPlayerNames.isEmpty {
receivedAllExpectation.fulfill()
}

}
.store(in: &cancellables)

sut.selectedPlayer.name = "John"

waitForExpectations(timeout: 2, handler: nil)
}

func testPlayerIsValid_whenNameIsNotEmpty_isTrue() {
PlayerDetailsViewModel.State.allCases.forEach { state in
let sut = makeSUT(state: state)
Expand Down

0 comments on commit b4b0308

Please sign in to comment.