-
Notifications
You must be signed in to change notification settings - Fork 12
/
ContactsCollection.spec.js
78 lines (73 loc) · 2.1 KB
/
ContactsCollection.spec.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
import CozyStackClient from './CozyStackClient'
import ContactsCollection from './ContactsCollection'
const stackMyselfResponse = {
data: {
type: 'io.cozy.contacts',
id: 'bf91cce0-ef48-0137-2638-543d7eb8149c',
attributes: {
fullname: 'Alice',
email: [{ address: 'alice@example.com', primary: true }]
},
meta: {
rev: '1-6516671ec'
}
}
}
describe('ContactsCollection', () => {
const setup = () => {
const stackClient = new CozyStackClient({})
stackClient.fetchJSON = jest
.fn()
.mockImplementation(async (method, route) => {
if (method === 'POST' && route === '/contacts/myself') {
return stackMyselfResponse
} else if (route.includes('_index')) {
return { result: 'exists' }
} else {
return { docs: [] }
}
})
const col = new ContactsCollection('io.cozy.contacts', stackClient)
return { stackClient, col }
}
beforeEach(() => {
jest.spyOn(console, 'warn').mockImplementation(() => {})
})
it('call find route if there is no selector', async () => {
const { col, stackClient } = setup()
await col.find()
expect(stackClient.fetchJSON).toHaveBeenCalledWith(
'POST',
'/data/io.cozy.contacts/_find',
expect.any(Object)
)
})
it('should use a special route for the myself contact', async () => {
const { col, stackClient } = setup()
const resp = await col.find({
me: true
})
expect(stackClient.fetchJSON).toHaveBeenCalledWith(
'POST',
'/contacts/myself'
)
expect(resp.data).toEqual([
expect.objectContaining({
_id: 'bf91cce0-ef48-0137-2638-543d7eb8149c',
id: 'bf91cce0-ef48-0137-2638-543d7eb8149c',
fullname: 'Alice'
})
])
})
it('should not use a special route for other types of find', async () => {
const { col, stackClient } = setup()
await col.find({
name: 'Alice'
})
expect(stackClient.fetchJSON).toHaveBeenCalledWith(
'POST',
'/data/io.cozy.contacts/_find',
expect.objectContaining({ selector: { name: 'Alice' } })
)
})
})