From bf6d4912030a9be119b9cf49024772378eda7bd1 Mon Sep 17 00:00:00 2001 From: RyosukeCla Date: Fri, 27 Oct 2023 19:38:37 +0900 Subject: [PATCH] add remote config test --- .../Nativebrik.xcodeproj/project.pbxproj | 4 + .../NativebrikTests/remote-config.swift | 104 ++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 ios/Nativebrik/NativebrikTests/remote-config.swift diff --git a/ios/Nativebrik/Nativebrik.xcodeproj/project.pbxproj b/ios/Nativebrik/Nativebrik.xcodeproj/project.pbxproj index 77949ed..4a3e9c9 100644 --- a/ios/Nativebrik/Nativebrik.xcodeproj/project.pbxproj +++ b/ios/Nativebrik/Nativebrik.xcodeproj/project.pbxproj @@ -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 */ @@ -109,6 +110,7 @@ C13634522AEB7D6600B9F437 /* YogaKit.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; name = YogaKit.xcframework; path = ../xcframeworks/YogaKit.xcframework; sourceTree = ""; }; C13634552AEB81EC00B9F437 /* experiment-test.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "experiment-test.swift"; sourceTree = ""; }; C136345F2AEBB49900B9F437 /* user.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = user.swift; sourceTree = ""; }; + C13634612AEBBC6500B9F437 /* remote-config.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "remote-config.swift"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -194,6 +196,7 @@ C13633AA2AEB769800B9F437 /* sdk.swift */, C13634552AEB81EC00B9F437 /* experiment-test.swift */, C136345F2AEBB49900B9F437 /* user.swift */, + C13634612AEBBC6500B9F437 /* remote-config.swift */, ); path = NativebrikTests; sourceTree = ""; @@ -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 */, ); diff --git a/ios/Nativebrik/NativebrikTests/remote-config.swift b/ios/Nativebrik/NativebrikTests/remote-config.swift new file mode 100644 index 0000000..c639312 --- /dev/null +++ b/ios/Nativebrik/NativebrikTests/remote-config.swift @@ -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() + } +}