-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from deskpro/feat/write-unit-tests
Unit tests
- Loading branch information
Showing
11 changed files
with
218 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
110 changes: 110 additions & 0 deletions
110
Tests/messenger-sdk-iosTests/DeskProSetup/DeskProSetupTest.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// | ||
// DeskproSetupTest.swift | ||
// | ||
// | ||
// Created by QSD BiH on 14. 2. 2024.. | ||
// | ||
|
||
import XCTest | ||
@testable import messenger_sdk_ios | ||
|
||
final class DeskproSetupTest: XCTestCase { | ||
|
||
var messenger: DeskPro! | ||
let appUrl = "test/url/data" | ||
let appId = "1" | ||
let vc = UIViewController() | ||
|
||
let user = User(name: "John Doe", firstName: "John", lastName: "Doe", email: "[email protected]") | ||
let jwtToken = "some-jwt-token" | ||
let deviceToken = "some-device-token" | ||
|
||
override func setUp() { | ||
let messengerConfig = MessengerConfig(appUrl: appUrl, appId: appId) | ||
messenger = DeskPro(messengerConfig: messengerConfig, containingViewController: vc) | ||
} | ||
|
||
override func tearDown() { | ||
messenger = nil | ||
} | ||
|
||
func testSetAndGetUserInfo() { | ||
messenger.setUserInfo(user: user) | ||
|
||
XCTAssertEqual(user, messenger.getUserInfo(), ErrorMessages.usersNotMatching) | ||
} | ||
|
||
func testSetAndGetUserInfoJson() { | ||
messenger.setUserInfo(user: user) | ||
|
||
if let jsonString = messenger.getUserInfoJson(), | ||
let jsonData = jsonString.data(using: .utf8) { | ||
let savedUser = try? JSONDecoder().decode(User.self, from: jsonData) | ||
XCTAssertEqual(user, savedUser, ErrorMessages.usersNotMatching) | ||
} | ||
} | ||
|
||
func testSetAndGetJwtToken() { | ||
messenger.authorizeUser(userJwt: jwtToken) | ||
|
||
XCTAssertEqual(jwtToken, messenger.getJwtToken(), ErrorMessages.tokensNotMatching) | ||
} | ||
|
||
func testSetAndGetPushRegistrationToken() { | ||
messenger.setPushRegistrationToken(token: deviceToken) | ||
|
||
XCTAssertEqual(deviceToken, messenger.getPushRegistrationToken(), ErrorMessages.tokensNotMatching) | ||
} | ||
|
||
func testClearData() { | ||
messenger.setUserInfo(user: user) | ||
messenger.authorizeUser(userJwt: jwtToken) | ||
messenger.setPushRegistrationToken(token: deviceToken) | ||
|
||
messenger.forgetUser() | ||
|
||
XCTAssertNil(messenger.getUserInfo(), ErrorMessages.userInfoNotNil) | ||
XCTAssertNil(messenger.getUserInfoJson(), ErrorMessages.userInfoJsonNotNil) | ||
XCTAssertNil(messenger.getJwtToken(), ErrorMessages.jwtTokenNotNil) | ||
XCTAssertNil(messenger.getPushRegistrationToken(), ErrorMessages.deviceTokenNotNil) | ||
} | ||
|
||
func testSetAndGetMultipleDeskProInstances() { | ||
let messengerConfig2 = MessengerConfig(appUrl: "some_url", appId: "2") | ||
let messenger2 = DeskPro(messengerConfig: messengerConfig2, containingViewController: vc) | ||
let user2 = User(name: "John Doe 2", firstName: "John 2", lastName: "Doe 2", email: "[email protected]") | ||
let jwtToken2 = "another-jwt-token" | ||
let deviceToken2 = "another-device-token" | ||
|
||
messenger.setUserInfo(user: user) | ||
messenger.authorizeUser(userJwt: jwtToken) | ||
messenger.setPushRegistrationToken(token: deviceToken) | ||
|
||
messenger2.setUserInfo(user: user2) | ||
messenger2.authorizeUser(userJwt: jwtToken2) | ||
messenger2.setPushRegistrationToken(token: deviceToken2) | ||
|
||
XCTAssertEqual(user2, messenger2.getUserInfo(), ErrorMessages.usersNotMatching) | ||
if let jsonString = messenger2.getUserInfoJson(), | ||
let jsonData = jsonString.data(using: .utf8) { | ||
let savedUser = try? JSONDecoder().decode(User.self, from: jsonData) | ||
XCTAssertEqual(user2, savedUser, ErrorMessages.usersNotMatching) | ||
} | ||
XCTAssertEqual(jwtToken2, messenger2.getJwtToken(), ErrorMessages.tokensNotMatching) | ||
XCTAssertEqual(deviceToken2, messenger2.getPushRegistrationToken(), ErrorMessages.tokensNotMatching) | ||
} | ||
|
||
func testPresentBuilder() { | ||
let presentBuilder = PresentBuilder(url: appUrl, appId: appId, coordinator: PresentCoordinator(containingViewController: vc, eventRouter: EventRouter())) | ||
XCTAssertEqual(appUrl, presentBuilder.getPath(), ErrorMessages.pathsNotMatching) | ||
|
||
_ = presentBuilder.chatHistory(1) | ||
XCTAssertEqual("\(appUrl)/chat_history/1", presentBuilder.getPath(), ErrorMessages.pathsNotMatching) | ||
|
||
_ = presentBuilder.article(1) | ||
XCTAssertEqual("\(appUrl)/chat_history/1/article/1", presentBuilder.getPath(), ErrorMessages.pathsNotMatching) | ||
|
||
_ = presentBuilder.comments() | ||
XCTAssertEqual("\(appUrl)/chat_history/1/article/1/comments", presentBuilder.getPath(), ErrorMessages.pathsNotMatching) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// | ||
// ErrorMessages.swift | ||
// | ||
// | ||
// Created by QSD BiH on 16. 2. 2024.. | ||
// | ||
|
||
import Foundation | ||
|
||
struct ErrorMessages { | ||
|
||
static var usersNotMatching = formatMessage("The users do not match.") | ||
static var tokensNotMatching = formatMessage("The tokens do not match.") | ||
static var pathsNotMatching = formatMessage("The paths do not match.") | ||
static var stringsNotMatching = formatMessage("The strings do not match.") | ||
static var userInfoNotNil = formatMessage("The user info is not nil.") | ||
static var userInfoJsonNotNil = formatMessage("The user info Json is not nil.") | ||
static var jwtTokenNotNil = formatMessage("The jwt token is not nil.") | ||
static var deviceTokenNotNil = formatMessage("The device token is not nil.") | ||
static var validNotificationData = formatMessage("The notification is valid.") | ||
static var invalidNotificationData = formatMessage("The notification is not valid.") | ||
|
||
private static func formatMessage(_ message: String) -> String { | ||
return "❌\(message)❌" | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
Tests/messenger-sdk-iosTests/PushNotifications/PushNotificationTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// PushNotificationTests.swift | ||
// | ||
// | ||
// Created by QSD BiH on 14. 2. 2024.. | ||
// | ||
|
||
import XCTest | ||
@testable import messenger_sdk_ios | ||
|
||
final class PushNotificationTests: XCTestCase { | ||
|
||
func testValidPushNotification() { | ||
let content = UNMutableNotificationContent() | ||
let userInfo = ["issuer" : "deskpro-messenger"] | ||
content.userInfo = userInfo as [AnyHashable : Any] | ||
|
||
XCTAssertTrue(DeskPro.isDeskProPushNotification(data: content.userInfo), ErrorMessages.invalidNotificationData) | ||
} | ||
|
||
func testInvalidPushNotification() { | ||
let content = UNMutableNotificationContent() | ||
let userInfo = ["issuer" : "some-messenger"] | ||
content.userInfo = userInfo as [AnyHashable : Any] | ||
|
||
XCTAssertFalse(DeskPro.isDeskProPushNotification(data: content.userInfo), ErrorMessages.validNotificationData) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
Tests/messenger-sdk-iosTests/SDKConnection/SDKConnectionTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// SDKConnectionTests.swift | ||
// | ||
// | ||
// Created by QSD BiH on 14. 2. 2024.. | ||
// | ||
|
||
import XCTest | ||
@testable import messenger_sdk_ios | ||
|
||
final class SDKConnectionTests: XCTestCase { | ||
|
||
func testConnectionWithSDK() { | ||
XCTAssertEqual(DeskPro.test(), "Hello world from Messenger!", ErrorMessages.stringsNotMatching) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.