2024-04-12 13:13:22 +01:00
|
|
|
//
|
2024-09-06 16:34:30 +03:00
|
|
|
// Copyright 2022-2024 New Vector Ltd.
|
2024-04-12 13:13:22 +01:00
|
|
|
//
|
2025-01-06 11:27:37 +01:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
|
|
|
// Please see LICENSE files in the repository root for full details.
|
2024-04-12 13:13:22 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
import XCTest
|
|
|
|
|
|
|
|
@testable import ElementX
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
class JoinRoomScreenViewModelTests: XCTestCase {
|
2025-02-10 18:31:12 +01:00
|
|
|
private enum TestMode {
|
|
|
|
case joined
|
|
|
|
case knocked
|
|
|
|
case invited
|
|
|
|
case banned
|
|
|
|
}
|
|
|
|
|
2024-04-12 13:13:22 +01:00
|
|
|
var viewModel: JoinRoomScreenViewModelProtocol!
|
2025-02-18 12:22:13 +00:00
|
|
|
|
2025-02-10 18:31:12 +01:00
|
|
|
var clientProxy: ClientProxyMock!
|
2025-02-18 12:22:13 +00:00
|
|
|
var appSettings: AppSettings!
|
2024-04-12 13:13:22 +01:00
|
|
|
|
|
|
|
var context: JoinRoomScreenViewModelType.Context {
|
|
|
|
viewModel.context
|
|
|
|
}
|
2024-10-17 16:00:51 +02:00
|
|
|
|
2025-02-18 12:22:13 +00:00
|
|
|
override func setUp() {
|
|
|
|
AppSettings.resetAllSettings()
|
|
|
|
appSettings = AppSettings()
|
|
|
|
ServiceLocator.shared.register(appSettings: appSettings)
|
|
|
|
}
|
|
|
|
|
2024-10-17 16:00:51 +02:00
|
|
|
override func tearDown() {
|
|
|
|
viewModel = nil
|
2025-02-10 18:31:12 +01:00
|
|
|
clientProxy = nil
|
2024-10-17 16:00:51 +02:00
|
|
|
AppSettings.resetAllSettings()
|
|
|
|
}
|
2024-04-12 13:13:22 +01:00
|
|
|
|
|
|
|
func testInteraction() async throws {
|
2025-02-18 12:22:13 +00:00
|
|
|
XCTAssertTrue(appSettings.seenInvites.isEmpty, "There shouldn't be any seen invites before running the tests.")
|
|
|
|
|
2024-04-12 13:13:22 +01:00
|
|
|
setupViewModel()
|
2025-02-18 12:22:13 +00:00
|
|
|
try await deferFulfillment(viewModel.context.$viewState) { $0.mode == .joinable }.fulfill()
|
|
|
|
|
|
|
|
XCTAssertTrue(appSettings.seenInvites.isEmpty, "Only an invited room should register the room ID as a seen invite.")
|
2024-04-12 13:13:22 +01:00
|
|
|
|
|
|
|
let deferred = deferFulfillment(viewModel.actionsPublisher) { $0 == .joined }
|
|
|
|
context.send(viewAction: .join)
|
|
|
|
try await deferred.fulfill()
|
|
|
|
}
|
|
|
|
|
2024-04-19 17:04:18 +03:00
|
|
|
func testAcceptInviteInteraction() async throws {
|
2025-02-18 12:22:13 +00:00
|
|
|
XCTAssertTrue(appSettings.seenInvites.isEmpty, "There shouldn't be any seen invites before running the tests.")
|
|
|
|
|
|
|
|
setupViewModel(mode: .invited)
|
|
|
|
try await deferFulfillment(viewModel.context.$viewState) { $0.mode == .invited(isDM: false) }.fulfill()
|
|
|
|
|
|
|
|
XCTAssertEqual(appSettings.seenInvites, ["1"], "The invited room's ID should be registered as a seen invite.")
|
2024-04-19 17:04:18 +03:00
|
|
|
|
|
|
|
let deferred = deferFulfillment(viewModel.actionsPublisher) { $0 == .joined }
|
|
|
|
context.send(viewAction: .acceptInvite)
|
|
|
|
try await deferred.fulfill()
|
2025-02-18 12:22:13 +00:00
|
|
|
|
|
|
|
XCTAssertTrue(appSettings.seenInvites.isEmpty, "The after accepting an invite the invite should be forgotten in case the user leaves.")
|
2024-04-19 17:04:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func testDeclineInviteInteraction() async throws {
|
2025-02-18 12:22:13 +00:00
|
|
|
XCTAssertTrue(appSettings.seenInvites.isEmpty, "There shouldn't be any seen invites before running the tests.")
|
|
|
|
|
2025-02-10 18:31:12 +01:00
|
|
|
setupViewModel(mode: .invited)
|
2024-04-19 17:04:18 +03:00
|
|
|
|
2025-02-18 12:22:13 +00:00
|
|
|
try await deferFulfillment(viewModel.context.$viewState) { $0.mode == .invited(isDM: false) }.fulfill()
|
|
|
|
XCTAssertEqual(appSettings.seenInvites, ["1"], "The invited room's ID should be registered as a seen invite.")
|
2024-04-19 17:04:18 +03:00
|
|
|
|
|
|
|
context.send(viewAction: .declineInvite)
|
|
|
|
|
|
|
|
XCTAssertEqual(viewModel.context.alertInfo?.id, .declineInvite)
|
2025-02-18 12:22:13 +00:00
|
|
|
let deferred = deferFulfillment(viewModel.actionsPublisher) { $0 == .dismiss }
|
2025-02-10 18:31:12 +01:00
|
|
|
context.alertInfo?.secondaryButton?.action?()
|
|
|
|
try await deferred.fulfill()
|
2025-02-18 12:22:13 +00:00
|
|
|
|
|
|
|
XCTAssertTrue(appSettings.seenInvites.isEmpty, "The after declining an invite the invite should be forgotten in case another invite is received.")
|
2024-04-19 17:04:18 +03:00
|
|
|
}
|
|
|
|
|
2024-10-17 16:00:51 +02:00
|
|
|
func testKnockedState() async throws {
|
2025-02-18 12:22:13 +00:00
|
|
|
XCTAssertTrue(appSettings.seenInvites.isEmpty, "There shouldn't be any seen invites before running the tests.")
|
2025-02-10 18:31:12 +01:00
|
|
|
setupViewModel(mode: .knocked)
|
2024-10-17 16:00:51 +02:00
|
|
|
|
2025-02-18 12:22:13 +00:00
|
|
|
try await deferFulfillment(viewModel.context.$viewState) { $0.mode == .knocked }.fulfill()
|
|
|
|
|
|
|
|
XCTAssertTrue(appSettings.seenInvites.isEmpty, "Only an invited room should register the room ID as a seen invite.")
|
2024-10-17 16:00:51 +02:00
|
|
|
}
|
|
|
|
|
2024-10-24 17:23:06 +02:00
|
|
|
func testCancelKnock() async throws {
|
2025-02-10 18:31:12 +01:00
|
|
|
setupViewModel(mode: .knocked)
|
2024-10-24 17:23:06 +02:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2025-02-10 18:31:12 +01:00
|
|
|
func testDeclineAndBlockInviteInteraction() async throws {
|
|
|
|
setupViewModel(mode: .invited)
|
|
|
|
let expectation = expectation(description: "Wait for the user to be ignored")
|
|
|
|
clientProxy.ignoreUserClosure = { userID in
|
|
|
|
defer { expectation.fulfill() }
|
|
|
|
XCTAssertEqual(userID, "@test:matrix.org")
|
|
|
|
return .success(())
|
|
|
|
}
|
|
|
|
|
|
|
|
try await deferFulfillment(viewModel.context.$viewState) { $0.roomDetails != nil }.fulfill()
|
|
|
|
|
|
|
|
context.send(viewAction: .declineInviteAndBlock(userID: "@test:matrix.org"))
|
|
|
|
|
|
|
|
XCTAssertEqual(viewModel.context.alertInfo?.id, .declineInviteAndBlock)
|
|
|
|
|
|
|
|
let deferred = deferFulfillment(viewModel.actionsPublisher) { action in
|
|
|
|
action == .dismiss
|
|
|
|
}
|
|
|
|
context.alertInfo?.secondaryButton?.action?()
|
|
|
|
try await deferred.fulfill()
|
|
|
|
|
|
|
|
await fulfillment(of: [expectation], timeout: 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testForgetRoom() async throws {
|
|
|
|
setupViewModel(mode: .banned)
|
|
|
|
|
|
|
|
try await deferFulfillment(viewModel.context.$viewState) { $0.roomDetails != nil }.fulfill()
|
|
|
|
|
|
|
|
let deferred = deferFulfillment(viewModel.actionsPublisher) { action in
|
|
|
|
action == .dismiss
|
|
|
|
}
|
|
|
|
context.send(viewAction: .forget)
|
|
|
|
try await deferred.fulfill()
|
|
|
|
}
|
|
|
|
|
|
|
|
private func setupViewModel(throwing: Bool = false, mode: TestMode = .joined) {
|
2025-01-20 19:07:11 +02:00
|
|
|
ServiceLocator.shared.settings.knockingEnabled = true
|
|
|
|
|
2025-02-10 18:31:12 +01:00
|
|
|
clientProxy = ClientProxyMock(.init())
|
2024-04-19 17:04:18 +03:00
|
|
|
|
2024-05-09 12:25:54 +01:00
|
|
|
clientProxy.joinRoomViaReturnValue = throwing ? .failure(.sdkError(ClientProxyMockError.generic)) : .success(())
|
2025-02-18 12:22:13 +00:00
|
|
|
clientProxy.joinRoomAliasReturnValue = clientProxy.joinRoomViaReturnValue
|
2024-04-19 17:04:18 +03:00
|
|
|
|
2025-02-10 18:31:12 +01:00
|
|
|
switch mode {
|
|
|
|
case .knocked:
|
2025-01-20 19:07:11 +02:00
|
|
|
clientProxy.roomPreviewForIdentifierViaReturnValue = .success(RoomPreviewProxyMock.knocked)
|
|
|
|
|
2024-10-17 16:00:51 +02:00
|
|
|
clientProxy.roomForIdentifierClosure = { _ in
|
2024-10-24 17:23:06 +02:00
|
|
|
let roomProxy = KnockedRoomProxyMock(.init())
|
|
|
|
// to test the cancel knock function
|
|
|
|
roomProxy.cancelKnockUnderlyingReturnValue = .success(())
|
|
|
|
return .knocked(roomProxy)
|
2024-10-17 16:00:51 +02:00
|
|
|
}
|
2025-02-10 18:31:12 +01:00
|
|
|
case .joined:
|
2025-01-20 19:07:11 +02:00
|
|
|
clientProxy.roomPreviewForIdentifierViaReturnValue = .success(RoomPreviewProxyMock.joinable)
|
2025-02-10 18:31:12 +01:00
|
|
|
case .invited:
|
|
|
|
clientProxy.roomPreviewForIdentifierViaReturnValue = .success(RoomPreviewProxyMock.invited())
|
|
|
|
clientProxy.roomForIdentifierClosure = { _ in
|
|
|
|
let roomProxy = InvitedRoomProxyMock(.init())
|
|
|
|
roomProxy.rejectInvitationReturnValue = .success(())
|
|
|
|
return .invited(roomProxy)
|
|
|
|
}
|
|
|
|
case .banned:
|
|
|
|
clientProxy.roomPreviewForIdentifierViaReturnValue = .success(RoomPreviewProxyMock.banned)
|
|
|
|
clientProxy.roomForIdentifierClosure = { _ in
|
|
|
|
let roomProxy = BannedRoomProxyMock(.init())
|
|
|
|
roomProxy.forgetRoomReturnValue = .success(())
|
|
|
|
return .banned(roomProxy)
|
|
|
|
}
|
2024-10-17 16:00:51 +02:00
|
|
|
}
|
|
|
|
|
2024-04-12 13:13:22 +01:00
|
|
|
viewModel = JoinRoomScreenViewModel(roomID: "1",
|
2024-05-09 12:25:54 +01:00
|
|
|
via: [],
|
2025-02-18 12:22:13 +00:00
|
|
|
appSettings: appSettings,
|
2024-04-19 17:04:18 +03:00
|
|
|
clientProxy: clientProxy,
|
2024-10-02 17:41:08 +01:00
|
|
|
mediaProvider: MediaProviderMock(configuration: .init()),
|
2024-04-19 17:04:18 +03:00
|
|
|
userIndicatorController: ServiceLocator.shared.userIndicatorController)
|
2024-04-12 13:13:22 +01:00
|
|
|
}
|
|
|
|
}
|