Skip to content

Commit

Permalink
updates unit tests and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Greer committed Nov 10, 2023
1 parent 54bcca5 commit 3600fa3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 23 deletions.
14 changes: 2 additions & 12 deletions swift-sdk/Internal/IterableEmbeddedManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,15 @@ class IterableEmbeddedManager: NSObject, IterableEmbeddedManagerProtocol {
retrieveEmbeddedMessages(completion: completion)
}

public func handleEmbeddedClick(message: IterableEmbeddedMessage?, buttonIdentifier: String?, clickedUrl: String) {
print("called embeddedMessageClicked IterableEmbeddedManager method.")
guard let message = message else {
print("Error: message is nil.")
return
}
public func handleEmbeddedClick(message: IterableEmbeddedMessage, buttonIdentifier: String?, clickedUrl: String) {

if let url = URL(string: clickedUrl) {
handleClick(clickedUrl: url, forMessage: message)
handleUrl(url: url.absoluteString)
} else {
print("Invalid URL: \(clickedUrl)")
}
}

private func handleClick(clickedUrl url: URL, forMessage message: IterableEmbeddedMessage) {

handleUrl(url: url.absoluteString)
}


private func createAction(fromUrlOrAction url: String) -> IterableAction? {
if let parsedUrl = URL(string: url), let _ = parsedUrl.scheme {
Expand Down
2 changes: 1 addition & 1 deletion swift-sdk/IterableEmbeddedManagerProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ import Foundation
func removeUpdateListener(_ listener: IterableEmbeddedUpdateDelegate)

func syncMessages(completion: @escaping () -> Void)
func handleEmbeddedClick(message: IterableEmbeddedMessage?, buttonIdentifier: String?, clickedUrl: String)
func handleEmbeddedClick(message: IterableEmbeddedMessage, buttonIdentifier: String?, clickedUrl: String)
}
53 changes: 43 additions & 10 deletions tests/unit-tests/EmbeddedManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ final class EmbeddedManagerTests: XCTestCase {

let mockApiClient = MockApiClient()

let manager = IterableEmbeddedManager(apiClient: mockApiClient)
let manager = IterableEmbeddedManager(apiClient: mockApiClient,
urlDelegate: nil,
urlOpener: MockUrlOpener(),
allowedProtocols: [])

let view1 = ViewWithUpdateDelegate(
onMessagesUpdatedCallback: {
Expand All @@ -32,7 +35,10 @@ final class EmbeddedManagerTests: XCTestCase {
// getMessages
func testGetMessagesWhenEmpty() {
let mockApiClient = MockApiClient()
let manager = IterableEmbeddedManager(apiClient: mockApiClient)
let manager = IterableEmbeddedManager(apiClient: mockApiClient,
urlDelegate: nil,
urlOpener: MockUrlOpener(),
allowedProtocols: [])
XCTAssertEqual(manager.getMessages().count, 0)
}
func testGetMessagesForPlacement() {
Expand All @@ -43,9 +49,12 @@ final class EmbeddedManagerTests: XCTestCase {
IterableEmbeddedMessage(messageId: "3", placementId: 2),
IterableEmbeddedMessage(messageId: "4", placementId: 3),
])
let manager = IterableEmbeddedManager(apiClient: mockApiClient)
let manager = IterableEmbeddedManager(apiClient: mockApiClient,
urlDelegate: nil,
urlOpener: MockUrlOpener(),
allowedProtocols: [])

manager.syncMessages {}
manager.syncMessages { }

let messagesForPlacement2 = manager.getMessages(for: 2)

Expand All @@ -67,7 +76,10 @@ final class EmbeddedManagerTests: XCTestCase {
IterableEmbeddedMessage(messageId: "2", placementId: 1),
])

let manager = IterableEmbeddedManager(apiClient: mockApiClient)
let manager = IterableEmbeddedManager(apiClient: mockApiClient,
urlDelegate: nil,
urlOpener: MockUrlOpener(),
allowedProtocols: [])

let view = ViewWithUpdateDelegate(
onMessagesUpdatedCallback: {
Expand All @@ -89,7 +101,10 @@ final class EmbeddedManagerTests: XCTestCase {

let mockApiClient = MockApiClient()
mockApiClient.setInvalidAPIKey()
let manager = IterableEmbeddedManager(apiClient: mockApiClient)
let manager = IterableEmbeddedManager(apiClient: mockApiClient,
urlDelegate: nil,
urlOpener: MockUrlOpener(),
allowedProtocols: [])

let view = ViewWithUpdateDelegate(
onMessagesUpdatedCallback: nil,
Expand All @@ -108,7 +123,10 @@ final class EmbeddedManagerTests: XCTestCase {
// notify multiple delegates
func testManagerNotifiesMultipleDelegates() {
let mockApiClient = MockApiClient()
let manager = IterableEmbeddedManager(apiClient: mockApiClient)
let manager = IterableEmbeddedManager(apiClient: mockApiClient,
urlDelegate: nil,
urlOpener: MockUrlOpener(),
allowedProtocols: [])

var delegate1Called = false
var delegate2Called = false
Expand Down Expand Up @@ -137,7 +155,10 @@ final class EmbeddedManagerTests: XCTestCase {
// add and remove listeners
func testManagerCorrectlyAddsAndRemovesListeners() {
let mockApiClient = MockApiClient()
let manager = IterableEmbeddedManager(apiClient: mockApiClient)
let manager = IterableEmbeddedManager(apiClient: mockApiClient,
urlDelegate: nil,
urlOpener: MockUrlOpener(),
allowedProtocols: [])

var delegateCalled = false

Expand Down Expand Up @@ -169,7 +190,11 @@ final class EmbeddedManagerTests: XCTestCase {
// init/deinit
func testManagerInitializationAndDeinitialization() {
let deinitExpectation = expectation(description: "Manager should deinitialize")
var manager: IterableEmbeddedManager? = IterableEmbeddedManager(apiClient: MockApiClient())
let mockApiClient = MockApiClient()
var manager: IterableEmbeddedManager? = IterableEmbeddedManager(apiClient: mockApiClient,
urlDelegate: nil,
urlOpener: MockUrlOpener(),
allowedProtocols: [])
manager?.onDeinit = {
deinitExpectation.fulfill()
}
Expand All @@ -184,7 +209,10 @@ final class EmbeddedManagerTests: XCTestCase {
let expectation = XCTestExpectation(description: "onMessagesUpdated called")

let mockApiClient = MockApiClient()
let manager = IterableEmbeddedManager(apiClient: mockApiClient)
let manager = IterableEmbeddedManager(apiClient: mockApiClient,
urlDelegate: nil,
urlOpener: MockUrlOpener(),
allowedProtocols: [])

let mockDelegate = ViewWithUpdateDelegate(
onMessagesUpdatedCallback: {
Expand Down Expand Up @@ -229,6 +257,11 @@ final class EmbeddedManagerTests: XCTestCase {
}
}

private class MockUrlOpener: NSObject, UrlOpenerProtocol {
func open(url: URL) {
}
}

private class MockApiClient: BlankApiClient {
private var newMessages = false
private var invalidApiKey = false
Expand Down

0 comments on commit 3600fa3

Please sign in to comment.