Current Folder

This commit is contained in:
Luis Fernando Molina committed 2020-11-20 14:05:59 +01:00
1 parent 8b3382289b
commit cfcada880d
4379 files changed

No files matched your search

-127
View File
@@ -1,127 +0,0 @@
'use strict';
const {
Animal,
Plant,
Mammal,
Algae,
ExplicitWithoutNew,
ImplicitWithoutNew,
ImplicitExplicitWithoutNew,
} = require('./fixtures/es5');
it('should setup the prototype chain correctly', () => {
const animal = new Animal('mammal');
const plant = new Plant('algae');
expect(Object.getPrototypeOf(animal)).toBe(Animal.prototype);
expect(Object.getPrototypeOf(Object.getPrototypeOf(animal))).toBe(Animal.WrappedClass.prototype);
expect(Object.getPrototypeOf(animal)).not.toBe(Plant.prototype);
expect(Object.getPrototypeOf(plant)).toBe(Plant.prototype);
expect(Object.getPrototypeOf(Object.getPrototypeOf(plant))).toBe(Plant.WrappedClass.prototype);
expect(Object.getPrototypeOf(plant)).not.toBe(Animal.prototype);
expect(animal instanceof Animal).toBe(true);
expect(animal instanceof Animal.WrappedClass).toBe(true);
expect(animal instanceof Plant).toBe(false);
expect(plant instanceof Plant).toBe(true);
expect(plant instanceof Plant.WrappedClass).toBe(true);
expect(plant instanceof Animal).toBe(false);
expect(animal.getType()).toBe('mammal');
expect(plant.getType()).toBe('algae');
});
it('should have a custom toStringTag', () => {
expect(Object.prototype.toString.call(new Animal())).toBe('[object Animal]');
expect(Object.prototype.toString.call(new Plant())).toBe('[object Plant]');
});
describe('is<className> method', () => {
it('should add a working is<className> static method', () => {
const animal = new Animal('mammal');
const plant = new Plant('algae');
expect(Animal.isAnimal(animal)).toBe(true);
expect(Animal.isAnimal(plant)).toBe(false);
expect(Plant.isPlant(plant)).toBe(true);
expect(Plant.isPlant(animal)).toBe(false);
});
it('should not crash if `null` or `undefined` is passed to is<ClassName>', () => {
expect(Animal.isAnimal(null)).toBe(false);
expect(Animal.isAnimal(undefined)).toBe(false);
});
it('should work correctly for deep inheritance scenarios', () => {
const mammal = new Mammal();
const algae = new Algae();
expect(Mammal.isMammal(mammal)).toBe(true);
expect(Animal.isAnimal(mammal)).toBe(true);
expect(Mammal.isMammal(algae)).toBe(false);
expect(Animal.isAnimal(algae)).toBe(false);
expect(Algae.isAlgae(algae)).toBe(true);
expect(Plant.isPlant(algae)).toBe(true);
expect(Algae.isAlgae(mammal)).toBe(false);
expect(Plant.isPlant(mammal)).toBe(false);
});
});
describe('new operator', () => {
it('should work on explicit without-new handling', () => {
const instance = new ExplicitWithoutNew();
const instance2 = ExplicitWithoutNew(); // eslint-disable-line new-cap
expect(Object.getPrototypeOf(instance)).toBe(ExplicitWithoutNew.prototype);
expect(Object.getPrototypeOf(Object.getPrototypeOf(instance))).toBe(ExplicitWithoutNew.WrappedClass.prototype);
expect(Object.getPrototypeOf(instance2)).toBe(ExplicitWithoutNew.prototype);
expect(Object.getPrototypeOf(Object.getPrototypeOf(instance2))).toBe(ExplicitWithoutNew.WrappedClass.prototype);
expect(instance instanceof ExplicitWithoutNew).toBe(true);
expect(instance instanceof ExplicitWithoutNew.WrappedClass).toBe(true);
expect(instance2 instanceof ExplicitWithoutNew).toBe(true);
expect(instance2 instanceof ExplicitWithoutNew.WrappedClass).toBe(true);
expect(instance.getLabel()).toBe('ExplicitWithoutNew');
expect(instance2.getLabel()).toBe('ExplicitWithoutNew');
});
it('should work on implicit without-new handling', () => {
const instance = new ImplicitWithoutNew();
const instanceNoNew = ImplicitWithoutNew(); // eslint-disable-line new-cap
expect(Object.getPrototypeOf(instance)).toBe(ImplicitWithoutNew.prototype);
expect(Object.getPrototypeOf(Object.getPrototypeOf(instance))).toBe(ImplicitWithoutNew.WrappedClass.prototype);
expect(Object.getPrototypeOf(instanceNoNew)).toBe(ImplicitWithoutNew.prototype);
expect(Object.getPrototypeOf(Object.getPrototypeOf(instanceNoNew))).toBe(ImplicitWithoutNew.WrappedClass.prototype);
expect(instance instanceof ImplicitWithoutNew).toBe(true);
expect(instance instanceof ImplicitWithoutNew.WrappedClass).toBe(true);
expect(instanceNoNew instanceof ImplicitWithoutNew).toBe(true);
expect(instanceNoNew instanceof ImplicitWithoutNew.WrappedClass).toBe(true);
expect(instance.getLabel()).toBe('ImplicitWithoutNew');
expect(instanceNoNew.getLabel()).toBe('ImplicitWithoutNew');
});
it('should work on explicit & implicit without-new handling', () => {
const instance = new ImplicitExplicitWithoutNew();
const instanceNoNew = ImplicitExplicitWithoutNew(); // eslint-disable-line new-cap
expect(Object.getPrototypeOf(instance)).toBe(ImplicitExplicitWithoutNew.prototype);
expect(Object.getPrototypeOf(Object.getPrototypeOf(instance))).toBe(ImplicitExplicitWithoutNew.WrappedClass.prototype);
expect(Object.getPrototypeOf(instanceNoNew)).toBe(ImplicitExplicitWithoutNew.prototype);
expect(Object.getPrototypeOf(Object.getPrototypeOf(instanceNoNew))).toBe(ImplicitExplicitWithoutNew.WrappedClass.prototype);
expect(instance instanceof ImplicitExplicitWithoutNew).toBe(true);
expect(instance instanceof ImplicitExplicitWithoutNew.WrappedClass).toBe(true);
expect(instanceNoNew instanceof ImplicitExplicitWithoutNew).toBe(true);
expect(instanceNoNew instanceof ImplicitExplicitWithoutNew.WrappedClass).toBe(true);
expect(instance.getLabel()).toBe('ImplicitExplicitWithoutNew');
expect(instanceNoNew.getLabel()).toBe('ImplicitExplicitWithoutNew');
});
});
-67
View File
@@ -1,67 +0,0 @@
'use strict';
const {
Animal,
Plant,
Mammal,
Algae,
} = require('./fixtures/es6');
it('should setup the prototype chain correctly', () => {
const animal = new Animal('mammal');
const plant = new Plant('algae');
expect(Object.getPrototypeOf(animal)).toBe(Animal.prototype);
expect(Object.getPrototypeOf(Object.getPrototypeOf(animal))).toBe(Animal.WrappedClass.prototype);
expect(Object.getPrototypeOf(animal)).not.toBe(Plant.prototype);
expect(Object.getPrototypeOf(plant)).toBe(Plant.prototype);
expect(Object.getPrototypeOf(Object.getPrototypeOf(plant))).toBe(Plant.WrappedClass.prototype);
expect(Object.getPrototypeOf(plant)).not.toBe(Animal.prototype);
expect(animal instanceof Animal).toBe(true);
expect(animal instanceof Animal.WrappedClass).toBe(true);
expect(animal instanceof Plant).toBe(false);
expect(plant instanceof Plant).toBe(true);
expect(plant instanceof Plant.WrappedClass).toBe(true);
expect(plant instanceof Animal).toBe(false);
expect(animal.getType()).toBe('mammal');
expect(plant.getType()).toBe('algae');
});
it('should have a custom toStringTag', () => {
expect(Object.prototype.toString.call(new Animal())).toBe('[object Animal]');
expect(Object.prototype.toString.call(new Plant())).toBe('[object Plant]');
});
describe('is<className> method', () => {
it('should add a working is<className> static method', () => {
const animal = new Animal('mammal');
const plant = new Plant('algae');
expect(Animal.isAnimal(animal)).toBe(true);
expect(Animal.isAnimal(plant)).toBe(false);
expect(Plant.isPlant(plant)).toBe(true);
expect(Plant.isPlant(animal)).toBe(false);
});
it('should not crash if `null` or `undefined` is passed to is<ClassName>', () => {
expect(Animal.isAnimal(null)).toBe(false);
expect(Animal.isAnimal(undefined)).toBe(false);
});
it('should work correctly for deep inheritance scenarios', () => {
const mammal = new Mammal();
const algae = new Algae();
expect(Mammal.isMammal(mammal)).toBe(true);
expect(Animal.isAnimal(mammal)).toBe(true);
expect(Mammal.isMammal(algae)).toBe(false);
expect(Animal.isAnimal(algae)).toBe(false);
expect(Algae.isAlgae(algae)).toBe(true);
expect(Plant.isPlant(algae)).toBe(true);
expect(Algae.isAlgae(mammal)).toBe(false);
expect(Plant.isPlant(mammal)).toBe(false);
});
});
-17
View File
@@ -1,17 +0,0 @@
'use strict';
const withIs = require('../../..');
const Plant = require('./Plant');
function Algae() {
Plant.call(this, 'algae');
}
Algae.prototype = Object.create(Plant.prototype);
Algae.prototype.constructor = Algae;
module.exports = withIs.proto(Algae, {
className: 'Algae',
symbolName: '@org/package/Algae',
});
module.exports.WrappedClass = Algae;
-17
View File
@@ -1,17 +0,0 @@
'use strict';
const withIs = require('../../..');
function Animal(type) {
this.type = type;
}
Animal.prototype.getType = function () {
return this.type;
};
module.exports = withIs.proto(Animal, {
className: 'Animal',
symbolName: '@org/package/Animal',
});
module.exports.WrappedClass = Animal;
-23
View File
@@ -1,23 +0,0 @@
'use strict';
const withIs = require('../../..');
function ExplicitWithoutNew_() {
if (!(this instanceof ExplicitWithoutNew)) {
return new ExplicitWithoutNew();
}
this.label = 'ExplicitWithoutNew';
}
ExplicitWithoutNew_.prototype.getLabel = function () {
return this.label;
};
const ExplicitWithoutNew = withIs.proto(ExplicitWithoutNew_, {
className: 'ExplicitWithoutNew',
symbolName: '@org/package/ExplicitWithoutNew',
});
module.exports = ExplicitWithoutNew;
module.exports.WrappedClass = ExplicitWithoutNew_;
@@ -1,25 +0,0 @@
'use strict';
const withIs = require('../../..');
function ImplicitExplicitWithoutNew_() {
/* istanbul ignore if */
if (!(this instanceof ImplicitExplicitWithoutNew)) {
return new ImplicitExplicitWithoutNew();
}
this.label = 'ImplicitExplicitWithoutNew';
}
ImplicitExplicitWithoutNew_.prototype.getLabel = function () {
return this.label;
};
const ImplicitExplicitWithoutNew = withIs.proto(ImplicitExplicitWithoutNew_, {
className: 'ImplicitExplicitWithoutNew',
symbolName: '@org/package/ImplicitExplicitWithoutNew',
withoutNew: true,
});
module.exports = ImplicitExplicitWithoutNew;
module.exports.WrappedClass = ImplicitExplicitWithoutNew_;
-18
View File
@@ -1,18 +0,0 @@
'use strict';
const withIs = require('../../..');
function ImplicitWithoutNew() {
this.label = 'ImplicitWithoutNew';
}
ImplicitWithoutNew.prototype.getLabel = function () {
return this.label;
};
module.exports = withIs.proto(ImplicitWithoutNew, {
className: 'ImplicitWithoutNew',
symbolName: '@org/package/ImplicitWithoutNew',
withoutNew: true,
});
module.exports.WrappedClass = ImplicitWithoutNew;
-17
View File
@@ -1,17 +0,0 @@
'use strict';
const withIs = require('../../..');
const Animal = require('./Animal');
function Mammal() {
Animal.call(this, 'mammal');
}
Mammal.prototype = Object.create(Animal.prototype);
Mammal.prototype.constructor = Mammal;
module.exports = withIs.proto(Mammal, {
className: 'Mammal',
symbolName: '@org/package/Mammal',
});
module.exports.WrappedClass = Mammal;
-17
View File
@@ -1,17 +0,0 @@
'use strict';
const withIs = require('../../..');
function Plant(type) {
this.type = type;
}
Plant.prototype.getType = function () {
return this.type;
};
module.exports = withIs.proto(Plant, {
className: 'Plant',
symbolName: '@org/package/Plant',
});
module.exports.WrappedClass = Plant;
-12
View File
@@ -1,12 +0,0 @@
'use strict';
module.exports = {
Animal: require('./Animal'),
Plant: require('./Plant'),
Mammal: require('./Mammal'),
Algae: require('./Algae'),
ExplicitWithoutNew: require('./ExplicitWithoutNew'),
ImplicitWithoutNew: require('./ImplicitWithoutNew'),
ImplicitExplicitWithoutNew: require('./ImplicitExplicitWithoutNew'),
};
-16
View File
@@ -1,16 +0,0 @@
'use strict';
const withIs = require('../../..');
const Plant = require('./Plant');
class Algae extends Plant {
constructor() {
super('algae');
}
}
module.exports = withIs(Algae, {
className: 'Algae',
symbolName: '@org/package/Algae',
});
module.exports.WrappedClass = Algae;
-19
View File
@@ -1,19 +0,0 @@
'use strict';
const withIs = require('../../..');
class Animal {
constructor(type) {
this.type = type;
}
getType() {
return this.type;
}
}
module.exports = withIs(Animal, {
className: 'Animal',
symbolName: '@org/package/Animal',
});
module.exports.WrappedClass = Animal;
-16
View File
@@ -1,16 +0,0 @@
'use strict';
const withIs = require('../../..');
const Animal = require('./Animal');
class Mammal extends Animal {
constructor() {
super('mammal');
}
}
module.exports = withIs(Mammal, {
className: 'Mammal',
symbolName: '@org/package/Mammal',
});
module.exports.WrappedClass = Mammal;
-19
View File
@@ -1,19 +0,0 @@
'use strict';
const withIs = require('../../..');
class Plant {
constructor(type) {
this.type = type;
}
getType() {
return this.type;
}
}
module.exports = withIs(Plant, {
className: 'Plant',
symbolName: '@org/package/Plant',
});
module.exports.WrappedClass = Plant;
-8
View File
@@ -1,8 +0,0 @@
'use strict';
module.exports = {
Animal: require('./Animal'),
Plant: require('./Plant'),
Mammal: require('./Mammal'),
Algae: require('./Algae'),
};