mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-05 11:58:32 +00:00
Add tests for linking a new device. (#4934)
* Replace GrantLoginWithQrCodeHandlerSDKMock with LinkNewDeviceServiceMock. Add tests for all initial states on the QRCodeLoginScreen. * Add tests for linking both mobile and desktop devices. * Add UI tests for linking a new device. * Don't show the Link Desktop Computer button when running on macOS. This mirrors the decision to hide the Sign In With QR Code button on the start screen.
This commit is contained in:
@@ -16,6 +16,8 @@ enum LinkNewDeviceScreenViewModelAction {
|
||||
struct LinkNewDeviceScreenViewState: BindableState {
|
||||
enum Mode: Equatable { case loading, readyToLink(isGeneratingCode: Bool), notSupported }
|
||||
var mode: Mode = .loading
|
||||
|
||||
let showLinkDesktopComputerButton: Bool
|
||||
}
|
||||
|
||||
enum LinkNewDeviceScreenViewAction {
|
||||
|
||||
@@ -22,7 +22,9 @@ class LinkNewDeviceScreenViewModel: LinkNewDeviceScreenViewModelType, LinkNewDev
|
||||
init(clientProxy: ClientProxyProtocol) {
|
||||
self.clientProxy = clientProxy
|
||||
|
||||
super.init(initialViewState: LinkNewDeviceScreenViewState())
|
||||
let isQRCodeScanningSupported = !ProcessInfo.processInfo.isiOSAppOnMac
|
||||
|
||||
super.init(initialViewState: LinkNewDeviceScreenViewState(showLinkDesktopComputerButton: isQRCodeScanningSupported))
|
||||
|
||||
Task { await checkQRCodeLoginSupport() }
|
||||
}
|
||||
|
||||
@@ -70,11 +70,13 @@ struct LinkNewDeviceScreen: View {
|
||||
.buttonStyle(.compound(.primary))
|
||||
.accessibilityIdentifier(A11yIdentifiers.linkNewDeviceScreen.mobileDevice)
|
||||
|
||||
Button { context.send(viewAction: .linkDesktopComputer) } label: {
|
||||
Label(L10n.screenLinkNewDeviceRootDesktopComputer, icon: \.computer)
|
||||
if context.viewState.showLinkDesktopComputerButton {
|
||||
Button { context.send(viewAction: .linkDesktopComputer) } label: {
|
||||
Label(L10n.screenLinkNewDeviceRootDesktopComputer, icon: \.computer)
|
||||
}
|
||||
.buttonStyle(.compound(.primary))
|
||||
.accessibilityIdentifier(A11yIdentifiers.linkNewDeviceScreen.desktopComputer)
|
||||
}
|
||||
.buttonStyle(.compound(.primary))
|
||||
.accessibilityIdentifier(A11yIdentifiers.linkNewDeviceScreen.desktopComputer)
|
||||
}
|
||||
.disabled(isGeneratingCode)
|
||||
case .notSupported:
|
||||
@@ -97,8 +99,6 @@ struct LinkNewDeviceScreen: View {
|
||||
|
||||
// MARK: - Previews
|
||||
|
||||
import MatrixRustSDKMocks
|
||||
|
||||
struct LinkNewDeviceScreen_Previews: PreviewProvider, TestablePreview {
|
||||
static let viewModel = makeViewModel(mode: .readyToLink(isGeneratingCode: false))
|
||||
static let generatingViewModel = makeViewModel(mode: .readyToLink(isGeneratingCode: true))
|
||||
@@ -143,7 +143,6 @@ struct LinkNewDeviceScreen_Previews: PreviewProvider, TestablePreview {
|
||||
return false
|
||||
}
|
||||
}
|
||||
clientProxy.linkNewDeviceServiceReturnValue = .init(handler: GrantLoginWithQrCodeHandlerSDKMock(.init(generateDelay: .seconds(20))))
|
||||
|
||||
let viewModel = LinkNewDeviceScreenViewModel(clientProxy: clientProxy)
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ enum QRCodeLoginScreenMode {
|
||||
/// Configures the screen to login this device by scanning a QR code.
|
||||
case login(QRCodeLoginServiceProtocol)
|
||||
/// Configures the screen to link another device by scanning a QR code.
|
||||
case linkDesktop(LinkNewDeviceService)
|
||||
case linkDesktop(LinkNewDeviceServiceProtocol)
|
||||
/// Configures the screen to link another device by showing it a QR code.
|
||||
case linkMobile(LinkNewDeviceService.LinkMobileProgressPublisher)
|
||||
}
|
||||
@@ -180,6 +180,13 @@ enum QRCodeLoginState: Equatable {
|
||||
}
|
||||
}
|
||||
|
||||
var isDisplayQR: Bool {
|
||||
switch self {
|
||||
case .displayQR: true
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
var isError: Bool {
|
||||
switch self {
|
||||
case .error, .scan(.scanFailed): true
|
||||
|
||||
@@ -153,7 +153,7 @@ class QRCodeLoginScreenViewModel: QRCodeLoginScreenViewModelType, QRCodeLoginScr
|
||||
|
||||
// TODO: when user cancels in UI then the underlying login needs to be cancelled too. It's unclear if we have that exposed in the bindings yet.
|
||||
|
||||
private func handleScan(qrData: Data, linkService: LinkNewDeviceService) {
|
||||
private func handleScan(qrData: Data, linkService: LinkNewDeviceServiceProtocol) {
|
||||
guard currentTask == nil else { return }
|
||||
|
||||
state.state = .scan(.connecting)
|
||||
|
||||
@@ -337,11 +337,7 @@ struct QRCodeLoginScreen_Previews: PreviewProvider, TestablePreview {
|
||||
static let deviceNotSignedInStateViewModel = QRCodeLoginScreenViewModel.mock(state: .scan(.scanFailed(.deviceNotSignedIn)))
|
||||
|
||||
// Showing
|
||||
static let showingStateViewModel = {
|
||||
let base64QRCode = GrantLoginWithQrCodeHandlerSDKMock.Configuration().generatedBase64QRCode
|
||||
let image = base64QRCode.data(using: .utf8).flatMap { UIImage(qrCodeData: $0) } ?? UIImage()
|
||||
return QRCodeLoginScreenViewModel.mock(state: .displayQR(image))
|
||||
}()
|
||||
static let showingStateViewModel = QRCodeLoginScreenViewModel.mock(state: .displayQR(LinkNewDeviceServiceMock.mockQRCodeImage))
|
||||
|
||||
// Displaying codes
|
||||
static let deviceCodeStateViewModel = QRCodeLoginScreenViewModel.mock(state: .displayCode(.deviceCode("12")))
|
||||
|
||||
Reference in New Issue
Block a user