@@ -69,24 +69,22 @@ class InAppTests: XCTestCase {
69
69
expectation1. fulfill ( )
70
70
}
71
71
72
- var internalApi : IterableAPIInternal !
73
72
let config = IterableConfig ( )
74
73
let mockUrlDelegate = MockUrlDelegate ( returnValue: true )
75
74
mockUrlDelegate. callback = { _, _ in
76
- XCTAssertEqual ( internalApi. inAppManager. getMessages ( ) . count, 0 )
77
75
expectation2. fulfill ( )
78
76
}
79
77
config. urlDelegate = mockUrlDelegate
80
78
81
- internalApi = IterableAPIInternal . initializeForTesting (
79
+ let internalApi = IterableAPIInternal . initializeForTesting (
82
80
config: config,
83
81
inAppFetcher: mockInAppFetcher,
84
82
inAppDisplayer: mockInAppDisplayer
85
83
)
86
84
87
- mockInAppFetcher. mockInAppPayloadFromServer ( internalApi: internalApi, TestInAppPayloadGenerator . createPayloadWithUrl ( numMessages: 1 ) ) . onSuccess { _ in
85
+ mockInAppFetcher. mockInAppPayloadFromServer ( internalApi: internalApi, TestInAppPayloadGenerator . createPayloadWithUrl ( numMessages: 1 ) ) . onSuccess { [ weak internalApi ] _ in
88
86
// first message has been processed by now
89
- XCTAssertEqual ( internalApi. inAppManager. getMessages ( ) . count, 0 )
87
+ XCTAssertEqual ( internalApi? . inAppManager. getMessages ( ) . count, 0 )
90
88
expectation3. fulfill ( )
91
89
}
92
90
@@ -117,9 +115,9 @@ class InAppTests: XCTestCase {
117
115
inAppDisplayer: mockInAppDisplayer
118
116
)
119
117
120
- mockInAppFetcher. mockInAppPayloadFromServer ( internalApi: internalApi, TestInAppPayloadGenerator . createPayloadWithUrl ( numMessages: 1 ) ) . onSuccess { _ in
121
- XCTAssertEqual ( internalApi. inAppManager. getMessages ( ) . count, 1 )
122
- XCTAssertEqual ( internalApi. inAppManager. getMessages ( ) [ 0 ] . didProcessTrigger, true )
118
+ mockInAppFetcher. mockInAppPayloadFromServer ( internalApi: internalApi, TestInAppPayloadGenerator . createPayloadWithUrl ( numMessages: 1 ) ) . onSuccess { [ weak internalApi ] _ in
119
+ XCTAssertEqual ( internalApi? . inAppManager. getMessages ( ) . count, 1 )
120
+ XCTAssertEqual ( internalApi? . inAppManager. getMessages ( ) [ 0 ] . didProcessTrigger, true )
123
121
124
122
expectation2. fulfill ( )
125
123
}
@@ -206,7 +204,11 @@ class InAppTests: XCTestCase {
206
204
inAppDisplayer: mockInAppDisplayer
207
205
)
208
206
209
- mockInAppFetcher. mockInAppPayloadFromServer ( internalApi: internalApi, payload) . onSuccess { _ in
207
+ mockInAppFetcher. mockInAppPayloadFromServer ( internalApi: internalApi, payload) . onSuccess { [ weak internalApi] _ in
208
+ guard let internalApi = internalApi else {
209
+ XCTFail ( " Expected internalApi to be not nil " )
210
+ return
211
+ }
210
212
let messages = internalApi. inAppManager. getMessages ( )
211
213
XCTAssertEqual ( messages. count, 3 )
212
214
XCTAssertEqual ( Set ( messages. map { $0. didProcessTrigger } ) , Set ( [ true , true , true ] ) )
@@ -222,11 +224,9 @@ class InAppTests: XCTestCase {
222
224
func testAutoShowInAppOpenUrlByDefault( ) {
223
225
let expectation1 = expectation ( description: " testAutoShowInAppOpenUrlByDefault " )
224
226
225
- var internalApi : IterableAPIInternal !
226
227
let mockInAppFetcher = MockInAppFetcher ( )
227
228
let mockUrlOpener = MockUrlOpener { url in
228
229
XCTAssertEqual ( url, TestInAppPayloadGenerator . getClickedUrl ( index: 1 ) )
229
- XCTAssertEqual ( internalApi. inAppManager. getMessages ( ) . count, 0 )
230
230
expectation1. fulfill ( )
231
231
}
232
232
@@ -235,7 +235,7 @@ class InAppTests: XCTestCase {
235
235
mockInAppDisplayer. click ( url: TestInAppPayloadGenerator . getClickedUrl ( index: 1 ) )
236
236
}
237
237
238
- internalApi = IterableAPIInternal . initializeForTesting (
238
+ let internalApi = IterableAPIInternal . initializeForTesting (
239
239
inAppFetcher: mockInAppFetcher,
240
240
inAppDisplayer: mockInAppDisplayer,
241
241
urlOpener: mockUrlOpener
@@ -363,7 +363,11 @@ class InAppTests: XCTestCase {
363
363
urlOpener: mockUrlOpener
364
364
)
365
365
366
- mockInAppFetcher. mockInAppPayloadFromServer ( internalApi: internalApi, TestInAppPayloadGenerator . createPayloadWithUrl ( numMessages: 1 ) ) . onSuccess { _ in
366
+ mockInAppFetcher. mockInAppPayloadFromServer ( internalApi: internalApi, TestInAppPayloadGenerator . createPayloadWithUrl ( numMessages: 1 ) ) . onSuccess { [ weak internalApi] _ in
367
+ guard let internalApi = internalApi else {
368
+ XCTFail ( " Expected internalApi to be not nil " )
369
+ return
370
+ }
367
371
let messages = internalApi. inAppManager. getMessages ( )
368
372
XCTAssertEqual ( messages. count, 1 )
369
373
@@ -374,13 +378,14 @@ class InAppTests: XCTestCase {
374
378
}
375
379
376
380
wait ( for: [ expectation1, expectation2] , timeout: testExpectationTimeout)
381
+
382
+ XCTAssertEqual ( internalApi. inAppManager. getMessages ( ) . count, 0 )
377
383
}
378
-
384
+
379
385
func testShowInAppWithNoConsume( ) {
380
- let expectation1 = expectation ( description: " testShowInAppWithNoConsume " )
386
+ let expectation1 = expectation ( description: " testShowInAppWithConsume " )
381
387
let expectation2 = expectation ( description: " url opened " )
382
388
383
- var internalApi : IterableAPIInternal !
384
389
let mockInAppFetcher = MockInAppFetcher ( )
385
390
386
391
let mockInAppDisplayer = MockInAppDisplayer ( )
@@ -390,41 +395,42 @@ class InAppTests: XCTestCase {
390
395
391
396
let mockUrlOpener = MockUrlOpener { url in
392
397
XCTAssertEqual ( url, TestInAppPayloadGenerator . getClickedUrl ( index: 1 ) )
393
-
394
- let messages = internalApi. inAppManager. getMessages ( )
395
- XCTAssertEqual ( messages. count, 1 )
396
- XCTAssertEqual ( messages [ 0 ] . didProcessTrigger, true )
397
398
expectation2. fulfill ( )
398
399
}
399
400
400
401
let config = IterableConfig ( )
401
402
config. inAppDelegate = MockInAppDelegate ( showInApp: . skip)
402
403
403
- internalApi = IterableAPIInternal . initializeForTesting (
404
+ let internalApi = IterableAPIInternal . initializeForTesting (
404
405
config: config,
405
406
inAppFetcher: mockInAppFetcher,
406
407
inAppDisplayer: mockInAppDisplayer,
407
408
urlOpener: mockUrlOpener
408
409
)
409
410
410
- mockInAppFetcher. mockInAppPayloadFromServer ( internalApi: internalApi, TestInAppPayloadGenerator . createPayloadWithUrl ( numMessages: 1 ) ) . onSuccess { _ in
411
+ mockInAppFetcher. mockInAppPayloadFromServer ( internalApi: internalApi, TestInAppPayloadGenerator . createPayloadWithUrl ( numMessages: 1 ) ) . onSuccess { [ weak internalApi] _ in
412
+ guard let internalApi = internalApi else {
413
+ XCTFail ( " Expected internalApi to be not nil " )
414
+ return
415
+ }
411
416
let messages = internalApi. inAppManager. getMessages ( )
412
- // Now show the first message, but don't consume
417
+ XCTAssertEqual ( messages. count, 1 )
418
+
413
419
internalApi. inAppManager. show ( message: messages [ 0 ] , consume: false ) { clickedUrl in
414
420
XCTAssertEqual ( clickedUrl, TestInAppPayloadGenerator . getClickedUrl ( index: 1 ) )
415
421
expectation1. fulfill ( )
416
422
}
417
423
}
418
424
419
425
wait ( for: [ expectation1, expectation2] , timeout: testExpectationTimeout)
426
+
427
+ XCTAssertEqual ( internalApi. inAppManager. getMessages ( ) . count, 1 )
420
428
}
421
-
429
+
422
430
func testShowInAppWithCustomAction( ) {
423
431
let expectation1 = expectation ( description: " testShowInAppWithCustomAction " )
424
432
let expectation2 = expectation ( description: " custom action called " )
425
- let expectation3 = expectation ( description: " count reduces " )
426
-
427
- var internalApi : IterableAPIInternal !
433
+
428
434
let mockInAppFetcher = MockInAppFetcher ( )
429
435
430
436
let mockInAppDisplayer = MockInAppDisplayer ( )
@@ -436,24 +442,24 @@ class InAppTests: XCTestCase {
436
442
mockCustomActionDelegate. callback = { customActionName, context in
437
443
XCTAssertEqual ( customActionName, TestInAppPayloadGenerator . getCustomActionName ( index: 1 ) )
438
444
XCTAssertEqual ( context. source, . inApp)
439
- DispatchQueue . main. asyncAfter ( deadline: . now( ) + 0.05 ) {
440
- XCTAssertEqual ( internalApi. inAppManager. getMessages ( ) . count, 0 )
441
- expectation3. fulfill ( )
442
- }
443
445
expectation2. fulfill ( )
444
446
}
445
447
446
448
let config = IterableConfig ( )
447
449
config. inAppDelegate = MockInAppDelegate ( showInApp: . skip)
448
450
config. customActionDelegate = mockCustomActionDelegate
449
451
450
- internalApi = IterableAPIInternal . initializeForTesting (
452
+ let internalApi = IterableAPIInternal . initializeForTesting (
451
453
config: config,
452
454
inAppFetcher: mockInAppFetcher,
453
455
inAppDisplayer: mockInAppDisplayer
454
456
)
455
457
456
- mockInAppFetcher. mockInAppPayloadFromServer ( internalApi: internalApi, TestInAppPayloadGenerator . createPayloadWithUrl ( numMessages: 1 ) ) . onSuccess { _ in
458
+ mockInAppFetcher. mockInAppPayloadFromServer ( internalApi: internalApi, TestInAppPayloadGenerator . createPayloadWithUrl ( numMessages: 1 ) ) . onSuccess { [ weak internalApi] _ in
459
+ guard let internalApi = internalApi else {
460
+ XCTFail ( " Expected internalApi to be not nil " )
461
+ return
462
+ }
457
463
let messages = internalApi. inAppManager. getMessages ( )
458
464
XCTAssertEqual ( messages. count, 1 , " expected 1 messages here " )
459
465
@@ -463,32 +469,26 @@ class InAppTests: XCTestCase {
463
469
}
464
470
}
465
471
466
- wait ( for: [ expectation1, expectation2, expectation3] , timeout: testExpectationTimeout)
472
+ wait ( for: [ expectation1, expectation2/*, expectation3*/] , timeout: testExpectationTimeout)
473
+ XCTAssertEqual ( internalApi. inAppManager. getMessages ( ) . count, 0 )
467
474
}
468
475
469
476
func testShowInAppWithIterableCustomActionDelete( ) {
470
- var internalApi : IterableAPIInternal !
471
- let expectation1 = expectation ( description: " correct number of messages " )
472
- let predicate = NSPredicate { ( _, _) -> Bool in
473
- internalApi. inAppManager. getMessages ( ) . count == 0
474
- }
475
- let expectation2 = expectation ( for: predicate, evaluatedWith: nil , handler: nil )
476
-
477
+ let expectation1 = expectation ( description: " message is shown " )
478
+
477
479
let mockInAppFetcher = MockInAppFetcher ( )
478
-
479
480
let iterableDeleteUrl = " iterable://delete "
480
481
let mockInAppDisplayer = MockInAppDisplayer ( )
481
482
mockInAppDisplayer. onShow. onSuccess { _ in
482
- let count = internalApi. inAppManager. getMessages ( ) . count
483
- XCTAssertEqual ( count, 1 )
484
483
mockInAppDisplayer. click ( url: URL ( string: iterableDeleteUrl) !)
484
+ expectation1. fulfill ( )
485
485
}
486
486
487
487
let config = IterableConfig ( )
488
488
config. inAppDelegate = MockInAppDelegate ( showInApp: . show)
489
489
config. logDelegate = AllLogDelegate ( )
490
490
491
- internalApi = IterableAPIInternal . initializeForTesting (
491
+ let internalApi = IterableAPIInternal . initializeForTesting (
492
492
config: config,
493
493
inAppFetcher: mockInAppFetcher,
494
494
inAppDisplayer: mockInAppDisplayer
@@ -509,12 +509,11 @@ class InAppTests: XCTestCase {
509
509
}
510
510
""" . toJsonDict ( )
511
511
512
- mockInAppFetcher. mockInAppPayloadFromServer ( internalApi: internalApi, payload) . onSuccess { _ in
513
- expectation1. fulfill ( )
514
- }
512
+ mockInAppFetcher. mockInAppPayloadFromServer ( internalApi: internalApi, payload)
515
513
516
514
wait ( for: [ expectation1] , timeout: testExpectationTimeout)
517
- wait ( for: [ expectation2] , timeout: testExpectationTimeout)
515
+
516
+ XCTAssertEqual ( internalApi. inAppManager. getMessages ( ) . count, 0 )
518
517
}
519
518
520
519
func testShowInAppWithIterableCustomActionDismiss( ) {
0 commit comments