@@ -4,19 +4,80 @@ import { describe, it } from 'mocha';
44import instanceOf from '../instanceOf' ;
55
66describe ( 'instanceOf' , ( ) => {
7+ it ( 'do not throw on values without prototype' , ( ) => {
8+ class Foo {
9+ // $FlowFixMe[unsupported-syntax]
10+ get [ Symbol . toStringTag ] ( ) {
11+ return 'Foo' ;
12+ }
13+ }
14+
15+ expect ( instanceOf ( true , Foo ) ) . to . equal ( false ) ;
16+ expect ( instanceOf ( null , Foo ) ) . to . equal ( false ) ;
17+ expect ( instanceOf ( Object . create ( null ) , Foo ) ) . to . equal ( false ) ;
18+ } ) ;
19+
20+ it ( 'detect name clashes with older versions of this lib' , ( ) => {
21+ function oldVersion ( ) {
22+ class Foo { }
23+ return Foo ;
24+ }
25+
26+ function newVersion ( ) {
27+ class Foo {
28+ // $FlowFixMe[unsupported-syntax]
29+ get [ Symbol . toStringTag ] ( ) {
30+ return 'Foo' ;
31+ }
32+ }
33+ return Foo ;
34+ }
35+
36+ const NewClass = newVersion ( ) ;
37+ const OldClass = oldVersion ( ) ;
38+ expect ( instanceOf ( new NewClass ( ) , NewClass ) ) . to . equal ( true ) ;
39+ expect ( ( ) => instanceOf ( new OldClass ( ) , NewClass ) ) . to . throw ( ) ;
40+ } ) ;
41+
42+ it ( 'allows instances to have share the same constructor name' , ( ) => {
43+ function getMinifiedClass ( tag : string ) {
44+ class SomeNameAfterMinification {
45+ // $FlowFixMe[unsupported-syntax]
46+ get [ Symbol . toStringTag ] ( ) {
47+ return tag ;
48+ }
49+ }
50+ return SomeNameAfterMinification ;
51+ }
52+
53+ const Foo = getMinifiedClass ( 'Foo' ) ;
54+ const Bar = getMinifiedClass ( 'Bar' ) ;
55+ expect ( instanceOf ( new Foo ( ) , Bar ) ) . to . equal ( false ) ;
56+ expect ( instanceOf ( new Bar ( ) , Foo ) ) . to . equal ( false ) ;
57+
58+ const DuplicateOfFoo = getMinifiedClass ( 'Foo' ) ;
59+ expect ( ( ) => instanceOf ( new DuplicateOfFoo ( ) , Foo ) ) . to . throw ( ) ;
60+ expect ( ( ) => instanceOf ( new Foo ( ) , DuplicateOfFoo ) ) . to . throw ( ) ;
61+ } ) ;
62+
763 it ( 'fails with descriptive error message' , ( ) => {
864 function getFoo ( ) {
9- class Foo { }
65+ class Foo {
66+ // $FlowFixMe[unsupported-syntax]
67+ get [ Symbol . toStringTag ] ( ) {
68+ return 'Foo' ;
69+ }
70+ }
1071 return Foo ;
1172 }
1273 const Foo1 = getFoo ( ) ;
1374 const Foo2 = getFoo ( ) ;
1475
1576 expect ( ( ) => instanceOf ( new Foo1 ( ) , Foo2 ) ) . to . throw (
16- / ^ C a n n o t u s e F o o " \[ o b j e c t O b j e c t \] " f r o m a n o t h e r m o d u l e o r r e a l m ./ m,
77+ / ^ C a n n o t u s e F o o " { } " f r o m a n o t h e r m o d u l e o r r e a l m ./ m,
1778 ) ;
1879 expect ( ( ) => instanceOf ( new Foo2 ( ) , Foo1 ) ) . to . throw (
19- / ^ C a n n o t u s e F o o " \[ o b j e c t O b j e c t \] " f r o m a n o t h e r m o d u l e o r r e a l m ./ m,
80+ / ^ C a n n o t u s e F o o " { } " f r o m a n o t h e r m o d u l e o r r e a l m ./ m,
2081 ) ;
2182 } ) ;
2283} ) ;
0 commit comments