Skip to content

Commit

Permalink
improved support for ready in RN apps (event is now sent after RN loa…
Browse files Browse the repository at this point in the history
…ds and not when detox initializes)
  • Loading branch information
talkol committed Sep 27, 2016
1 parent f514d91 commit 60b7405
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 4 deletions.
20 changes: 18 additions & 2 deletions detox/ios/Detox/DetoxManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ @interface DetoxManager()
@property (nonatomic, retain) WebSocket *websocket;
@property (nonatomic, retain) TestRunner *testRunner;
@property (nonatomic, retain) ReactNativeSupport *reactNativeSupport;
@property (nonatomic, assign) BOOL isReady;

@end

Expand All @@ -34,6 +35,7 @@ - (instancetype)init
self = [super init];
if (self == nil) return nil;

self.isReady = NO;
self.websocket = [[WebSocket alloc] init];
self.websocket.delegate = self;
self.testRunner = [[TestRunner alloc] init];
Expand All @@ -49,6 +51,15 @@ - (void) connectToServer:(NSString*)url withSessionId:(NSString*)sessionId
[self.websocket connectToServer:url withSessionId:sessionId];
}

- (void) websocketDidConnect
{
if (![self.reactNativeSupport isReactNativeApp])
{
self.isReady = YES;
[self.websocket sendAction:@"ready" withParams:@{}];
}
}

- (void) websocketDidReceiveAction:(NSString *)type withParams:(NSDictionary *)params
{
if ([type isEqualToString:@"invoke"])
Expand All @@ -59,7 +70,10 @@ - (void) websocketDidReceiveAction:(NSString *)type withParams:(NSDictionary *)p

if ([type isEqualToString:@"isReady"])
{
[self.websocket sendAction:@"ready" withParams:@{}];
if (self.isReady)
{
[self.websocket sendAction:@"ready" withParams:@{}];
}
return;
}

Expand All @@ -72,6 +86,7 @@ - (void) websocketDidReceiveAction:(NSString *)type withParams:(NSDictionary *)p

if ([type isEqualToString:@"reactNativeReload"])
{
self.isReady = NO;
[self.reactNativeSupport reloadApp];
return;
}
Expand Down Expand Up @@ -101,7 +116,8 @@ - (void)testRunnerOnError:(NSString *)error

- (void)reactNativeAppDidLoad
{
[self.websocket sendAction:@"reactNativeAppLoaded" withParams:@{}];
self.isReady = YES;
[self.websocket sendAction:@"ready" withParams:@{}];
}

@end
1 change: 1 addition & 0 deletions detox/ios/Detox/ReactNativeSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

@property (nonatomic, assign) id<ReactNativeSupportDelegate> delegate;

- (BOOL) isReactNativeApp;
- (void) reloadApp;

@end
Expand Down
5 changes: 5 additions & 0 deletions detox/ios/Detox/ReactNativeSupport.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ - (void)dealloc
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (BOOL) isReactNativeApp
{
return (NSClassFromString(@"RCTBridge") != nil);
}

- (void) reloadApp
{
[self removeIdlingResources];
Expand Down
1 change: 1 addition & 0 deletions detox/ios/Detox/WebSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

@protocol WebSocketDelegate <NSObject>

- (void)websocketDidConnect;
- (void)websocketDidReceiveAction:(NSString*)type withParams:(NSDictionary*)params;

@end
Expand Down
2 changes: 1 addition & 1 deletion detox/ios/Detox/WebSocket.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ - (void) receiveAction:(NSString*)json
- (void)webSocketDidOpen:(SRWebSocket *)webSocket
{
[self sendAction:@"login" withParams:@{@"sessionId": self.sessionId, @"role": @"testee"}];
[self sendAction:@"ready" withParams:@{}];
if (self.delegate) [self.delegate websocketDidConnect];
}

- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessageWithString:(NSString *)string
Expand Down
2 changes: 1 addition & 1 deletion detox/src/ios/simulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function _waitUntilReady(onReady) {
}

function reloadReactNativeApp(onLoad) {
websocket.waitForNextAction('reactNativeAppLoaded', onLoad);
websocket.waitForNextAction('ready', onLoad);
websocket.sendAction('reactNativeReload');
}

Expand Down

0 comments on commit 60b7405

Please sign in to comment.