-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathgetter-tests.js
81 lines (73 loc) · 2.42 KB
/
getter-tests.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
import { isGetter, getFlattenedDeps, fromKeyPath } from '../src/getter'
import { Set, List, is } from 'immutable'
describe('Getter', () => {
describe('isGetter', () => {
it('should return false for primitives', () => {
expect(isGetter(1)).toBe(false)
expect(isGetter('foo')).toBe(false)
expect(isGetter(false)).toBe(false)
expect(isGetter(null)).toBe(false)
})
it('should return true for arrays with a functions at end', () => {
expect(isGetter([['foo'], (x) => x])).toBe(true)
expect(isGetter([['foo', 'bar'], (x) => x])).toBe(true)
expect(isGetter([['foo', 'bar'], ['bar', 'baz'], (a, b) => a + b])).toBe(true)
})
it('should return true nested getters', () => {
var getter1 = [['foo'], (x) => x]
expect(isGetter([getter1, ['foo'], (a, b) => a + b])).toBe(true)
})
})
describe('fromKeyPath', () => {
it('should throw an Error for a nonvalid KeyPath', () => {
var invalidKeypath = 'foo.bar'
expect(function() {
fromKeyPath(invalidKeypath)
}).toThrow()
})
})
describe('getFlattenedDeps', function() {
describe('when passed the identity getter', () => {
it('should return a set with only an empty list', () => {
var getter = [[], (x) => x]
var result = getFlattenedDeps(getter)
var expected = Set().add(List())
expect(is(result, expected)).toBe(true)
})
})
describe('when passed a flat getter', () => {
it('return all keypaths', () => {
var getter = [
['store1', 'key1'],
['store2', 'key2'],
(a, b) => 1,
]
var result = getFlattenedDeps(getter)
var expected = Set()
.add(List(['store1', 'key1']))
.add(List(['store2', 'key2']))
expect(is(result, expected)).toBe(true)
})
})
describe('when passed getter with a getter dependency', () => {
it('should return flattened keypaths', () => {
var getter1 = [
['store1', 'key1'],
['store2', 'key2'],
(a, b) => 1,
]
var getter2 = [
getter1,
['store3', 'key3'],
(a, b) => 1,
]
var result = getFlattenedDeps(getter2)
var expected = Set()
.add(List(['store1', 'key1']))
.add(List(['store2', 'key2']))
.add(List(['store3', 'key3']))
expect(is(result, expected)).toBe(true)
})
})
})
})