Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store cancellables in ManagedPlayerViewModelTests #210

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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