Skip to content

Commit 20bd1ba

Browse files
committed
chore: added tests
1 parent fe8eb34 commit 20bd1ba

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

TestRunner/app/tests/ApiTests.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,52 @@ describe(module.id, function () {
314314
JSApi.new().methodError(1);
315315
}).toThrowError(/JS error/);
316316
});
317+
it("throws JS Error wrapping NSError when no error arg is passed", function () {
318+
var isThrown = false;
319+
try {
320+
// TNSApi.methodError(errorCode, error: NSError**)
321+
// Calling without the last interop.Reference should cause the runtime to
322+
// throw a JS Error that wraps the native NSError (for non-zero errorCode).
323+
TNSApi.new().methodError(1);
324+
} catch (e) {
325+
isThrown = true;
326+
327+
// Basic shape checks
328+
expect(e).toBeDefined();
329+
expect(e.message).toEqual(jasmine.any(String));
330+
expect(e.stack).toEqual(jasmine.any(String)); // proper JS stack present
331+
332+
// Fields we attach from the NSError
333+
expect(e.code).toBe(1);
334+
expect(e.domain).toBe("TNSErrorDomain");
335+
336+
// nativeException should be the wrapped NSError object
337+
expect(e.nativeException).toBeDefined();
338+
// The wrapped object should behave like an NSError proxy/wrapper
339+
// (we assert existence of localizedDescription property)
340+
expect(typeof e.nativeException.localizedDescription === "string" || e.nativeException.localizedDescription instanceof String).toBe(true);
341+
} finally {
342+
expect(isThrown).toBe(true);
343+
}
344+
});
345+
346+
it("does not throw when error arg is passed and the error ref is filled", function () {
347+
// When the caller passes an interop.Reference() as the last argument,
348+
// the runtime should not throw; it should return the method's boolean
349+
// result and write the NSError into the reference.
350+
var errorRef = new interop.Reference();
351+
var result = TNSApi.new().methodError(1, errorRef);
352+
353+
// The method returns false for non-zero error code
354+
expect(result).toBe(false);
355+
356+
// The errorRef should be populated with an NSError
357+
expect(errorRef.value instanceof NSError).toBe(true);
358+
359+
// Validate the NSError contents
360+
expect(errorRef.value.code).toBe(1);
361+
expect(errorRef.value.domain).toBe("TNSErrorDomain");
362+
});
317363

318364
// it("NSErrorExpose", function () {
319365
// var JSApi = TNSApi.extend({

0 commit comments

Comments
 (0)