Skip to content

Commit

Permalink
Add test to verify that JS responses work (marcuswestin#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcuswestin committed Feb 2, 2016
1 parent b59c15f commit 5e40fef
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
17 changes: 17 additions & 0 deletions Tests/WebViewJavascriptBridgeTests/BridgeTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,23 @@ - (void)classSpecificTestObjectEncoding:(Class)cls webView:(id)webView {
loadEchoSample(webView);
}

- (void)testJavascriptReceiveResponse {
[self classSpecificTestJavascriptReceiveResponse:[WebViewJavascriptBridge class] webView:_uiWebView];
[self classSpecificTestJavascriptReceiveResponse:[WKWebViewJavascriptBridge class] webView:_wkWebView];
[self waitForExpectationsWithTimeout:3 handler:NULL];
}
- (void)classSpecificTestJavascriptReceiveResponse:(Class)cls webView:(id)webView {
WebViewJavascriptBridge *bridge = [self bridgeForCls:cls webView:webView];
loadEchoSample(webView);
XCTestExpectation *callbackInvocked = [self expectationWithDescription:@"Callback invoked"];
[bridge registerHandler:@"objcEchoToJs" handler:^(id data, WVJBResponseCallback responseCallback) {
responseCallback(data);
}];
[bridge callHandler:@"jsRcvResponseTest" data:nil responseCallback:^(id responseData) {
XCTAssertEqualObjects(responseData, @"Response from JS");
[callbackInvocked fulfill];
}];
}

- (WebViewJavascriptBridge*)bridgeForCls:(Class)cls webView:(id)webView {
if (cls == [WebViewJavascriptBridge class]) {
Expand Down
13 changes: 11 additions & 2 deletions Tests/WebViewJavascriptBridgeTests/echo.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,19 @@
}

setupWebViewJavascriptBridge(function(bridge) {
WebViewJavascriptBridge.callHandler('Greet', 'Hello world');
WebViewJavascriptBridge.registerHandler('echoHandler', function(data, responseCallback) {
bridge.callHandler('Greet', 'Hello world');
bridge.registerHandler('echoHandler', function(data, responseCallback) {
responseCallback(data)
})
bridge.registerHandler('jsRcvResponseTest', function(data, responseCallback) {
bridge.callHandler('objcEchoToJs', { foo:'bar' }, function(response) {
if (response && response.foo == 'bar') {
responseCallback("Response from JS")
} else {
responseCallback("Failed")
}
})
})
})
</script>
</body></html>

0 comments on commit 5e40fef

Please sign in to comment.