-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
102 lines (74 loc) · 1.85 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
var test = require('tape');
var sinon = require('sinon');
var pureHandler = require('./index');
test('it should return a function', assert => {
const noop = () => {};
assert.equals(typeof pureHandler(noop), 'function');
assert.end();
});
test('it should bind this for values', assert => {
const expected = 123;
const handlerFunc = ({ foo }) => assert.equals(foo, expected);
const obj = {
foo: expected,
handler: pureHandler(handlerFunc)
}
obj.handler();
assert.end();
});
test('it should bind this for functions', assert => {
const expected = 123;
const handlerFunc = ({ func }) => assert.equals(func(), expected);
const obj = {
foo: expected,
func: function() {
return this.foo;
},
handler: pureHandler(handlerFunc)
}
obj.handler();
assert.end();
});
test('it should expand bound attributes object when returned', assert => {
const handlerFunc = () => ({
attributes: {
bar: 234
}
});
const obj = {
attributes: {
foo: 123
},
handler: pureHandler(handlerFunc)
}
obj.handler();
const expected = { foo: 123, bar: 234 };
const actual = obj.attributes;
assert.deepEqual(actual, expected);
assert.end();
});
test('it should call emit when present in return value', assert => {
const handlerFunc = () => ({
emit: [':tell', 'Hello World']
});
const obj = {
emit: sinon.spy(),
handler: pureHandler(handlerFunc)
};
obj.handler();
assert.true(obj.emit.calledWith(':tell', 'Hello World'));
assert.end();
});
test('it should work with promises', assert => {
const handlerFunc = () => Promise.resolve({
emit: [':tell', 'Hello World']
});
const obj = {
emit: sinon.spy(),
handler: pureHandler(handlerFunc)
};
obj.handler().then(() => {
assert.true(obj.emit.calledWith(':tell', 'Hello World'));
assert.end();
});
});