Skip to content

Commit

Permalink
Add unit tests for NewTabPageNextStepsCardsProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoy committed Dec 11, 2024
1 parent 81e0076 commit 2a58f70
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 2 deletions.
13 changes: 12 additions & 1 deletion DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 54;
objectVersion = 70;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -5000,6 +5000,10 @@
FD23FD2C2886A81D007F6985 /* AutoconsentManagement.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AutoconsentManagement.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFileSystemSynchronizedRootGroup section */
3792D51A2D09C72200FC15D6 /* NewTabPage */ = {isa = PBXFileSystemSynchronizedRootGroup; explicitFileTypes = {}; explicitFolders = (); path = NewTabPage; sourceTree = "<group>"; };
/* End PBXFileSystemSynchronizedRootGroup section */

/* Begin PBXFrameworksBuildPhase section */
3706FCA6293F65D500E42796 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
Expand Down Expand Up @@ -8035,6 +8039,7 @@
1D3B1AB7293405F5006F4388 /* PasswordManagers */,
B6106BA126A7BE430013B453 /* Permissions */,
CD34F0BE2C886462006826BE /* MaliciousSiteProtection */,
3792D51A2D09C72200FC15D6 /* NewTabPage */,
37D2377E287EFECD00BCE03B /* PinnedTabs */,
4B0511EE262CAEB300F6079C /* Preferences */,
31E163BB293A577200963C10 /* PrivacyReferenceTests */,
Expand Down Expand Up @@ -9975,6 +9980,9 @@
B6AEB5552BA3042300781A09 /* PBXTargetDependency */,
37079A93294236F20031BB3C /* PBXTargetDependency */,
);
fileSystemSynchronizedGroups = (
3792D51A2D09C72200FC15D6 /* NewTabPage */,
);
name = "Unit Tests App Store";
packageProductDependencies = (
3706FDD6293F661700E42796 /* OHHTTPStubs */,
Expand Down Expand Up @@ -10462,6 +10470,9 @@
B6E6BA1D2BA2E049008AA7E1 /* PBXTargetDependency */,
B6CAC23D2B8F0EC6006CD402 /* PBXTargetDependency */,
);
fileSystemSynchronizedGroups = (
3792D51A2D09C72200FC15D6 /* NewTabPage */,
);
name = "Unit Tests";
packageProductDependencies = (
B6DA44162616C13800DD1EC2 /* OHHTTPStubs */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ final class NewTabPageNextStepsCardsProvider: NewTabPageNextStepsCardsProviding

var cardsPublisher: AnyPublisher<[NewTabPageNextStepsCardsClient.CardID], Never> {
let features = continueSetUpModel.$featuresMatrix.dropFirst().removeDuplicates()
let cardsDidBecomeOutdated = appearancePreferences.$isContinueSetUpCardsViewOutdated.dropFirst().removeDuplicates()
let cardsDidBecomeOutdated = appearancePreferences.$isContinueSetUpCardsViewOutdated.removeDuplicates()

return Publishers.CombineLatest(features, cardsDidBecomeOutdated)
.map { features, isOutdated -> [NewTabPageNextStepsCardsClient.CardID] in
Expand Down
121 changes: 121 additions & 0 deletions UnitTests/NewTabPage/NewTabPageNextStepsCardsProviderTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
//
// NewTabPageNextStepsCardsProviderTests.swift
//
// Copyright © 2024 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import BrowserServicesKit
import Combine
import NewTabPage
import XCTest
@testable import DuckDuckGo_Privacy_Browser

final class NewTabPageNextStepsCardsProviderTests: XCTestCase {
var provider: NewTabPageNextStepsCardsProvider!

@MainActor
override func setUp() async throws {
let privacyConfigManager = MockPrivacyConfigurationManager()
let config = MockPrivacyConfiguration()
privacyConfigManager.privacyConfig = config

let continueSetUpModel = HomePage.Models.ContinueSetUpModel(
defaultBrowserProvider: CapturingDefaultBrowserProvider(),
dockCustomizer: DockCustomizerMock(),
dataImportProvider: CapturingDataImportProvider(),
tabOpener: TabCollectionViewModelTabOpener(tabCollectionViewModel: TabCollectionViewModel()),
emailManager: EmailManager(storage: MockEmailStorage()),
duckPlayerPreferences: DuckPlayerPreferencesPersistorMock(),
privacyConfigurationManager: privacyConfigManager
)

provider = NewTabPageNextStepsCardsProvider(
continueSetUpModel: continueSetUpModel,
appearancePreferences: AppearancePreferences(persistor: MockAppearancePreferencesPersistor())
)
}

func testWhenCardsViewIsNotOutdatedThenCardsAreReportedByModel() {
provider.appearancePreferences.isContinueSetUpCardsViewOutdated = false
provider.continueSetUpModel.featuresMatrix = [[.defaultBrowser, .dock, .emailProtection]]

XCTAssertEqual(provider.cards, [.defaultApp, .addAppToDockMac, .emailProtection])
}

func testWhenCardsViewIsOutdatedThenCardsAreEmpty() {
provider.appearancePreferences.isContinueSetUpCardsViewOutdated = true
provider.continueSetUpModel.featuresMatrix = [[.defaultBrowser, .dock, .emailProtection]]

XCTAssertEqual(provider.cards, [])
}

func testWhenCardsViewIsNotOutdatedThenCardsAreEmitted() {
provider.appearancePreferences.isContinueSetUpCardsViewOutdated = false
provider.continueSetUpModel.featuresMatrix = [[.defaultBrowser]]

var cardsEvents = [[NewTabPageNextStepsCardsClient.CardID]]()

let cancellable = provider.cardsPublisher
.sink { cards in
cardsEvents.append(cards)
}

provider.continueSetUpModel.featuresMatrix = [[.dock]]
provider.continueSetUpModel.featuresMatrix = [[.dock, .duckplayer]]
provider.continueSetUpModel.featuresMatrix = [[.defaultBrowser]]

cancellable.cancel()
XCTAssertEqual(cardsEvents, [[.addAppToDockMac], [.addAppToDockMac, .duckplayer], [.defaultApp]])
}

func testWhenCardsViewIsOutdatedThenEmptyCardsAreEmitted() {
provider.appearancePreferences.isContinueSetUpCardsViewOutdated = true
provider.continueSetUpModel.featuresMatrix = [[.defaultBrowser]]

var cardsEvents = [[NewTabPageNextStepsCardsClient.CardID]]()

let cancellable = provider.cardsPublisher
.sink { cards in
cardsEvents.append(cards)
}

provider.continueSetUpModel.featuresMatrix = [[.dock]]
provider.continueSetUpModel.featuresMatrix = [[.duckplayer]]
provider.continueSetUpModel.featuresMatrix = [[.defaultBrowser]]

cancellable.cancel()
XCTAssertEqual(cardsEvents, [[], [], []])
}

func testWhenCardsViewBecomesOutdatedThenCardsStopBeingEmitted() {
provider.appearancePreferences.isContinueSetUpCardsViewOutdated = false
provider.continueSetUpModel.featuresMatrix = [[.defaultBrowser]]

var cardsEvents = [[NewTabPageNextStepsCardsClient.CardID]]()

let cancellable = provider.cardsPublisher
.sink { cards in
cardsEvents.append(cards)
}

provider.continueSetUpModel.featuresMatrix = [[.dock]]
provider.continueSetUpModel.featuresMatrix = [[.dock, .duckplayer]]
provider.appearancePreferences.isContinueSetUpCardsViewOutdated = true
provider.continueSetUpModel.featuresMatrix = [[.defaultBrowser]]

cancellable.cancel()
XCTAssertEqual(cardsEvents, [[.addAppToDockMac], [.addAppToDockMac, .duckplayer], [], []])
}
}

0 comments on commit 2a58f70

Please sign in to comment.