-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest.js
83 lines (66 loc) · 1.59 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
import test from 'ava';
import magicIterable from './index.js';
test('main', t => {
let index = 0;
const createFixture = () => ({
increment(value) {
index += value;
return index;
},
});
const array = [
createFixture(),
createFixture(),
createFixture(),
createFixture(),
];
const magicArray = magicIterable(array);
t.true(Array.isArray(magicArray));
t.deepEqual(magicArray.increment(2), [2, 4, 6, 8]);
t.is(index, 8);
});
test('`this` works correctly', t => {
const fixture = {
index: 0,
increment(value) {
this.index += value;
return this.index;
},
};
const array = [fixture, fixture, fixture, fixture];
t.deepEqual(magicIterable(array).increment(2), [2, 4, 6, 8]);
t.is(fixture.index, 8);
});
test('does not work on heterogeneous iterable', t => {
const createFixture = () => ({
foo() {},
});
const array = [
createFixture(),
createFixture(),
{},
createFixture(),
];
const magicArray = magicIterable(array);
t.throws(() => {
magicArray.foo();
}, {
message: /Item 3 of the iterable is missing the foo\(\) method/,
});
});
test('should work on array of non-objects', t => {
t.deepEqual(magicIterable(['a', 'b']).includes('b'), [false, true]);
});
test('should only apply to the items of the iterable', t => {
const fixture = {
foo() {
return '🦄';
},
};
const array = [fixture, fixture];
array.foo = () => '🤡';
t.deepEqual(magicIterable(array).foo(), ['🦄', '🦄']);
});
test.failing('should support properties, not just methods', t => {
t.deepEqual(magicIterable(['a', 'ab', 'abc']).length.toString(), ['1', '2', '3']);
});