-
Notifications
You must be signed in to change notification settings - Fork 2
/
enumeration.test.js
153 lines (120 loc) · 4.58 KB
/
enumeration.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import Foliage from '../Foliage'
import Immutable from 'immutable'
describe('Enumeration', function() {
let array = [1, 2, 3, 4 ]
let object = { a: 1, b: 2, c: 3, d: 4 }
let map = Immutable.Map(object)
let list = Immutable.Set(array)
let tests = new Foliage([
[ 'map', (i => i + 1) ],
[ 'reduce', ((a, b) => a + b), 0],
[ 'filter', (n => (n % 2 === 0)) ],
[ 'some', (i => i === 2) ],
[ 'every', (i => i < 10) ],
[ 'join', ', '],
[ 'indexOf', 2]
])
tests.forEach(function([method, ...args]) {
describe(`Foliage::${method}`, function() {
it ('works at the root level with arrays', function() {
let plant = new Foliage(array)
let expected = array[method](...args)
plant[method](...args).should.eql(expected)
})
it ('works at the root level with objects', function() {
let plant = new Foliage(object)
let expected = plant.values()[method](...args)
plant[method](...args).should.eql(expected)
})
it ('works at the root level with maps', function() {
let plant = new Foliage(map)
let expected = plant.values()[method](...args)
plant[method](...args).should.eql(expected)
})
it ('works at the root level with sets', function() {
let plant = new Foliage(map)
let expected = plant.values()[method](...args)
plant[method](...args).should.eql(expected)
})
it ('works at a sub-level with arrays', function() {
let plant = new Foliage({ body: array })
let query = plant.refine('body')
let expected = array[method](...args)
query[method](...args).should.eql(expected)
})
it ('works at a sub-level level with objects', function() {
let plant = new Foliage({ body : object })
let query = plant.refine('body')
let expected = query.values()[method](...args)
query[method](...args).should.eql(expected)
})
it ('works at a sub-level with sets', function() {
let plant = new Foliage({ body: set })
let query = plant.refine('body')
let expected = array[method](...args)
query[method](...args).should.eql(expected)
})
it ('works at a sub-level level with maps', function() {
let plant = new Foliage({ body : map })
let query = plant.refine('body')
let expected = query.values()[method](...args)
query[method](...args).should.eql(expected)
})
})
})
// Find must be tested separately because the native implementation is not
// well supported
describe('Foliage::find', function() {
let even = n => (n % 2 === 0)
it ('returns the first answer of an array filter', function() {
let plant = new Foliage([ 1, 2, 3, 4])
plant.find(even).should.eql(plant.filter(even).unshift())
})
it ('returns the first answer of an object filter', function() {
let plant = new Foliage({ a: 1, b: 2, c: 3, d: 4 })
plant.find(even).should.eql(plant.filter(even).unshift())
})
})
describe('Foliage::first', function() {
it ('returns the first value in an array', function() {
let plant = new Foliage([ 1, 2, 3, 4])
plant.first().should.equal(1)
})
it ('returns the first value in an object', function() {
let plant = new Foliage({ a: 1, b: 2, c: 3, d: 4 })
plant.first().should.equal(1)
})
})
describe('Foliage::last', function() {
it ('returns the last value in an array', function() {
let plant = new Foliage([ 1, 2, 3, 4])
plant.last().should.equal(4)
})
it ('returns the last value of an object', function() {
let plant = new Foliage({ a: 1, b: 2, c: 3, d: 4 })
plant.last().should.equal(4)
})
})
describe('Foliage::size', function() {
it ('returns the last value in an array', function() {
let plant = new Foliage([ 1, 2, 3, 4])
plant.size().should.equal(4)
})
it ('returns the last value of an object', function() {
let plant = new Foliage({ a: 1, b: 2, c: 3 })
plant.size().should.equal(3)
})
})
describe('Foliage::includes', function() {
it ('returns a boolean if a value is present in an array', function() {
let plant = new Foliage([ 1, 2, 3, 4])
plant.includes(1).should.equal(true)
plant.includes('a').should.equal(false)
})
it ('returns a boolean if a value is present in an object', function() {
let plant = new Foliage({ a: 1, b: 2, c: 3, d: 5 })
plant.includes(1).should.equal(true)
plant.includes('a').should.equal(false)
})
})
})