Skip to content

Commit

Permalink
test: Refactor ContactsCollection test describe
Browse files Browse the repository at this point in the history
  • Loading branch information
zatteo committed Apr 3, 2024
1 parent 7425c7d commit 6ec9814
Showing 1 changed file with 55 additions and 52 deletions.
107 changes: 55 additions & 52 deletions packages/cozy-stack-client/src/ContactsCollection.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,63 +16,66 @@ const stackMyselfResponse = {
}

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(() => {})
})
describe('find', () => {
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 }
}

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)
)
})
beforeEach(() => {
jest.spyOn(console, 'warn').mockImplementation(() => {})
})

it('should use a special route for the myself contact', async () => {
const { col, stackClient } = setup()
const resp = await col.find({
me: true
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)
)
})
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 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'
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' } })
)
})
expect(stackClient.fetchJSON).toHaveBeenCalledWith(
'POST',
'/data/io.cozy.contacts/_find',
expect.objectContaining({ selector: { name: 'Alice' } })
)
})
})

0 comments on commit 6ec9814

Please sign in to comment.