mirror of
https://github.com/status-im/realm-js.git
synced 2025-01-11 06:46:03 +00:00
Support Promise-based async tests from Xcode tests
We already supported this for React Native and Node test runners. Now we support returning promises from tests everywhere.
This commit is contained in:
parent
b1db10e177
commit
56a0b903f9
@ -27,5 +27,6 @@
|
||||
|
||||
- (JSValue *)loadModuleFromURL:(NSURL *)url error:(NSError **)error;
|
||||
- (JSValue *)loadJSONFromURL:(NSURL *)url error:(NSError **)error;
|
||||
- (JSValue *)loadGlobalModule:(NSString *)name relativeToURL:(NSURL *)url error:(NSError **)error;
|
||||
|
||||
@end
|
||||
|
@ -195,6 +195,18 @@ static NSString * const RJSModuleLoaderErrorDomain = @"RJSModuleLoaderErrorDomai
|
||||
BOOL isDirectory;
|
||||
|
||||
if ([fileManager fileExistsAtPath:moduleURL.path isDirectory:&isDirectory] && isDirectory) {
|
||||
NSURL *packageURL = [moduleURL URLByAppendingPathComponent:@"package.json"];
|
||||
NSDictionary *package;
|
||||
|
||||
if ([fileManager fileExistsAtPath:packageURL.path]) {
|
||||
NSError *error;
|
||||
NSData *data = [NSData dataWithContentsOfURL:packageURL options:0 error:&error];
|
||||
|
||||
package = data ? [NSJSONSerialization JSONObjectWithData:data options:0 error:&error] : nil;
|
||||
NSAssert(package, @"%@", error);
|
||||
}
|
||||
|
||||
moduleURL = [moduleURL URLByAppendingPathComponent:package[@"main"] ?: @"index.js"];
|
||||
return [self loadModuleFromURL:moduleURL error:error];
|
||||
}
|
||||
|
||||
|
@ -31,12 +31,23 @@
|
||||
|
||||
+ (XCTestSuite *)defaultTestSuite {
|
||||
XCTestSuite *suite = [super defaultTestSuite];
|
||||
JSContext *context = [[JSContext alloc] init];
|
||||
|
||||
// We need a JS context from a UIWebView so it has setTimeout, Promise, etc.
|
||||
UIWebView *webView = [[UIWebView alloc] init];
|
||||
JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
|
||||
RJSModuleLoader *moduleLoader = [[RJSModuleLoader alloc] initWithContext:context];
|
||||
NSURL *realmURL = [[NSBundle bundleForClass:self] URLForResource:@"index" withExtension:@"js" subdirectory:@"lib"];
|
||||
NSURL *scriptURL = [[NSBundle bundleForClass:self] URLForResource:@"index" withExtension:@"js" subdirectory:@"js"];
|
||||
NSError *error;
|
||||
|
||||
// The ES6 global Promise constructor was added in iOS 8.
|
||||
if (![context[@"Promise"] isObject]) {
|
||||
JSValue *promiseModule = [moduleLoader loadGlobalModule:@"es6-promise" relativeToURL:scriptURL error:&error];
|
||||
NSAssert(promiseModule, @"%@", error);
|
||||
|
||||
context[@"Promise"] = promiseModule[@"Promise"];
|
||||
}
|
||||
|
||||
// Create Realm constructor in the JS context.
|
||||
RJSInitializeInContext(context.JSGlobalContextRef);
|
||||
|
||||
@ -73,10 +84,32 @@
|
||||
JSContext *context = testObject.context;
|
||||
context.exception = nil;
|
||||
|
||||
[testObject invokeMethod:@"runTest" withArguments:@[NSStringFromClass(self.class), method]];
|
||||
JSValue *promise = [testObject invokeMethod:@"runTest" withArguments:@[NSStringFromClass(self.class), method]];
|
||||
|
||||
JSValue *exception = context.exception;
|
||||
if (exception) {
|
||||
if (context.exception) {
|
||||
[self recordException:context.exception];
|
||||
return;
|
||||
}
|
||||
|
||||
if ([promise isObject]) {
|
||||
XCTestExpectation *expectation = [self expectationWithDescription:@"Promise resolved or rejected"];
|
||||
|
||||
JSValue *onFulfilled = [JSValue valueWithObject:^() {
|
||||
[expectation fulfill];
|
||||
} inContext:context];
|
||||
|
||||
JSValue *onRejected = [JSValue valueWithObject:^(JSValue *error) {
|
||||
[self recordException:error];
|
||||
[expectation fulfill];
|
||||
} inContext:context];
|
||||
|
||||
[promise invokeMethod:@"then" withArguments:@[onFulfilled, onRejected]];
|
||||
|
||||
[self waitForExpectationsWithTimeout:5.0 handler:NULL];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)recordException:(JSValue *)exception {
|
||||
JSValue *message = [exception hasProperty:@"message"] ? exception[@"message"] : exception;
|
||||
NSString *source = [exception hasProperty:@"sourceURL"] ? [exception[@"sourceURL"] toString] : nil;
|
||||
NSUInteger line = [exception hasProperty:@"line"] ? [exception[@"line"] toUInt32] - 1 : 0;
|
||||
@ -92,6 +125,5 @@
|
||||
atLine:sourceURL ? line : __LINE__
|
||||
expected:YES];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
@ -1,5 +1,8 @@
|
||||
{
|
||||
"name": "realm-tests",
|
||||
"version": "0.0.1",
|
||||
"private": true
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"es6-promise": "^3.2.1"
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user