| 
 | 1 | +//  | 
 | 2 | +//  ValidateStoredEventCheckUnknownToKnownUserTest.swift  | 
 | 3 | +//  unit-tests  | 
 | 4 | +//  | 
 | 5 | +//  Created by Apple on 20/09/24.  | 
 | 6 | +//  Copyright © 2024 Iterable. All rights reserved.  | 
 | 7 | +//  | 
 | 8 | + | 
 | 9 | +import XCTest  | 
 | 10 | +@testable import IterableSDK  | 
 | 11 | + | 
 | 12 | +final class ValidateStoredEventCheckUnknownToKnownUserTest: XCTestCase, AuthProvider  {  | 
 | 13 | +    private static let apiKey = "zeeApiKey"  | 
 | 14 | +    private let authToken = "asdf"  | 
 | 15 | +    private let dateProvider = MockDateProvider()  | 
 | 16 | +    let mockSession = MockNetworkSession(statusCode: 200)  | 
 | 17 | +    let localStorage = MockLocalStorage()  | 
 | 18 | + | 
 | 19 | +    var auth: Auth {  | 
 | 20 | +        Auth(userId: nil, email: nil, authToken: authToken, userIdAnon: nil)  | 
 | 21 | +    }  | 
 | 22 | + | 
 | 23 | +    override func setUp() {  | 
 | 24 | +        super.setUp()  | 
 | 25 | +    }  | 
 | 26 | + | 
 | 27 | +    func data(from jsonString: String) -> Data? {  | 
 | 28 | +        return jsonString.data(using: .utf8)  | 
 | 29 | +    }  | 
 | 30 | + | 
 | 31 | +    override func tearDown() {  | 
 | 32 | +           // Clean up after each test  | 
 | 33 | +           super.tearDown()  | 
 | 34 | +       }  | 
 | 35 | + | 
 | 36 | +    // Helper function to wait for a specified duration  | 
 | 37 | +    private func waitForDuration(seconds: TimeInterval) {  | 
 | 38 | +        let waitExpectation = expectation(description: "Waiting for \(seconds) seconds")  | 
 | 39 | +        DispatchQueue.main.asyncAfter(deadline: .now() + seconds) {  | 
 | 40 | +            waitExpectation.fulfill()  | 
 | 41 | +        }  | 
 | 42 | +        wait(for: [waitExpectation], timeout: seconds + 1)  | 
 | 43 | +    }  | 
 | 44 | + | 
 | 45 | +    func testCriteriaCustomEventCheck() {  // criteria not met with merge false with setUserId  | 
 | 46 | +        let config = IterableConfig()  | 
 | 47 | +        config.enableAnonTracking = true  | 
 | 48 | +        IterableAPI.initializeForTesting(apiKey: ValidateStoredEventCheckUnknownToKnownUserTest.apiKey,  | 
 | 49 | +                                                                   config: config,  | 
 | 50 | +                                                                   networkSession: mockSession,  | 
 | 51 | +                                                                   localStorage: localStorage)  | 
 | 52 | + | 
 | 53 | +        IterableAPI.track(event: "animal-found", dataFields: ["type": "cat", "count": 16, "vaccinated": true])  | 
 | 54 | +        IterableAPI.track(purchase: 10.0, items: [CommerceItem(id: "mocha", name: "Mocha", price: 10.0, quantity: 17, dataFields: nil)])  | 
 | 55 | +        IterableAPI.updateCart(items: [CommerceItem(id: "fdsafds", name: "sneakers", price: 4, quantity: 3, dataFields: ["timestemp_createdAt": Int(Date().timeIntervalSince1970)])])  | 
 | 56 | +        IterableAPI.track(event: "button-clicked", dataFields: ["lastPageViewed":"signup page", "timestemp_createdAt": Int(Date().timeIntervalSince1970)])  | 
 | 57 | +        waitForDuration(seconds: 3)  | 
 | 58 | + | 
 | 59 | +        IterableAPI.setUserId("testuser123")  | 
 | 60 | + | 
 | 61 | +        if let events = self.localStorage.anonymousUserEvents {  | 
 | 62 | +            XCTAssertFalse(events.isEmpty, "Expected events to be logged")  | 
 | 63 | +        } else {  | 
 | 64 | +           XCTFail("Expected events to be logged but found nil")  | 
 | 65 | +        }  | 
 | 66 | + | 
 | 67 | +        self.waitForDuration(seconds: 3)  | 
 | 68 | + | 
 | 69 | +        //Sync Completed  | 
 | 70 | +        if self.localStorage.anonymousUserEvents != nil {  | 
 | 71 | +            XCTFail("Expected local stored Event nil but found")  | 
 | 72 | +        } else {  | 
 | 73 | +            XCTAssertNil(self.localStorage.anonymousUserEvents, "Event found nil as event Sync Completed")  | 
 | 74 | +        }  | 
 | 75 | +    }  | 
 | 76 | + | 
 | 77 | + | 
 | 78 | +}  | 
0 commit comments