Skip to content

Commit

Permalink
really try to fix the unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
themiswang committed Dec 7, 2023
1 parent 9d9b101 commit 366ec95
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion FirebaseRemoteConfig/Interop/RemoteConfigInterop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ import Foundation

@objc(FIRRemoteConfigInterop)
public protocol RemoteConfigInterop {
func registerRolloutsStateSubscriber(_ subscriber: RolloutsStateSubscriber?,
func registerRolloutsStateSubscriber(_ subscriber: RolloutsStateSubscriber,
for namespace: String)
}
4 changes: 4 additions & 0 deletions FirebaseRemoteConfig/Sources/FIRRemoteConfigComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ NS_ASSUME_NONNULL_BEGIN
/// Cached instances of Remote Config objects.
@property(nonatomic, strong) NSMutableDictionary<NSString *, FIRRemoteConfig *> *instances;

/// Clear all the component instances from the singleton which created previously, this is for
/// testing only
+ (void)clearAllComponentInstances;

/// Default method for retrieving a Remote Config instance, or creating one if it doesn't exist.
- (FIRRemoteConfig *)remoteConfigForNamespace:(NSString *)remoteConfigNamespace;

Expand Down
9 changes: 9 additions & 0 deletions FirebaseRemoteConfig/Sources/FIRRemoteConfigComponent.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

@implementation FIRRemoteConfigComponent

// Because Component now need to register two protocols (provider and interop), we need a way to
// return the same component instance for both registered protocol, this singleton pattern allow us
// to return the same component object for both registration callback.
static NSMutableDictionary<NSString *, FIRRemoteConfigComponent *> *_componentInstances = nil;

+ (FIRRemoteConfigComponent *)getComponentForApp:(FIRApp *)app {
Expand All @@ -40,6 +43,12 @@ + (FIRRemoteConfigComponent *)getComponentForApp:(FIRApp *)app {
return nil;
}

+ (void)clearAllComponentInstances {
@synchronized(_componentInstances) {
[_componentInstances removeAllObjects];
}
}

/// Default method for retrieving a Remote Config instance, or creating one if it doesn't exist.
- (FIRRemoteConfig *)remoteConfigForNamespace:(NSString *)remoteConfigNamespace {
if (!remoteConfigNamespace) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import XCTest
class MockRCInterop: RemoteConfigInterop {
weak var subscriber: FirebaseRemoteConfigInterop.RolloutsStateSubscriber?
func registerRolloutsStateSubscriber(_ subscriber: FirebaseRemoteConfigInterop
.RolloutsStateSubscriber?,
.RolloutsStateSubscriber,
for namespace: String) {
self.subscriber = subscriber
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ - (void)tearDown {

// Clear out any apps that were called with `configure`.
[FIRApp resetApps];
[FIRRemoteConfigComponent clearAllComponentInstances];
}

- (void)testRCInstanceCreationAndCaching {
Expand Down Expand Up @@ -93,7 +94,7 @@ - (void)testInitialization {
}

- (void)testRegistersAsLibrary {
// Now compoment has two register, one is provider and another one is Interop
// Now component has two register, one is provider and another one is Interop
XCTAssertEqual([FIRRemoteConfigComponent componentsToRegister].count, 2);

// Configure a test FIRApp for fetching an instance of the FIRRemoteConfigProvider.
Expand Down
2 changes: 2 additions & 0 deletions FirebaseRemoteConfig/Tests/Unit/RCNRemoteConfigTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#import <OCMock/OCMock.h>
#import <XCTest/XCTest.h>

#import "FirebaseRemoteConfig/Sources/FIRRemoteConfigComponent.h"
#import "FirebaseRemoteConfig/Sources/Private/FIRRemoteConfig_Private.h"
#import "FirebaseRemoteConfig/Sources/Private/RCNConfigFetch.h"
#import "FirebaseRemoteConfig/Sources/Public/FirebaseRemoteConfig/FIRRemoteConfig.h"
Expand Down Expand Up @@ -286,6 +287,7 @@ __unsafe_unretained void (^handler)(FIRRemoteConfigFetchStatus status,

- (void)tearDown {
[_DBManager removeDatabaseOnDatabaseQueueAtPath:_DBPath];
[FIRRemoteConfigComponent clearAllComponentInstances];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:_userDefaultsSuiteName];
[_DBManagerMock stopMocking];
_DBManagerMock = nil;
Expand Down

0 comments on commit 366ec95

Please sign in to comment.