mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 12:07:55 +00:00
Switch callbacks to combine (#1710)
* #750 - Convert the SoftLogoutScreen to combine * #750 - Convert the UserSessionFlowCoordinator to Combine * #750 - Convert the AnalyticsPromptScreen to Combine * #750 - Convert the LoginScreen to Combine * #750 - Convert the ServerSelectionScreen to Combine * #750 - Convert the EmojiPickerScreen to Combine * #750 - Convert the HomeScreen to Combine * #750 - Convert the MediaUploadPreviewScreen to Combine * #750 - Convert the OnboardingScreen to Combine * Rename `Onboarding` to `OnboardingScreen` * #750 - Convert the ReportContentScreen to Combine * #750 - Convert the RoomDetailsSscreen to Combine * #750 - Convert the RoomMemberDetailsScreen to Combine * #750 - Convert the RoomMembersListScreen to Combine * #750 - Convert the SessionVerificationScreen to Combine * #750 - Convert the SettingsScreen to Combine * #750 - Convert the AdvancedSettingsScreen to Combine * #750 - Convert the DeveloperOptionsScreen to Combine * Fix the unit tests * Use .sink action and the same cancellables constructor everywhere * Cleanup cancellables when setting up tests
This commit is contained in:
@@ -26,13 +26,14 @@ class CreateRoomScreenViewModelTests: XCTestCase {
|
||||
var userSession: MockUserSession!
|
||||
|
||||
private let usersSubject = CurrentValueSubject<[UserProfileProxy], Never>([])
|
||||
private var cancellables: Set<AnyCancellable> = []
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
|
||||
var context: CreateRoomViewModel.Context {
|
||||
viewModel.context
|
||||
}
|
||||
|
||||
override func setUpWithError() throws {
|
||||
cancellables.removeAll()
|
||||
clientProxy = MockClientProxy(userID: "@a:b.com")
|
||||
userSession = MockUserSession(clientProxy: clientProxy, mediaProvider: MockMediaProvider())
|
||||
let parameters = CreateRoomFlowParameters()
|
||||
|
||||
@@ -23,12 +23,11 @@ import XCTest
|
||||
class HomeScreenViewModelTests: XCTestCase {
|
||||
var viewModel: HomeScreenViewModelProtocol!
|
||||
var clientProxy: MockClientProxy!
|
||||
|
||||
var context: HomeScreenViewModelType.Context! {
|
||||
viewModel.context
|
||||
}
|
||||
var context: HomeScreenViewModelType.Context! { viewModel.context }
|
||||
var cancellables = Set<AnyCancellable>()
|
||||
|
||||
override func setUpWithError() throws {
|
||||
cancellables.removeAll()
|
||||
clientProxy = MockClientProxy(userID: "@mock:client.com")
|
||||
viewModel = HomeScreenViewModel(userSession: MockUserSession(clientProxy: clientProxy,
|
||||
mediaProvider: MockMediaProvider()),
|
||||
@@ -43,16 +42,19 @@ class HomeScreenViewModelTests: XCTestCase {
|
||||
let mockRoomId = "mock_room_id"
|
||||
var correctResult = false
|
||||
var selectedRoomId = ""
|
||||
viewModel.callback = { result in
|
||||
switch result {
|
||||
case .presentRoom(let roomId):
|
||||
correctResult = true
|
||||
selectedRoomId = roomId
|
||||
default:
|
||||
break
|
||||
|
||||
viewModel.actions
|
||||
.sink { action in
|
||||
switch action {
|
||||
case .presentRoom(let roomId):
|
||||
correctResult = true
|
||||
selectedRoomId = roomId
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.store(in: &cancellables)
|
||||
|
||||
context.send(viewAction: .selectRoom(roomIdentifier: mockRoomId))
|
||||
await Task.yield()
|
||||
XCTAssert(correctResult)
|
||||
@@ -61,15 +63,18 @@ class HomeScreenViewModelTests: XCTestCase {
|
||||
|
||||
func testTapUserAvatar() async throws {
|
||||
var correctResult = false
|
||||
viewModel.callback = { result in
|
||||
switch result {
|
||||
case .presentSettingsScreen:
|
||||
correctResult = true
|
||||
default:
|
||||
break
|
||||
|
||||
viewModel.actions
|
||||
.sink { action in
|
||||
switch action {
|
||||
case .presentSettingsScreen:
|
||||
correctResult = true
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.store(in: &cancellables)
|
||||
|
||||
context.send(viewAction: .userMenu(action: .settings))
|
||||
await Task.yield()
|
||||
XCTAssert(correctResult)
|
||||
@@ -97,15 +102,17 @@ class HomeScreenViewModelTests: XCTestCase {
|
||||
let mockRoomId = "1"
|
||||
var correctResult = false
|
||||
let expectation = expectation(description: #function)
|
||||
viewModel.callback = { result in
|
||||
switch result {
|
||||
case .roomLeft(let roomIdentifier):
|
||||
correctResult = roomIdentifier == mockRoomId
|
||||
default:
|
||||
break
|
||||
viewModel.actions
|
||||
.sink { action in
|
||||
switch action {
|
||||
case .roomLeft(let roomIdentifier):
|
||||
correctResult = roomIdentifier == mockRoomId
|
||||
default:
|
||||
break
|
||||
}
|
||||
expectation.fulfill()
|
||||
}
|
||||
expectation.fulfill()
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
let room: RoomProxyMock = .init(with: .init(id: mockRoomId, displayName: "Some room"))
|
||||
room.leaveRoomClosure = { .success(()) }
|
||||
clientProxy.roomForIdentifierMocks[mockRoomId] = room
|
||||
@@ -118,14 +125,16 @@ class HomeScreenViewModelTests: XCTestCase {
|
||||
func testShowRoomDetails() async throws {
|
||||
let mockRoomId = "1"
|
||||
var correctResult = false
|
||||
viewModel.callback = { result in
|
||||
switch result {
|
||||
case .presentRoomDetails(let roomIdentifier):
|
||||
correctResult = roomIdentifier == mockRoomId
|
||||
default:
|
||||
break
|
||||
viewModel.actions
|
||||
.sink { action in
|
||||
switch action {
|
||||
case .presentRoomDetails(let roomIdentifier):
|
||||
correctResult = roomIdentifier == mockRoomId
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
context.send(viewAction: .showRoomDetails(roomIdentifier: mockRoomId))
|
||||
await Task.yield()
|
||||
XCTAssertNil(context.alertInfo)
|
||||
|
||||
@@ -25,12 +25,16 @@ class InviteUsersScreenViewModelTests: XCTestCase {
|
||||
var clientProxy: MockClientProxy!
|
||||
var userDiscoveryService: UserDiscoveryServiceMock!
|
||||
|
||||
private var cancellables: Set<AnyCancellable> = []
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
|
||||
var context: InviteUsersScreenViewModel.Context {
|
||||
viewModel.context
|
||||
}
|
||||
|
||||
override func setUp() {
|
||||
cancellables.removeAll()
|
||||
}
|
||||
|
||||
func testSelectUser() {
|
||||
setupWithRoomType(roomType: .draft)
|
||||
XCTAssertTrue(context.viewState.selectedUsers.isEmpty)
|
||||
|
||||
@@ -26,6 +26,7 @@ class MessageForwardingScreenViewModelTests: XCTestCase {
|
||||
var cancellables = Set<AnyCancellable>()
|
||||
|
||||
override func setUpWithError() throws {
|
||||
cancellables.removeAll()
|
||||
viewModel = MessageForwardingScreenViewModel(roomSummaryProvider: MockRoomSummaryProvider(state: .loaded(.mockRooms)), sourceRoomID: "1")
|
||||
context = viewModel.context
|
||||
}
|
||||
|
||||
+1
-1
@@ -18,6 +18,6 @@ import XCTest
|
||||
|
||||
@testable import ElementX
|
||||
|
||||
class OnboardingViewModelTests: XCTestCase {
|
||||
class OnboardingScreenViewModelTests: XCTestCase {
|
||||
// Nothing to test, the view model has no mutable state.
|
||||
}
|
||||
@@ -14,6 +14,7 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import Combine
|
||||
import MatrixRustSDK
|
||||
import SwiftUI
|
||||
import XCTest
|
||||
@@ -26,8 +27,10 @@ class RoomDetailsScreenViewModelTests: XCTestCase {
|
||||
var roomProxyMock: RoomProxyMock!
|
||||
var notificationSettingsProxyMock: NotificationSettingsProxyMock!
|
||||
var context: RoomDetailsScreenViewModelType.Context { viewModel.context }
|
||||
var cancellables = Set<AnyCancellable>()
|
||||
|
||||
override func setUp() {
|
||||
cancellables.removeAll()
|
||||
roomProxyMock = RoomProxyMock(with: .init(displayName: "Test", joinedMembersCount: 0))
|
||||
notificationSettingsProxyMock = NotificationSettingsProxyMock(with: NotificationSettingsProxyMockConfiguration())
|
||||
viewModel = RoomDetailsScreenViewModel(accountUserID: "@owner:somewhere.com",
|
||||
@@ -84,15 +87,17 @@ class RoomDetailsScreenViewModelTests: XCTestCase {
|
||||
roomProxyMock.leaveRoomClosure = {
|
||||
.success(())
|
||||
}
|
||||
viewModel.callback = { action in
|
||||
switch action {
|
||||
case .leftRoom:
|
||||
break
|
||||
default:
|
||||
XCTFail("leftRoom expected")
|
||||
viewModel.actions
|
||||
.sink { action in
|
||||
switch action {
|
||||
case .leftRoom:
|
||||
break
|
||||
default:
|
||||
XCTFail("leftRoom expected")
|
||||
}
|
||||
expectation.fulfill()
|
||||
}
|
||||
expectation.fulfill()
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
context.send(viewAction: .confirmLeave)
|
||||
await fulfillment(of: [expectation])
|
||||
XCTAssertEqual(roomProxyMock.leaveRoomCallsCount, 1)
|
||||
@@ -263,14 +268,16 @@ class RoomDetailsScreenViewModelTests: XCTestCase {
|
||||
XCTAssertTrue(context.viewState.canInviteUsers)
|
||||
|
||||
var callbackCorrectlyCalled = false
|
||||
viewModel.callback = { action in
|
||||
switch action {
|
||||
case .requestInvitePeoplePresentation:
|
||||
callbackCorrectlyCalled = true
|
||||
default:
|
||||
callbackCorrectlyCalled = false
|
||||
viewModel.actions
|
||||
.sink { action in
|
||||
switch action {
|
||||
case .requestInvitePeoplePresentation:
|
||||
callbackCorrectlyCalled = true
|
||||
default:
|
||||
callbackCorrectlyCalled = false
|
||||
}
|
||||
}
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
|
||||
context.send(viewAction: .processTapInvite)
|
||||
await Task.yield()
|
||||
|
||||
@@ -23,9 +23,10 @@ import Combine
|
||||
class RoomFlowCoordinatorTests: XCTestCase {
|
||||
var roomFlowCoordinator: RoomFlowCoordinator!
|
||||
var navigationStackCoordinator: NavigationStackCoordinator!
|
||||
private var cancellables: Set<AnyCancellable> = .init()
|
||||
var cancellables = Set<AnyCancellable>()
|
||||
|
||||
override func setUp() async throws {
|
||||
cancellables.removeAll()
|
||||
let clientProxy = MockClientProxy(userID: "hi@bob", roomSummaryProvider: MockRoomSummaryProvider(state: .loaded(.mockRooms)))
|
||||
let mediaProvider = MockMediaProvider()
|
||||
let userSession = MockUserSession(clientProxy: clientProxy, mediaProvider: mediaProvider)
|
||||
|
||||
@@ -26,9 +26,10 @@ class RoomNotificationSettingsScreenViewModelTests: XCTestCase {
|
||||
var roomProxyMock: RoomProxyMock!
|
||||
var notificationSettingsProxyMock: NotificationSettingsProxyMock!
|
||||
var context: RoomNotificationSettingsScreenViewModelType.Context { viewModel.context }
|
||||
var cancellables: Set<AnyCancellable> = []
|
||||
var cancellables = Set<AnyCancellable>()
|
||||
|
||||
override func setUpWithError() throws {
|
||||
cancellables.removeAll()
|
||||
roomProxyMock = RoomProxyMock(with: .init(displayName: "Test", joinedMembersCount: 0))
|
||||
notificationSettingsProxyMock = NotificationSettingsProxyMock(with: NotificationSettingsProxyMockConfiguration())
|
||||
viewModel = RoomNotificationSettingsScreenViewModel(notificationSettingsProxy: notificationSettingsProxyMock,
|
||||
@@ -172,8 +173,8 @@ class RoomNotificationSettingsScreenViewModelTests: XCTestCase {
|
||||
|
||||
var actionSent: RoomNotificationSettingsScreenViewModelAction?
|
||||
viewModel.actions
|
||||
.sink { value in
|
||||
actionSent = value
|
||||
.sink { action in
|
||||
actionSent = action
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
|
||||
@@ -207,8 +208,8 @@ class RoomNotificationSettingsScreenViewModelTests: XCTestCase {
|
||||
|
||||
var actionSent: RoomNotificationSettingsScreenViewModelAction?
|
||||
viewModel.actions
|
||||
.sink { value in
|
||||
actionSent = value
|
||||
.sink { action in
|
||||
actionSent = action
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ class RoomScreenViewModelTests: XCTestCase {
|
||||
var cancellables = Set<AnyCancellable>()
|
||||
|
||||
override func setUp() async throws {
|
||||
cancellables.removeAll()
|
||||
userIndicatorControllerMock = UserIndicatorControllerMock.default
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import Combine
|
||||
import XCTest
|
||||
|
||||
@testable import ElementX
|
||||
@@ -22,8 +23,10 @@ import XCTest
|
||||
class SettingsScreenViewModelTests: XCTestCase {
|
||||
var viewModel: SettingsScreenViewModelProtocol!
|
||||
var context: SettingsScreenViewModelType.Context!
|
||||
var cancellables = Set<AnyCancellable>()
|
||||
|
||||
@MainActor override func setUpWithError() throws {
|
||||
cancellables.removeAll()
|
||||
let userSession = MockUserSession(clientProxy: MockClientProxy(userID: ""),
|
||||
mediaProvider: MockMediaProvider())
|
||||
viewModel = SettingsScreenViewModel(userSession: userSession, appSettings: ServiceLocator.shared.settings)
|
||||
@@ -32,14 +35,17 @@ class SettingsScreenViewModelTests: XCTestCase {
|
||||
|
||||
@MainActor func testLogout() async throws {
|
||||
var correctResult = false
|
||||
viewModel.callback = { result in
|
||||
switch result {
|
||||
case .logout:
|
||||
correctResult = true
|
||||
default:
|
||||
break
|
||||
|
||||
viewModel.actions
|
||||
.sink { action in
|
||||
switch action {
|
||||
case .logout:
|
||||
correctResult = true
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
|
||||
context.send(viewAction: .logout)
|
||||
await Task.yield()
|
||||
@@ -48,10 +54,12 @@ class SettingsScreenViewModelTests: XCTestCase {
|
||||
|
||||
func testReportBug() async throws {
|
||||
var correctResult = false
|
||||
viewModel.callback = { result in
|
||||
correctResult = result == .reportBug
|
||||
}
|
||||
|
||||
viewModel.actions
|
||||
.sink { action in
|
||||
correctResult = action == .reportBug
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
|
||||
context.send(viewAction: .reportBug)
|
||||
await Task.yield()
|
||||
XCTAssert(correctResult)
|
||||
@@ -59,10 +67,12 @@ class SettingsScreenViewModelTests: XCTestCase {
|
||||
|
||||
func testAnalytics() async throws {
|
||||
var correctResult = false
|
||||
viewModel.callback = { result in
|
||||
correctResult = result == .analytics
|
||||
}
|
||||
|
||||
viewModel.actions
|
||||
.sink { action in
|
||||
correctResult = action == .analytics
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
|
||||
context.send(viewAction: .analytics)
|
||||
await Task.yield()
|
||||
XCTAssert(correctResult)
|
||||
|
||||
@@ -24,13 +24,14 @@ class StaticLocationScreenViewModelTests: XCTestCase {
|
||||
var viewModel: StaticLocationScreenViewModelProtocol!
|
||||
|
||||
private let usersSubject = CurrentValueSubject<[UserProfileProxy], Never>([])
|
||||
private var cancellables: Set<AnyCancellable> = []
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
|
||||
var context: StaticLocationScreenViewModel.Context {
|
||||
viewModel.context
|
||||
}
|
||||
|
||||
override func setUpWithError() throws {
|
||||
cancellables.removeAll()
|
||||
let viewModel = StaticLocationScreenViewModel(interactionMode: .picker)
|
||||
viewModel.state.bindings.isLocationAuthorized = true
|
||||
self.viewModel = viewModel
|
||||
|
||||
@@ -21,10 +21,10 @@ final class UserSessionTests: XCTestCase {
|
||||
var userSession: UserSession!
|
||||
let clientProxy = MockClientProxy(userID: "@test:user.net")
|
||||
|
||||
private var cancellables: Set<AnyCancellable> = []
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
|
||||
override func setUpWithError() throws {
|
||||
cancellables = []
|
||||
cancellables.removeAll()
|
||||
userSession = UserSession(clientProxy: clientProxy, mediaProvider: MockMediaProvider())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user