UNPKG

clone-class

Version:

Clone an ES6 Class as Another Class Name for Isolating Class Static Properties.

60 lines 3.39 kB
#!/usr/bin/env -S node --no-warnings --loader ts-node/esm "use strict"; /** * Wechaty - https://github.com/chatie/wechaty * * @copyright 2016-2018 Huan LI <zixia@zixia.net> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ Object.defineProperty(exports, "__esModule", { value: true }); const tstest_1 = require("tstest"); const fixture_class_js_1 = require("../tests/fixtures/fixture-class.js"); const clone_class_js_1 = require("./clone-class.js"); (0, tstest_1.test)('cloneClass smoke testing', async (t) => { const EXPECTED_NUMBER1 = 1; const EXPECTED_NUMBER2 = 2; const NewClass1 = (0, clone_class_js_1.cloneClass)(fixture_class_js_1.FixtureClass); const NewClass2 = (0, clone_class_js_1.cloneClass)(fixture_class_js_1.FixtureClass); t.ok(NewClass1.prototype instanceof fixture_class_js_1.FixtureClass, 'should extend right'); t.not(NewClass1, NewClass2, 'NewClass1 should different with NewClass2'); t.not(NewClass1, fixture_class_js_1.FixtureClass, 'NewClass1 should different with FixtureClass'); NewClass1.staticMethod(EXPECTED_NUMBER1); t.equal(NewClass1.staticNumber, EXPECTED_NUMBER1, 'should set static number to EXPECTED_NUMBER1'); NewClass2.staticMethod(EXPECTED_NUMBER2); t.equal(NewClass2.staticNumber, EXPECTED_NUMBER2, 'should set static number to EXPECTED_NUMBER2'); const nc1 = new NewClass1(EXPECTED_NUMBER1, EXPECTED_NUMBER2); const nc2 = new NewClass2(EXPECTED_NUMBER1, EXPECTED_NUMBER2); t.ok(nc1 instanceof fixture_class_js_1.FixtureClass, 'nc1 should instanceof FixtureClass'); t.ok(nc1 instanceof NewClass1, 'nc1 should instanceof NewClass1'); t.equal(nc1.sum(), EXPECTED_NUMBER1 + EXPECTED_NUMBER1 + EXPECTED_NUMBER2, 'should sum right for 1 + 1 + 2'); t.equal(nc2.sum(), EXPECTED_NUMBER2 + EXPECTED_NUMBER1 + EXPECTED_NUMBER2, 'should sum right for 2 + 1 + 2'); }); (0, tstest_1.test)('cloneClass return NewClass with Original Name', async (t) => { const NewClass = (0, clone_class_js_1.cloneClass)(fixture_class_js_1.FixtureClass); t.equal(NewClass.name, fixture_class_js_1.FixtureClass.name, 'should clone the same name for Class'); }); (0, tstest_1.test)('throw error when lowercase static property initilized with defination', async (t) => { class Test { } Test.n = { mof: 42 }; t.throws(() => (0, clone_class_js_1.cloneClass)(Test), 'should throw when the static property initialized with a object in defination'); }); (0, tstest_1.test)('permit static property start with a captial letter to be initilized with defination', async (t) => { class Test { } Test.Data = { mof: 42 }; t.doesNotThrow(() => (0, clone_class_js_1.cloneClass)(Test), 'should not throw when the static property start with a captial letter that initialized with a object in defination'); }); //# sourceMappingURL=clone-class.spec.js.map