Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also fix a bug where the proto inheritance chain was not setup properly. Refactored tests to their own files and improved the overall test suite.
- Loading branch information
Showing
17 changed files
with
442 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,127 @@ | ||
'use strict'; | ||
|
||
const withIs = require('..'); | ||
const { | ||
Animal, | ||
Plant, | ||
Mammal, | ||
Algae, | ||
|
||
const Circle = withIs.proto(function () { | ||
if (!(this instanceof Circle)) { // eslint-disable-line no-invalid-this | ||
return new Circle(); | ||
} | ||
}, { className: 'Circle', symbolName: '@org/package/circle' }); | ||
ExplicitWithoutNew, | ||
ImplicitWithoutNew, | ||
ImplicitExplicitWithoutNew, | ||
} = require('./fixtures/es5'); | ||
|
||
const Square = withIs.proto(function () { | ||
if (!(this instanceof Square)) { // eslint-disable-line no-invalid-this | ||
return new Square(); | ||
} | ||
}, { className: 'Square', symbolName: '@org/package/square' }); | ||
it('should setup the prototype chain correctly', () => { | ||
const animal = new Animal('mammal'); | ||
const plant = new Plant('algae'); | ||
|
||
const circle = new Circle(); | ||
const square = new Square(); | ||
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); | ||
|
||
test('circle is an instance of Circle class', () => { | ||
expect(Circle.isCircle(circle)).toBe(true); | ||
}); | ||
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); | ||
|
||
test('square is not an instance of Circle class', () => { | ||
expect(Circle.isCircle(square)).toBe(false); | ||
expect(animal.getType()).toBe('mammal'); | ||
expect(plant.getType()).toBe('algae'); | ||
}); | ||
|
||
test('square is an instance of Square class', () => { | ||
expect(Square.isSquare(square)).toBe(true); | ||
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]'); | ||
}); | ||
|
||
test('circle is not an instance of Square class', () => { | ||
expect(Square.isSquare(circle)).toBe(false); | ||
}); | ||
describe('is<className> method', () => { | ||
it('should add a working is<className> static method', () => { | ||
const animal = new Animal('mammal'); | ||
const plant = new Plant('algae'); | ||
|
||
test('calling without new', () => { | ||
const circle = Circle(); // eslint-disable-line new-cap | ||
expect(Animal.isAnimal(animal)).toBe(true); | ||
expect(Animal.isAnimal(plant)).toBe(false); | ||
expect(Plant.isPlant(plant)).toBe(true); | ||
expect(Plant.isPlant(animal)).toBe(false); | ||
}); | ||
|
||
expect(Circle.isCircle(circle)).toBe(true); | ||
}); | ||
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(); | ||
|
||
test('undefined/null is not an instance of any class', () => { | ||
expect(Circle.isCircle(undefined)).toBe(false); | ||
expect(Circle.isCircle(null)).toBe(false); | ||
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); | ||
}); | ||
}); | ||
|
||
test('check custom tag of Square class', () => { | ||
expect(Object.prototype.toString.call(square)).toBe('[object Square]'); | ||
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,67 @@ | ||
'use strict'; | ||
|
||
const withIs = require('..'); | ||
|
||
const Person = withIs(class { | ||
constructor(name, city) { | ||
this.name = name; | ||
this.city = city; | ||
} | ||
}, { className: 'Person', symbolName: '@org/package-x/person' }); | ||
|
||
const Animal = withIs(class { | ||
constructor(species) { | ||
this.species = species; | ||
} | ||
}, { className: 'Animal', symbolName: '@org/package-y/person' }); | ||
|
||
const diogo = new Person('Diogo', 'Porto'); | ||
const wolf = new Animal('Wolf'); | ||
|
||
test('person is an instance of Person class', () => { | ||
expect(Person.isPerson(diogo)).toBe(true); | ||
}); | ||
const { | ||
Animal, | ||
Plant, | ||
Mammal, | ||
Algae, | ||
} = require('./fixtures/es6'); | ||
|
||
test('wolf is not an instance of Person class', () => { | ||
expect(Person.isPerson(wolf)).toBe(false); | ||
}); | ||
it('should setup the prototype chain correctly', () => { | ||
const animal = new Animal('mammal'); | ||
const plant = new Plant('algae'); | ||
|
||
test('wolf is an instance of Animal class', () => { | ||
expect(Animal.isAnimal(wolf)).toBe(true); | ||
}); | ||
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); | ||
|
||
test('person is not an instance of Animal class', () => { | ||
expect(Animal.isAnimal(diogo)).toBe(false); | ||
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'); | ||
}); | ||
|
||
test('undefined/null is not an instance of any class', () => { | ||
expect(Person.isPerson(undefined)).toBe(false); | ||
expect(Person.isPerson(null)).toBe(false); | ||
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]'); | ||
}); | ||
|
||
test('check custom tag of class', () => { | ||
expect(Object.prototype.toString.call(diogo)).toBe('[object Person]'); | ||
expect(Object.prototype.toString.call(wolf)).toBe('[object Animal]'); | ||
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); | ||
}); | ||
}); |
Oops, something went wrong.