Skip to content

Commit

Permalink
store cancellables in ManagedPlayerViewModelTests (player-ui#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
hborawski authored and mercillo committed Nov 21, 2023
1 parent 1e55527 commit 6543efa
Showing 1 changed file with 23 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 {

Task { await viewModel.next() }

await assertPublished(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 {

Task { await viewModel.next() }

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

let result = """
{
Expand All @@ -73,7 +80,8 @@ class ManagedPlayerViewModelTests: XCTestCase {

viewModel.result = .success(state)

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

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

let result = """
{
Expand All @@ -126,7 +134,7 @@ class ManagedPlayerViewModelTests: XCTestCase {

viewModel.result = .success(state)

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

viewModel.result = .failure(PlayerError.jsConversionFailure)

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

viewModel.retry()

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

await viewModel.next(state)

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

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

await viewModel.next()

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

let result = """
{
Expand All @@ -176,7 +184,8 @@ class ManagedPlayerViewModelTests: XCTestCase {

viewModel.result = .success(state)

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

viewModel.result = .failure(PlayerError.jsConversionFailure)

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

viewModel.reset()

Expand All @@ -194,11 +203,11 @@ class ManagedPlayerViewModelTests: XCTestCase {
return true
}
return false
}
}.store(in: &bag)

await viewModel.next()

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

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

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

viewModel.result = .failure(.jsConversionFailure)

Expand Down

0 comments on commit 6543efa

Please sign in to comment.