-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathcache_spec.ts
More file actions
202 lines (160 loc) · 6.02 KB
/
cache_spec.ts
File metadata and controls
202 lines (160 loc) · 6.02 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import Promise from 'bluebird'
import { cache } from '../../lib/cache'
import { fs } from '../../lib/util/fs'
describe('lib/cache', () => {
beforeEach(async () => {
await cache.remove()
})
context('projects', () => {
describe('#insertProject', () => {
it('inserts project by path', async () => {
await cache.insertProject('foo/bar')
const projects = await cache.__get('PROJECTS')
expect(projects).to.deep.eq(['foo/bar'])
})
it('inserts project at the start', async () => {
await cache.insertProject('foo')
await cache.insertProject('bar')
const projects = await cache.__get('PROJECTS')
expect(projects).to.deep.eq(['bar', 'foo'])
})
it('can insert multiple projects in a row', async () => {
await cache.insertProject('baz')
await cache.insertProject('bar')
await cache.insertProject('foo')
const projects = await cache.__get('PROJECTS')
expect(projects).to.deep.eq(['foo', 'bar', 'baz'])
})
it('moves project to start if it already exists', async () => {
await cache.insertProject('foo')
await cache.insertProject('bar')
await cache.insertProject('baz')
await cache.insertProject('bar')
const projects = await cache.__get('PROJECTS')
expect(projects).to.deep.eq(['bar', 'baz', 'foo'])
})
})
describe('#removeProject', () => {
it('removes project by path', async () => {
await cache.insertProject('/Users/brian/app')
await cache.removeProject('/Users/brian/app')
const projects = await cache.__get('PROJECTS')
expect(projects).to.deep.eq([])
})
})
})
describe('#getProjectRoots', () => {
beforeEach(function () {
this.statAsync = sinon.stub(fs, 'statAsync')
})
afterEach(function () {
this.statAsync.restore()
})
it('returns an array of paths', async function () {
this.statAsync.withArgs('/Users/brian/app').resolves()
this.statAsync.withArgs('/Users/sam/app2').resolves()
await cache.insertProject('/Users/brian/app')
await cache.insertProject('/Users/sam/app2')
const paths = await cache.getProjectRoots()
expect(paths).to.deep.eq(['/Users/sam/app2', '/Users/brian/app'])
})
it('removes any paths which no longer exist on the filesystem', async function () {
this.statAsync.withArgs('/Users/brian/app').resolves()
this.statAsync.withArgs('/Users/sam/app2').rejects(new Error())
await cache.insertProject('/Users/brian/app')
await cache.insertProject('/Users/sam/app2')
const paths = await cache.getProjectRoots()
expect(paths).to.deep.eq(['/Users/brian/app'])
// we have to wait on the write event because
// of process.nextTick
await Promise.delay(100)
const projects = await cache.__get('PROJECTS')
expect(projects).to.deep.eq(['/Users/brian/app'])
})
})
})
context('project preferences', () => {
it('should insert a projects preferences into the cache', async () => {
const testProjectTitle = 'launchpad'
const testPreferences = { testingType: 'e2e', browserPath: '/some/test/path' }
await cache.insertProjectPreferences(testProjectTitle, testPreferences)
const preferences = await cache.__get('PROJECT_PREFERENCES')
expect(preferences[testProjectTitle]).to.deep.equal(testPreferences)
})
it('should insert multiple projects preferences into the cache', async () => {
const testProjectTitle = 'launchpad'
const testPreferences = { testingType: 'e2e', browserPath: '/some/test/path' }
const anotherTestProjectTitle = 'launchpad'
const anotherTestPreferene = { testingType: 'e2e', browserPath: '/some/test/path' }
await cache.insertProjectPreferences(testProjectTitle, testPreferences)
await cache.insertProjectPreferences(anotherTestProjectTitle, anotherTestPreferene)
const preferences = await cache.__get('PROJECT_PREFERENCES')
expect(preferences).to.have.property(testProjectTitle)
expect(preferences).to.have.property(anotherTestProjectTitle)
})
it('should clear the projects preferred preferences', async () => {
const testProjectTitle = 'launchpad'
const testPreferences = { testingType: 'e2e', browserPath: '/some/test/path' }
await cache.insertProjectPreferences(testProjectTitle, testPreferences)
await cache.removeProjectPreferences(testProjectTitle)
const preferences = await cache.__get('PROJECT_PREFERENCES')
expect(preferences[testProjectTitle]).to.equal(null)
})
})
context('#setUser / #getUser', () => {
beforeEach(function () {
this.user = {
id: 1,
name: 'brian',
email: 'a@b.com',
authToken: '1111-2222-3333-4444',
}
})
it('sets and gets user', async function () {
await cache.setUser(this.user)
const user = await cache.getUser()
expect(user).to.deep.eq(this.user)
})
})
context('#removeUser', () => {
it('sets user to empty object', async function () {
await cache.setUser(this.user)
await cache.removeUser()
const user = await cache.getUser()
expect(user).to.deep.eq({})
})
})
context('queues public methods', () => {
it('is able to write both values', async function () {
await Promise.all([
cache.setUser({ name: 'brian', authToken: 'auth-token-123' }),
cache.insertProject('foo'),
])
const json = await cache._read()
expect(json).to.deep.eq({
USER: {
name: 'brian',
authToken: 'auth-token-123',
},
PROJECTS: ['foo'],
PROJECT_PREFERENCES: {},
PROJECTS_CONFIG: {},
COHORTS: {},
})
})
})
context('cohorts', () => {
it('should get no cohorts when empty', async function () {
const cohorts = await cache.getCohorts()
expect(cohorts).to.deep.eq({})
})
it('should insert a cohort', async function () {
const cohort = {
name: 'cohort_id',
cohort: 'A',
}
await cache.insertCohort(cohort)
const cohorts = await cache.getCohorts()
expect(cohorts).to.deep.eq({ [cohort.name]: cohort })
})
})