Beam/UnitTests/Sources/JoinRoomScreenViewModelTests.swift
Stefan Ceriu 8577f53613
Refactor the JoinRoom screen to take advantage of newer APIs and support more joinRule/membership combinations (i.e. invite required, restricted, banned) (#3685)
- expose the full RoomPreview and RoomMembershipDetails through their own proxies
- implement standard mocks for all the different combinations
- converge on a single room info provider
- rebuild all the previews
- prioritise the preview data over the room one.
2025-01-20 19:07:11 +02:00

104 lines
3.5 KiB
Swift

//
// Copyright 2022-2024 New Vector Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
// Please see LICENSE files in the repository root for full details.
//
import XCTest
@testable import ElementX
@MainActor
class JoinRoomScreenViewModelTests: XCTestCase {
var viewModel: JoinRoomScreenViewModelProtocol!
var context: JoinRoomScreenViewModelType.Context {
viewModel.context
}
override func tearDown() {
viewModel = nil
AppSettings.resetAllSettings()
}
func testInteraction() async throws {
setupViewModel()
let deferred = deferFulfillment(viewModel.actionsPublisher) { $0 == .joined }
context.send(viewAction: .join)
try await deferred.fulfill()
}
func testAcceptInviteInteraction() async throws {
setupViewModel()
let deferred = deferFulfillment(viewModel.actionsPublisher) { $0 == .joined }
context.send(viewAction: .acceptInvite)
try await deferred.fulfill()
}
func testDeclineInviteInteraction() async throws {
setupViewModel()
try await deferFulfillment(viewModel.context.$viewState) { $0.roomDetails != nil }.fulfill()
context.send(viewAction: .declineInvite)
XCTAssertEqual(viewModel.context.alertInfo?.id, .declineInvite)
}
func testKnockedState() async throws {
setupViewModel(knocked: true)
try await deferFulfillment(viewModel.context.$viewState) { state in
state.mode == .knocked
}.fulfill()
}
func testCancelKnock() async throws {
setupViewModel(knocked: true)
try await deferFulfillment(viewModel.context.$viewState) { state in
state.mode == .knocked
}.fulfill()
context.send(viewAction: .cancelKnock)
XCTAssertEqual(viewModel.context.alertInfo?.id, .cancelKnock)
let deferred = deferFulfillment(viewModel.actionsPublisher) { action in
action == .dismiss
}
context.alertInfo?.secondaryButton?.action?()
try await deferred.fulfill()
}
private func setupViewModel(throwing: Bool = false, knocked: Bool = false) {
ServiceLocator.shared.settings.knockingEnabled = true
let clientProxy = ClientProxyMock(.init())
clientProxy.joinRoomViaReturnValue = throwing ? .failure(.sdkError(ClientProxyMockError.generic)) : .success(())
if knocked {
clientProxy.roomPreviewForIdentifierViaReturnValue = .success(RoomPreviewProxyMock.knocked)
clientProxy.roomForIdentifierClosure = { _ in
let roomProxy = KnockedRoomProxyMock(.init())
// to test the cancel knock function
roomProxy.cancelKnockUnderlyingReturnValue = .success(())
return .knocked(roomProxy)
}
} else {
clientProxy.roomPreviewForIdentifierViaReturnValue = .success(RoomPreviewProxyMock.joinable)
}
viewModel = JoinRoomScreenViewModel(roomID: "1",
via: [],
appSettings: ServiceLocator.shared.settings,
clientProxy: clientProxy,
mediaProvider: MediaProviderMock(configuration: .init()),
userIndicatorController: ServiceLocator.shared.userIndicatorController)
}
}