|
| 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 | +} |
0 commit comments