Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FXIOS-10094 [Unit Tests] Add store utility for tests #22172

Merged
merged 2 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions firefox-ios/Client.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,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 @@ -6949,6 +6950,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 @@ -10536,6 +10538,7 @@
C8E78BDC27F4A1E700C48BAA /* HistoryDeletionUtilityTests.swift */,
8A1E3BE128CBACD7003388C4 /* SponsoredContentFilterUtilityTests.swift */,
8ACA8F7529198D6400D3075D /* ThrottlerTests.swift */,
8AA8389C2CA2FEFC003FA256 /* StoreTestUtility.swift */,
);
path = Utils;
sourceTree = "<group>";
Expand Down Expand Up @@ -16082,6 +16085,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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion i think we might not need this protocol, i'd just add the StoreTestUtilityHelper directly as property of the Tester class. Let me know what you think! thanks @cyndichin 😃

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment! Hmm, I wanted a way to ensure that any test that uses the store will conform to the protocol, which will throw compiler errors if the user does not have the setup or reset methods and then can use the StoreTestUtilityHelper for convenience. Happy to remove if you don't think it's useful though.

Does it seem unnecessary extra code for you? Do you think we should open up to the team?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok Thank you for clarifying @cyndichin yes that makes sense to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great, thanks! I'll also present this to the team tomorrow as well so can keep this PR open until then.

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() {
cyndichin marked this conversation as resolved.
Show resolved Hide resolved
store = Store(
state: AppState(),
reducer: AppState.reducer,
middlewares: middlewares
)
}
}