Skip to content

Commit

Permalink
store cancellables in ManagedPlayerViewModelTests (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
hborawski authored Oct 31, 2023
1 parent 80a0ff7 commit f3e16ed
Showing 1 changed file with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ class ManagedPlayerViewModelTests: XCTestCase {
let flow1 = FlowData.COUNTER
let flow2 = FlowData.COUNTER.replacingOccurrences(of: "counter-flow", with: "counter-flow-2")

var bag = Set<AnyCancellable>()

override func tearDown() {
bag.forEach { $0.cancel() }
bag.removeAll()
}

func testViewModelSuccessFlow() async throws {
let flowManager = ConstantFlowManager([flow1], delay: 0)

Expand All @@ -27,7 +34,7 @@ class ManagedPlayerViewModelTests: XCTestCase {

await viewModel.next()

waitOnChange(AnyPublisher(viewModel.$flow)) { $0 == self.flow1 }
waitOnChange(AnyPublisher(viewModel.$flow)) { $0 == self.flow1 }.store(in: &bag)

let result = """
{
Expand Down Expand Up @@ -55,7 +62,7 @@ class ManagedPlayerViewModelTests: XCTestCase {

await viewModel.next()

waitOnChange(AnyPublisher(viewModel.$flow)) { $0 == self.flow1 }
waitOnChange(AnyPublisher(viewModel.$flow)) { $0 == self.flow1 }.store(in: &bag)

let result = """
{
Expand All @@ -74,7 +81,7 @@ class ManagedPlayerViewModelTests: XCTestCase {
viewModel.result = .success(state)

try await Task.sleep(nanoseconds: 1_000_000_000)
waitOnChange(AnyPublisher(viewModel.$flow)) { $0 == self.flow2 }
waitOnChange(AnyPublisher(viewModel.$flow)) { $0 == self.flow2 }.store(in: &bag)
}

func testViewModelManagerError() async {
Expand Down Expand Up @@ -109,7 +116,7 @@ class ManagedPlayerViewModelTests: XCTestCase {
return true
}
return false
}
}.store(in: &bag)

let result = """
{
Expand All @@ -128,7 +135,7 @@ class ManagedPlayerViewModelTests: XCTestCase {
viewModel.result = .success(state)
try await Task.sleep(nanoseconds: 1_000_000_000)

waitOnChange(AnyPublisher(viewModel.$flow)) { $0 == self.flow2 }
waitOnChange(AnyPublisher(viewModel.$flow)) { $0 == self.flow2 }.store(in: &bag)

viewModel.result = .failure(PlayerError.jsConversionFailure)

Expand All @@ -137,7 +144,7 @@ class ManagedPlayerViewModelTests: XCTestCase {
return true
}
return false
}
}.store(in: &bag)

viewModel.retry()
try await Task.sleep(nanoseconds: 1_000_000_000)
Expand All @@ -147,11 +154,11 @@ class ManagedPlayerViewModelTests: XCTestCase {
return true
}
return false
}
}.store(in: &bag)

await viewModel.next(state)

waitOnChange(AnyPublisher(viewModel.$flow)) { $0 == self.flow2 }
waitOnChange(AnyPublisher(viewModel.$flow)) { $0 == self.flow2 }.store(in: &bag)
}

func testViewModelReset() async throws {
Expand All @@ -161,7 +168,7 @@ class ManagedPlayerViewModelTests: XCTestCase {

await viewModel.next()

waitOnChange(AnyPublisher(viewModel.$flow)) { $0 == self.flow1 }
waitOnChange(AnyPublisher(viewModel.$flow)) { $0 == self.flow1 }.store(in: &bag)

let result = """
{
Expand All @@ -180,7 +187,7 @@ class ManagedPlayerViewModelTests: XCTestCase {
viewModel.result = .success(state)

try await Task.sleep(nanoseconds: 1_000_000_000)
waitOnChange(AnyPublisher(viewModel.$flow), timeout: 10) { $0 == self.flow2 }
waitOnChange(AnyPublisher(viewModel.$flow), timeout: 10) { $0 == self.flow2 }.store(in: &bag)

viewModel.result = .failure(PlayerError.jsConversionFailure)

Expand All @@ -189,7 +196,7 @@ class ManagedPlayerViewModelTests: XCTestCase {
return true
}
return false
}
}.store(in: &bag)

viewModel.reset()
try await Task.sleep(nanoseconds: 1_000_000_000)
Expand All @@ -199,11 +206,11 @@ class ManagedPlayerViewModelTests: XCTestCase {
return true
}
return false
}
}.store(in: &bag)

await viewModel.next()

waitOnChange(AnyPublisher(viewModel.$flow)) { $0 == self.flow1 }
waitOnChange(AnyPublisher(viewModel.$flow)) { $0 == self.flow1 }.store(in: &bag)
}

func testViewModelErrorFlow() async {
Expand All @@ -219,7 +226,7 @@ class ManagedPlayerViewModelTests: XCTestCase {
completed.fulfill()
}

waitOnChange(AnyPublisher(viewModel.$flow)) { $0 != nil }
waitOnChange(AnyPublisher(viewModel.$flow)) { $0 != nil }.store(in: &bag)

viewModel.result = .failure(.jsConversionFailure)

Expand Down

0 comments on commit f3e16ed

Please sign in to comment.