From af17fd22c3170517b0f087edb707a89697768f2c Mon Sep 17 00:00:00 2001 From: hborawski Date: Fri, 27 Oct 2023 16:51:36 -0700 Subject: [PATCH] store cancellables in ManagedPlayerViewModelTests --- .../ManagedPlayerViewModelTests.swift | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/ios/packages/swiftui/Tests/ManagedPlayer/ManagedPlayerViewModelTests.swift b/ios/packages/swiftui/Tests/ManagedPlayer/ManagedPlayerViewModelTests.swift index bfd0c8069..16e37c4ae 100644 --- a/ios/packages/swiftui/Tests/ManagedPlayer/ManagedPlayerViewModelTests.swift +++ b/ios/packages/swiftui/Tests/ManagedPlayer/ManagedPlayerViewModelTests.swift @@ -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() + + override func tearDown() { + bag.forEach { $0.cancel() } + bag.removeAll() + } + func testViewModelSuccessFlow() async throws { let flowManager = ConstantFlowManager([flow1], delay: 0) @@ -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 = """ { @@ -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 = """ { @@ -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 { @@ -109,7 +116,7 @@ class ManagedPlayerViewModelTests: XCTestCase { return true } return false - } + }.store(in: &bag) let result = """ { @@ -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) @@ -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) @@ -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 { @@ -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 = """ { @@ -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) @@ -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) @@ -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 { @@ -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)