Skip to content

Commit

Permalink
Add FXIOS-10094 [Unit Tests] Add store utility for tests (#22172)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyndichin authored Sep 26, 2024
1 parent 8031d3f commit e81df41
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
4 changes: 4 additions & 0 deletions firefox-ios/Client.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,7 @@
8AA020EF2B9A37E500771DE0 /* NimbusSplashScreenFeatureLayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AA020EE2B9A37E500771DE0 /* NimbusSplashScreenFeatureLayer.swift */; };
8AA6ADB52742B567004EEE23 /* TelemetryWrapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AA6ADB42742B567004EEE23 /* TelemetryWrapperTests.swift */; };
8AA7347B28AEDB3100443D24 /* PocketViewModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AA7347A28AEDB3100443D24 /* PocketViewModelTests.swift */; };
8AA8389D2CA2FEFC003FA256 /* StoreTestUtility.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AA8389C2CA2FEFC003FA256 /* StoreTestUtility.swift */; };
8AAAB0592C1B7240008830B3 /* MockRustFirefoxSuggest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AAAB0582C1B723F008830B3 /* MockRustFirefoxSuggest.swift */; };
8AAAB05B2C1B7268008830B3 /* ClientTabsSearchWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AAAB05A2C1B7268008830B3 /* ClientTabsSearchWrapper.swift */; };
8AAAB05F2C1B72C9008830B3 /* SearchHighlightItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AAAB05E2C1B72C9008830B3 /* SearchHighlightItem.swift */; };
Expand Down Expand Up @@ -6944,6 +6945,7 @@
8AA020EE2B9A37E500771DE0 /* NimbusSplashScreenFeatureLayer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NimbusSplashScreenFeatureLayer.swift; sourceTree = "<group>"; };
8AA6ADB42742B567004EEE23 /* TelemetryWrapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TelemetryWrapperTests.swift; sourceTree = "<group>"; };
8AA7347A28AEDB3100443D24 /* PocketViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PocketViewModelTests.swift; sourceTree = "<group>"; };
8AA8389C2CA2FEFC003FA256 /* StoreTestUtility.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StoreTestUtility.swift; sourceTree = "<group>"; };
8AAAB0582C1B723F008830B3 /* MockRustFirefoxSuggest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MockRustFirefoxSuggest.swift; sourceTree = "<group>"; };
8AAAB05A2C1B7268008830B3 /* ClientTabsSearchWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClientTabsSearchWrapper.swift; sourceTree = "<group>"; };
8AAAB05E2C1B72C9008830B3 /* SearchHighlightItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchHighlightItem.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -10538,6 +10540,7 @@
C8E78BDC27F4A1E700C48BAA /* HistoryDeletionUtilityTests.swift */,
8A1E3BE128CBACD7003388C4 /* SponsoredContentFilterUtilityTests.swift */,
8ACA8F7529198D6400D3075D /* ThrottlerTests.swift */,
8AA8389C2CA2FEFC003FA256 /* StoreTestUtility.swift */,
);
path = Utils;
sourceTree = "<group>";
Expand Down Expand Up @@ -16078,6 +16081,7 @@
21534904288201E300FADB4D /* GleanPlumbMessageManagerTests.swift in Sources */,
AB03032F2AB8561700DCD8EF /* FakespotOptInViewModelTests.swift in Sources */,
21BFEEF82A05A0370033048D /* TabMigrationUtilityTests.swift in Sources */,
8AA8389D2CA2FEFC003FA256 /* StoreTestUtility.swift in Sources */,
DFD1046D2B23539600938418 /* ProductAdsCacheTests.swift in Sources */,
E1390FB628B040E900C9EF3E /* WallpaperSelectorViewModelTests.swift in Sources */,
39AF317429DAE37E00F8E6F7 /* NimbusMessagingMessageTests.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import XCTest

@testable import Client

final class MicrosurveyMiddlewareTests: XCTestCase {
final class MicrosurveyMiddlewareIntegrationTests: XCTestCase, StoreTestUtility {
let storeUtilityHelper = StoreTestUtilityHelper()
override func setUp() {
super.setUp()
Glean.shared.resetGlean(clearStores: true)
DependencyHelperMock().bootstrapDependencies()
setupTestingStore()
}

override func tearDown() {
Expand All @@ -22,8 +24,6 @@ final class MicrosurveyMiddlewareTests: XCTestCase {
}

func testDismissSurveyAction() throws {
setupTestingStore()

let action = getAction(for: .closeSurvey)
store.dispatch(action)

Expand All @@ -41,8 +41,6 @@ final class MicrosurveyMiddlewareTests: XCTestCase {
}

func testPrivacyNoticeTappedAction() throws {
setupTestingStore()

let action = getAction(for: .tapPrivacyNotice)
store.dispatch(action)

Expand All @@ -60,8 +58,6 @@ final class MicrosurveyMiddlewareTests: XCTestCase {
}

func testSubmitSurveyAction() throws {
setupTestingStore()

let action = MicrosurveyAction(
surveyId: "microsurvey-id",
userSelection: "Neutral",
Expand All @@ -85,8 +81,6 @@ final class MicrosurveyMiddlewareTests: XCTestCase {
}

func testConfirmationViewedAction() throws {
setupTestingStore()

let action = getAction(for: .confirmationViewed)
store.dispatch(action)

Expand All @@ -103,7 +97,8 @@ final class MicrosurveyMiddlewareTests: XCTestCase {
)
}

private func setupAppState() -> AppState {
// MARK: StoreTestUtility
func setupAppState() -> AppState {
return AppState(
activeScreens: ActiveScreensState(
screens: [
Expand All @@ -122,21 +117,16 @@ final class MicrosurveyMiddlewareTests: XCTestCase {
)
}

private func setupTestingStore() {
store = Store(
state: setupAppState(),
reducer: AppState.reducer,
func setupTestingStore() {
storeUtilityHelper.setupTestingStore(
with: setupAppState(),
middlewares: [MicrosurveyMiddleware().microsurveyProvider]
)
}

// In order to avoid flaky tests, we should reset the store
// similar to production
private func resetTestingStore() {
store = Store(
state: AppState(),
reducer: AppState.reducer,
middlewares: middlewares
)
func resetTestingStore() {
storeUtilityHelper.resetTestingStore()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/

import Foundation
import Redux
@testable import Client

protocol StoreTestUtility {
func setupAppState() -> AppState
func setupTestingStore()
func resetTestingStore()
}

/// Utility class used when replacing the global store for testing purposes
class StoreTestUtilityHelper {
func setupTestingStore(with appState: AppState, middlewares: [Middleware<AppState>]) {
store = Store(
state: appState,
reducer: AppState.reducer,
middlewares: middlewares
)
}

/// In order to avoid flaky tests, we should reset the store
/// similar to production
func resetTestingStore() {
store = Store(
state: AppState(),
reducer: AppState.reducer,
middlewares: middlewares
)
}
}

0 comments on commit e81df41

Please sign in to comment.