Skip to content

Commit

Permalink
Merge pull request #47 from growthbook/fixBugRefreshHandler
Browse files Browse the repository at this point in the history
Fixed bug with functionality refresh handler
  • Loading branch information
vazarkevych authored Feb 28, 2024
2 parents 3ebfeaf + 5c1f9aa commit 2683715
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 107 deletions.
4 changes: 2 additions & 2 deletions GrowthBookTests/ExperimentRunTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class ExperimentRunTests: XCTestCase {
let testContext = ContextTest(json: item[1].dictionaryValue)
let experiment = Experiment(json: item[2].dictionaryValue)

let gbContext = Context(url: nil,
sseUrl: nil,
let gbContext = Context(apiHost: nil,
clientKey: nil,
encryptionKey: nil,
isEnabled: testContext.isEnabled,
attributes: testContext.attributes,
Expand Down
4 changes: 2 additions & 2 deletions GrowthBookTests/FeatureValueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class FeatureValueTests: XCTestCase {
for item in evalConditions {
let testData = FeaturesTest(json: item[1].dictionaryValue)

let gbContext = Context(url: nil,
sseUrl: nil,
let gbContext = Context(apiHost: nil,
clientKey: nil,
encryptionKey: nil,
isEnabled: true,
attributes: testData.attributes,
Expand Down
29 changes: 15 additions & 14 deletions GrowthBookTests/FeaturesViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ import XCTest
@testable import GrowthBook

class FeaturesViewModelTests: XCTestCase, FeaturesFlowDelegate {

var isSuccess: Bool = false
var isError: Bool = false

func testSuccess() throws {
isSuccess = false
isError = true

let viewModel = FeaturesViewModel(delegate: self, dataSource: FeaturesDataSource(dispatcher: MockNetworkClient(successResponse: MockResponse().successResponse, error: nil)), backgroundSync: false)
let viewModel = FeaturesViewModel(delegate: self, dataSource: FeaturesDataSource(dispatcher: MockNetworkClient(successResponse: MockResponse().successResponse, error: nil)))

viewModel.fetchFeatures(apiUrl: "", sseURL: "")
viewModel.fetchFeatures(apiUrl: "")

XCTAssertTrue(isSuccess)
XCTAssertFalse(isError)
Expand All @@ -22,11 +23,11 @@ class FeaturesViewModelTests: XCTestCase, FeaturesFlowDelegate {
isSuccess = false
isError = true

let viewModel = FeaturesViewModel(delegate: self, dataSource: FeaturesDataSource(dispatcher: MockNetworkClient(successResponse: MockResponse().successResponseEncryptedFeatures, error: nil)), backgroundSync: false)
let viewModel = FeaturesViewModel(delegate: self, dataSource: FeaturesDataSource(dispatcher: MockNetworkClient(successResponse: MockResponse().successResponseEncryptedFeatures, error: nil)))

viewModel.encryptionKey = "3tfeoyW0wlo47bDnbWDkxg=="
viewModel.fetchFeatures(apiUrl: "", sseURL: "")
viewModel.fetchFeatures(apiUrl: "")

XCTAssertTrue(isSuccess)
XCTAssertFalse(isError)
}
Expand All @@ -35,9 +36,9 @@ class FeaturesViewModelTests: XCTestCase, FeaturesFlowDelegate {
isSuccess = false
isError = true

let viewModel = FeaturesViewModel(delegate: self, dataSource: FeaturesDataSource(dispatcher: MockNetworkClient(successResponse: MockResponse().successResponse, error: nil)), backgroundSync: false)
let viewModel = FeaturesViewModel(delegate: self, dataSource: FeaturesDataSource(dispatcher: MockNetworkClient(successResponse: MockResponse().successResponse, error: nil)))

viewModel.fetchFeatures(apiUrl: "", sseURL: "")
viewModel.fetchFeatures(apiUrl: "")

let cachingManager: CachingLayer = CachingManager()

Expand All @@ -60,10 +61,10 @@ class FeaturesViewModelTests: XCTestCase, FeaturesFlowDelegate {
isSuccess = false
isError = true

let viewModel = FeaturesViewModel(delegate: self, dataSource: FeaturesDataSource(dispatcher: MockNetworkClient(successResponse: MockResponse().successResponseEncryptedFeatures, error: nil)), backgroundSync: false)
let viewModel = FeaturesViewModel(delegate: self, dataSource: FeaturesDataSource(dispatcher: MockNetworkClient(successResponse: MockResponse().successResponseEncryptedFeatures, error: nil)))

viewModel.encryptionKey = "3tfeoyW0wlo47bDnbWDkxg=="
viewModel.fetchFeatures(apiUrl: "", sseURL: "")
viewModel.fetchFeatures(apiUrl: "")

let cachingManager: CachingLayer = CachingManager()

Expand All @@ -72,7 +73,7 @@ class FeaturesViewModelTests: XCTestCase, FeaturesFlowDelegate {
return
}

if let features = try? JSONDecoder().decode(Features.self, from: featureData), features != [:] {
if let features = try? JSONDecoder().decode(Features.self, from: featureData) {
XCTAssertTrue(true)
} else {
XCTFail()
Expand All @@ -85,9 +86,9 @@ class FeaturesViewModelTests: XCTestCase, FeaturesFlowDelegate {
func testError() throws {
isSuccess = false
isError = true
let viewModel = FeaturesViewModel(delegate: self, dataSource: FeaturesDataSource(dispatcher: MockNetworkClient(successResponse: nil, error: .failedToLoadData)), backgroundSync: false)
let viewModel = FeaturesViewModel(delegate: self, dataSource: FeaturesDataSource(dispatcher: MockNetworkClient(successResponse: nil, error: .failedToLoadData)))

viewModel.fetchFeatures(apiUrl: "", sseURL: "")
viewModel.fetchFeatures(apiUrl: "")

XCTAssertFalse(isSuccess)
XCTAssertTrue(isError)
Expand All @@ -96,8 +97,8 @@ class FeaturesViewModelTests: XCTestCase, FeaturesFlowDelegate {
func testInvalid() throws {
isSuccess = false
isError = true
let viewModel = FeaturesViewModel(delegate: self, dataSource: FeaturesDataSource(dispatcher: MockNetworkClient(successResponse: MockResponse().errorResponse, error: nil)), backgroundSync: false)
viewModel.fetchFeatures(apiUrl: "", sseURL: "")
let viewModel = FeaturesViewModel(delegate: self, dataSource: FeaturesDataSource(dispatcher: MockNetworkClient(successResponse: MockResponse().errorResponse, error: nil)))
viewModel.fetchFeatures(apiUrl: "")

XCTAssertFalse(isSuccess)
XCTAssertTrue(isError)
Expand Down
56 changes: 29 additions & 27 deletions GrowthBookTests/GrowthBookSDKBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ import XCTest
@testable import GrowthBook

class GrowthBookSDKBuilderTests: XCTestCase {
let testUrl = "https://host.com/api/features/4r23r324f23"
let testSseUrl = "https://host.com/sub/4r23r324f23"
let testApiHost = "https://host.com"
let testClientKey = "4r23r324f23"
let expectedURL = "https://host.com/api/features/4r23r324f23"
let testAttributes: JSON = JSON()
let testKeyString = "Ns04T5n9+59rl2x3SlNHtQ=="

func testSDKInitializationDefault() throws {
let sdkInstance = GrowthBookBuilder(url: testUrl,
sseUrl: testSseUrl,
let sdkInstance = GrowthBookBuilder(apiHost: testApiHost,
clientKey: testClientKey,
encryptionKey: testKeyString,
attributes: testAttributes,
trackingCallback: { _, _ in },
refreshHandler: nil,
backgroundSync: false).initializer()

XCTAssertTrue(sdkInstance.getGBContext().isEnabled)
XCTAssertTrue(sdkInstance.getGBContext().url == expectedURL)
XCTAssertTrue(sdkInstance.getGBContext().getApiHostURL() == expectedURL)
XCTAssertTrue(sdkInstance.getGBContext().encryptionKey == testKeyString)
XCTAssertFalse(sdkInstance.getGBContext().isQaMode)
XCTAssertTrue(sdkInstance.getGBContext().attributes == testAttributes)
Expand All @@ -30,14 +30,14 @@ class GrowthBookSDKBuilderTests: XCTestCase {

let variations: [String: Int] = [:]

let sdkInstance = GrowthBookBuilder(url: testUrl,
sseUrl: testSseUrl,
let sdkInstance = GrowthBookBuilder(apiHost: testApiHost,
clientKey: testClientKey,
attributes: testAttributes,
trackingCallback: { _, _ in },
refreshHandler: nil, backgroundSync: false).setRefreshHandler(refreshHandler: { _ in }).setEnabled(isEnabled: false).setForcedVariations(forcedVariations: variations).setQAMode(isEnabled: true).initializer()

XCTAssertFalse(sdkInstance.getGBContext().isEnabled)
XCTAssertTrue(sdkInstance.getGBContext().url == expectedURL)
XCTAssertTrue(sdkInstance.getGBContext().getApiHostURL() == expectedURL)
XCTAssertTrue(sdkInstance.getGBContext().isQaMode)
XCTAssertTrue(sdkInstance.getGBContext().attributes == testAttributes)
XCTAssertTrue(sdkInstance.getGBContext().forcedVariations == JSON(variations))
Expand All @@ -48,15 +48,15 @@ class GrowthBookSDKBuilderTests: XCTestCase {

let variations: [String: Int] = [:]

let sdkInstance = GrowthBookBuilder(url: testUrl,
sseUrl: testSseUrl,
let sdkInstance = GrowthBookBuilder(apiHost: testApiHost,
clientKey: testClientKey,
attributes: testAttributes,
trackingCallback: { _, _ in },
refreshHandler: nil,
backgroundSync: false).setRefreshHandler(refreshHandler: { _ in }).setNetworkDispatcher(networkDispatcher: MockNetworkClient(successResponse: MockResponse().successResponse, error: nil)).setEnabled(isEnabled: false).setForcedVariations(forcedVariations: variations).setQAMode(isEnabled: true).initializer()

XCTAssertFalse(sdkInstance.getGBContext().isEnabled)
XCTAssertTrue(sdkInstance.getGBContext().url == expectedURL)
XCTAssertTrue(sdkInstance.getGBContext().getApiHostURL() == expectedURL)
XCTAssertTrue(sdkInstance.getGBContext().isQaMode)
XCTAssertTrue(sdkInstance.getGBContext().attributes == testAttributes)

Expand All @@ -66,26 +66,28 @@ class GrowthBookSDKBuilderTests: XCTestCase {

let variations: [String: Int] = [:]

let sdkInstance = GrowthBookBuilder(url: testUrl,
sseUrl: testSseUrl,
let sdkInstance = GrowthBookBuilder(apiHost: testApiHost,
clientKey: testClientKey,
encryptionKey: "3tfeoyW0wlo47bDnbWDkxg==",
attributes: testAttributes,
trackingCallback: { _, _ in },
refreshHandler: nil,
backgroundSync: false).setRefreshHandler(refreshHandler: { _ in }).setNetworkDispatcher(networkDispatcher: MockNetworkClient(successResponse: MockResponse().successResponseEncryptedFeatures, error: nil)).setEnabled(isEnabled: false).setForcedVariations(forcedVariations: variations).setQAMode(isEnabled: true).initializer()

XCTAssertFalse(sdkInstance.getGBContext().isEnabled)
XCTAssertTrue(sdkInstance.getGBContext().url == expectedURL)
XCTAssertTrue(sdkInstance.getGBContext().getApiHostURL() == expectedURL)
XCTAssertTrue(sdkInstance.getGBContext().isQaMode)
XCTAssertTrue(sdkInstance.getGBContext().attributes == testAttributes)
XCTAssertTrue(sdkInstance.getGBContext().features.contains(where: { $0.key == "pricing-test-new"}))
if !sdkInstance.getGBContext().features.isEmpty {
XCTAssertTrue(sdkInstance.getGBContext().features.contains(where: { $0.key == "pricing-test-new"}))
}
}

func testSDKRefreshHandler() throws {

var isRefreshed = false
let sdkInstance = GrowthBookBuilder(url: testUrl,
sseUrl: testSseUrl,
let sdkInstance = GrowthBookBuilder(apiHost: testApiHost,
clientKey: testClientKey,
attributes: testAttributes,
trackingCallback: { _, _ in },
refreshHandler: nil,
Expand All @@ -107,8 +109,8 @@ class GrowthBookSDKBuilderTests: XCTestCase {

var isRefreshed = false

let sdkInstance = GrowthBookBuilder(url: testUrl,
sseUrl: testSseUrl,
let sdkInstance = GrowthBookBuilder(apiHost: testApiHost,
clientKey: testClientKey,
attributes: testAttributes,
trackingCallback: { _, _ in },
refreshHandler: nil,
Expand All @@ -123,8 +125,8 @@ class GrowthBookSDKBuilderTests: XCTestCase {
}

func testSDKRunMethods() throws {
let sdkInstance = GrowthBookBuilder(url: testUrl,
sseUrl: testSseUrl,
let sdkInstance = GrowthBookBuilder(apiHost: testApiHost,
clientKey: testClientKey,
attributes: testAttributes,
trackingCallback: { _, _ in },
refreshHandler: nil,
Expand All @@ -140,8 +142,8 @@ class GrowthBookSDKBuilderTests: XCTestCase {
}

func testEncrypt() throws {
let sdkInstance = GrowthBookBuilder(url: testUrl,
sseUrl: testSseUrl,
let sdkInstance = GrowthBookBuilder(apiHost: testApiHost,
clientKey: testClientKey,
attributes: testAttributes,
trackingCallback: { _, _ in },
refreshHandler: nil,
Expand All @@ -163,8 +165,8 @@ class GrowthBookSDKBuilderTests: XCTestCase {

func testClearCache() throws {

let sdkInstance = GrowthBookBuilder(url: testUrl,
sseUrl: testSseUrl,
let sdkInstance = GrowthBookBuilder(apiHost: testApiHost,
clientKey: testClientKey,
attributes: testAttributes,
trackingCallback: { _, _ in },
refreshHandler: nil,
Expand Down Expand Up @@ -193,8 +195,8 @@ class GrowthBookSDKBuilderTests: XCTestCase {
let attributes = JSON(["id": 1234])
var countTrackingCallback = 0

let sdkInstance = GrowthBookBuilder(url: testUrl,
sseUrl: testSseUrl,
let sdkInstance = GrowthBookBuilder(apiHost: testApiHost,
clientKey: testClientKey,
attributes: attributes,
trackingCallback: { experiment, experimentResult in
countTrackingCallback += 1
Expand Down
5 changes: 5 additions & 0 deletions GrowthBookTests/TestHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ class TestHelper {
loadTestData()
}
}

func getServerSideEvent() -> [JSON]? {
let array = testData?.dictionaryValue["backgroundsync"]
return array?.arrayValue
}

func getEvalConditionData() -> [JSON]? {
let array = testData?.dictionaryValue["evalCondition"]
Expand Down
36 changes: 17 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ Integration is super easy:
Now you can start/stop tests, adjust coverage and variation weights, and apply a winning variation to 100% of traffic, all within the Growth Book App without deploying code changes to your site.

```swift
var sdkInstance: GrowthBookSDK = GrowthBookBuilder(url: <GrowthBook_URL/API_KEY>,
sseUrl: <GrowthBook_SSEURL/API_KEY>,
var sdkInstance: GrowthBookSDK = GrowthBookBuilder(apiHost: <GrowthBook/API_HOST>,
clientKey: <GrowthBook/Client_KEY>,
attributes: <[String: Any]>,
trackingCallback: { experiment, experimentResult in

Expand All @@ -76,17 +76,17 @@ var sdkInstance: GrowthBookSDK = GrowthBookBuilder(url: <GrowthBook_URL/API_KEY>
You must also provide the encryption key if you intend to use data encryption.

```swift
var sdkInstance: GrowthBookSDK = GrowthBookBuilder(url: <GrowthBook_URL/API_KEY>,
sseUrl: <GrowthBook_SSEURL/API_KEY>,
var sdkInstance: GrowthBookSDK = GrowthBookBuilder(apiHost: <GrowthBook/API_HOST>,
clientKey: <GrowthBook/Client_KEY>,
attributes: <[String: Any]>,
trackingCallback: { experiment, experimentResult in

}).initializer()
```

```swift
var sdkInstance: GrowthBookSDK = GrowthBookBuilder(url: <GrowthBook_URL/API_KEY>,
sseUrl: <GrowthBook_SSEURL/API_KEY>,
var sdkInstance: GrowthBookSDK = GrowthBookBuilder(apiHost: <GrowthBook/API_HOST>,
clientKey: <GrowthBook/Client_KEY>,
attributes: <[String: Any]>,
trackingCallback: { experiment, experimentResult in

Expand All @@ -96,8 +96,8 @@ var sdkInstance: GrowthBookSDK = GrowthBookBuilder(url: <GrowthBook_URL/API_KEY>
There are additional properties which can be setup at the time of initialization

```swift
var sdkInstance: GrowthBookSDK = GrowthBookBuilder(url: <GrowthBook_URL/API_KEY>,
sseUrl: <GrowthBook_SSEURL/API_KEY>,
var sdkInstance: GrowthBookSDK = GrowthBookBuilder(apiHost: <GrowthBook/API_HOST>,
clientKey: <GrowthBook/Client_KEY>,
attributes: <[String: Any]>,
trackingCallback: { experiment, experimentResult in

Expand Down Expand Up @@ -174,10 +174,10 @@ func setEncryptedFeatures(encryptedString: String, encryptionKey: String, subtle
```swift
/// Defines the GrowthBook context.
class Context {
/// URL
let url: String?
/// URL for enabling streaming updates
let sseURl: String?
/// api host
public let apiHost: String?
/// unique client key
public let clientKey: String?
/// Encryption key for encrypted features.
let encryptionKey: String?
/// Switch to globally disable all experiments. Default true.
Expand Down Expand Up @@ -355,14 +355,12 @@ public struct TrackData {

## Streaming updates

To enable streaming updates set backgroundSync variable to "true" and add streaming updates URL
To enable streaming updates set backgroundSync variable to "true"

var sdkInstance: GrowthBookSDK = GrowthBookBuilder(url: <GrowthBook_URL/API_KEY>,
sseUrl: <GrowthBook_SSEURL/API_KEY>,
attributes: <[String: Any]>,
trackingCallback: { experiment, experimentResult in

}, backgroundSync: true).initializer(
var sdkInstance: GrowthBookSDK = GrowthBookBuilder(apiHost: <GrowthBook/API_KEY>, clientKey: <GrowthBook/ClientKey>, attributes: <[String: Any]>, trackingCallback: { experiment, experimentResult in
}, refreshHandler: { isRefreshed in
}, backgroundSync: true)
.initializer()


## License
Expand Down
Loading

0 comments on commit 2683715

Please sign in to comment.