3 import test from 'tape'
4 import protochain from './'
6 test('protochain', t => {
7 t.test('finds correct prototype chain', t => {
9 strictEqualArray(t, protochain(obj), [Object.prototype])
10 strictEqualArray(t, protochain(Object.create(obj)), [obj, Object.prototype])
11 strictEqualArray(t, protochain(new Error('message')), [Error.prototype, Object.prototype])
12 strictEqualArray(t, protochain(new TypeError('message')), [TypeError.prototype, Error.prototype, Object.prototype])
13 strictEqualArray(t, protochain(new String()), [String.prototype, Object.prototype])
14 strictEqualArray(t, protochain(new Number()), [Number.prototype, Object.prototype])
15 strictEqualArray(t, protochain(new RegExp('abc')), [RegExp.prototype, Object.prototype])
16 strictEqualArray(t, protochain(new Date()), [Date.prototype, Object.prototype])
20 t.test('null prototype is handled correctly', t => {
21 let noProtoObject = Object.create(null)
22 strictEqualArray(t, protochain(noProtoObject), [])
23 strictEqualArray(t, protochain(Object.create(noProtoObject)), [noProtoObject])
27 t.test('non-object values cooerce to object counterparts correctly', t => {
28 strictEqualArray(t, protochain('abc'), [String.prototype, Object.prototype])
29 strictEqualArray(t, protochain(123), [Number.prototype, Object.prototype])
30 strictEqualArray(t, protochain(/abc/), [RegExp.prototype, Object.prototype])
31 strictEqualArray(t, protochain(true), [Boolean.prototype, Object.prototype])
32 strictEqualArray(t, protochain(false), [Boolean.prototype, Object.prototype])
33 strictEqualArray(t, protochain(''), [String.prototype, Object.prototype])
34 strictEqualArray(t, protochain(0), [Number.prototype, Object.prototype])
38 t.test('null values produce empty list', t => {
39 strictEqualArray(t, protochain(), [])
40 strictEqualArray(t, protochain(undefined), [])
41 strictEqualArray(t, protochain(null), [])
45 t.test('examples', t => {
48 function FancyPerson() {
51 FancyPerson.prototype = Object.create(Person.prototype)
53 strictEqualArray(t, protochain(new Person()), [Person.prototype, Object.prototype])
54 strictEqualArray(t, protochain(new FancyPerson()), [FancyPerson.prototype, Person.prototype, Object.prototype])
58 // note this will in-fact be compiled to ES5
60 strictEqualArray(t, protochain(new Person()), [Person.prototype, Object.prototype])
62 class FancyPerson extends Person {}
63 strictEqualArray(t, protochain(new FancyPerson()), [FancyPerson.prototype, Person.prototype, Object.prototype])
68 // new native types which may not be supported
70 if (typeof Symbol !== 'undefined') {
71 t.test('symbol support', t => {
72 let foo = Symbol('foo')
73 strictEqualArray(t, protochain(foo), [Symbol.prototype, Object.prototype])
78 if (typeof Promise !== 'undefined') {
79 t.test('promise support', t => {
80 let foo = new Promise((Y, N) => Y())
81 strictEqualArray(t, protochain(foo), [Promise.prototype, Object.prototype])
89 function strictEqualArray(t, a, b) {
90 a.forEach((item, index) => t.strictEqual(a[index], b[index], `strictEqual at index ${index}`))
91 t.equal(a.length, b.length, 'same length')