This repository has been archived by the owner on Dec 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
170 lines (144 loc) · 3.48 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
'use strict'
const ko = require('knockout')
const test = require('ava')
require('./src')
test('extenders/ko-projections basic functionality', (t) => {
t.plan(3)
const foos = ko.observableArray([
{ id: 1, text: 'foo' },
{ id: 2, text: 'bar' },
{ id: 3, text: 'baz' },
{ id: 4, text: 'qux' }
]).extend({
_: true
})
t.deepEqual(foos(), [
{ id: 1, text: 'foo' },
{ id: 2, text: 'bar' },
{ id: 3, text: 'baz' },
{ id: 4, text: 'qux' }
], 'initializes')
let isLazy = true
const flippedFoos = foos._.map((foo) => {
isLazy = false
foo.text = foo.text.split('').reverse().join('')
return foo
})
t.truthy(isLazy, 'is lazy')
t.deepEqual(flippedFoos(), [
{ id: 1, text: 'oof' },
{ id: 2, text: 'rab' },
{ id: 3, text: 'zab' },
{ id: 4, text: 'xuq' }
], 'calls ko-projections prototype func')
})
test('extenders/ko-projections chainability', (t) => {
t.plan(2)
const foos = ko.observableArray([
{ id: 1, text: 'foo' },
{ id: 2, text: 'bar' },
{ id: 3, text: 'baz' },
{ id: 4, text: 'qux' }
]).extend({
_: true
})
let isLazy = true
const flippedAndFilteredFoos = foos
._.map((foo) => {
isLazy = false
foo.text = foo.text.split('').reverse().join('')
return foo
})
._.filter((foo) => {
isLazy = false
return foo.id % 2 === 0
})
t.truthy(isLazy)
t.deepEqual(flippedAndFilteredFoos(), [
{ id: 2, text: 'rab' },
{ id: 4, text: 'xuq' }
])
})
test('extenders/ko-projections non-chainable functions', (t) => {
t.plan(5)
const foos = ko.observableArray([
{ id: 1, text: 'foo' },
{ id: 2, text: 'bar' },
{ id: 3, text: 'baz' },
{ id: 4, text: 'qux' }
]).extend({
_: true
})
let isLazy = true
const fooFunc = foos
._.each(() => {
isLazy = false
t.truthy(true)
})
t.truthy(isLazy)
fooFunc()
})
test('extenders/ko-projections non-chainable functions that return undefined', (t) => {
t.plan(1)
const foos = ko.observableArray([
{ id: 1, text: 'foo' },
{ id: 2, text: 'bar' },
{ id: 3, text: 'baz' },
{ id: 4, text: 'qux' }
]).extend({
_: true
})
foos._.find((f) => f.id === 5)()
t.pass()
})
test('extenders/ko-projections works w/ dependency chain', (t) => {
t.plan(3)
const foos = ko.observableArray([
{ id: 1, text: 'foo' },
{ id: 2, text: 'bar' },
{ id: 3, text: 'baz' },
{ id: 4, text: 'qux' }
]).extend({
_: true
})
let isLazy = true
const wantsOdds = ko.observable(true)
const flippedAndFilteredFoos = foos
._.map((foo) => {
isLazy = false
foo.text = foo.text.split('').reverse().join('')
return foo
})
._.filter((foo) => {
isLazy = false
return foo.id % 2 === (wantsOdds() ? 1 : 0)
})
t.truthy(isLazy)
t.deepEqual(flippedAndFilteredFoos(), [
{ id: 1, text: 'oof' },
{ id: 3, text: 'zab' }
])
wantsOdds(false)
ko.tasks.runEarly()
t.deepEqual(flippedAndFilteredFoos(), [
{ id: 2, text: 'rab' },
{ id: 4, text: 'xuq' }
])
})
// Haven't decided yet if this is worth implementing...
//
// test('extenders/ko-projections can selectively extend', (t) => {
// t.plan(2)
//
// const foos = ko.observableArray([
// { id: 1, text: 'foo' },
// { id: 2, text: 'bar' },
// { id: 3, text: 'baz' },
// { id: 4, text: 'qux' }
// ]).extend({
// _: { map: true }
// })
//
// t.truthy(typeof foos._map === 'function')
// t.truthy(typeof foos._filter !== 'function')
// })