@@ -8,6 +8,7 @@ class KommunicateTests: XCTestCase {
88 static var showConversationsCalled = false
99 static var createConversationsCalled = false
1010 static var loggedIn = true
11+ static var conversationID : String ?
1112
1213 override class var isLoggedIn : Bool {
1314 return loggedIn
@@ -130,7 +131,206 @@ class KommunicateTests: XCTestCase {
130131 expectation. fulfill ( )
131132 }
132133 }
134+
135+ func testCreateAndLaunchConversationWithCustomData( ) {
136+ KommunicateMock . applozicClientType = ApplozicClientMock . self
137+ let expectation = self . expectation ( description: " Completion handler called " )
138+
139+ let kmConversation = KMConversationBuilder ( )
140+ . useLastConversation ( false )
141+ . setPreFilledMessage ( " This is a sample prefilled message. " )
142+ . withMetaData ( [ " TestMetadata " : " SampleValue " ] )
143+ . withConversationTitle ( " Automation Conversation " )
144+ . build ( )
145+
146+ if KommunicateMock . isLoggedIn {
147+ launchConversation ( kmConversation, expectation: expectation)
148+ } else {
149+ KommunicateMock . registerUserAsVistor { response, error in
150+ if let error = error {
151+ XCTFail ( " User registration failed: \( error. localizedDescription) " )
152+ expectation. fulfill ( )
153+ return
154+ }
155+ KommunicateMock . loggedIn = true
156+ self . launchConversation ( kmConversation, expectation: expectation)
157+ }
158+ }
159+ waitForExpectations ( timeout: 30 )
160+ }
161+
162+ private var testWindow : UIWindow ? // Property to retain UIWindow during the test lifecycle
163+
164+ private func launchConversation( _ conversation: KMConversation , expectation: XCTestExpectation ) {
165+ // Retain the UIWindow to prevent deallocation
166+ let dummyViewController = UIViewController ( )
167+ testWindow = UIWindow ( frame: UIScreen . main. bounds)
168+ testWindow? . rootViewController = dummyViewController
169+ testWindow? . makeKeyAndVisible ( )
170+
171+ // Launch the conversation
172+ KommunicateMock . launchConversation ( conversation: conversation, viewController: dummyViewController) { result in
173+ switch result {
174+ case . success( let conversationId) :
175+ print ( " Conversation id: " , conversationId)
176+ XCTAssertTrue ( true , " Conversation created successfully. " )
177+ case . failure( let kmConversationError) :
178+ XCTAssertNotNil ( kmConversationError, " Conversation creation failed " )
179+ XCTFail ( " Failed to create conversation. " )
180+ }
181+ expectation. fulfill ( )
182+ }
183+ }
184+
185+ func testUpdateConversationAssignee( ) {
186+ KommunicateMock . applozicClientType = ApplozicClientMock . self
187+ let expectation = self . expectation ( description: " Completion handler called " )
188+ let assigneeId = " alex-nwqih "
189+
190+ // Helper function to handle conversation update
191+ func updateConversation( with conversationId: String ) {
192+ let conversation = KMConversationBuilder ( )
193+ . withClientConversationId ( conversationId)
194+ . withConversationAssignee ( assigneeId)
195+ . build ( )
196+
197+ KommunicateMock . updateConversation ( conversation: conversation) { response in
198+ switch response {
199+ case . success:
200+ XCTAssertTrue ( true , " Conversation is updated successfully " )
201+ case . failure:
202+ XCTFail ( " Failed to update conversation " )
203+ }
204+ expectation. fulfill ( )
205+ }
206+ }
207+
208+ if let conversationId = KommunicateMock . conversationID {
209+ // If conversationID exists, update conversation
210+ updateConversation ( with: conversationId)
211+ } else {
212+ // Otherwise, create a new conversation and update
213+ let kmConversation = KMConversationBuilder ( )
214+ . useLastConversation ( false )
215+ . withMetaData ( [ " TestMetadata " : " SampleValue " ] )
216+ . withConversationTitle ( " Automation Conversation " )
217+ . build ( )
218+
219+ KommunicateMock . createConversation ( conversation: kmConversation) { [ weak self] result in
220+ switch result {
221+ case . success( let conversationId) :
222+ KommunicateMock . conversationID = conversationId
223+ KommunicateMock . loggedIn = true
224+ updateConversation ( with: conversationId)
225+ case . failure( let error) :
226+ XCTAssertNotNil ( error, " Conversation creation failed " )
227+ expectation. fulfill ( )
228+ }
229+ }
230+ }
231+
232+ waitForExpectations ( timeout: 30 )
233+ }
234+
235+ func testUpdateTeamID( ) {
236+ KommunicateMock . applozicClientType = ApplozicClientMock . self
237+ let expectation = self . expectation ( description: " Completion handler called " )
238+ let teamID = " 107732724 "
239+
240+ // Helper function to handle conversation update
241+ func updateConversation( with conversationId: String ) {
242+ let conversation = KMConversationBuilder ( )
243+ . withClientConversationId ( conversationId)
244+ . withTeamId ( teamID)
245+ . build ( )
246+
247+ KommunicateMock . updateConversation ( conversation: conversation) { response in
248+ switch response {
249+ case . success:
250+ XCTAssertTrue ( true , " Conversation is updated successfully " )
251+ case . failure:
252+ XCTFail ( " Failed to update conversation " )
253+ }
254+ expectation. fulfill ( )
255+ }
256+ }
257+
258+ if let conversationId = KommunicateMock . conversationID {
259+ // If conversationID exists, update conversation
260+ updateConversation ( with: conversationId)
261+ } else {
262+ // Otherwise, create a new conversation and update
263+ let kmConversation = KMConversationBuilder ( )
264+ . useLastConversation ( false )
265+ . withMetaData ( [ " TestMetadata " : " SampleValue " ] )
266+ . withConversationTitle ( " Automation Conversation " )
267+ . build ( )
268+
269+ KommunicateMock . createConversation ( conversation: kmConversation) { [ weak self] result in
270+ switch result {
271+ case . success( let conversationId) :
272+ KommunicateMock . conversationID = conversationId
273+ KommunicateMock . loggedIn = true
274+ updateConversation ( with: conversationId)
275+ case . failure( let error) :
276+ XCTAssertNotNil ( error, " Conversation creation failed " )
277+ expectation. fulfill ( )
278+ }
279+ }
280+ }
281+
282+ waitForExpectations ( timeout: 30 )
283+ }
284+
285+ func testUpdateConversationMetadata( ) {
286+ KommunicateMock . applozicClientType = ApplozicClientMock . self
287+ let expectation = self . expectation ( description: " Completion handler called " )
288+ let metaData = [ " name " : " Alice " , " city " : " London " , " hobby " : " Painting " ]
289+
290+ // Helper function to handle conversation update
291+ func updateConversation( with conversationId: String ) {
292+ let conversation = KMConversationBuilder ( )
293+ . withClientConversationId ( conversationId)
294+ . withMetaData ( metaData)
295+ . build ( )
296+
297+ KommunicateMock . updateConversation ( conversation: conversation) { response in
298+ switch response {
299+ case . success:
300+ XCTAssertTrue ( true , " Conversation is updated successfully " )
301+ case . failure:
302+ XCTFail ( " Failed to update conversation " )
303+ }
304+ expectation. fulfill ( )
305+ }
306+ }
133307
308+ if let conversationId = KommunicateMock . conversationID {
309+ // If conversationID exists, update conversation
310+ updateConversation ( with: conversationId)
311+ } else {
312+ // Otherwise, create a new conversation and update
313+ let kmConversation = KMConversationBuilder ( )
314+ . useLastConversation ( false )
315+ . withMetaData ( [ " TestMetadata " : " SampleValue " ] )
316+ . withConversationTitle ( " Automation Conversation " )
317+ . build ( )
318+
319+ KommunicateMock . createConversation ( conversation: kmConversation) { [ weak self] result in
320+ switch result {
321+ case . success( let conversationId) :
322+ KommunicateMock . conversationID = conversationId
323+ updateConversation ( with: conversationId)
324+ KommunicateMock . loggedIn = true
325+ case . failure( let error) :
326+ XCTAssertNotNil ( error, " Conversation creation failed " )
327+ expectation. fulfill ( )
328+ }
329+ }
330+ }
331+
332+ waitForExpectations ( timeout: 30 )
333+ }
134334
135335 func testSendMessageFunction( ) {
136336 KommunicateMock . applozicClientType = ApplozicClientMock . self
0 commit comments