Skip to content

Commit e5abd55

Browse files
committed
fix: fix non-boolean being returned for null/undefined values
Refactored tests a bit.
1 parent cf90e4a commit e5abd55

File tree

4 files changed

+96
-76
lines changed

4 files changed

+96
-76
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function withIs(Class, { className, symbolName }) {
2424
},
2525
}[className];
2626

27-
ClassIsWrapper[`is${className}`] = (obj) => obj && Boolean(obj[symbol]);
27+
ClassIsWrapper[`is${className}`] = (obj) => !!(obj && obj[symbol]);
2828

2929
return ClassIsWrapper;
3030
}
@@ -51,7 +51,7 @@ function withIsProto(Class, { className, symbolName }) {
5151
},
5252
});
5353

54-
ClassIsWrapper[`is${className}`] = (obj) => obj && Boolean(obj[symbol]);
54+
ClassIsWrapper[`is${className}`] = (obj) => !!(obj && obj[symbol]);
5555

5656
return ClassIsWrapper;
5757
}

test/es5.test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict';
2+
3+
const withIs = require('..');
4+
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' });
10+
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' });
16+
17+
const circle = new Circle();
18+
const square = new Square();
19+
20+
test('circle is an instance of Circle class', () => {
21+
expect(Circle.isCircle(circle)).toBe(true);
22+
});
23+
24+
test('square is not an instance of Circle class', () => {
25+
expect(Circle.isCircle(square)).toBe(false);
26+
});
27+
28+
test('square is an instance of Square class', () => {
29+
expect(Square.isSquare(square)).toBe(true);
30+
});
31+
32+
test('circle is not an instance of Square class', () => {
33+
expect(Square.isSquare(circle)).toBe(false);
34+
});
35+
36+
test('calling without new', () => {
37+
const circle = Circle(); // eslint-disable-line new-cap
38+
39+
expect(Circle.isCircle(circle)).toBe(true);
40+
});
41+
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);
45+
});
46+
47+
test('check custom tag of Square class', () => {
48+
expect(Object.prototype.toString.call(square)).toBe('[object Square]');
49+
});

test/es6.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
3+
const withIs = require('..');
4+
5+
const Person = withIs(class {
6+
constructor(name, city) {
7+
this.name = name;
8+
this.city = city;
9+
}
10+
}, { className: 'Person', symbolName: '@org/package-x/person' });
11+
12+
const Animal = withIs(class {
13+
constructor(species) {
14+
this.species = species;
15+
}
16+
}, { className: 'Animal', symbolName: '@org/package-y/person' });
17+
18+
const diogo = new Person('Diogo', 'Porto');
19+
const wolf = new Animal('Wolf');
20+
21+
test('person is an instance of Person class', () => {
22+
expect(Person.isPerson(diogo)).toBe(true);
23+
});
24+
25+
test('wolf is not an instance of Person class', () => {
26+
expect(Person.isPerson(wolf)).toBe(false);
27+
});
28+
29+
test('wolf is an instance of Animal class', () => {
30+
expect(Animal.isAnimal(wolf)).toBe(true);
31+
});
32+
33+
test('person is not an instance of Animal class', () => {
34+
expect(Animal.isAnimal(diogo)).toBe(false);
35+
});
36+
37+
test('undefined/null is not an instance of any class', () => {
38+
expect(Person.isPerson(undefined)).toBe(false);
39+
expect(Person.isPerson(null)).toBe(false);
40+
});
41+
42+
test('check custom tag of class', () => {
43+
expect(Object.prototype.toString.call(diogo)).toBe('[object Person]');
44+
expect(Object.prototype.toString.call(wolf)).toBe('[object Animal]');
45+
});

test/index.test.js

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)