Files
element-x-ios/UnitTests/Sources/InviteUsersViewModelTests.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

244 lines
10 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.
//
import Combine
@testable import ElementX
import Testing
@MainActor
final class InviteUsersScreenViewModelTests {
var viewModel: InviteUsersScreenViewModelProtocol!
var userDiscoveryService: UserDiscoveryServiceMock!
var clientProxy: ClientProxyMock!
var context: InviteUsersScreenViewModel.Context {
viewModel.context
}
@Test
func selectUser() {
let roomProxy = JoinedRoomProxyMock(.init(name: "newroom", members: []))
roomProxy.inviteUserIDReturnValue = .success(())
setupViewModel(roomProxy: roomProxy, isSkippable: true)
#expect(context.viewState.selectedUsers.isEmpty)
context.send(viewAction: .toggleUser(.mockAlice))
#expect(context.viewState.selectedUsers.count == 1)
#expect(context.viewState.selectedUsers.first?.userID == UserProfileProxy.mockAlice.userID)
}
@Test
func reselectUser() {
let roomProxy = JoinedRoomProxyMock(.init(name: "newroom", members: []))
roomProxy.inviteUserIDReturnValue = .success(())
setupViewModel(roomProxy: roomProxy, isSkippable: true)
#expect(context.viewState.selectedUsers.isEmpty)
context.send(viewAction: .toggleUser(.mockAlice))
#expect(context.viewState.selectedUsers.count == 1)
#expect(context.viewState.selectedUsers.first?.userID == UserProfileProxy.mockAlice.userID)
context.send(viewAction: .toggleUser(.mockAlice))
#expect(context.viewState.selectedUsers.isEmpty)
}
@Test
func deselectUser() {
let roomProxy = JoinedRoomProxyMock(.init(name: "newroom", members: []))
roomProxy.inviteUserIDReturnValue = .success(())
setupViewModel(roomProxy: roomProxy, isSkippable: true)
#expect(context.viewState.selectedUsers.isEmpty)
context.send(viewAction: .toggleUser(.mockAlice))
#expect(context.viewState.selectedUsers.count == 1)
#expect(context.viewState.selectedUsers.first?.userID == UserProfileProxy.mockAlice.userID)
context.send(viewAction: .toggleUser(.mockAlice))
#expect(context.viewState.selectedUsers.isEmpty)
}
@Test
func inviteButton() async throws {
let mockedMembers: [RoomMemberProxyMock] = [.mockAlice, .mockBob]
let roomProxy = JoinedRoomProxyMock(.init(name: "test", members: mockedMembers))
roomProxy.inviteUserIDReturnValue = .success(())
setupViewModel(roomProxy: roomProxy, isSkippable: false)
let deferredState = deferFulfillment(viewModel.context.$viewState) { state in
state.isUserSelected(.mockAlice)
}
context.send(viewAction: .toggleUser(.mockAlice))
try await deferredState.fulfill()
let deferredAction = deferFulfillment(viewModel.actions) { action in
switch action {
case .dismiss:
return true
case .openRoom:
return false
}
}
context.send(viewAction: .proceed)
try await deferredAction.fulfill()
#expect(roomProxy.inviteUserIDReceivedInvocations == [RoomMemberProxyMock.mockAlice.userID])
}
// MARK: - History Sharing
@Test
func invitingUnknownUsersOpensConfirmationDialog() async throws {
let mockedMembers: [RoomMemberProxyMock] = [.mockAlice, .mockBob]
let roomProxy = JoinedRoomProxyMock(.init(name: "test", members: mockedMembers))
roomProxy.inviteUserIDReturnValue = .success(())
setupViewModel(roomProxy: roomProxy, isSkippable: false)
// Mock the lack of cached user identity
clientProxy.userIdentityForFallBackToServerReturnValue = .success(nil)
let deferredState = deferFulfillment(viewModel.context.$viewState) { state in
state.isUserSelected(.mockAlice) && state.usersToConfirm.contains(.mockAlice)
}
context.send(viewAction: .toggleUser(.mockAlice))
try await deferredState.fulfill()
context.send(viewAction: .proceed)
#expect(context.presentConfirmationDialog)
let deferredAction = deferFulfillment(viewModel.actions) { action in
switch action {
case .dismiss:
return true
case .openRoom:
return false
}
}
context.send(viewAction: .confirmUnknownUsers)
try await deferredAction.fulfill()
#expect(roomProxy.inviteUserIDReceivedInvocations == [RoomMemberProxyMock.mockAlice.userID])
}
@Test
func removeButtonRemovesUnknownUsers() async throws {
let mockedMembers: [RoomMemberProxyMock] = [.mockAlice, .mockBob]
let roomProxy = JoinedRoomProxyMock(.init(name: "test", members: mockedMembers))
roomProxy.inviteUserIDReturnValue = .success(())
setupViewModel(roomProxy: roomProxy, isSkippable: false)
// Mock the lack of cached user identity
clientProxy.userIdentityForFallBackToServerReturnValue = .success(nil)
var deferredState = deferFulfillment(viewModel.context.$viewState) { state in
state.isUserSelected(.mockAlice) && state.usersToConfirm.contains(.mockAlice)
}
context.send(viewAction: .toggleUser(.mockAlice))
try await deferredState.fulfill()
context.send(viewAction: .proceed)
#expect(context.presentConfirmationDialog)
deferredState = deferFulfillment(viewModel.context.$viewState) { state in
!state.usersToConfirm.contains(.mockAlice) && !state.selectedUsers.contains(.mockAlice)
}
context.send(viewAction: .removeUnknownUsers)
try await deferredState.fulfill()
}
// MARK: - Draft (new room)
@Test
func createsNewRoomInDraftMode() async throws {
userDiscoveryService = UserDiscoveryServiceMock()
userDiscoveryService.searchProfilesWithReturnValue = .success([])
clientProxy = ClientProxyMock(.init(userID: "@mock:client.com"))
let newRoomID = "!newroom:example.com"
clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartReturnValue = .success(newRoomID)
viewModel = InviteUsersScreenViewModel(userSession: UserSessionMock(.init(clientProxy: clientProxy)),
roomType: .draft(mandatoryInvitees: [.mockAlice]),
isSkippable: false,
userDiscoveryService: userDiscoveryService,
userIndicatorController: UserIndicatorControllerMock(),
appSettings: .volatile())
// The locked invitee starts pre-selected and locked.
#expect(context.viewState.selectedUsers.map(\.userID) == [UserProfileProxy.mockAlice.userID])
#expect(context.viewState.isInviteeMandatory(.mockAlice))
// The proceed button is disabled while the only selected user is the locked invitee.
#expect(!context.viewState.hasInvitableSelectedUsers)
// Selecting a non-locked user enables the proceed button.
var deferredState = deferFulfillment(viewModel.context.$viewState) { state in
state.isUserSelected(.mockBob)
}
context.send(viewAction: .toggleUser(.mockBob))
try await deferredState.fulfill()
#expect(context.viewState.hasInvitableSelectedUsers)
// Deselecting the non-locked user disables the proceed button again.
deferredState = deferFulfillment(viewModel.context.$viewState) { state in
!state.isUserSelected(.mockBob)
}
context.send(viewAction: .toggleUser(.mockBob))
try await deferredState.fulfill()
#expect(!context.viewState.hasInvitableSelectedUsers)
// Re-select the non-locked user before proceeding.
deferredState = deferFulfillment(viewModel.context.$viewState) { state in
state.isUserSelected(.mockBob)
}
context.send(viewAction: .toggleUser(.mockBob))
try await deferredState.fulfill()
let deferredAction = deferFulfillment(viewModel.actions) { action in
if case .openRoom(let roomID) = action, roomID == newRoomID {
return true
}
return false
}
context.send(viewAction: .proceed)
try await deferredAction.fulfill()
let args = try #require(clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartReceivedArguments)
#expect(args.name == nil)
#expect(args.topic == nil)
#expect(args.accessType == .private)
#expect(args.isSpace == false)
#expect(args.userIDs == [UserProfileProxy.mockAlice.userID, UserProfileProxy.mockBob.userID])
#expect(args.avatarURL == nil)
#expect(args.aliasLocalPart == nil)
}
// MARK: - Helpers
private func setupViewModel(roomProxy: JoinedRoomProxyProtocol, isSkippable: Bool) {
userDiscoveryService = UserDiscoveryServiceMock()
userDiscoveryService.searchProfilesWithReturnValue = .success([])
clientProxy = ClientProxyMock(.init(userID: "@mock:client.com"))
let viewModel = InviteUsersScreenViewModel(userSession: UserSessionMock(.init(clientProxy: clientProxy)),
roomType: .existingRoom(roomProxy: roomProxy),
isSkippable: isSkippable,
userDiscoveryService: userDiscoveryService,
userIndicatorController: UserIndicatorControllerMock(),
appSettings: .volatile())
viewModel.state.usersSection = .init(type: .suggestions, users: [.mockAlice, .mockBob, .mockCharlie])
self.viewModel = viewModel
}
}