Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions DemoApp/REES46.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@
75E2378C2C334A5300119197 /* sdk */ = {
isa = PBXGroup;
children = (
D07DC5102E689AE500D27E7D /* init */,
D0B99FA12DB16C740012E4F0 /* unsubscribe */,
D0B99FA02DB16C5A0012E4F0 /* subscribe */,
D0B70C162D522DAF0001782A /* SearchServiceTests.swift */,
Expand Down Expand Up @@ -363,6 +364,13 @@
path = generators;
sourceTree = "<group>";
};
D07DC5102E689AE500D27E7D /* init */ = {
isa = PBXGroup;
children = (
);
path = init;
sourceTree = "<group>";
};
D0B99FA02DB16C5A0012E4F0 /* subscribe */ = {
isa = PBXGroup;
children = (
Expand Down
70 changes: 65 additions & 5 deletions DemoApp/Rees46DemoTests/SdkTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,64 @@ class SdkTests: XCTestCase {
var sdk: PersonalizationSDK?
let shopId = "357382bf66ac0ce2f1722677c59511"
let TAG = "SdkTests"


override func setUp() {
super.setUp()
sdk = nil
}

override func tearDown() {
sdk = nil
super.tearDown()
}

func trackEventAndCheck<T>(
eventId: String,
getValue: @escaping () -> T?,
checkValue: @escaping (T?) -> Bool,
failureMessage: String
) {
sdk?.track(event: .productView(id: eventId)) { (response) in
let expectation = XCTestExpectation(description: "Track event completion")
sdk?.track(event: .productView(id: eventId)) { response in
let value = getValue()
let result = checkValue(value)
print("\(self.TAG): Checking value \(String(describing: value)), result: \(result)")
XCTAssert(result, "\(self.TAG): \(failureMessage)")
expectation.fulfill()
}
let result = XCTWaiter.wait(for: [expectation], timeout: 10.0)
if result != .completed {
XCTFail("Timeout in trackEventAndCheck: \(result)")
}
}

func test_device_id_initialization() {
sdk = createPersonalizationSDK(
shopId: shopId,
apiDomain: Constants.testApiDomain, // Унифицируем с другими тестами
parentViewController: nil
)
trackEventAndCheck(
eventId: "123",
getValue: { self.sdk?.getDeviceId() },
checkValue: { deviceId in
!deviceId!.isEmpty
guard let deviceId = deviceId else { return false }
return !deviceId.isEmpty
},
failureMessage: "\(self.TAG): deviceId is empty after initialization"
)
}

func test_device_id_rewrite() {
sdk = createPersonalizationSDK(
shopId: shopId,
apiDomain: Constants.testApiDomain,
parentViewController: nil
)
let oldDeviceId = sdk?.getDeviceId()
sdk = createPersonalizationSDK(
shopId: shopId,
apiDomain: Constants.testApiDomain,
parentViewController: nil
)
trackEventAndCheck(
Expand All @@ -55,4 +75,44 @@ class SdkTests: XCTestCase {
failureMessage: "\(self.TAG): deviceId did not rewrite as expected"
)
}

func test_init_with_reinitialization_true_calls_completion() {
let expectation = XCTestExpectation(description: "Completion called with needReInitialization true")

sdk = createPersonalizationSDK(
shopId: shopId,
apiDomain: Constants.testApiDomain,
parentViewController: nil,
needReInitialization: true
) { error in
print("\(self.TAG): test_init_with_reinitialization_true_calls_completion: error = \(error?.localizedDescription ?? "nil")")
XCTAssertNil(error, "Initialization failed with error: \(error?.localizedDescription ?? "unknown")")
expectation.fulfill()
}

let result = XCTWaiter.wait(for: [expectation], timeout: 10.0)
if result != .completed {
XCTFail("Timeout in test_init_with_reinitialization_true_calls_completion: \(result)")
}
}

func test_init_with_reinitialization_false_calls_completion() {
let expectation = XCTestExpectation(description: "Completion called with needReInitialization false")

sdk = createPersonalizationSDK(
shopId: shopId,
apiDomain: Constants.testApiDomain,
parentViewController: nil,
needReInitialization: false
) { error in
print("\(self.TAG): test_init_with_reinitialization_false_calls_completion: error = \(error?.localizedDescription ?? "nil")")
XCTAssertNil(error, "Initialization failed with error: \(error?.localizedDescription ?? "unknown")")
expectation.fulfill()
}

let result = XCTWaiter.wait(for: [expectation], timeout: 10.0)
if result != .completed {
XCTFail("Timeout in test_init_with_reinitialization_false_calls_completion: \(result)")
}
}
}
97 changes: 54 additions & 43 deletions DemoApp/Rees46DemoTests/subscribe/SubscribeForBackInStock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,29 @@ class SubscribeForBackInStock: XCTestCase {
private var testEmail: String!
private var testPhone: String!

var sdk: PersonalizationSDK!
private var sdk: PersonalizationSDK!

override func setUp() {
super.setUp()

testEmail = MockGenerator.generateEmail()
testPhone = MockGenerator.generatePhoneNumber()

let expectation = XCTestExpectation(description: "SDK init")

sdk = createPersonalizationSDK(
shopId: Constants.testShopId,
apiDomain: Constants.testApiDomain,
enableLogs: true
)
enableLogs: true,
needReInitialization: true
) { error in
if let error = error {
XCTFail("SDK init failed: \(error.localizedDescription)")
}
expectation.fulfill()
}

wait(for: [expectation], timeout: Constants.defaultTimeout)
}

override func tearDown() {
Expand All @@ -30,17 +41,17 @@ class SubscribeForBackInStock: XCTestCase {

sdk.subscribeForBackInStock(
id: Constants.testItemId,
email: testEmail,
completion: { result in
switch result {
case .success:
print("Subscribe with email succeeded")
case .failure(let error):
XCTFail("Subscribe with email failed: \(error.localizedDescription)")
}
expectation.fulfill()
email: testEmail
) { result in
switch result {
case .success:
print("Subscribe with email succeeded")
case .failure(let error):
XCTFail("Subscribe with email failed: \(error.localizedDescription)")
}
)
expectation.fulfill()
}

wait(for: [expectation], timeout: Constants.defaultTimeout)
}

Expand All @@ -49,17 +60,17 @@ class SubscribeForBackInStock: XCTestCase {

sdk.subscribeForBackInStock(
id: Constants.testItemId,
phone: testPhone,
completion: { result in
switch result {
case .success:
print("Subscribe with phone succeeded")
case .failure(let error):
XCTFail("Subscribe with phone failed: \(error.localizedDescription)")
}
expectation.fulfill()
phone: testPhone
) { result in
switch result {
case .success:
print("Subscribe with phone succeeded")
case .failure(let error):
XCTFail("Subscribe with phone failed: \(error.localizedDescription)")
}
)
expectation.fulfill()
}

wait(for: [expectation], timeout: Constants.defaultTimeout)
}

Expand All @@ -69,35 +80,35 @@ class SubscribeForBackInStock: XCTestCase {
sdk.subscribeForBackInStock(
id: Constants.testItemId,
email: testEmail,
phone: testPhone,
completion: { result in
switch result {
case .success:
print("Subscribe with email and phone succeeded")
case .failure(let error):
XCTFail("Subscribe with email and phone failed: \(error.localizedDescription)")
}
expectation.fulfill()
phone: testPhone
) { result in
switch result {
case .success:
print("Subscribe with email+phone succeeded")
case .failure(let error):
XCTFail("Subscribe with email+phone failed: \(error.localizedDescription)")
}
)
expectation.fulfill()
}

wait(for: [expectation], timeout: Constants.defaultTimeout)
}

func testSubscribeForBackInStock_withoutContactInfo() {
let expectation = XCTestExpectation(description: "Subscribe without contact info")

sdk.subscribeForBackInStock(
id: Constants.testItemId,
completion: { result in
switch result {
case .success:
print("Subscribe with did succeeded")
case .failure(let error):
XCTFail("Subscribe with did failed: \(error.localizedDescription)")
}
expectation.fulfill()
id: Constants.testItemId
) { result in
switch result {
case .success:
print("Subscribe without contact info succeeded")
case .failure(let error):
XCTFail("Subscribe without contact info failed: \(error.localizedDescription)")
}
)
expectation.fulfill()
}

wait(for: [expectation], timeout: Constants.defaultTimeout)
}
}
Loading