Files
element-x-ios/UnitTests/Sources/RoomDetailsEditScreenViewModelTests.swift
T
Michael 22d14ff8e4 AppSettings are Mockable (#5589)
* create DependenciesProtocol and replace ServiceLocator registration with it

replace mocking calls for ServiceLocator to Dependencies

replace ServiceLocator with instance level calls

replace ServiceLocator configs with TestDependencies config in Tests

replace servicelocator usage in analytics service

replace remaining ServiceLocator calls in unit tests

that depended on the UnitTestAppCoordinator configuration with per suite dependencies instances

fix swiftlint warning

remove remaining references to ServiceLocator

make dependencies properties consistent in tests

delete Dependencies and DependenciesProtocol

fix whitespace modifications

updates from PR feedback

a pass over converting AppSettings and AnalyticsService test instance properties to per test instantiation

fix a missed PR feedback

Sync up after rebase

create DependenciesProtocol and replace ServiceLocator registration with it

replace mocking calls for ServiceLocator to Dependencies

replace ServiceLocator with instance level calls

replace ServiceLocator configs with TestDependencies config in Tests

replace servicelocator usage in analytics service

replace remaining ServiceLocator calls in unit tests

that depended on the UnitTestAppCoordinator configuration with per suite dependencies instances

fix swiftlint warning

remove remaining references to ServiceLocator

make dependencies properties consistent in tests

delete Dependencies and DependenciesProtocol

* AppSettings is instance level mockable now

* replace mocked and preview usages of reset + AppSettings global init with

AppSettings(store: UserDefaultsMock())

* AppSettings.UserDefaultsKeys is now a RawRep struct, making it extensible

* update tests with new AppSettings so it builds

* update remaining AppSettings test/mock inits

* AppSettings.UserDefaultsKey conformed to ExpressibleByStringLiteral

* addressed issues found in first pass ai review

* update AppSettings instantiations after rebase

* fix accessibility tests coordinator after rebase

* Rename to VolatileUserDefaults

* AppSettings init uses mandatory parameter

* Revert UserDefaultsKey to enum

* Add TrackedUserDefaults to be able to reset the defaults object

* Added volatile static func to AppSettings

* Use .volatile() static func instead of full init

* review fixups

* Resolve build issues after rebase

* updated enterprise module

* remove the static reset settings call from the preview tests

---------

Co-authored-by: Mauro Romito <mauro.romito@element.io>
2026-05-22 15:14:08 +02:00

174 lines
6.1 KiB
Swift

//
// Copyright 2025 Element Creations Ltd.
// Copyright 2022-2025 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.
//
@testable import ElementX
import MatrixRustSDK
import Testing
@MainActor
struct RoomDetailsEditScreenViewModelTests {
var viewModel: RoomDetailsEditScreenViewModel!
var userIndicatorController: UserIndicatorControllerMock!
var context: RoomDetailsEditScreenViewModelType.Context {
viewModel.context
}
@Test
mutating func cannotSaveOnLanding() {
setupViewModel(roomProxyConfiguration: .init(name: "Some room", members: [.mockMeAdmin]))
#expect(!context.viewState.canSave)
}
@Test
mutating func canEdit() async throws {
setupViewModel(roomProxyConfiguration: .init(name: "Some room", members: [.mockMeAdmin]))
let deferred = deferFulfillment(context.$viewState) { $0.canEditName }
try await deferred.fulfill()
#expect(context.viewState.canEditAvatar)
#expect(context.viewState.canEditName)
#expect(context.viewState.canEditTopic)
}
@Test
mutating func cannotEdit() {
setupViewModel(roomProxyConfiguration: .init(name: "Some room", members: [.mockMe]))
#expect(!context.viewState.canEditAvatar)
#expect(!context.viewState.canEditName)
#expect(!context.viewState.canEditTopic)
}
@Test
mutating func nameDidChange() {
setupViewModel(roomProxyConfiguration: .init(name: "Some room", members: [.mockMeAdmin]))
context.name = "name"
#expect(context.viewState.nameDidChange)
#expect(context.viewState.canSave)
}
@Test
mutating func topicDidChange() {
setupViewModel(roomProxyConfiguration: .init(name: "Some room", members: [.mockMeAdmin]))
context.topic = "topic"
#expect(context.viewState.topicDidChange)
#expect(context.viewState.canSave)
}
@Test
mutating func avatarDidChange() {
setupViewModel(roomProxyConfiguration: .init(name: "Some room", avatarURL: .mockMXCAvatar, members: [.mockMeAdmin]))
context.send(viewAction: .removeImage)
#expect(context.viewState.avatarDidChange)
#expect(context.viewState.canSave)
}
@Test
mutating func emptyNameCannotBeSaved() {
setupViewModel(roomProxyConfiguration: .init(name: "Some room", members: [.mockMeAdmin]))
context.name = ""
#expect(!context.viewState.canSave)
}
@Test
mutating func avatarPickerShowsSheet() {
setupViewModel(roomProxyConfiguration: .init(name: "Some room", members: [.mockMeAdmin]))
context.name = "name"
#expect(!context.showMediaSheet)
context.send(viewAction: .presentMediaSource)
#expect(context.showMediaSheet)
}
@Test
mutating func saveTriggersViewModelAction() async throws {
setupViewModel(roomProxyConfiguration: .init(name: "Some room", members: [.mockMeAdmin]))
let deferred = deferFulfillment(viewModel.actions) { action in
action == .saveFinished
}
context.name = "name"
context.send(viewAction: .save)
let action = try await deferred.fulfill()
#expect(action == .saveFinished)
}
@Test
mutating func cancelWithoutChanges() async throws {
setupViewModel(roomProxyConfiguration: .init(name: "Some room", members: [.mockMeAdmin]))
#expect(!context.viewState.canSave)
#expect(context.alertInfo == nil)
let deferred = deferFulfillment(viewModel.actions) { $0 == .cancel }
context.send(viewAction: .cancel)
try await deferred.fulfill()
#expect(context.alertInfo == nil)
}
@Test
mutating func cancelWithChangesAndDiscard() async throws {
setupViewModel(roomProxyConfiguration: .init(name: "Some room", members: [.mockMeAdmin]))
context.name = "name"
#expect(context.viewState.canSave)
#expect(context.alertInfo == nil)
context.send(viewAction: .cancel)
#expect(context.alertInfo != nil)
let deferred = deferFulfillment(viewModel.actions) { $0 == .cancel }
context.alertInfo?.secondaryButton?.action?() // Discard
try await deferred.fulfill()
}
@Test
mutating func cancelWithChangesAndSave() async throws {
setupViewModel(roomProxyConfiguration: .init(name: "Some room", members: [.mockMeAdmin]))
context.name = "name"
#expect(context.viewState.canSave)
#expect(context.alertInfo == nil)
context.send(viewAction: .cancel)
#expect(context.alertInfo != nil)
let deferred = deferFulfillment(viewModel.actions) { $0 == .saveFinished }
context.alertInfo?.primaryButton.action?() // Save
try await deferred.fulfill()
}
@Test
mutating func errorShownOnFailedFetchOfMedia() async {
setupViewModel(roomProxyConfiguration: .init(name: "Some room", members: [.mockMeAdmin]))
viewModel.didSelectMediaUrl(url: .picturesDirectory)
try? await Task.sleep(for: .milliseconds(100))
#expect(context.alertInfo != nil)
}
@Test
mutating func deleteAvatar() {
setupViewModel(roomProxyConfiguration: .init(name: "Some room", avatarURL: .mockMXCAvatar, members: [.mockMeAdmin]))
#expect(context.viewState.avatarURL != nil)
context.send(viewAction: .removeImage)
#expect(context.viewState.avatarURL == nil)
}
// MARK: - Private
private mutating func setupViewModel(roomProxyConfiguration: JoinedRoomProxyMockConfiguration) {
userIndicatorController = UserIndicatorControllerMock.default
viewModel = .init(roomProxy: JoinedRoomProxyMock(roomProxyConfiguration),
userSession: UserSessionMock(.init()),
mediaUploadingPreprocessor: MediaUploadingPreprocessor(appSettings: .volatile()),
userIndicatorController: userIndicatorController)
}
}