-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
163 lines (141 loc) · 4.45 KB
/
index.test.ts
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
import { maintain, retrieve, store, clear, purge } from '../src'
interface Spies {
[key: string]: jest.SpyInstance
}
describe('rich-local-storage', () => {
const spies: Spies = {}
beforeEach(() => {
['setItem', 'getItem', 'removeItem', 'clear'].forEach((fn: string) => {
const mock = jest.fn(localStorage[fn])
spies[fn] = jest.spyOn(localStorage.__proto__, fn).mockImplementation(mock)
})
})
afterEach(() => {
Object.keys(spies).forEach((key: string) => spies[key].mockRestore())
})
test('→ store a string', async () => {
store('foo', {
expiry: 1200,
value: 'bar'
})
expect(retrieve('foo')).toEqual('bar')
expect(spies.setItem).toHaveBeenCalledTimes(1)
expect(spies.getItem).toHaveBeenCalledTimes(1)
})
test('→ store a number', async () => {
store('age', {
expiry: 1200,
value: 36
})
expect(retrieve('age')).toEqual(36)
})
test('→ store an array of strings', async () => {
const value = ['john', 'adam', 'rachel']
store('names', {
expiry: 1200,
value
})
expect(retrieve('names')).toEqual(value)
})
test('→ store an array of numbers', async () => {
const value = [0, 1, 2]
store('counts', {
expiry: 1200,
value
})
expect(retrieve('counts')).toEqual(value)
})
test('→ store a non-nested object', async () => {
const value = {
name: 'Greg',
surname: 'King',
age: 21
}
store('king', {
expiry: 1200,
value
})
expect(retrieve('king')).toEqual(value)
})
test('→ store a nested object', async () => {
const value = {
name: 'Greg',
surname: 'King',
age: 21,
parents: {
mother: 'Anna',
father: 'George',
line: 4
},
siblings: {
number: 2,
names: ['john', 'ken']
}
}
store('king', {
expiry: 1200,
value
})
expect(retrieve('king')).toEqual(value)
})
test('→ store a `null` object', async () => {
store('wrong', {
expiry: 1200,
value: null
})
expect(retrieve('wrong')).toEqual(null)
})
test('→ `maintain` does not remove the property stored if within the expiry', async () => {
store('item', { expiry: 120, value: 'test' })
maintain()
expect(retrieve('item')).toEqual('test')
})
test('→ `maintain` removes the property stored if within the expiry', async () => {
store('item', { expiry: -30, value: 'test' })
maintain()
expect(retrieve('item')).toEqual(null)
})
test('→ `maintain` removes the multiple properties stored if within the expiry', async () => {
store('a', { expiry: -15, value: 'a' })
store('b', { expiry: 120, value: 'b' })
store('c', { expiry: -30, value: 'c' })
maintain()
expect(retrieve('a')).toEqual(null)
expect(retrieve('b')).toEqual('b')
expect(retrieve('c')).toEqual(null)
})
test('→ `maintain` acts only on items which represent the cache framework format', async () => {
localStorage.setItem('a', 'xxx')
localStorage.setItem('b', 'yyy')
store('c', { expiry: 120, value: 'c' })
maintain()
expect(localStorage.getItem('a')).toEqual('xxx')
expect(retrieve('c')).toEqual('c')
})
test('→ multiple `maintain()` calls do not affect storage if expiry is not reached', async () => {
store('a', { expiry: 120, value: 'a' })
maintain()
store('b', { expiry: 120, value: 'b' })
maintain()
expect(retrieve('a')).toEqual('a')
expect(retrieve('b')).toEqual('b')
})
test('→ `clear` empties the full storage', async () => {
store('a', { expiry: 120, value: 'a' })
store('b', { expiry: 120, value: 'b' })
clear()
expect(retrieve('a')).toEqual(null)
expect(retrieve('b')).toEqual(null)
})
test('→ purge a single item', async () => {
store('a', { expiry: 120, value: 'a' })
store('b', { expiry: 120, value: 'b' })
purge('a')
expect(retrieve('a')).toEqual(null)
expect(retrieve('b')).toEqual('b')
})
test('→ retrieval attempt returns `null` if the value is not in the acceptable format', async () => {
localStorage.setItem('dummyItem', 'somevalue')
expect(retrieve('dummyItem')).toEqual(null)
})
})