forked from marcuswestin/store.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operations_test.js
73 lines (64 loc) · 1.69 KB
/
operations_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
var { each, map } = require('../src/util')
var { deepEqual } = require('../tests/util')
require('./operations')
module.exports = {
setup: setup,
}
function setup(store) {
test('push', function() {
_testArrayOp('push', [], [
[],
['a'],
['b','c'],
[null],
[[], {}]
])
})
test('unshift', function() {
_testArrayOp('unshift', undefined, [
[],
['a'],
['b','c'],
[null],
[[], {}]
])
})
test('pop', function() {
var arr = ['a', 'b', 'c', null, [[], {}]]
// Call pop arr.length + 1 times. No args each time
var argsList = map(arr, function() { return [] }).concat([])
_testArrayOp('pop', arr, argsList)
})
test('shift', function() {
var arr = ['a', 'b', 'c', null, [[], {}]]
// Call shift arr.length + 1 times. No args each time
var argsList = map(arr, function() { return [] }).concat([])
_testArrayOp('shift', arr, argsList)
})
test('assign', function() {
store.clearAll()
var expect = { bar:'cat', mat:{ hat:'bat', arr:[1,2,3] }}
store.assign('foo', expect)
assert(deepEqual(store.get('foo'), expect))
var add = { bar:'cat2', mat:{ hat:'bat2' }, newProp:'newProp'}
store.assign('foo', add)
each(add, function(val, key) {
expect[key] = val
})
assert(deepEqual(store.get('foo'), expect))
})
function _testArrayOp(fnName, arr, argLists) {
var key = 'test-'+fnName
store.set(key, arr)
arr = (arr || [])
var arrFn = arr[fnName]
var storeFn = store[fnName]
each(argLists, function(args) {
var expectedFnResult = arrFn.apply(arr, args)
var actualFnResult = storeFn.apply(store, [key].concat(args))
assert(deepEqual(expectedFnResult, actualFnResult))
var actual = store.get(key)
assert(deepEqual(arr, actual))
})
}
}