Skip to content

Commit

Permalink
add remote config test
Browse files Browse the repository at this point in the history
  • Loading branch information
RyosukeCla committed Oct 27, 2023
1 parent 8767244 commit bf6d491
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ios/Nativebrik/Nativebrik.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
C13634562AEB81EC00B9F437 /* experiment-test.swift in Sources */ = {isa = PBXBuildFile; fileRef = C13634552AEB81EC00B9F437 /* experiment-test.swift */; };
C136345E2AEBB15200B9F437 /* ViewInspector in Frameworks */ = {isa = PBXBuildFile; productRef = C136345D2AEBB15200B9F437 /* ViewInspector */; };
C13634602AEBB49900B9F437 /* user.swift in Sources */ = {isa = PBXBuildFile; fileRef = C136345F2AEBB49900B9F437 /* user.swift */; };
C13634622AEBBC6500B9F437 /* remote-config.swift in Sources */ = {isa = PBXBuildFile; fileRef = C13634612AEBBC6500B9F437 /* remote-config.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -109,6 +110,7 @@
C13634522AEB7D6600B9F437 /* YogaKit.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; name = YogaKit.xcframework; path = ../xcframeworks/YogaKit.xcframework; sourceTree = "<group>"; };
C13634552AEB81EC00B9F437 /* experiment-test.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "experiment-test.swift"; sourceTree = "<group>"; };
C136345F2AEBB49900B9F437 /* user.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = user.swift; sourceTree = "<group>"; };
C13634612AEBBC6500B9F437 /* remote-config.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "remote-config.swift"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -194,6 +196,7 @@
C13633AA2AEB769800B9F437 /* sdk.swift */,
C13634552AEB81EC00B9F437 /* experiment-test.swift */,
C136345F2AEBB49900B9F437 /* user.swift */,
C13634612AEBBC6500B9F437 /* remote-config.swift */,
);
path = NativebrikTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -362,6 +365,7 @@
buildActionMask = 2147483647;
files = (
C13634562AEB81EC00B9F437 /* experiment-test.swift in Sources */,
C13634622AEBBC6500B9F437 /* remote-config.swift in Sources */,
C13633AB2AEB769800B9F437 /* sdk.swift in Sources */,
C13634602AEBB49900B9F437 /* user.swift in Sources */,
);
Expand Down
104 changes: 104 additions & 0 deletions ios/Nativebrik/NativebrikTests/remote-config.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//
// remote-config.swift
// NativebrikTests
//
// Created by Ryosuke Suzuki on 2023/10/27.
//

import XCTest
import ViewInspector
import SwiftUI
@testable import Nativebrik

// https://nativebrik.com/experiments/result?projectId=ckto7v223akg00ag3jsg
let PROJECT_ID_FOR_TEST = "ckto7v223akg00ag3jsg"
// https://nativebrik.com/experiments/result?projectId=ckto7v223akg00ag3jsg&id=ckto9eq23akg00ag3jt0
let REMOTE_CONFIG_ID_1_FOR_TEST = "REMOTE_CONFIG_1"
let REMOTE_CONFIG_1_FOR_TEST_MESSAGE = "hello"
let UNKNOWN_EXPERIMENT_ID = "UNKNOWN_ID_XXXXXX"

final class RemoteConfigTests: XCTestCase {
func testRemoteConfigShouldFetch() {
let expectation = expectation(description: "Fetch remote config for test")

var didLoadingPhaseCome = false
let client = NativebrikClient(projectId: PROJECT_ID_FOR_TEST)
client.experiment.remoteConfig(REMOTE_CONFIG_ID_1_FOR_TEST) { phase in
switch phase {
case .completed(let variant):
let message = variant.getAsString("message")
XCTAssertEqual(message, REMOTE_CONFIG_1_FOR_TEST_MESSAGE)
expectation.fulfill()
case .loading:
didLoadingPhaseCome = true
case .failure:
XCTFail("should found the remote config")
expectation.fulfill()
}
}

waitForExpectations(timeout: 5) { error in
if let error = error {
XCTFail("waitForExpectationsWithTimeout errored: \(error)")
}
XCTAssertTrue(didLoadingPhaseCome)
}
}

func testRemoteConfigShouldNotFetch() {
let expectation = expectation(description: "Fetch non-exist remote config for test")

var didLoadingPhaseCome = false
let client = NativebrikClient(projectId: PROJECT_ID_FOR_TEST)
client.experiment.remoteConfig(UNKNOWN_EXPERIMENT_ID) { phase in
switch phase {
case .completed:
XCTFail("should found the remote config")
case .loading:
didLoadingPhaseCome = true
case .failure:
expectation.fulfill()
}
}

waitForExpectations(timeout: 5) { error in
if let error = error {
XCTFail("waitForExpectationsWithTimeout errored: \(error)")
}
XCTAssertTrue(didLoadingPhaseCome)
}
}
}

final class RemoteConfigAsViewTests: XCTestCase {

struct RemoteView: View {
@EnvironmentObject var nativebrik: NativebrikClient
var body: some View {
Group {
nativebrik
.experiment
.remoteConfigAsView(REMOTE_CONFIG_ID_1_FOR_TEST) { phase in
switch phase {
case .completed(let variant):
Text(variant.get("message") ?? "No")
default:
Text("Not Found")
}
}
}
}
}

struct ContentView: View {
var body: some View {
NativebrikProvider(client: NativebrikClient(projectId: PROJECT_ID_FOR_TEST)) {
RemoteView()
}
}
}

func testShouldFetch() throws {
let _ = ContentView()
}
}

0 comments on commit bf6d491

Please sign in to comment.