aboutsummaryrefslogtreecommitdiff
path: root/test/js-native-api/test_constructor/test.js
diff options
context:
space:
mode:
authorGabriel Schulhof <gabriel.schulhof@intel.com>2018-11-17 12:34:54 -0800
committerGabriel Schulhof <gabriel.schulhof@intel.com>2018-12-04 13:58:17 -0800
commit938e11882b96e19b443477571455088baaa054d8 (patch)
treeeb828a60957a2881995ba9a83f44a32a18fbff16 /test/js-native-api/test_constructor/test.js
parent83ee137c4565112177f22f2c735b266b22262220 (diff)
downloadandroid-node-v8-938e11882b96e19b443477571455088baaa054d8.tar.gz
android-node-v8-938e11882b96e19b443477571455088baaa054d8.tar.bz2
android-node-v8-938e11882b96e19b443477571455088baaa054d8.zip
test: partition N-API tests
Partition test/addons-napi into test/js-native-api and test/node-api to isolate the Node.js-agnostic portion of the N-API tests from the Node.js-specific portion. PR-URL: https://github.com/nodejs/node/pull/24557 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Diffstat (limited to 'test/js-native-api/test_constructor/test.js')
-rw-r--r--test/js-native-api/test_constructor/test.js50
1 files changed, 50 insertions, 0 deletions
diff --git a/test/js-native-api/test_constructor/test.js b/test/js-native-api/test_constructor/test.js
new file mode 100644
index 0000000000..616ba6c2a2
--- /dev/null
+++ b/test/js-native-api/test_constructor/test.js
@@ -0,0 +1,50 @@
+'use strict';
+const common = require('../../common');
+const assert = require('assert');
+
+// Testing api calls for a constructor that defines properties
+const TestConstructor = require(`./build/${common.buildType}/test_constructor`);
+const test_object = new TestConstructor();
+
+assert.strictEqual(test_object.echo('hello'), 'hello');
+
+test_object.readwriteValue = 1;
+assert.strictEqual(test_object.readwriteValue, 1);
+test_object.readwriteValue = 2;
+assert.strictEqual(test_object.readwriteValue, 2);
+
+assert.throws(() => { test_object.readonlyValue = 3; },
+ /^TypeError: Cannot assign to read only property 'readonlyValue' of object '#<MyObject>'$/);
+
+assert.ok(test_object.hiddenValue);
+
+// Properties with napi_enumerable attribute should be enumerable.
+const propertyNames = [];
+for (const name in test_object) {
+ propertyNames.push(name);
+}
+assert.ok(propertyNames.includes('echo'));
+assert.ok(propertyNames.includes('readwriteValue'));
+assert.ok(propertyNames.includes('readonlyValue'));
+assert.ok(!propertyNames.includes('hiddenValue'));
+assert.ok(!propertyNames.includes('readwriteAccessor1'));
+assert.ok(!propertyNames.includes('readwriteAccessor2'));
+assert.ok(!propertyNames.includes('readonlyAccessor1'));
+assert.ok(!propertyNames.includes('readonlyAccessor2'));
+
+// The napi_writable attribute should be ignored for accessors.
+test_object.readwriteAccessor1 = 1;
+assert.strictEqual(test_object.readwriteAccessor1, 1);
+assert.strictEqual(test_object.readonlyAccessor1, 1);
+assert.throws(() => { test_object.readonlyAccessor1 = 3; },
+ /^TypeError: Cannot assign to read only property 'readonlyAccessor1' of object '#<MyObject>'$/);
+test_object.readwriteAccessor2 = 2;
+assert.strictEqual(test_object.readwriteAccessor2, 2);
+assert.strictEqual(test_object.readonlyAccessor2, 2);
+assert.throws(() => { test_object.readonlyAccessor2 = 3; },
+ /^TypeError: Cannot assign to read only property 'readonlyAccessor2' of object '#<MyObject>'$/);
+
+// validate that static properties are on the class as opposed
+// to the instance
+assert.strictEqual(TestConstructor.staticReadonlyAccessor1, 10);
+assert.strictEqual(test_object.staticReadonlyAccessor1, undefined);