From ae3cb484860c5afe4e6b6feedf6a0549c029e35b Mon Sep 17 00:00:00 2001 From: Peter Csajtai Date: Tue, 29 Aug 2023 15:03:46 +0200 Subject: [PATCH] Update ConfigCatClientTests.swift --- .../ConfigCatTests/ConfigCatClientTests.swift | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/Tests/ConfigCatTests/ConfigCatClientTests.swift b/Tests/ConfigCatTests/ConfigCatClientTests.swift index 540d5cf..919d553 100755 --- a/Tests/ConfigCatTests/ConfigCatClientTests.swift +++ b/Tests/ConfigCatTests/ConfigCatClientTests.swift @@ -551,6 +551,7 @@ class ConfigCatClientTests: XCTestCase { engine.enqueueResponse(response: Response(body: "", statusCode: 404)) var error = "" var changed = false + var ready = false let hooks = Hooks() let client = ConfigCatClient(sdkKey: "test", pollingMode: PollingModes.manualPoll(), httpEngine: engine, hooks: hooks) client.hooks.addOnError { e in @@ -559,6 +560,10 @@ class ConfigCatClientTests: XCTestCase { client.hooks.addOnConfigChanged { _ in changed = true } + client.hooks.addOnReady { state in + ready = true + XCTAssertEqual(ClientReadyState.noFlagData, state) + } let expectation = self.expectation(description: "wait for response") client.forceRefresh { r in @@ -574,9 +579,45 @@ class ConfigCatClientTests: XCTestCase { wait(for: [expectation2], timeout: 5) waitFor { - changed && error.starts(with: "Your SDK Key seems to be wrong. You can find the valid SDK Key at https://app.configcat.com/sdkkey.") && error.contains("404") + changed && ready && error.starts(with: "Your SDK Key seems to be wrong. You can find the valid SDK Key at https://app.configcat.com/sdkkey.") && error.contains("404") } } + + func testReadyHookGetValue() { + let engine = MockEngine() + engine.enqueueResponse(response: Response(body: String(format: testJsonFormat, "\"fake\""), statusCode: 200)) + let client = ConfigCatClient(sdkKey: "test", pollingMode: PollingModes.autoPoll(), httpEngine: engine) + + let expectation = self.expectation(description: "wait for response") + + client.hooks.addOnReady { state in + XCTAssertEqual(ClientReadyState.hasUpToDateFlagData, state) + client.getValue(for: "fakeKey", defaultValue: "") { val in + XCTAssertEqual("fake", val) + expectation.fulfill() + } + } + + wait(for: [expectation], timeout: 5) + } + + func testReadyHookGetValueSnapshot() { + let engine = MockEngine() + engine.enqueueResponse(response: Response(body: String(format: testJsonFormat, "\"fake\""), statusCode: 200)) + let client = ConfigCatClient(sdkKey: "test", pollingMode: PollingModes.autoPoll(), httpEngine: engine) + + let expectation = self.expectation(description: "wait for response") + + client.hooks.addOnReady { state in + XCTAssertEqual(ClientReadyState.hasUpToDateFlagData, state) + let snapshot = client.snapshot() + let val = snapshot.getValue(for: "fakeKey", defaultValue: "") + XCTAssertEqual("fake", val) + expectation.fulfill() + } + + wait(for: [expectation], timeout: 5) + } func testLazyCache() throws { let engine = MockEngine()