This repository has been archived by the owner on Aug 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest.js
130 lines (106 loc) · 2.42 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import test from 'ava';
import fn from '.';
test('defaults and args are passed', t => {
t.plan(2);
fn({
defaults: {foo: 'bar'}
}, (opts, args) => {
t.deepEqual(opts, {foo: 'bar'});
t.deepEqual(args, ['uni', 'corn']);
})('uni', 'corn');
});
test('chainableMethods extend the options passed', t => {
t.plan(2);
fn({
defaults: {foo: 'bar'},
chainableMethods: {
moo: {cow: true}
}
}, (opts, args) => {
t.deepEqual(opts, {foo: 'bar', cow: true});
t.deepEqual(args, ['duck', 'goose']);
}).moo('duck', 'goose');
});
test('last item in the chain takes precedence', t => {
t.plan(4);
const config = {
chainableMethods: {
foo: {foo: true},
notFoo: {foo: false}
}
};
let expected = true;
function isExpected(opts) {
t.is(opts.foo, expected);
}
const notFoo = fn(config, isExpected).notFoo;
const foo = fn(config, isExpected).foo;
foo();
notFoo.foo();
expected = false;
notFoo();
foo.notFoo();
});
test('can extend a target object', t => {
const ctx = {};
const result = fn({
chainableMethods: {
def: {},
foo: {foo: true},
notFoo: {foo: false},
bar: {bar: true}
}
}, (opts, args) => [opts, args], ctx);
t.is(result, ctx);
t.deepEqual(ctx.def(), [{}, []]);
t.deepEqual(ctx.foo('baz'), [{foo: true}, ['baz']]);
t.deepEqual(ctx.notFoo('quz'), [{foo: false}, ['quz']]);
t.deepEqual(ctx.bar.foo.notFoo(), [{foo: false, bar: true}, []]);
});
test('this is preserved', t => {
const ctx = {};
fn({
chainableMethods: {
def: {},
foo: {foo: true},
notFoo: {foo: false},
bar: {bar: true}
}
}, function (opts) {
t.is(this, ctx);
return opts;
}, ctx);
t.deepEqual(ctx.def(), {});
t.deepEqual(ctx.foo.bar(), {foo: true, bar: true});
});
test('this is preserved correctly using prototypes', t => {
function Constructor() {}
fn({
chainableMethods: {
def: {},
foo: {foo: true},
notFoo: {foo: false},
bar: {bar: true}
}
}, function (opts) {
return [this, opts];
}, Constructor.prototype);
const c1 = new Constructor();
const c2 = new Constructor();
t.is(c1.def()[0], c1);
t.is(c1.foo.bar()[0], c1);
t.is(c2.def()[0], c2);
t.is(c2.bar.foo()[0], c2);
});
test('spread option spreads arguments', t => {
const def = fn({
spread: true,
chainableMethods: {
foo: {foo: true}
}
}, function () {
return Array.prototype.slice.call(arguments);
});
t.deepEqual(def('a', 'b'), [{}, 'a', 'b']);
t.deepEqual(def.foo('c', 'd'), [{foo: true}, 'c', 'd']);
});