Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoy committed Nov 19, 2024
1 parent 2d25e9e commit 4592434
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 63 deletions.
6 changes: 6 additions & 0 deletions DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,8 @@
376788132CECF03200F59D83 /* NewTabPageRMFClientTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 376788112CECF03000F59D83 /* NewTabPageRMFClientTests.swift */; };
376788152CED308200F59D83 /* NewTabPageConfigurationClientTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 376788142CED308000F59D83 /* NewTabPageConfigurationClientTests.swift */; };
376788162CED308200F59D83 /* NewTabPageConfigurationClientTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 376788142CED308000F59D83 /* NewTabPageConfigurationClientTests.swift */; };
376788182CED4C4100F59D83 /* NewTabPageTestsHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 376788172CED4C3A00F59D83 /* NewTabPageTestsHelper.swift */; };
376788192CED4C4100F59D83 /* NewTabPageTestsHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 376788172CED4C3A00F59D83 /* NewTabPageTestsHelper.swift */; };
3768D8382C24BFF5004120AE /* RemoteMessageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3768D8372C24BFF5004120AE /* RemoteMessageView.swift */; };
3768D8392C24BFF5004120AE /* RemoteMessageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3768D8372C24BFF5004120AE /* RemoteMessageView.swift */; };
3768D83B2C24C0A8004120AE /* RemoteMessageViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3768D83A2C24C0A8004120AE /* RemoteMessageViewModel.swift */; };
Expand Down Expand Up @@ -3648,6 +3650,7 @@
3767880E2CECD5A200F59D83 /* NewTabPageUserScriptTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewTabPageUserScriptTests.swift; sourceTree = "<group>"; };
376788112CECF03000F59D83 /* NewTabPageRMFClientTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewTabPageRMFClientTests.swift; sourceTree = "<group>"; };
376788142CED308000F59D83 /* NewTabPageConfigurationClientTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewTabPageConfigurationClientTests.swift; sourceTree = "<group>"; };
376788172CED4C3A00F59D83 /* NewTabPageTestsHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewTabPageTestsHelper.swift; sourceTree = "<group>"; };
3768D8372C24BFF5004120AE /* RemoteMessageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoteMessageView.swift; sourceTree = "<group>"; };
3768D83A2C24C0A8004120AE /* RemoteMessageViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoteMessageViewModel.swift; sourceTree = "<group>"; };
3768D83F2C29C1F1004120AE /* ActiveRemoteMessageModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActiveRemoteMessageModel.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -5794,6 +5797,7 @@
3767880A2CECCB6000F59D83 /* NewTabPage */ = {
isa = PBXGroup;
children = (
376788172CED4C3A00F59D83 /* NewTabPageTestsHelper.swift */,
3767880E2CECD5A200F59D83 /* NewTabPageUserScriptTests.swift */,
3767880B2CECCB6C00F59D83 /* NewTabPageActionsManagerTests.swift */,
376788112CECF03000F59D83 /* NewTabPageRMFClientTests.swift */,
Expand Down Expand Up @@ -12301,6 +12305,7 @@
3706FE3F293F661700E42796 /* FileStoreMock.swift in Sources */,
B6619F042B17123200CD9186 /* DataImportViewModelTests.swift in Sources */,
1D8C2FE62B70F4C4005E4BBD /* TabSnapshotExtensionTests.swift in Sources */,
376788192CED4C4100F59D83 /* NewTabPageTestsHelper.swift in Sources */,
3706FE40293F661700E42796 /* BWResponseTests.swift in Sources */,
3706FE41293F661700E42796 /* DownloadListCoordinatorTests.swift in Sources */,
986189E72A7CFB3E001B4519 /* LocalBookmarkStoreSavingTests.swift in Sources */,
Expand Down Expand Up @@ -14046,6 +14051,7 @@
4B70C00227B0793D000386ED /* CrashReportTests.swift in Sources */,
B6656E0D2B29C733008798A1 /* FileImportViewLocalizationTests.swift in Sources */,
37D23787287F5C2900BCE03B /* PinnedTabsViewModelTests.swift in Sources */,
376788182CED4C4100F59D83 /* NewTabPageTestsHelper.swift in Sources */,
4B9DB0542A983B55000927DB /* MockWaitlistStorage.swift in Sources */,
37E260922C8A3EB4006EE07F /* MockHomePageSettingsModelNavigator.swift in Sources */,
4BF4EA5027C71F26004E57C4 /* PasswordManagementListSectionTests.swift in Sources */,
Expand Down
58 changes: 30 additions & 28 deletions DuckDuckGo/NewTabPage/NewTabPageRMFClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
//

import Combine
import Common
import RemoteMessaging
import UserScript

Expand Down Expand Up @@ -79,9 +80,8 @@ final class NewTabPageRMFClient: NewTabPageScriptClient {
}

private func dismiss(params: Any, original: WKScriptMessage) async throws -> Encodable? {
guard let paramsDict = params as? [String: Any],
let id = paramsDict["id"] as? String,
id == remoteMessageProvider.remoteMessage?.id
guard let remoteMessageParams: NewTabPageUserScript.RemoteMessageParams = DecodableHelper.decode(from: params),
remoteMessageParams.id == remoteMessageProvider.remoteMessage?.id
else {
return nil
}
Expand All @@ -91,9 +91,8 @@ final class NewTabPageRMFClient: NewTabPageScriptClient {
}

private func primaryAction(params: Any, original: WKScriptMessage) async throws -> Encodable? {
guard let paramsDict = params as? [String: Any],
let id = paramsDict["id"] as? String,
id == remoteMessageProvider.remoteMessage?.id
guard let remoteMessageParams: NewTabPageUserScript.RemoteMessageParams = DecodableHelper.decode(from: params),
remoteMessageParams.id == remoteMessageProvider.remoteMessage?.id
else {
return nil
}
Expand All @@ -112,9 +111,8 @@ final class NewTabPageRMFClient: NewTabPageScriptClient {
}

private func secondaryAction(params: Any, original: WKScriptMessage) async throws -> Encodable? {
guard let paramsDict = params as? [String: Any],
let id = paramsDict["id"] as? String,
id == remoteMessageProvider.remoteMessage?.id
guard let remoteMessageParams: NewTabPageUserScript.RemoteMessageParams = DecodableHelper.decode(from: params),
remoteMessageParams.id == remoteMessageProvider.remoteMessage?.id
else {
return nil
}
Expand Down Expand Up @@ -156,8 +154,12 @@ final class NewTabPageRMFClient: NewTabPageScriptClient {

extension NewTabPageUserScript {

struct RemoteMessageParams: Codable {
let id: String
}

struct RMFData: Encodable {
var content: RMFMessage?
let content: RMFMessage?
}

enum RMFMessage: Encodable, Equatable {
Expand Down Expand Up @@ -207,39 +209,39 @@ extension NewTabPageUserScript {
struct SmallMessage: Encodable, Equatable {
let messageType = "small"

var id: String
var titleText: String
var descriptionText: String
let id: String
let titleText: String
let descriptionText: String
}

struct MediumMessage: Encodable, Equatable {
let messageType = "medium"

var id: String
var titleText: String
var descriptionText: String
var icon: RMFIcon
let id: String
let titleText: String
let descriptionText: String
let icon: RMFIcon
}

struct BigSingleActionMessage: Encodable, Equatable {
let messageType = "big_single_action"

var id: String
var titleText: String
var descriptionText: String
var icon: RMFIcon
var primaryActionText: String
let id: String
let titleText: String
let descriptionText: String
let icon: RMFIcon
let primaryActionText: String
}

struct BigTwoActionMessage: Encodable, Equatable {
let messageType = "big_two_action"

var id: String
var titleText: String
var descriptionText: String
var icon: RMFIcon
var primaryActionText: String
var secondaryActionText: String
let id: String
let titleText: String
let descriptionText: String
let icon: RMFIcon
let primaryActionText: String
let secondaryActionText: String
}

enum RMFIcon: String, Encodable {
Expand Down
16 changes: 2 additions & 14 deletions UnitTests/NewTabPage/NewTabPageConfigurationClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,25 +125,13 @@ final class NewTabPageConfigurationClientTests: XCTestCase {

func sendMessage<Response: Encodable>(named methodName: NewTabPageConfigurationClient.MessageName, parameters: Any = [], file: StaticString = #file, line: UInt = #line) async throws -> Response {
let handler = try XCTUnwrap(userScript.handler(forMethodNamed: methodName.rawValue), file: file, line: line)
let response = try await handler(asJSON(parameters), .init())
let response = try await handler(NewTabPageTestsHelper.asJSON(parameters), .init())
return try XCTUnwrap(response as? Response, file: file, line: line)
}

func sendMessageExpectingNilResponse(named methodName: NewTabPageConfigurationClient.MessageName, parameters: Any = [], file: StaticString = #file, line: UInt = #line) async throws {
let handler = try XCTUnwrap(userScript.handler(forMethodNamed: methodName.rawValue), file: file, line: line)
let response = try await handler(asJSON(parameters), .init())
let response = try await handler(NewTabPageTestsHelper.asJSON(parameters), .init())
XCTAssertNil(response, file: file, line: line)
}

func asJSON(_ value: Any, file: StaticString = #file, line: UInt = #line) throws -> Any {
if JSONSerialization.isValidJSONObject(value) {
return value
}
if let encodableValue = value as? Encodable {
let jsonData = try JSONEncoder().encode(encodableValue)
return try JSONSerialization.jsonObject(with: jsonData)
}
XCTFail("invalid JSON value", file: file, line: line)
return []
}
}
Loading

0 comments on commit 4592434

Please sign in to comment.