mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-03 06:55:59 +00:00
Add the same unsaved changes alerts that Android has. (#4803)
* Add an alert to Discard or Save when there are unsaved changes on the RoomDetailsEditScreen. * Add an alert to Discard or Save when there are unsaved changes on the UserDetailsEditScreen. * Add an alert to Discard or Save when there are unsaved changes on the SecurityAndPrivacyScreen. * Update strings.
This commit is contained in:
@@ -71,7 +71,7 @@ class RoomDetailsEditScreenViewModelTests: XCTestCase {
|
||||
XCTAssertFalse(context.viewState.canSave)
|
||||
}
|
||||
|
||||
func testSaveShowsSheet() {
|
||||
func testAvatarPickerShowsSheet() {
|
||||
setupViewModel(roomProxyConfiguration: .init(name: "Some room", members: [.mockMeAdmin]))
|
||||
context.name = "name"
|
||||
XCTAssertFalse(context.showMediaSheet)
|
||||
@@ -93,6 +93,47 @@ class RoomDetailsEditScreenViewModelTests: XCTestCase {
|
||||
XCTAssertEqual(action, .saveFinished)
|
||||
}
|
||||
|
||||
func testCancelWithoutChanges() async throws {
|
||||
setupViewModel(roomProxyConfiguration: .init(name: "Some room", members: [.mockMeAdmin]))
|
||||
XCTAssertFalse(context.viewState.canSave)
|
||||
XCTAssertNil(context.alertInfo)
|
||||
|
||||
var deferred = deferFulfillment(viewModel.actions) { $0 == .cancel }
|
||||
context.send(viewAction: .cancel)
|
||||
try await deferred.fulfill()
|
||||
XCTAssertNil(context.alertInfo)
|
||||
}
|
||||
|
||||
func testCancelWithChangesAndDiscard() async throws {
|
||||
setupViewModel(roomProxyConfiguration: .init(name: "Some room", members: [.mockMeAdmin]))
|
||||
context.name = "name"
|
||||
XCTAssertTrue(context.viewState.canSave)
|
||||
XCTAssertNil(context.alertInfo)
|
||||
|
||||
context.send(viewAction: .cancel)
|
||||
|
||||
XCTAssertNotNil(context.alertInfo)
|
||||
|
||||
let deferred = deferFulfillment(viewModel.actions) { $0 == .cancel }
|
||||
context.alertInfo?.secondaryButton?.action?() // Discard
|
||||
try await deferred.fulfill()
|
||||
}
|
||||
|
||||
func testCancelWithChangesAndSave() async throws {
|
||||
setupViewModel(roomProxyConfiguration: .init(name: "Some room", members: [.mockMeAdmin]))
|
||||
context.name = "name"
|
||||
XCTAssertTrue(context.viewState.canSave)
|
||||
XCTAssertNil(context.alertInfo)
|
||||
|
||||
context.send(viewAction: .cancel)
|
||||
|
||||
XCTAssertNotNil(context.alertInfo)
|
||||
|
||||
let deferred = deferFulfillment(viewModel.actions) { $0 == .saveFinished }
|
||||
context.alertInfo?.primaryButton.action?() // Save
|
||||
try await deferred.fulfill()
|
||||
}
|
||||
|
||||
func testErrorShownOnFailedFetchOfMedia() async throws {
|
||||
setupViewModel(roomProxyConfiguration: .init(name: "Some room", members: [.mockMeAdmin]))
|
||||
viewModel.didSelectMediaUrl(url: .picturesDirectory)
|
||||
|
||||
@@ -88,6 +88,67 @@ class SecurityAndPrivacyScreenViewModelTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func testSave() async throws {
|
||||
setupViewModel(isSpaceSettingsEnabled: false, joinedParentSpaces: [], joinRule: .public)
|
||||
|
||||
// Saving shouldn't dismiss this screen (or trigger any other action).
|
||||
let deferred = deferFailure(viewModel.actionsPublisher, timeout: 1) { _ in true }
|
||||
|
||||
context.desiredSettings.accessType = .inviteOnly
|
||||
context.send(viewAction: .save)
|
||||
|
||||
try await deferred.fulfill()
|
||||
}
|
||||
|
||||
func testCancelWithChangesAndDiscard() async throws {
|
||||
setupViewModel(isSpaceSettingsEnabled: false, joinedParentSpaces: [], joinRule: .public)
|
||||
context.desiredSettings.accessType = .inviteOnly
|
||||
XCTAssertFalse(context.viewState.isSaveDisabled)
|
||||
XCTAssertNil(context.alertInfo)
|
||||
|
||||
context.send(viewAction: .cancel)
|
||||
|
||||
XCTAssertNotNil(context.alertInfo)
|
||||
|
||||
let deferred = deferFulfillment(viewModel.actionsPublisher) { $0 == .dismiss }
|
||||
context.alertInfo?.secondaryButton?.action?() // Discard
|
||||
try await deferred.fulfill()
|
||||
}
|
||||
|
||||
func testCancelWithChangesAndSave() async throws {
|
||||
setupViewModel(isSpaceSettingsEnabled: false, joinedParentSpaces: [], joinRule: .public)
|
||||
context.desiredSettings.accessType = .inviteOnly
|
||||
XCTAssertFalse(context.viewState.isSaveDisabled)
|
||||
XCTAssertNil(context.alertInfo)
|
||||
|
||||
context.send(viewAction: .cancel)
|
||||
|
||||
XCTAssertNotNil(context.alertInfo)
|
||||
|
||||
let deferred = deferFulfillment(viewModel.actionsPublisher) { $0 == .dismiss }
|
||||
context.alertInfo?.primaryButton.action?() // Save
|
||||
try await deferred.fulfill()
|
||||
}
|
||||
|
||||
func testCancelWithChangesAndSaveWithFailure() async throws {
|
||||
setupViewModel(isSpaceSettingsEnabled: false, joinedParentSpaces: [], joinRule: .public)
|
||||
roomProxy.updateJoinRuleReturnValue = .failure(.sdkError(RoomProxyMockError.generic))
|
||||
context.desiredSettings.accessType = .inviteOnly
|
||||
XCTAssertFalse(context.viewState.isSaveDisabled)
|
||||
XCTAssertNil(context.alertInfo)
|
||||
|
||||
context.send(viewAction: .cancel)
|
||||
|
||||
XCTAssertNotNil(context.alertInfo)
|
||||
|
||||
// The screen should not be dismissed if a failure occurred.
|
||||
let deferred = deferFailure(viewModel.actionsPublisher, timeout: 1) { _ in true }
|
||||
context.alertInfo?.primaryButton.action?() // Save
|
||||
try await deferred.fulfill()
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
private func setupViewModel(isSpaceSettingsEnabled: Bool,
|
||||
joinedParentSpaces: [SpaceRoomProxyProtocol],
|
||||
joinRule: JoinRule) {
|
||||
@@ -98,6 +159,7 @@ class SecurityAndPrivacyScreenViewModelTests: XCTestCase {
|
||||
members: .allMembersAsCreator,
|
||||
joinRule: joinRule,
|
||||
isVisibleInPublicDirectory: true))
|
||||
roomProxy.updateJoinRuleReturnValue = .success(())
|
||||
|
||||
viewModel = SecurityAndPrivacyScreenViewModel(roomProxy: roomProxy,
|
||||
clientProxy: ClientProxyMock(.init(userIDServerName: "matrix.org",
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
//
|
||||
// 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
|
||||
import XCTest
|
||||
|
||||
@testable import ElementX
|
||||
|
||||
@MainActor
|
||||
class UserDetailsEditScreenViewModelTests: XCTestCase {
|
||||
var viewModel: UserDetailsEditScreenViewModel!
|
||||
|
||||
var userIndicatorController: UserIndicatorControllerMock!
|
||||
|
||||
var context: UserDetailsEditScreenViewModelType.Context {
|
||||
viewModel.context
|
||||
}
|
||||
|
||||
func testCannotSaveOnLanding() {
|
||||
setupViewModel()
|
||||
XCTAssertFalse(context.viewState.canSave)
|
||||
}
|
||||
|
||||
func testNameDidChange() {
|
||||
setupViewModel()
|
||||
context.name = "name"
|
||||
XCTAssertTrue(context.viewState.nameDidChange)
|
||||
XCTAssertTrue(context.viewState.canSave)
|
||||
}
|
||||
|
||||
func testEmptyNameCannotBeSaved() {
|
||||
setupViewModel()
|
||||
context.name = ""
|
||||
XCTAssertFalse(context.viewState.canSave)
|
||||
}
|
||||
|
||||
func testAvatarPickerShowsSheet() {
|
||||
setupViewModel()
|
||||
context.name = "name"
|
||||
XCTAssertFalse(context.showMediaSheet)
|
||||
context.send(viewAction: .presentMediaSource)
|
||||
XCTAssertTrue(context.showMediaSheet)
|
||||
}
|
||||
|
||||
func testSave() async throws {
|
||||
setupViewModel()
|
||||
|
||||
// Saving shouldn't dismiss this screen (or trigger any other action).
|
||||
let deferred = deferFailure(viewModel.actions, timeout: 1) { _ in true }
|
||||
|
||||
context.name = "name"
|
||||
context.send(viewAction: .save)
|
||||
|
||||
try await deferred.fulfill()
|
||||
}
|
||||
|
||||
func testCancelWithChangesAndDiscard() async throws {
|
||||
setupViewModel()
|
||||
context.name = "name"
|
||||
XCTAssertTrue(context.viewState.canSave)
|
||||
XCTAssertNil(context.alertInfo)
|
||||
|
||||
context.send(viewAction: .cancel)
|
||||
|
||||
XCTAssertNotNil(context.alertInfo)
|
||||
|
||||
let deferred = deferFulfillment(viewModel.actions) { $0 == .dismiss }
|
||||
context.alertInfo?.secondaryButton?.action?() // Discard
|
||||
try await deferred.fulfill()
|
||||
}
|
||||
|
||||
func testCancelWithChangesAndSave() async throws {
|
||||
setupViewModel()
|
||||
context.name = "name"
|
||||
XCTAssertTrue(context.viewState.canSave)
|
||||
XCTAssertNil(context.alertInfo)
|
||||
|
||||
context.send(viewAction: .cancel)
|
||||
|
||||
XCTAssertNotNil(context.alertInfo)
|
||||
|
||||
let deferred = deferFulfillment(viewModel.actions) { $0 == .dismiss }
|
||||
context.alertInfo?.primaryButton.action?() // Save
|
||||
try await deferred.fulfill()
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
private func setupViewModel() {
|
||||
userIndicatorController = UserIndicatorControllerMock.default
|
||||
|
||||
viewModel = .init(userSession: UserSessionMock(.init()),
|
||||
mediaUploadingPreprocessor: MediaUploadingPreprocessor(appSettings: ServiceLocator.shared.settings),
|
||||
userIndicatorController: userIndicatorController)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user