|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 |
| -const withIs = require('..'); |
| 3 | +const { |
| 4 | + Animal, |
| 5 | + Plant, |
| 6 | + Mammal, |
| 7 | + Algae, |
4 | 8 |
|
5 |
| -const Circle = withIs.proto(function () { |
6 |
| - if (!(this instanceof Circle)) { // eslint-disable-line no-invalid-this |
7 |
| - return new Circle(); |
8 |
| - } |
9 |
| -}, { className: 'Circle', symbolName: '@org/package/circle' }); |
| 9 | + ExplicitWithoutNew, |
| 10 | + ImplicitWithoutNew, |
| 11 | + ImplicitExplicitWithoutNew, |
| 12 | +} = require('./fixtures/es5'); |
10 | 13 |
|
11 |
| -const Square = withIs.proto(function () { |
12 |
| - if (!(this instanceof Square)) { // eslint-disable-line no-invalid-this |
13 |
| - return new Square(); |
14 |
| - } |
15 |
| -}, { className: 'Square', symbolName: '@org/package/square' }); |
| 14 | +it('should setup the prototype chain correctly', () => { |
| 15 | + const animal = new Animal('mammal'); |
| 16 | + const plant = new Plant('algae'); |
16 | 17 |
|
17 |
| -const circle = new Circle(); |
18 |
| -const square = new Square(); |
| 18 | + expect(Object.getPrototypeOf(animal)).toBe(Animal.prototype); |
| 19 | + expect(Object.getPrototypeOf(Object.getPrototypeOf(animal))).toBe(Animal.WrappedClass.prototype); |
| 20 | + expect(Object.getPrototypeOf(animal)).not.toBe(Plant.prototype); |
| 21 | + expect(Object.getPrototypeOf(plant)).toBe(Plant.prototype); |
| 22 | + expect(Object.getPrototypeOf(Object.getPrototypeOf(plant))).toBe(Plant.WrappedClass.prototype); |
| 23 | + expect(Object.getPrototypeOf(plant)).not.toBe(Animal.prototype); |
19 | 24 |
|
20 |
| -test('circle is an instance of Circle class', () => { |
21 |
| - expect(Circle.isCircle(circle)).toBe(true); |
22 |
| -}); |
| 25 | + expect(animal instanceof Animal).toBe(true); |
| 26 | + expect(animal instanceof Animal.WrappedClass).toBe(true); |
| 27 | + expect(animal instanceof Plant).toBe(false); |
| 28 | + expect(plant instanceof Plant).toBe(true); |
| 29 | + expect(plant instanceof Plant.WrappedClass).toBe(true); |
| 30 | + expect(plant instanceof Animal).toBe(false); |
23 | 31 |
|
24 |
| -test('square is not an instance of Circle class', () => { |
25 |
| - expect(Circle.isCircle(square)).toBe(false); |
| 32 | + expect(animal.getType()).toBe('mammal'); |
| 33 | + expect(plant.getType()).toBe('algae'); |
26 | 34 | });
|
27 | 35 |
|
28 |
| -test('square is an instance of Square class', () => { |
29 |
| - expect(Square.isSquare(square)).toBe(true); |
| 36 | +it('should have a custom toStringTag', () => { |
| 37 | + expect(Object.prototype.toString.call(new Animal())).toBe('[object Animal]'); |
| 38 | + expect(Object.prototype.toString.call(new Plant())).toBe('[object Plant]'); |
30 | 39 | });
|
31 | 40 |
|
32 |
| -test('circle is not an instance of Square class', () => { |
33 |
| - expect(Square.isSquare(circle)).toBe(false); |
34 |
| -}); |
| 41 | +describe('is<className> method', () => { |
| 42 | + it('should add a working is<className> static method', () => { |
| 43 | + const animal = new Animal('mammal'); |
| 44 | + const plant = new Plant('algae'); |
35 | 45 |
|
36 |
| -test('calling without new', () => { |
37 |
| - const circle = Circle(); // eslint-disable-line new-cap |
| 46 | + expect(Animal.isAnimal(animal)).toBe(true); |
| 47 | + expect(Animal.isAnimal(plant)).toBe(false); |
| 48 | + expect(Plant.isPlant(plant)).toBe(true); |
| 49 | + expect(Plant.isPlant(animal)).toBe(false); |
| 50 | + }); |
38 | 51 |
|
39 |
| - expect(Circle.isCircle(circle)).toBe(true); |
40 |
| -}); |
| 52 | + it('should not crash if `null` or `undefined` is passed to is<ClassName>', () => { |
| 53 | + expect(Animal.isAnimal(null)).toBe(false); |
| 54 | + expect(Animal.isAnimal(undefined)).toBe(false); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should work correctly for deep inheritance scenarios', () => { |
| 58 | + const mammal = new Mammal(); |
| 59 | + const algae = new Algae(); |
41 | 60 |
|
42 |
| -test('undefined/null is not an instance of any class', () => { |
43 |
| - expect(Circle.isCircle(undefined)).toBe(false); |
44 |
| - expect(Circle.isCircle(null)).toBe(false); |
| 61 | + expect(Mammal.isMammal(mammal)).toBe(true); |
| 62 | + expect(Animal.isAnimal(mammal)).toBe(true); |
| 63 | + expect(Mammal.isMammal(algae)).toBe(false); |
| 64 | + expect(Animal.isAnimal(algae)).toBe(false); |
| 65 | + |
| 66 | + expect(Algae.isAlgae(algae)).toBe(true); |
| 67 | + expect(Plant.isPlant(algae)).toBe(true); |
| 68 | + expect(Algae.isAlgae(mammal)).toBe(false); |
| 69 | + expect(Plant.isPlant(mammal)).toBe(false); |
| 70 | + }); |
45 | 71 | });
|
46 | 72 |
|
47 |
| -test('check custom tag of Square class', () => { |
48 |
| - expect(Object.prototype.toString.call(square)).toBe('[object Square]'); |
| 73 | +describe('new operator', () => { |
| 74 | + it('should work on explicit without-new handling', () => { |
| 75 | + const instance = new ExplicitWithoutNew(); |
| 76 | + const instance2 = ExplicitWithoutNew(); // eslint-disable-line new-cap |
| 77 | + |
| 78 | + expect(Object.getPrototypeOf(instance)).toBe(ExplicitWithoutNew.prototype); |
| 79 | + expect(Object.getPrototypeOf(Object.getPrototypeOf(instance))).toBe(ExplicitWithoutNew.WrappedClass.prototype); |
| 80 | + expect(Object.getPrototypeOf(instance2)).toBe(ExplicitWithoutNew.prototype); |
| 81 | + expect(Object.getPrototypeOf(Object.getPrototypeOf(instance2))).toBe(ExplicitWithoutNew.WrappedClass.prototype); |
| 82 | + |
| 83 | + expect(instance instanceof ExplicitWithoutNew).toBe(true); |
| 84 | + expect(instance instanceof ExplicitWithoutNew.WrappedClass).toBe(true); |
| 85 | + expect(instance2 instanceof ExplicitWithoutNew).toBe(true); |
| 86 | + expect(instance2 instanceof ExplicitWithoutNew.WrappedClass).toBe(true); |
| 87 | + |
| 88 | + expect(instance.getLabel()).toBe('ExplicitWithoutNew'); |
| 89 | + expect(instance2.getLabel()).toBe('ExplicitWithoutNew'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should work on implicit without-new handling', () => { |
| 93 | + const instance = new ImplicitWithoutNew(); |
| 94 | + const instanceNoNew = ImplicitWithoutNew(); // eslint-disable-line new-cap |
| 95 | + |
| 96 | + expect(Object.getPrototypeOf(instance)).toBe(ImplicitWithoutNew.prototype); |
| 97 | + expect(Object.getPrototypeOf(Object.getPrototypeOf(instance))).toBe(ImplicitWithoutNew.WrappedClass.prototype); |
| 98 | + expect(Object.getPrototypeOf(instanceNoNew)).toBe(ImplicitWithoutNew.prototype); |
| 99 | + expect(Object.getPrototypeOf(Object.getPrototypeOf(instanceNoNew))).toBe(ImplicitWithoutNew.WrappedClass.prototype); |
| 100 | + |
| 101 | + expect(instance instanceof ImplicitWithoutNew).toBe(true); |
| 102 | + expect(instance instanceof ImplicitWithoutNew.WrappedClass).toBe(true); |
| 103 | + expect(instanceNoNew instanceof ImplicitWithoutNew).toBe(true); |
| 104 | + expect(instanceNoNew instanceof ImplicitWithoutNew.WrappedClass).toBe(true); |
| 105 | + |
| 106 | + expect(instance.getLabel()).toBe('ImplicitWithoutNew'); |
| 107 | + expect(instanceNoNew.getLabel()).toBe('ImplicitWithoutNew'); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should work on explicit & implicit without-new handling', () => { |
| 111 | + const instance = new ImplicitExplicitWithoutNew(); |
| 112 | + const instanceNoNew = ImplicitExplicitWithoutNew(); // eslint-disable-line new-cap |
| 113 | + |
| 114 | + expect(Object.getPrototypeOf(instance)).toBe(ImplicitExplicitWithoutNew.prototype); |
| 115 | + expect(Object.getPrototypeOf(Object.getPrototypeOf(instance))).toBe(ImplicitExplicitWithoutNew.WrappedClass.prototype); |
| 116 | + expect(Object.getPrototypeOf(instanceNoNew)).toBe(ImplicitExplicitWithoutNew.prototype); |
| 117 | + expect(Object.getPrototypeOf(Object.getPrototypeOf(instanceNoNew))).toBe(ImplicitExplicitWithoutNew.WrappedClass.prototype); |
| 118 | + |
| 119 | + expect(instance instanceof ImplicitExplicitWithoutNew).toBe(true); |
| 120 | + expect(instance instanceof ImplicitExplicitWithoutNew.WrappedClass).toBe(true); |
| 121 | + expect(instanceNoNew instanceof ImplicitExplicitWithoutNew).toBe(true); |
| 122 | + expect(instanceNoNew instanceof ImplicitExplicitWithoutNew.WrappedClass).toBe(true); |
| 123 | + |
| 124 | + expect(instance.getLabel()).toBe('ImplicitExplicitWithoutNew'); |
| 125 | + expect(instanceNoNew.getLabel()).toBe('ImplicitExplicitWithoutNew'); |
| 126 | + }); |
49 | 127 | });
|
0 commit comments