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>
This commit is contained in:
Michael
2026-05-22 08:14:08 -05:00
committed by GitHub
parent 97d4a35af7
commit 22d14ff8e4
122 changed files with 572 additions and 747 deletions
@@ -229,40 +229,30 @@ struct SecurityAndPrivacyScreen: View {
// MARK: - Previews
struct SecurityAndPrivacyScreen_Previews: PreviewProvider, TestablePreview {
static let inviteOnlyViewModel = {
AppSettings.resetAllSettings()
return SecurityAndPrivacyScreenViewModel(roomProxy: JoinedRoomProxyMock(.init(members: .allMembersAsCreator,
joinRule: .invite)),
clientProxy: ClientProxyMock(.init()),
userIndicatorController: UserIndicatorControllerMock(),
appSettings: AppSettings())
}()
static let inviteOnlyViewModel = SecurityAndPrivacyScreenViewModel(roomProxy: JoinedRoomProxyMock(.init(members: .allMembersAsCreator,
joinRule: .invite)),
clientProxy: ClientProxyMock(.init()),
userIndicatorController: UserIndicatorControllerMock(),
appSettings: .volatile())
static let publicViewModel = {
AppSettings.resetAllSettings()
return SecurityAndPrivacyScreenViewModel(roomProxy: JoinedRoomProxyMock(.init(isEncrypted: false,
canonicalAlias: "#room:matrix.org",
members: .allMembersAsCreator,
joinRule: .public,
isVisibleInPublicDirectory: true)),
clientProxy: ClientProxyMock(.init(userIDServerName: "matrix.org")),
userIndicatorController: UserIndicatorControllerMock(),
appSettings: AppSettings())
}()
static let publicViewModel = SecurityAndPrivacyScreenViewModel(roomProxy: JoinedRoomProxyMock(.init(isEncrypted: false,
canonicalAlias: "#room:matrix.org",
members: .allMembersAsCreator,
joinRule: .public,
isVisibleInPublicDirectory: true)),
clientProxy: ClientProxyMock(.init(userIDServerName: "matrix.org")),
userIndicatorController: UserIndicatorControllerMock(),
appSettings: .volatile())
static let publicNoAddressViewModel = {
AppSettings.resetAllSettings()
return SecurityAndPrivacyScreenViewModel(roomProxy: JoinedRoomProxyMock(.init(isEncrypted: false,
members: .allMembersAsCreator,
joinRule: .public)),
clientProxy: ClientProxyMock(.init(userIDServerName: "matrix.org")),
userIndicatorController: UserIndicatorControllerMock(),
appSettings: AppSettings())
}()
static let publicNoAddressViewModel = SecurityAndPrivacyScreenViewModel(roomProxy: JoinedRoomProxyMock(.init(isEncrypted: false,
members: .allMembersAsCreator,
joinRule: .public)),
clientProxy: ClientProxyMock(.init(userIDServerName: "matrix.org")),
userIndicatorController: UserIndicatorControllerMock(),
appSettings: .volatile())
static let singleSpaceMembersViewModel = {
AppSettings.resetAllSettings()
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
let space = [SpaceServiceRoom].mockSingleRoom[0]
@@ -278,8 +268,7 @@ struct SecurityAndPrivacyScreen_Previews: PreviewProvider, TestablePreview {
}()
static let multipleSpacesMembersViewModel = {
AppSettings.resetAllSettings()
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
let spaces = [SpaceServiceRoom].mockJoinedSpaces
@@ -295,8 +284,7 @@ struct SecurityAndPrivacyScreen_Previews: PreviewProvider, TestablePreview {
}()
static let askToJoinViewModel = {
AppSettings.resetAllSettings()
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
appSettings.knockingEnabled = true
return SecurityAndPrivacyScreenViewModel(roomProxy: JoinedRoomProxyMock(.init(isEncrypted: false,
@@ -310,8 +298,7 @@ struct SecurityAndPrivacyScreen_Previews: PreviewProvider, TestablePreview {
}()
static let singleAskToJoinSpaceMembersViewModel = {
AppSettings.resetAllSettings()
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
appSettings.knockingEnabled = true
let space = [SpaceServiceRoom].mockSingleRoom[0]
@@ -328,8 +315,7 @@ struct SecurityAndPrivacyScreen_Previews: PreviewProvider, TestablePreview {
}()
static let multipleAskToJoinSpacesMembersViewModel = {
AppSettings.resetAllSettings()
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
appSettings.knockingEnabled = true
let spaces = [SpaceServiceRoom].mockJoinedSpaces
@@ -345,18 +331,15 @@ struct SecurityAndPrivacyScreen_Previews: PreviewProvider, TestablePreview {
appSettings: appSettings)
}()
static let publicSpaceViewModel = {
AppSettings.resetAllSettings()
return SecurityAndPrivacyScreenViewModel(roomProxy: JoinedRoomProxyMock(.init(isSpace: true,
isEncrypted: false,
canonicalAlias: "#space:matrix.org",
members: .allMembersAsCreator,
joinRule: .public,
isVisibleInPublicDirectory: true)),
clientProxy: ClientProxyMock(.init(userIDServerName: "matrix.org")),
userIndicatorController: UserIndicatorControllerMock(),
appSettings: AppSettings())
}()
static let publicSpaceViewModel = SecurityAndPrivacyScreenViewModel(roomProxy: JoinedRoomProxyMock(.init(isSpace: true,
isEncrypted: false,
canonicalAlias: "#space:matrix.org",
members: .allMembersAsCreator,
joinRule: .public,
isVisibleInPublicDirectory: true)),
clientProxy: ClientProxyMock(.init(userIDServerName: "matrix.org")),
userIndicatorController: UserIndicatorControllerMock(),
appSettings: .volatile())
static var previews: some View {
ElementNavigationStack {