2023-05-26 15:47:12 +02:00
|
|
|
//
|
2024-09-06 16:34:30 +03:00
|
|
|
// Copyright 2022-2024 New Vector Ltd.
|
2023-05-26 15:47:12 +02: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.
|
2023-05-26 15:47:12 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
import MatrixRustSDK
|
|
|
|
import XCTest
|
|
|
|
|
|
|
|
@testable import ElementX
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
class RoomDetailsEditScreenViewModelTests: XCTestCase {
|
|
|
|
var viewModel: RoomDetailsEditScreenViewModel!
|
|
|
|
|
2023-06-06 10:46:04 +02:00
|
|
|
var userIndicatorController: UserIndicatorControllerMock!
|
2023-05-26 15:47:12 +02:00
|
|
|
|
|
|
|
var context: RoomDetailsEditScreenViewModelType.Context {
|
|
|
|
viewModel.context
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCannotSaveOnLanding() {
|
2024-03-05 14:08:34 +00:00
|
|
|
setupViewModel(roomProxyConfiguration: .init(name: "Some room", members: [.mockMeAdmin]))
|
2023-05-26 15:47:12 +02:00
|
|
|
XCTAssertFalse(context.viewState.canSave)
|
|
|
|
}
|
|
|
|
|
2024-03-05 14:08:34 +00:00
|
|
|
func testCanEdit() async throws {
|
|
|
|
setupViewModel(roomProxyConfiguration: .init(name: "Some room", members: [.mockMeAdmin]))
|
|
|
|
|
|
|
|
let deferred = deferFulfillment(context.$viewState) { $0.canEditName }
|
|
|
|
try await deferred.fulfill()
|
|
|
|
|
2023-05-26 15:47:12 +02:00
|
|
|
XCTAssertTrue(context.viewState.canEditAvatar)
|
|
|
|
XCTAssertTrue(context.viewState.canEditName)
|
|
|
|
XCTAssertTrue(context.viewState.canEditTopic)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCannotEdit() {
|
2024-03-05 14:08:34 +00:00
|
|
|
setupViewModel(roomProxyConfiguration: .init(name: "Some room", members: [.mockMe]))
|
2023-05-26 15:47:12 +02:00
|
|
|
XCTAssertFalse(context.viewState.canEditAvatar)
|
|
|
|
XCTAssertFalse(context.viewState.canEditName)
|
|
|
|
XCTAssertFalse(context.viewState.canEditTopic)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testNameDidChange() {
|
2024-03-05 14:08:34 +00:00
|
|
|
setupViewModel(roomProxyConfiguration: .init(name: "Some room", members: [.mockMeAdmin]))
|
2023-05-26 15:47:12 +02:00
|
|
|
context.name = "name"
|
|
|
|
XCTAssertTrue(context.viewState.nameDidChange)
|
|
|
|
XCTAssertTrue(context.viewState.canSave)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testTopicDidChange() {
|
2024-03-05 14:08:34 +00:00
|
|
|
setupViewModel(roomProxyConfiguration: .init(name: "Some room", members: [.mockMeAdmin]))
|
2023-05-26 15:47:12 +02:00
|
|
|
context.topic = "topic"
|
|
|
|
XCTAssertTrue(context.viewState.topicDidChange)
|
|
|
|
XCTAssertTrue(context.viewState.canSave)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAvatarDidChange() {
|
2024-11-28 08:34:38 +00:00
|
|
|
setupViewModel(roomProxyConfiguration: .init(name: "Some room", avatarURL: .mockMXCAvatar, members: [.mockMeAdmin]))
|
2023-05-26 15:47:12 +02:00
|
|
|
context.send(viewAction: .removeImage)
|
|
|
|
XCTAssertTrue(context.viewState.avatarDidChange)
|
|
|
|
XCTAssertTrue(context.viewState.canSave)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testEmptyNameCannotBeSaved() {
|
2024-03-05 14:08:34 +00:00
|
|
|
setupViewModel(roomProxyConfiguration: .init(name: "Some room", members: [.mockMeAdmin]))
|
2023-05-26 15:47:12 +02:00
|
|
|
context.name = ""
|
|
|
|
XCTAssertFalse(context.viewState.canSave)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSaveShowsSheet() {
|
2024-03-05 14:08:34 +00:00
|
|
|
setupViewModel(roomProxyConfiguration: .init(name: "Some room", members: [.mockMeAdmin]))
|
2023-05-26 15:47:12 +02:00
|
|
|
context.name = "name"
|
|
|
|
XCTAssertFalse(context.showMediaSheet)
|
|
|
|
context.send(viewAction: .presentMediaSource)
|
|
|
|
XCTAssertTrue(context.showMediaSheet)
|
|
|
|
}
|
|
|
|
|
2023-07-12 18:59:46 +01:00
|
|
|
func testSaveTriggersViewModelAction() async throws {
|
2024-03-05 14:08:34 +00:00
|
|
|
setupViewModel(roomProxyConfiguration: .init(name: "Some room", members: [.mockMeAdmin]))
|
2023-07-12 18:59:46 +01:00
|
|
|
|
2023-09-26 13:28:29 +03:00
|
|
|
let deferred = deferFulfillment(viewModel.actions) { action in
|
|
|
|
action == .saveFinished
|
|
|
|
}
|
|
|
|
|
2023-05-26 15:47:12 +02:00
|
|
|
context.name = "name"
|
|
|
|
context.send(viewAction: .save)
|
2023-09-26 13:28:29 +03:00
|
|
|
|
2023-07-12 18:59:46 +01:00
|
|
|
let action = try await deferred.fulfill()
|
2023-05-26 15:47:12 +02:00
|
|
|
XCTAssertEqual(action, .saveFinished)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testErrorShownOnFailedFetchOfMedia() async throws {
|
2024-03-05 14:08:34 +00:00
|
|
|
setupViewModel(roomProxyConfiguration: .init(name: "Some room", members: [.mockMeAdmin]))
|
2023-05-26 15:47:12 +02:00
|
|
|
viewModel.didSelectMediaUrl(url: .picturesDirectory)
|
2023-06-06 10:46:04 +02:00
|
|
|
try? await Task.sleep(for: .milliseconds(100))
|
|
|
|
XCTAssertNotNil(userIndicatorController.alertInfo)
|
2023-05-26 15:47:12 +02:00
|
|
|
}
|
|
|
|
|
2023-06-06 16:14:34 +02:00
|
|
|
func testDeleteAvatar() {
|
2024-11-28 08:34:38 +00:00
|
|
|
setupViewModel(roomProxyConfiguration: .init(name: "Some room", avatarURL: .mockMXCAvatar, members: [.mockMeAdmin]))
|
2023-06-06 16:14:34 +02:00
|
|
|
XCTAssertNotNil(context.viewState.avatarURL)
|
|
|
|
context.send(viewAction: .removeImage)
|
|
|
|
XCTAssertNil(context.viewState.avatarURL)
|
|
|
|
}
|
|
|
|
|
2023-05-26 15:47:12 +02:00
|
|
|
// MARK: - Private
|
|
|
|
|
2024-08-20 16:13:27 +03:00
|
|
|
private func setupViewModel(roomProxyConfiguration: JoinedRoomProxyMockConfiguration) {
|
2023-06-06 10:46:04 +02:00
|
|
|
userIndicatorController = UserIndicatorControllerMock.default
|
2024-08-20 16:13:27 +03:00
|
|
|
viewModel = .init(roomProxy: JoinedRoomProxyMock(roomProxyConfiguration),
|
2024-10-02 17:41:08 +01:00
|
|
|
mediaProvider: MediaProviderMock(configuration: .init()),
|
2024-10-14 14:48:59 +01:00
|
|
|
mediaUploadingPreprocessor: MediaUploadingPreprocessor(appSettings: ServiceLocator.shared.settings),
|
2023-05-26 15:47:12 +02:00
|
|
|
userIndicatorController: userIndicatorController)
|
|
|
|
}
|
|
|
|
}
|