UNPKG

es6-class-privates

Version:

Provides a method of converting underscored pseudo-private class members into truly private class members

96 lines (69 loc) 2.82 kB
'use strict'; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var classPrivates = require('..'); var testClass = function testClass(my) { var Test = function () { function Test() { _classCallCheck(this, Test); my.bindAllTo(this); } /* eslint-disable class-methods-use-this */ _createClass(Test, [{ key: '_privateMethod', value: function _privateMethod() {} }, { key: '$protectedMethod', value: function $protectedMethod() {} }, { key: 'publicMethod', value: function publicMethod() {} /* eslint-enable class-methods-use-this */ }]); return Test; }(); return my.restrict(Test); }; describe('Strictly private', function () { it('should make underscored functions private', function (done) { var my = classPrivates.makeMine(); var Test = testClass(my); var test = new Test(); expect(test._privateMethod).toBeUndefined(); // eslint-disable-line no-undef done(); }); it('should make underscored functions accessible via `my`', function (done) { var my = classPrivates.makeMine(); var Test = testClass(my); expect(my(Test).privateMethod).toBeInstanceOf(Function); // eslint-disable-line no-undef done(); }); it('should allow restricted access to private variables', function (done) { var my = classPrivates.makeMine(); var Test = function () { function Test() { _classCallCheck(this, Test); my.bindAllTo(this); my(this).privateVariable = 'test'; } _createClass(Test, [{ key: '_privateMethod', value: function _privateMethod() { return my(this).privateVariable; } }, { key: 'publicMethod', value: function publicMethod() { return my(this).privateMethod(); } }]); return Test; }(); var TestClass = my.restrict(Test); var test = new TestClass(); expect(test.publicMethod()).toBe('test'); // eslint-disable-line no-undef done(); }); }); describe('Running protection', function () {}); describe('Strange configurations', function () {});