Add UnitTestsAppCoordinator.

This commit is contained in:
Doug
2023-04-25 17:12:56 +01:00
committed by Stefan Ceriu
parent d49112848a
commit 3eae3b8f86
7 changed files with 50 additions and 48 deletions

View File

@@ -19,13 +19,13 @@ import SwiftUI
@main
struct Application: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) private var applicationDelegate
private let appCoordinator: AppCoordinatorProtocol!
private let appCoordinator: AppCoordinatorProtocol
init() {
if Tests.isRunningUITests {
appCoordinator = UITestsAppCoordinator()
} else if Tests.isRunningUnitTests {
appCoordinator = nil
appCoordinator = UnitTestsAppCoordinator()
} else {
appCoordinator = AppCoordinator()
}
@@ -33,15 +33,11 @@ struct Application: App {
var body: some Scene {
WindowGroup {
if Tests.isRunningUnitTests {
EmptyView()
} else {
appCoordinator.toPresentable()
.task {
appCoordinator.start()
}
.statusBarHidden(shouldHideStatusBar)
}
appCoordinator.toPresentable()
.task {
appCoordinator.start()
}
.statusBarHidden(shouldHideStatusBar)
}
}

View File

@@ -1,5 +1,5 @@
//
// Copyright 2023 New Vector Ltd
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -14,14 +14,24 @@
// limitations under the License.
//
import XCTest
import SwiftUI
@testable import ElementX
extension XCTestCase {
func setupAppSettings() {
AppSettings.configureWithSuiteName("io.element.elementx.unitests")
class UnitTestsAppCoordinator: AppCoordinatorProtocol {
let notificationManager: NotificationManagerProtocol = NotificationManagerMock()
init() {
ServiceLocator.shared.register(userIndicatorController: MockUserIndicatorController())
AppSettings.configureWithSuiteName("io.element.elementx.unittests")
AppSettings.reset()
ServiceLocator.shared.register(appSettings: AppSettings())
ServiceLocator.shared.register(bugReportService: BugReportServiceMock())
ServiceLocator.shared.register(analytics: Analytics(client: AnalyticsClientMock()))
}
func start() { }
func toPresentable() -> AnyView {
AnyView(ProgressView("Running Unit Tests"))
}
}

View File

@@ -20,15 +20,12 @@ import XCTest
@MainActor
class AnalyticsSettingsScreenViewModelTests: XCTestCase {
private var applicationSettings: AppSettings!
private var appSettings: AppSettings { ServiceLocator.shared.settings }
private var viewModel: AnalyticsSettingsScreenViewModelProtocol!
private var context: AnalyticsSettingsScreenViewModelType.Context!
@MainActor override func setUpWithError() throws {
AppSettings.configureWithSuiteName("io.element.elementx.unitests")
AppSettings.reset()
applicationSettings = AppSettings()
ServiceLocator.shared.register(appSettings: applicationSettings)
let analyticsClient = AnalyticsClientMock()
analyticsClient.isRunning = false
ServiceLocator.shared.register(analytics: Analytics(client: analyticsClient))
@@ -42,13 +39,13 @@ class AnalyticsSettingsScreenViewModelTests: XCTestCase {
}
func testOptIn() {
applicationSettings.analyticsConsentState = .optedOut
appSettings.analyticsConsentState = .optedOut
context.send(viewAction: .toggleAnalytics)
XCTAssertTrue(context.enableAnalytics)
}
func testOptOut() {
applicationSettings.analyticsConsentState = .optedIn
appSettings.analyticsConsentState = .optedIn
context.send(viewAction: .toggleAnalytics)
XCTAssertFalse(context.enableAnalytics)
}

View File

@@ -19,15 +19,13 @@ import AnalyticsEvents
import XCTest
class AnalyticsTests: XCTestCase {
private var applicationSettings: AppSettings!
private var appSettings: AppSettings { ServiceLocator.shared.settings }
private var analyticsClient: AnalyticsClientMock!
private var bugReportService: BugReportServiceMock!
override func setUp() {
AppSettings.configureWithSuiteName("io.element.elementx.unitests")
AppSettings.reset()
applicationSettings = AppSettings()
ServiceLocator.shared.register(appSettings: applicationSettings)
bugReportService = BugReportServiceMock()
bugReportService.isRunning = false
ServiceLocator.shared.register(bugReportService: bugReportService)
@@ -47,7 +45,7 @@ class AnalyticsTests: XCTestCase {
func testAnalyticsPromptUserDeclinedPostHog() {
// Given an existing install of the app where the user previously declined PostHog
applicationSettings.analyticsConsentState = .optedOut
appSettings.analyticsConsentState = .optedOut
// When the user is prompted for analytics
let showPrompt = ServiceLocator.shared.analytics.shouldShowAnalyticsPrompt
@@ -58,7 +56,7 @@ class AnalyticsTests: XCTestCase {
func testAnalyticsPromptUserAcceptedPostHog() {
// Given an existing install of the app where the user previously accepted PostHog
applicationSettings.analyticsConsentState = .optedIn
appSettings.analyticsConsentState = .optedIn
// When the user is prompted for analytics
let showPrompt = ServiceLocator.shared.analytics.shouldShowAnalyticsPrompt
@@ -81,7 +79,7 @@ class AnalyticsTests: XCTestCase {
// When analytics is opt-out
ServiceLocator.shared.analytics.optOut()
// Then analytics should be disabled
XCTAssertEqual(applicationSettings.analyticsConsentState, .optedOut)
XCTAssertEqual(appSettings.analyticsConsentState, .optedOut)
XCTAssertFalse(ServiceLocator.shared.analytics.isEnabled)
XCTAssertFalse(ServiceLocator.shared.analytics.isRunning)
XCTAssertFalse(analyticsClient.isRunning)
@@ -96,7 +94,7 @@ class AnalyticsTests: XCTestCase {
// When analytics is opt-in
ServiceLocator.shared.analytics.optIn()
// The analytics should be enabled
XCTAssertEqual(applicationSettings.analyticsConsentState, .optedIn)
XCTAssertEqual(appSettings.analyticsConsentState, .optedIn)
XCTAssertTrue(ServiceLocator.shared.analytics.isEnabled)
// Analytics client and the bug report service should have been started
XCTAssertTrue(analyticsClient.startCalled)
@@ -105,7 +103,7 @@ class AnalyticsTests: XCTestCase {
func testAnalyticsStartIfNotEnabled() {
// Given an existing install of the app where the user previously declined the tracking
applicationSettings.analyticsConsentState = .optedOut
appSettings.analyticsConsentState = .optedOut
// Analytics should not start
XCTAssertFalse(ServiceLocator.shared.analytics.isEnabled)
ServiceLocator.shared.analytics.startIfEnabled()
@@ -115,7 +113,7 @@ class AnalyticsTests: XCTestCase {
func testAnalyticsStartIfEnabled() {
// Given an existing install of the app where the user previously accpeted the tracking
applicationSettings.analyticsConsentState = .optedIn
appSettings.analyticsConsentState = .optedIn
// Analytics should start
XCTAssertTrue(ServiceLocator.shared.analytics.isEnabled)
ServiceLocator.shared.analytics.startIfEnabled()
@@ -188,14 +186,14 @@ class AnalyticsTests: XCTestCase {
func testResetConsentState() {
// Given an existing install of the app where the user previously accpeted the tracking
applicationSettings.analyticsConsentState = .optedIn
appSettings.analyticsConsentState = .optedIn
XCTAssertFalse(ServiceLocator.shared.analytics.shouldShowAnalyticsPrompt)
// When forgetting analytics consents
ServiceLocator.shared.analytics.resetConsentState()
// Then the analytics prompt should be presented again
XCTAssertEqual(applicationSettings.analyticsConsentState, .unknown)
XCTAssertEqual(appSettings.analyticsConsentState, .unknown)
XCTAssertTrue(ServiceLocator.shared.analytics.shouldShowAnalyticsPrompt)
}
}

View File

@@ -62,7 +62,10 @@ class BugReportViewModelTests: XCTestCase {
func testSendReportWithSuccess() async throws {
let mockService = BugReportServiceMock()
mockService.submitBugReportProgressListenerReturnValue = SubmitBugReportResponse(reportUrl: "https://test.test")
mockService.submitBugReportProgressListenerClosure = { _, _ in
await Task.yield()
return SubmitBugReportResponse(reportUrl: "https://test.test")
}
let viewModel = BugReportScreenViewModel(bugReportService: mockService,
userID: "@mock.client.com",
deviceID: nil,

View File

@@ -27,13 +27,11 @@ final class NotificationManagerTests: XCTestCase {
private var shouldDisplayInAppNotificationReturnValue = false
private var handleInlineReplyDelegateCalled = false
private var notificationTappedDelegateCalled = false
private var settings: AppSettings!
private var appSettings: AppSettings { ServiceLocator.shared.settings }
override func setUp() {
AppSettings.configureWithSuiteName("io.element.elementx.unitests")
AppSettings.reset()
settings = AppSettings()
ServiceLocator.shared.register(appSettings: settings)
notificationManager = NotificationManager(notificationCenter: notificationCenter)
notificationManager.start()
@@ -64,7 +62,7 @@ final class NotificationManagerTests: XCTestCase {
let pushkeyData = Data("1234".utf8)
_ = await notificationManager.register(with: pushkeyData)
XCTAssertEqual(clientProxy.setPusherArgument?.identifiers.pushkey, pushkeyData.base64EncodedString())
XCTAssertEqual(clientProxy.setPusherArgument?.identifiers.appId, settings?.pusherAppId)
XCTAssertEqual(clientProxy.setPusherArgument?.identifiers.appId, appSettings.pusherAppId)
XCTAssertEqual(clientProxy.setPusherArgument?.appDisplayName, "\(InfoPlistReader.main.bundleDisplayName) (iOS)")
XCTAssertEqual(clientProxy.setPusherArgument?.deviceDisplayName, UIDevice.current.name)
XCTAssertNotNil(clientProxy.setPusherArgument?.profileTag)
@@ -73,7 +71,7 @@ final class NotificationManagerTests: XCTestCase {
XCTFail("Http kind expected")
return
}
XCTAssertEqual(data.url, settings?.pushGatewayBaseURL.absoluteString)
XCTAssertEqual(data.url, appSettings.pushGatewayBaseURL.absoluteString)
XCTAssertEqual(data.format, .eventIdOnly)
let defaultPayload = APNSPayload(aps: APSInfo(mutableContent: 1,
alert: APSAlert(locKey: "Notification",
@@ -83,15 +81,15 @@ final class NotificationManagerTests: XCTestCase {
}
func test_whenRegisteredAndPusherTagNotSetInSettings_tagGeneratedAndSavedInSettings() async throws {
settings?.pusherProfileTag = nil
appSettings.pusherProfileTag = nil
_ = await notificationManager.register(with: Data())
XCTAssertNotNil(settings?.pusherProfileTag)
XCTAssertNotNil(appSettings.pusherProfileTag)
}
func test_whenRegisteredAndPusherTagIsSetInSettings_tagNotGenerated() async throws {
settings?.pusherProfileTag = "12345"
appSettings.pusherProfileTag = "12345"
_ = await notificationManager.register(with: Data())
XCTAssertEqual(settings?.pusherProfileTag, "12345")
XCTAssertEqual(appSettings.pusherProfileTag, "12345")
}
func test_whenShowLocalNotification_notificationRequestGetsAdded() async throws {

View File

@@ -36,7 +36,7 @@ class StartChatScreenViewModelTests: XCTestCase {
let userSession = MockUserSession(clientProxy: clientProxy, mediaProvider: MockMediaProvider())
viewModel = StartChatScreenViewModel(userSession: userSession, userIndicatorController: nil, userDiscoveryService: userDiscoveryService)
setupAppSettings()
AppSettings.reset()
ServiceLocator.shared.settings.startChatUserSuggestionsEnabled = true
}