@@ -27,17 +27,89 @@ with services provided by OneSignal.
2727
2828import XCTest
2929@testable import OneSignalInAppMessages
30+ import OneSignalOSCore
31+ import OneSignalUser
32+ import OneSignalCoreMocks
33+ import OneSignalOSCoreMocks
34+ import OneSignalUserMocks
35+ import OneSignalInAppMessagesMocks
3036
37+ /**
38+ These tests can include some Obj-C InAppMessagingIntegrationTests migrations.
39+ */
3140final class IAMIntegrationTests : XCTestCase {
3241 override func setUpWithError( ) throws {
33- // Put setup code here. This method is called before the invocation of each test method in the class.
42+ OneSignalCoreMocks . clearUserDefaults ( )
43+ OneSignalUserMocks . reset ( )
44+ OSConsistencyManager . shared. reset ( )
45+ // Temp. logging to help debug during testing
46+ OneSignalLog . setLogLevel ( . LL_VERBOSE)
3447 }
35- override func tearDownWithError( ) throws {
36- // Put teardown code here. This method is called after the invocation of each test method in the class.
48+
49+ override func tearDownWithError( ) throws { }
50+
51+ /**
52+ Test IAMs should display even when IAMs are paused.
53+ */
54+ func testPreviewIAMIsDisplayedOnPause( ) throws {
55+ /* Setup */
56+ OneSignalCoreImpl . setSharedClient ( MockOneSignalClient ( ) )
57+ // App ID is set because there are guards against nil App ID
58+ OneSignalConfigManager . setAppId ( " test-app-id " )
59+
60+ // 1. Pause IAMs
61+ OneSignalInAppMessages . __paused ( true )
62+
63+ // 2. Create a preview message
64+ let messageJson = IAMTestHelpers . testMessagePreviewJson ( )
65+ let message = OSInAppMessageInternal . instance ( withJson: messageJson)
66+ XCTAssertNotNil ( message, " Preview message should be created successfully " )
67+
68+ // 3. Present the preview message
69+ OSMessagingController . sharedInstance ( ) . present ( inAppPreviewMessage: message)
70+
71+ // 4. Verify that the preview IAM is showing even when paused
72+ XCTAssertTrue ( OSMessagingController . sharedInstance ( ) . isInAppMessageShowing)
3773 }
38- func testExample( ) throws {
39- OneSignalLog . setLogLevel ( . LL_VERBOSE)
40- OneSignalInAppMessages . getFromServer ( " foobar " )
41- // OSMessagingController.sharedInstance()
74+
75+ /**
76+ Pausing IAMs will not evaluate messages.
77+ */
78+ func testPausingIAMs_doesNotCreateMessageQueue( ) throws {
79+ /* Setup */
80+
81+ let client = MockOneSignalClient ( )
82+ OneSignalCoreImpl . setSharedClient ( client)
83+
84+ // 1. App ID is set because there are guards against nil App ID
85+ OneSignalConfigManager . setAppId ( " test-app-id " )
86+
87+ // 2. Set up mock responses for the anonymous user, as the user needs an OSID
88+ MockUserRequests . setDefaultCreateAnonUserResponses ( with: client)
89+
90+ // 3. Set up mock responses for fetching IAMs
91+ let message = IAMTestHelpers . testMessageJsonWithTrigger ( property: " session_time " , triggerId: " test_id1 " , type: 1 , value: 10.0 )
92+ let response = IAMTestHelpers . testFetchMessagesResponse ( messages: [ message] )
93+ client. setMockResponseForRequest (
94+ request: " <OSRequestGetInAppMessages from apps/test-app-id/subscriptions/ \( testPushSubId) /iams> " ,
95+ response: response)
96+
97+ // 4. Unblock the Consistency Manager to allow fetching of IAMs
98+ ConsistencyManagerTestHelpers . setDefaultRywToken ( id: anonUserOSID)
99+
100+ // 5. Pausing should prevent messages from being evaluated and shown
101+ OneSignalInAppMessages . __paused ( true )
102+
103+ // 6. Start the user manager to generate a user instance
104+ OneSignalUserManagerImpl . sharedInstance. start ( )
105+ OneSignalCoreMocks . waitForBackgroundThreads ( seconds: 0.5 )
106+
107+ // 7. Fetch IAMs
108+ OneSignalInAppMessages . getFromServer ( testPushSubId)
109+ OneSignalCoreMocks . waitForBackgroundThreads ( seconds: 0.5 )
110+
111+ // Make sure no IAM is showing, and the queue has no IAMs
112+ XCTAssertFalse ( OSMessagingController . sharedInstance ( ) . isInAppMessageShowing)
113+ XCTAssertEqual ( OSMessagingController . sharedInstance ( ) . messageDisplayQueue. count, 0 )
42114 }
43115}
0 commit comments