Skip to content

Commit 158f8c6

Browse files
committed
split out mocks
1 parent 69960f0 commit 158f8c6

20 files changed

+916
-527
lines changed

swift-sdk.xcodeproj/project.pbxproj

Lines changed: 273 additions & 1 deletion
Large diffs are not rendered by default.

tests/common/CommonMocks.swift

Lines changed: 0 additions & 526 deletions
Large diffs are not rendered by default.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// Copyright © 2022 Iterable. All rights reserved.
3+
//
4+
5+
import Foundation
6+
7+
@testable import IterableSDK
8+
9+
struct MockAPNSTypeChecker: APNSTypeCheckerProtocol {
10+
let apnsType: APNSType
11+
12+
init(apnsType: APNSType) {
13+
self.apnsType = apnsType
14+
}
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// Copyright © 2022 Iterable. All rights reserved.
3+
//
4+
5+
import Foundation
6+
7+
@testable import IterableSDK
8+
9+
@objc public class MockApplicationStateProvider: NSObject, ApplicationStateProviderProtocol {
10+
override private convenience init() {
11+
self.init(applicationState: .active)
12+
}
13+
14+
@objc public init(applicationState: UIApplication.State) {
15+
self.applicationState = applicationState
16+
}
17+
18+
public var applicationState: UIApplication.State
19+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// Copyright © 2022 Iterable. All rights reserved.
3+
//
4+
5+
import Foundation
6+
7+
@testable import IterableSDK
8+
9+
@objcMembers
10+
public class MockCustomActionDelegate: NSObject, IterableCustomActionDelegate {
11+
// returnValue is reserved for future, don't rely on this
12+
override private convenience init() {
13+
self.init(returnValue: false)
14+
}
15+
16+
public init(returnValue: Bool) {
17+
self.returnValue = returnValue
18+
}
19+
20+
private(set) var returnValue: Bool
21+
private(set) var action: IterableAction?
22+
private(set) var context: IterableActionContext?
23+
var callback: ((String, IterableActionContext) -> Void)?
24+
25+
public func handle(iterableCustomAction action: IterableAction, inContext context: IterableActionContext) -> Bool {
26+
self.action = action
27+
self.context = context
28+
callback?(action.type, context)
29+
return returnValue
30+
}
31+
}

tests/common/MockDateProvider.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// Copyright © 2022 Iterable. All rights reserved.
3+
//
4+
5+
import Foundation
6+
7+
@testable import IterableSDK
8+
9+
class MockDateProvider: DateProviderProtocol {
10+
var currentDate = Date()
11+
12+
func reset() {
13+
currentDate = Date()
14+
}
15+
}

tests/common/MockInAppDelegate.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// Copyright © 2022 Iterable. All rights reserved.
3+
//
4+
5+
import Foundation
6+
7+
@testable import IterableSDK
8+
9+
class MockInAppDelegate: IterableInAppDelegate {
10+
var onNewMessageCallback: ((IterableInAppMessage) -> Void)?
11+
12+
init(showInApp: InAppShowResponse = .show) {
13+
self.showInApp = showInApp
14+
}
15+
16+
func onNew(message: IterableInAppMessage) -> InAppShowResponse {
17+
onNewMessageCallback?(message)
18+
return showInApp
19+
}
20+
21+
private let showInApp: InAppShowResponse
22+
}

tests/common/MockInAppDisplayer.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// Copyright © 2022 Iterable. All rights reserved.
3+
//
4+
5+
import Foundation
6+
7+
@testable import IterableSDK
8+
9+
class MockInAppDisplayer: InAppDisplayerProtocol {
10+
// when a message is shown this is called back
11+
var onShow: Fulfill<IterableInAppMessage, IterableError> = Fulfill<IterableInAppMessage, IterableError>()
12+
13+
func isShowingInApp() -> Bool {
14+
showing
15+
}
16+
17+
// This is not resolved until a url is clicked.
18+
func showInApp(message: IterableInAppMessage, onClickCallback: ((URL) -> Void)?) -> ShowResult {
19+
guard showing == false else {
20+
onShow.reject(with: IterableError.general(description: "showing something else"))
21+
return .notShown("showing something else")
22+
}
23+
24+
showing = true
25+
self.onClickCallback = onClickCallback
26+
27+
onShow.resolve(with: message)
28+
29+
return .shown
30+
}
31+
32+
// Mimics clicking a url
33+
func click(url: URL) {
34+
ITBInfo()
35+
showing = false
36+
DispatchQueue.main.async { [weak self] in
37+
self?.onClickCallback?(url)
38+
}
39+
}
40+
41+
private var onClickCallback: ((URL) -> Void)?
42+
private var showing = false
43+
}

tests/common/MockInAppFetcher.swift

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//
2+
// Copyright © 2022 Iterable. All rights reserved.
3+
//
4+
5+
import Foundation
6+
7+
@testable import IterableSDK
8+
9+
class MockInAppFetcher: InAppFetcherProtocol {
10+
var syncCallback: (() -> Void)?
11+
12+
init(messages: [IterableInAppMessage] = []) {
13+
ITBInfo()
14+
for message in messages {
15+
messagesMap[message.messageId] = message
16+
}
17+
}
18+
19+
deinit {
20+
ITBInfo()
21+
}
22+
23+
func fetch() -> Pending<[IterableInAppMessage], Error> {
24+
ITBInfo()
25+
26+
syncCallback?()
27+
28+
return Fulfill(value: messagesMap.values)
29+
}
30+
31+
@discardableResult func mockMessagesAvailableFromServer(internalApi: InternalIterableAPI?, messages: [IterableInAppMessage]) -> Pending<Int, Error> {
32+
ITBInfo()
33+
34+
messagesMap = OrderedDictionary<String, IterableInAppMessage>()
35+
36+
messages.forEach {
37+
messagesMap[$0.messageId] = $0
38+
}
39+
40+
let result = Fulfill<Int, Error>()
41+
42+
let inAppManager = internalApi?.inAppManager
43+
inAppManager?.scheduleSync().onSuccess { [weak inAppManager = inAppManager] _ in
44+
result.resolve(with: inAppManager?.getMessages().count ?? 0)
45+
}
46+
47+
return result
48+
}
49+
50+
@discardableResult func mockInAppPayloadFromServer(internalApi: InternalIterableAPI?, _ payload: [AnyHashable: Any]) -> Pending<Int, Error> {
51+
ITBInfo()
52+
return mockMessagesAvailableFromServer(internalApi: internalApi, messages: InAppTestHelper.inAppMessages(fromPayload: payload))
53+
}
54+
55+
func add(message: IterableInAppMessage) {
56+
messagesMap[message.messageId] = message
57+
}
58+
59+
var messages: [IterableInAppMessage] {
60+
messagesMap.values
61+
}
62+
63+
private var messagesMap = OrderedDictionary<String, IterableInAppMessage>()
64+
}

tests/common/MockInAppPersister.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// Copyright © 2022 Iterable. All rights reserved.
3+
//
4+
5+
import Foundation
6+
7+
@testable import IterableSDK
8+
9+
class MockInAppPersister: InAppPersistenceProtocol {
10+
private var messages = [IterableInAppMessage]()
11+
12+
func getMessages() -> [IterableInAppMessage] {
13+
messages
14+
}
15+
16+
func persist(_ messages: [IterableInAppMessage]) {
17+
self.messages = messages
18+
}
19+
20+
func clear() {
21+
messages.removeAll()
22+
}
23+
}

0 commit comments

Comments
 (0)