Improve tests' reliability (#763)

* Create publisher extension into the unit test target

* Add ViewModelContext test extension

* Refactor BugReportViewModelTests

* Fix failing UTs

* Idea PublishedClosure

* Refactor RoomDetailsViewModelTests

* Replace more Task.yield/Task.sleep

* Move leaveRoom/ignore/unignore under the @MainActor

* Revert "Idea PublishedClosure"

This reverts commit 4ab25291041f0dbd99083baf9d95bc6647f1fd97.

* Make process(viewAction:) sync

* Refactor BugReportViewModel callback to a publisher

* Fix UTs

* Refactor ReportContentViewModel

* Fix ui test build error

* Try make sonar happy

* Empty commit

* Revert "Try make sonar happy"

This reverts commit 97804b19373a8f55f12174ccbf27f1fd8db583b7.

* Rename ui test identifier

* Cleanup

* Callback -> actions refactor

* Update template

* Add publisher in TemplateCoordinator

* Add env variable in IntegrationTests.xctestplan

* Add async sequence extension

* Amend integration test plan

* Remove env variable from target.yml

* Cleanup

* Fix failing UI tests
This commit is contained in:
Alfonso Grillo
2023-04-05 17:07:12 +02:00
committed by GitHub
parent 7e47169a23
commit 155fcbbf6e
55 changed files with 273 additions and 233 deletions

View File

@@ -14,6 +14,7 @@
// limitations under the License.
//
import Combine
import SwiftUI
struct TemplateCoordinatorParameters {
@@ -30,8 +31,12 @@ enum TemplateCoordinatorAction {
final class TemplateCoordinator: CoordinatorProtocol {
private let parameters: TemplateCoordinatorParameters
private var viewModel: TemplateViewModelProtocol
private let actionsSubject: PassthroughSubject<TemplateCoordinatorAction, Never> = .init()
private var cancellables: Set<AnyCancellable> = .init()
var callback: ((TemplateCoordinatorAction) -> Void)?
var actions: AnyPublisher<TemplateCoordinatorAction, Never> {
actionsSubject.eraseToAnyPublisher()
}
init(parameters: TemplateCoordinatorParameters) {
self.parameters = parameters
@@ -40,16 +45,17 @@ final class TemplateCoordinator: CoordinatorProtocol {
}
func start() {
viewModel.callback = { [weak self] action in
viewModel.actions.sink { [weak self] action in
guard let self else { return }
switch action {
case .accept:
MXLog.info("User accepted the prompt.")
self.callback?(.accept)
self.actionsSubject.send(.accept)
case .cancel:
self.callback?(.cancel)
self.actionsSubject.send(.cancel)
}
}
.store(in: &cancellables)
}
func toPresentable() -> AnyView {

View File

@@ -14,12 +14,17 @@
// limitations under the License.
//
import Combine
import SwiftUI
typealias TemplateViewModelType = StateStoreViewModel<TemplateViewState, TemplateViewAction>
class TemplateViewModel: TemplateViewModelType, TemplateViewModelProtocol {
var callback: ((TemplateViewModelAction) -> Void)?
private var actionsSubject: PassthroughSubject<TemplateViewModelAction, Never> = .init()
var actions: AnyPublisher<TemplateViewModelAction, Never> {
actionsSubject.eraseToAnyPublisher()
}
init(promptType: TemplatePromptType, initialCount: Int = 0) {
super.init(initialViewState: TemplateViewState(promptType: promptType, count: 0))
@@ -27,12 +32,12 @@ class TemplateViewModel: TemplateViewModelType, TemplateViewModelProtocol {
// MARK: - Public
override func process(viewAction: TemplateViewAction) async {
override func process(viewAction: TemplateViewAction) {
switch viewAction {
case .accept:
callback?(.accept)
actionsSubject.send(.accept)
case .cancel:
callback?(.cancel)
actionsSubject.send(.cancel)
case .incrementCount:
state.count += 1
case .decrementCount:

View File

@@ -14,10 +14,10 @@
// limitations under the License.
//
import Foundation
import Combine
@MainActor
protocol TemplateViewModelProtocol {
var callback: ((TemplateViewModelAction) -> Void)? { get set }
var actions: AnyPublisher<TemplateViewModelAction, Never> { get }
var context: TemplateViewModelType.Context { get }
}

View File

@@ -38,15 +38,12 @@ class TemplateScreenViewModelTests: XCTestCase {
func testCounter() async throws {
context.send(viewAction: .incrementCount)
await Task.yield()
XCTAssertEqual(context.viewState.count, 1)
context.send(viewAction: .incrementCount)
await Task.yield()
XCTAssertEqual(context.viewState.count, 2)
context.send(viewAction: .decrementCount)
await Task.yield()
XCTAssertEqual(context.viewState.count, 1)
}
}