Skip to content

Commit c4c0197

Browse files
Merge pull request #312 from Iterable/test/mob-1536-inbox
[MOB-1536] - Convert InboxTests to not use IterableAPI static members
2 parents 6f92eca + d2e0a9c commit c4c0197

File tree

1 file changed

+65
-56
lines changed

1 file changed

+65
-56
lines changed

tests/swift-sdk-swift-tests/InboxTests.swift

Lines changed: 65 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class InboxTests: XCTestCase {
1111
override class func setUp() {
1212
super.setUp()
1313
TestUtils.clearTestUserDefaults()
14-
IterableAPI.internalImplementation = nil
1514
}
1615

1716
func testInboxOrdering() {
@@ -20,7 +19,7 @@ class InboxTests: XCTestCase {
2019
let config = IterableConfig()
2120
config.logDelegate = AllLogDelegate()
2221

23-
IterableAPI.initializeForTesting(
22+
let internalAPI = IterableAPIInternal.initializeForTesting(
2423
config: config,
2524
inAppFetcher: mockInAppFetcher
2625
)
@@ -63,8 +62,8 @@ class InboxTests: XCTestCase {
6362
}
6463
""".toJsonDict()
6564

66-
mockInAppFetcher.mockInAppPayloadFromServer(payload).onSuccess { _ in
67-
let messages = IterableAPI.inAppManager.getInboxMessages()
65+
mockInAppFetcher.mockInAppPayloadFromServer(internalApi: internalAPI, payload).onSuccess { _ in
66+
let messages = internalAPI.inAppManager.getInboxMessages()
6867
XCTAssertEqual(messages.count, 2)
6968
XCTAssertEqual(messages[0].messageId, "message2")
7069
XCTAssertEqual(messages[1].messageId, "message4")
@@ -80,7 +79,7 @@ class InboxTests: XCTestCase {
8079
let config = IterableConfig()
8180
config.logDelegate = AllLogDelegate()
8281

83-
IterableAPI.initializeForTesting(
82+
let internalAPI = IterableAPIInternal.initializeForTesting(
8483
config: config,
8584
inAppFetcher: mockInAppFetcher
8685
)
@@ -108,18 +107,18 @@ class InboxTests: XCTestCase {
108107
}
109108
""".toJsonDict()
110109

111-
mockInAppFetcher.mockInAppPayloadFromServer(payload).onSuccess { _ in
112-
let messages = IterableAPI.inAppManager.getInboxMessages()
110+
mockInAppFetcher.mockInAppPayloadFromServer(internalApi: internalAPI, payload).onSuccess { _ in
111+
let messages = internalAPI.inAppManager.getInboxMessages()
113112
XCTAssertEqual(messages.count, 2)
114113

115-
IterableAPI.inAppManager.set(read: true, forMessage: messages[1])
114+
internalAPI.inAppManager.set(read: true, forMessage: messages[1])
116115

117116
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
118117
XCTAssertEqual(messages[0].read, false)
119118
XCTAssertEqual(messages[1].read, true)
120119

121-
let unreadMessages = IterableAPI.inAppManager.getInboxMessages().filter { $0.read == false }
122-
XCTAssertEqual(IterableAPI.inAppManager.getUnreadInboxMessagesCount(), 1)
120+
let unreadMessages = internalAPI.inAppManager.getInboxMessages().filter { $0.read == false }
121+
XCTAssertEqual(internalAPI.inAppManager.getUnreadInboxMessagesCount(), 1)
123122
XCTAssertEqual(unreadMessages.count, 1)
124123
XCTAssertEqual(unreadMessages[0].read, false)
125124
expectation1.fulfill()
@@ -133,7 +132,7 @@ class InboxTests: XCTestCase {
133132
let expectation1 = expectation(description: "testReceiveReadMessage")
134133
let mockInAppFetcher = MockInAppFetcher()
135134

136-
IterableAPI.initializeForTesting(inAppFetcher: mockInAppFetcher)
135+
let internalAPI = IterableAPIInternal.initializeForTesting(inAppFetcher: mockInAppFetcher)
137136

138137
let payload = """
139138
{"inAppMessages": [{
@@ -147,8 +146,8 @@ class InboxTests: XCTestCase {
147146
}
148147
""".toJsonDict()
149148

150-
mockInAppFetcher.mockInAppPayloadFromServer(payload).onSuccess { _ in
151-
let messages = IterableAPI.inAppManager.getInboxMessages()
149+
mockInAppFetcher.mockInAppPayloadFromServer(internalApi: internalAPI, payload).onSuccess { _ in
150+
let messages = internalAPI.inAppManager.getInboxMessages()
152151

153152
if let msg = messages.first, msg.read {
154153
expectation1.fulfill()
@@ -166,7 +165,7 @@ class InboxTests: XCTestCase {
166165
let config = IterableConfig()
167166
config.logDelegate = AllLogDelegate()
168167

169-
IterableAPI.initializeForTesting(
168+
let internalAPI = IterableAPIInternal.initializeForTesting(
170169
config: config,
171170
inAppFetcher: mockInAppFetcher
172171
)
@@ -194,13 +193,13 @@ class InboxTests: XCTestCase {
194193
}
195194
""".toJsonDict()
196195

197-
mockInAppFetcher.mockInAppPayloadFromServer(payload).onSuccess { _ in
198-
let messages = IterableAPI.inAppManager.getInboxMessages()
196+
mockInAppFetcher.mockInAppPayloadFromServer(internalApi: internalAPI, payload).onSuccess { _ in
197+
let messages = internalAPI.inAppManager.getInboxMessages()
199198
XCTAssertEqual(messages.count, 2)
200199

201-
IterableAPI.inAppManager.remove(message: messages[0], location: .inbox, source: .inboxSwipe)
200+
internalAPI.inAppManager.remove(message: messages[0], location: .inbox, source: .inboxSwipe)
202201
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
203-
let newMessages = IterableAPI.inAppManager.getInboxMessages()
202+
let newMessages = internalAPI.inAppManager.getInboxMessages()
204203
XCTAssertEqual(newMessages.count, 1)
205204
expectation1.fulfill()
206205
}
@@ -215,6 +214,7 @@ class InboxTests: XCTestCase {
215214
let expectation3 = expectation(description: "Right url callback")
216215
let expectation4 = expectation(description: "wait for messages")
217216

217+
var internalAPI: IterableAPIInternal!
218218
let mockInAppFetcher = MockInAppFetcher()
219219

220220
let mockInAppDisplayer = MockInAppDisplayer()
@@ -228,15 +228,15 @@ class InboxTests: XCTestCase {
228228
mockUrlDelegate.callback = { url, _ in
229229
XCTAssertEqual(url.absoluteString, "https://someurl.com")
230230
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
231-
XCTAssertEqual(IterableAPI.inAppManager.getUnreadInboxMessagesCount(), 1)
231+
XCTAssertEqual(internalAPI.inAppManager.getUnreadInboxMessagesCount(), 1)
232232
expectation2.fulfill()
233233
}
234234
}
235235
let config = IterableConfig()
236236
config.urlDelegate = mockUrlDelegate
237237
config.logDelegate = AllLogDelegate()
238238

239-
IterableAPI.initializeForTesting(
239+
internalAPI = IterableAPIInternal.initializeForTesting(
240240
config: config,
241241
inAppFetcher: mockInAppFetcher,
242242
inAppDisplayer: mockInAppDisplayer
@@ -265,14 +265,14 @@ class InboxTests: XCTestCase {
265265
}
266266
""".toJsonDict()
267267

268-
mockInAppFetcher.mockInAppPayloadFromServer(payload).onSuccess { _ in
268+
mockInAppFetcher.mockInAppPayloadFromServer(internalApi: internalAPI, payload).onSuccess { _ in
269269
expectation4.fulfill()
270270
}
271271
wait(for: [expectation4], timeout: testExpectationTimeout)
272-
XCTAssertEqual(IterableAPI.inAppManager.getUnreadInboxMessagesCount(), 2)
272+
XCTAssertEqual(internalAPI.inAppManager.getUnreadInboxMessagesCount(), 2)
273273

274-
let messages = IterableAPI.inAppManager.getInboxMessages()
275-
IterableAPI.inAppManager.show(message: messages[0], consume: false) { clickedUrl in
274+
let messages = internalAPI.inAppManager.getInboxMessages()
275+
internalAPI.inAppManager.show(message: messages[0], consume: false) { clickedUrl in
276276
XCTAssertEqual(clickedUrl!.absoluteString, "https://someurl.com")
277277
expectation3.fulfill()
278278
}
@@ -285,19 +285,20 @@ class InboxTests: XCTestCase {
285285
expectation1.expectedFulfillmentCount = 2
286286
let expectation2 = expectation(description: "testInboxNewMessagesCallback: finish payload processing")
287287

288+
var internalAPI: IterableAPIInternal!
288289
let mockInAppFetcher = MockInAppFetcher()
289290

290291
var callbackCount = 0
291292
let mockInAppDelegate = MockInAppDelegate(showInApp: .skip)
292293
let mockNotificationCenter = MockNotificationCenter()
293294
mockNotificationCenter.addCallback(forNotification: .iterableInboxChanged) {
294-
let messages = IterableAPI.inAppManager.getInboxMessages()
295+
let messages = internalAPI.inAppManager.getInboxMessages()
295296
if callbackCount == 0 {
296297
XCTAssertEqual(messages.count, 1)
297-
XCTAssertEqual(messages[0].messageId, "message0", "inboxMessages: \(IterableAPI.inAppManager.getInboxMessages())")
298+
XCTAssertEqual(messages[0].messageId, "message0", "inboxMessages: \(internalAPI.inAppManager.getInboxMessages())")
298299
} else {
299300
XCTAssertEqual(messages.count, 2)
300-
XCTAssertEqual(messages[1].messageId, "message1", "inboxMessages: \(IterableAPI.inAppManager.getInboxMessages())")
301+
XCTAssertEqual(messages[1].messageId, "message1", "inboxMessages: \(internalAPI.inAppManager.getInboxMessages())")
301302
}
302303
expectation1.fulfill()
303304
callbackCount += 1
@@ -307,7 +308,7 @@ class InboxTests: XCTestCase {
307308
config.inAppDelegate = mockInAppDelegate
308309
config.logDelegate = AllLogDelegate()
309310

310-
IterableAPI.initializeForTesting(
311+
internalAPI = IterableAPIInternal.initializeForTesting(
311312
config: config,
312313
inAppFetcher: mockInAppFetcher,
313314
notificationCenter: mockNotificationCenter
@@ -328,7 +329,7 @@ class InboxTests: XCTestCase {
328329
}
329330
""".toJsonDict()
330331

331-
mockInAppFetcher.mockInAppPayloadFromServer(payload).onSuccess { _ in
332+
mockInAppFetcher.mockInAppPayloadFromServer(internalApi: internalAPI, payload).onSuccess { _ in
332333
expectation2.fulfill()
333334
}
334335
wait(for: [expectation2], timeout: testExpectationTimeout)
@@ -356,7 +357,7 @@ class InboxTests: XCTestCase {
356357
}
357358
""".toJsonDict()
358359

359-
mockInAppFetcher.mockInAppPayloadFromServer(payload2)
360+
mockInAppFetcher.mockInAppPayloadFromServer(internalApi: internalAPI, payload2)
360361

361362
wait(for: [expectation1], timeout: testExpectationTimeout)
362363
}
@@ -383,22 +384,25 @@ class InboxTests: XCTestCase {
383384
persister.clear()
384385
persister.persist(messages)
385386

387+
var internalAPI: IterableAPIInternal!
386388
let config = IterableConfig()
387389
let mockInAppDelegate = MockInAppDelegate(showInApp: .skip)
388390

389391
let mockNotificationCenter = MockNotificationCenter()
390392
mockNotificationCenter.addCallback(forNotification: .iterableInboxChanged) {
391-
let messages = IterableAPI.inAppManager.getInboxMessages()
392-
XCTAssertEqual(messages.count, 1)
393-
expectation1.fulfill()
393+
DispatchQueue.main.async {
394+
let messages = internalAPI.inAppManager.getInboxMessages()
395+
XCTAssertEqual(messages.count, 1)
396+
expectation1.fulfill()
397+
}
394398
}
395399

396400
config.inAppDelegate = mockInAppDelegate
397401
config.logDelegate = AllLogDelegate()
398402

399403
let mockInAppFetcher = MockInAppFetcher(messages: messages)
400404

401-
IterableAPI.initializeForTesting(
405+
internalAPI = IterableAPIInternal.initializeForTesting(
402406
config: config,
403407
inAppFetcher: mockInAppFetcher,
404408
inAppPersister: persister,
@@ -417,6 +421,7 @@ class InboxTests: XCTestCase {
417421
let expectation3 = expectation(description: "payload 1 processed")
418422
let expectation4 = expectation(description: "payload 2 processed")
419423

424+
var internalAPI: IterableAPIInternal!
420425
let mockInAppFetcher = MockInAppFetcher()
421426

422427
var inAppCallbackCount = 0
@@ -434,23 +439,25 @@ class InboxTests: XCTestCase {
434439
var inboxCallbackCount = 0
435440
let mockNotificationCenter = MockNotificationCenter()
436441
mockNotificationCenter.addCallback(forNotification: .iterableInboxChanged) {
437-
let messages = IterableAPI.inAppManager.getInboxMessages()
438-
if inboxCallbackCount == 0 {
439-
XCTAssertEqual(messages.count, 1, "inboxMessages: \(IterableAPI.inAppManager.getInboxMessages())")
440-
XCTAssertEqual(messages[0].messageId, "message0")
441-
} else {
442-
XCTAssertEqual(messages.count, 2)
443-
XCTAssertEqual(messages[1].messageId, "message1")
442+
DispatchQueue.main.async {
443+
let messages = internalAPI.inAppManager.getInboxMessages()
444+
if inboxCallbackCount == 0 {
445+
XCTAssertEqual(messages.count, 1, "inboxMessages: \(internalAPI.inAppManager.getInboxMessages())")
446+
XCTAssertEqual(messages[0].messageId, "message0")
447+
} else {
448+
XCTAssertEqual(messages.count, 2)
449+
XCTAssertEqual(messages[1].messageId, "message1")
450+
}
451+
expectation1.fulfill()
452+
inboxCallbackCount += 1
444453
}
445-
expectation1.fulfill()
446-
inboxCallbackCount += 1
447454
}
448455

449456
let config = IterableConfig()
450457
config.inAppDelegate = mockInAppDelegate
451458
config.logDelegate = AllLogDelegate()
452459

453-
IterableAPI.initializeForTesting(
460+
internalAPI = IterableAPIInternal.initializeForTesting(
454461
config: config,
455462
inAppFetcher: mockInAppFetcher,
456463
notificationCenter: mockNotificationCenter
@@ -479,7 +486,7 @@ class InboxTests: XCTestCase {
479486
}
480487
""".toJsonDict()
481488

482-
mockInAppFetcher.mockInAppPayloadFromServer(payload).onSuccess { _ in
489+
mockInAppFetcher.mockInAppPayloadFromServer(internalApi: internalAPI, payload).onSuccess { _ in
483490
expectation3.fulfill()
484491
}
485492
wait(for: [expectation3], timeout: testExpectationTimeout)
@@ -523,7 +530,7 @@ class InboxTests: XCTestCase {
523530
}
524531
""".toJsonDict()
525532

526-
mockInAppFetcher.mockInAppPayloadFromServer(payload2).onSuccess { _ in
533+
mockInAppFetcher.mockInAppPayloadFromServer(internalApi: internalAPI, payload2).onSuccess { _ in
527534
expectation4.fulfill()
528535
}
529536

@@ -535,6 +542,7 @@ class InboxTests: XCTestCase {
535542
let expectation2 = expectation(description: "Unread count decrements after showing")
536543
let expectation3 = expectation(description: "testShowNowAndInboxMessage: wait for processing")
537544

545+
var internalAPI: IterableAPIInternal!
538546
let mockInAppFetcher = MockInAppFetcher()
539547

540548
let mockInAppDisplayer = MockInAppDisplayer()
@@ -548,16 +556,16 @@ class InboxTests: XCTestCase {
548556
mockUrlDelegate.callback = { url, _ in
549557
XCTAssertEqual(url.absoluteString, "https://someurl.com")
550558
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
551-
XCTAssertEqual(IterableAPI.inAppManager.getUnreadInboxMessagesCount(), 0)
552-
XCTAssertEqual(IterableAPI.inAppManager.getMessages().count, 2)
559+
XCTAssertEqual(internalAPI.inAppManager.getUnreadInboxMessagesCount(), 0)
560+
XCTAssertEqual(internalAPI.inAppManager.getMessages().count, 2)
553561
expectation2.fulfill()
554562
}
555563
}
556564
let config = IterableConfig()
557565
config.urlDelegate = mockUrlDelegate
558566
config.logDelegate = AllLogDelegate()
559567

560-
IterableAPI.initializeForTesting(
568+
internalAPI = IterableAPIInternal.initializeForTesting(
561569
config: config,
562570
inAppFetcher: mockInAppFetcher,
563571
inAppDisplayer: mockInAppDisplayer
@@ -586,7 +594,7 @@ class InboxTests: XCTestCase {
586594
}
587595
""".toJsonDict()
588596

589-
mockInAppFetcher.mockInAppPayloadFromServer(payload).onSuccess { _ in
597+
mockInAppFetcher.mockInAppPayloadFromServer(internalApi: internalAPI, payload).onSuccess { _ in
590598
expectation3.fulfill()
591599
}
592600
wait(for: [expectation3, expectation1, expectation2], timeout: testExpectationTimeout)
@@ -598,15 +606,16 @@ class InboxTests: XCTestCase {
598606
let expectation1 = expectation(description: "initial messages sent")
599607
let expectation2 = expectation(description: "inbox change notification is fired on logout")
600608

609+
var internalAPI: IterableAPIInternal!
601610
let mockInAppFetcher = MockInAppFetcher()
602611
let mockNotificationCenter = MockNotificationCenter()
603612

604-
IterableAPI.initializeForTesting(
613+
internalAPI = IterableAPIInternal.initializeForTesting(
605614
config: IterableConfig(),
606615
inAppFetcher: mockInAppFetcher,
607616
notificationCenter: mockNotificationCenter
608617
)
609-
IterableAPI.email = "[email protected]"
618+
internalAPI.email = "[email protected]"
610619

611620
let payload = """
612621
{"inAppMessages":
@@ -623,20 +632,20 @@ class InboxTests: XCTestCase {
623632
}
624633
""".toJsonDict()
625634

626-
mockInAppFetcher.mockInAppPayloadFromServer(payload).onSuccess { _ in
627-
XCTAssertEqual(IterableAPI.inAppManager.getMessages().count, 1)
635+
mockInAppFetcher.mockInAppPayloadFromServer(internalApi: internalAPI, payload).onSuccess { _ in
636+
XCTAssertEqual(internalAPI.inAppManager.getMessages().count, 1)
628637
expectation1.fulfill()
629638

630639
mockNotificationCenter.addCallback(forNotification: .iterableInboxChanged) {
631640
expectation2.fulfill()
632641
}
633-
IterableAPI.email = nil
642+
internalAPI.email = nil
634643
}
635644

636645
wait(for: [expectation1], timeout: testExpectationTimeout)
637646

638647
let predicate = NSPredicate { (_, _) -> Bool in
639-
IterableAPI.inAppManager.getMessages().count == 0
648+
internalAPI.inAppManager.getMessages().count == 0
640649
}
641650

642651
let expectation3 = expectation(for: predicate, evaluatedWith: nil, handler: nil)

0 commit comments

Comments
 (0)