Skip to content

Commit

Permalink
🧪Added Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joaodordio committed Jan 9, 2025
1 parent 51a699d commit b92af1d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
4 changes: 4 additions & 0 deletions swift-sdk.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
00B6FACE210E88ED007535CF /* prod-1.mobileprovision in Resources */ = {isa = PBXBuildFile; fileRef = 00B6FACD210E874D007535CF /* prod-1.mobileprovision */; };
00B6FAD1210E8D90007535CF /* dev-1.mobileprovision in Resources */ = {isa = PBXBuildFile; fileRef = 00B6FAD0210E8D90007535CF /* dev-1.mobileprovision */; };
00CB31B621096129004ACDEC /* TestUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 00CB31B4210960C4004ACDEC /* TestUtils.swift */; };
092D01942D3038F600E3066A /* NotificationObserverTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 092D01932D3038F600E3066A /* NotificationObserverTests.swift */; };
1CBFFE1A2A97AEEF00ED57EE /* EmbeddedManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CBFFE162A97AEEE00ED57EE /* EmbeddedManagerTests.swift */; };
1CBFFE1B2A97AEEF00ED57EE /* EmbeddedMessagingProcessorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CBFFE172A97AEEE00ED57EE /* EmbeddedMessagingProcessorTests.swift */; };
1CBFFE1C2A97AEEF00ED57EE /* EmbeddedSessionManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CBFFE182A97AEEE00ED57EE /* EmbeddedSessionManagerTests.swift */; };
Expand Down Expand Up @@ -543,6 +544,7 @@
00B6FACD210E874D007535CF /* prod-1.mobileprovision */ = {isa = PBXFileReference; lastKnownFileType = file; path = "prod-1.mobileprovision"; sourceTree = "<group>"; };
00B6FAD0210E8D90007535CF /* dev-1.mobileprovision */ = {isa = PBXFileReference; lastKnownFileType = file; path = "dev-1.mobileprovision"; sourceTree = "<group>"; };
00CB31B4210960C4004ACDEC /* TestUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestUtils.swift; sourceTree = "<group>"; };
092D01932D3038F600E3066A /* NotificationObserverTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationObserverTests.swift; sourceTree = "<group>"; };
1CBFFE162A97AEEE00ED57EE /* EmbeddedManagerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EmbeddedManagerTests.swift; sourceTree = "<group>"; };
1CBFFE172A97AEEE00ED57EE /* EmbeddedMessagingProcessorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EmbeddedMessagingProcessorTests.swift; sourceTree = "<group>"; };
1CBFFE182A97AEEE00ED57EE /* EmbeddedSessionManagerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EmbeddedSessionManagerTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -936,6 +938,7 @@
552A0AA9280E249C00A80963 /* notification-tests */ = {
isa = PBXGroup;
children = (
092D01932D3038F600E3066A /* NotificationObserverTests.swift */,
55B37FC32297135F0042F13A /* NotificationMetadataTests.swift */,
AC2C667F20D31B1F00D46CC9 /* NotificationResponseTests.swift */,
);
Expand Down Expand Up @@ -2186,6 +2189,7 @@
5588DFD128C0465E000697D7 /* MockAPNSTypeChecker.swift in Sources */,
00B6FACC210E8484007535CF /* APNSTypeCheckerTests.swift in Sources */,
AC8F35A2239806B500302994 /* InboxViewControllerViewModelTests.swift in Sources */,
092D01942D3038F600E3066A /* NotificationObserverTests.swift in Sources */,
AC995F9A2166EEB50099A184 /* CommonMocks.swift in Sources */,
5588DFE128C046B7000697D7 /* MockLocalStorage.swift in Sources */,
1CBFFE1B2A97AEEF00ED57EE /* EmbeddedMessagingProcessorTests.swift in Sources */,
Expand Down
18 changes: 9 additions & 9 deletions tests/unit-tests/Mocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import XCTest

// Note: This is used only by swift tests. So can't put this in Common
class MockNotificationStateProvider: NotificationStateProviderProtocol {
func isNotificationsEnabled(withCallback callback: @escaping (Bool) -> Void) {
callback(enabled)
}

func registerForRemoteNotifications() {
expectation?.fulfill()
}
var enabled: Bool
private let expectation: XCTestExpectation?

init(enabled: Bool, expectation: XCTestExpectation? = nil) {
self.enabled = enabled
self.expectation = expectation
}

private let enabled: Bool
private let expectation: XCTestExpectation?
func isNotificationsEnabled(withCallback callback: @escaping (Bool) -> Void) {
callback(enabled)
}

func registerForRemoteNotifications() {
expectation?.fulfill()
}
}
44 changes: 44 additions & 0 deletions tests/unit-tests/NotificationObserverTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import XCTest
@testable import IterableSDK

class NotificationObserverTests: XCTestCase {
private var internalAPI: InternalIterableAPI!
private var mockNotificationStateProvider: MockNotificationStateProvider!
private var mockLocalStorage: MockLocalStorage!
private var mockNotificationCenter: MockNotificationCenter!

override func setUp() {
super.setUp()

mockNotificationStateProvider = MockNotificationStateProvider(enabled: false)
mockLocalStorage = MockLocalStorage()
mockNotificationCenter = MockNotificationCenter()

let config = IterableConfig()
internalAPI = InternalIterableAPI.initializeForTesting(
config: config,
notificationStateProvider: mockNotificationStateProvider,
localStorage: mockLocalStorage,
notificationCenter: mockNotificationCenter
)
}

func testNotificationStateChangeUpdatesStorage() {
// Arrange
mockLocalStorage.isNotificationsEnabled = false
mockNotificationStateProvider.enabled = true

// Act
mockNotificationCenter.post(name: UIApplication.didBecomeActiveNotification, object: nil, userInfo: nil)

// Small delay to allow async operation to complete
let expectation = XCTestExpectation(description: "Wait for state update")
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
expectation.fulfill()
}
wait(for: [expectation], timeout: 1.0)

// Assert
XCTAssertTrue(mockLocalStorage.isNotificationsEnabled)
}
}

0 comments on commit b92af1d

Please sign in to comment.