Skip to content

Commit aa069d2

Browse files
maickirahul-malik
authored andcommitted
Add basic Objective-C and JS bridging test (#124)
* Add basic Objective-C and JS bridging test * Add sending a json representation to JS layer * Move ObjC and JS bridge testing into it's own file
1 parent f09ae27 commit aa069d2

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import Foundation
2+
import JavaScriptCore
3+
import XCTest
4+
5+
@testable import Objective_C
6+
7+
class ObjcJavaScriptCoreBridgingTestSuite: XCTestCase {
8+
9+
static let _context: JSContext = JSContext()!
10+
11+
override class func setUp() {
12+
super.setUp();
13+
injectJSFileIntoContext(fileName: "bundle", context: _context);
14+
}
15+
16+
// Inject a js file located in plank/Examples/Shared/
17+
class func injectJSFileIntoContext(fileName: String, context: JSContext) {
18+
let jsFileName = "\(fileName).js"
19+
// The currentDirectoryPath is: plank/Examples/Cocoa/
20+
let currentPath = FileManager.default.currentDirectoryPath
21+
22+
let pathURL = URL(fileURLWithPath: currentPath)
23+
let finalPathURL = pathURL.appendingPathComponent("..")
24+
.appendingPathComponent("Shared")
25+
.appendingPathComponent(jsFileName)
26+
do {
27+
let bundleContents = try String(contentsOf: finalPathURL, encoding: .utf8)
28+
context.evaluateScript(bundleContents)
29+
} catch {
30+
print("Couldn't inject \(jsFileName) into JSContext")
31+
}
32+
}
33+
34+
var context: JSContext {
35+
return ObjcJavaScriptCoreBridgingTestSuite._context
36+
}
37+
38+
func testImageModelBridgingGetImage() {
39+
// Expected modelc dictionary
40+
let imageModelDictionary: JSONDict = [
41+
"height": 300,
42+
"width": 200,
43+
"url": "https://picsum.photos/200/300"
44+
]
45+
let expectedImage = Image(modelDictionary: imageModelDictionary);
46+
let expectedImageDictionaryRepresentation = expectedImage.dictionaryObjectRepresentation()
47+
48+
// Get image from js context
49+
let getTestImageModelJSONFunction = context.objectForKeyedSubscript("getTestImageModelJSON")!
50+
let jsImageModel = getTestImageModelJSONFunction.call(withArguments:[]).toObject() as! JSONDict
51+
52+
XCTAssert(expectedImage.isEqual(to: Image(modelDictionary:jsImageModel)))
53+
XCTAssert(jsImageModel == expectedImageDictionaryRepresentation)
54+
}
55+
56+
func testImageModelBridgingSendImage() {
57+
// Expected model dictionary
58+
let imageModelDictionary: JSONDict = [
59+
"height": 300,
60+
"width": 200,
61+
"url": "https://picsum.photos/200/300"
62+
]
63+
let imageModel = Image(modelDictionary: imageModelDictionary);
64+
let imageModelDictionaryRepresentation = imageModel.dictionaryObjectRepresentation()
65+
66+
let testImageModelJSON = context.objectForKeyedSubscript("sendTestImageModelJSON")!
67+
XCTAssert(testImageModelJSON.call(withArguments:[imageModelDictionaryRepresentation]).toBool())
68+
}
69+
}

Examples/Shared/bundle.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
// Some helper to throw exceptions within JS
4+
5+
function assert(condition, message) {
6+
if (!condition) {
7+
message = message || 'Assertion failed';
8+
if (typeof Error !== 'undefined') {
9+
throw new Error(message);
10+
}
11+
throw message; // Fallback
12+
}
13+
}
14+
15+
// Testing funtions
16+
17+
function getTestImageModelJSON() {
18+
return {
19+
height: 300,
20+
width: 200,
21+
url: 'https://picsum.photos/200/300',
22+
};
23+
}
24+
25+
function sendTestImageModelJSON(passed) {
26+
const expected = getTestImageModelJSON();
27+
28+
return (
29+
expected.height === passed.height &&
30+
expected.width === passed.width &&
31+
expected.url === passed.url
32+
);
33+
}

plank.xcodeproj/xcshareddata/xcschemes/plank-Package.xcscheme

Whitespace-only changes.

0 commit comments

Comments
 (0)