Skip to content

Commit

Permalink
feat(NotesCollection): Add get method
Browse files Browse the repository at this point in the history
This method will allow me to use real time on cozy-notes
  • Loading branch information
cballevre committed Dec 19, 2023
1 parent 383cfd5 commit 72574fb
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
13 changes: 13 additions & 0 deletions docs/api/cozy-stack-client.md
Expand Up @@ -1181,12 +1181,25 @@ Implements `DocumentCollection` API to interact with the /notes endpoint of the
**Kind**: global class

* [NotesCollection](#NotesCollection)
* [.get(id)](#NotesCollection+get) ⇒ <code>Object</code>
* [.all()](#NotesCollection+all) ⇒ <code>Object</code>
* [.destroy(note)](#NotesCollection+destroy) ⇒ <code>Object</code>
* [.create(options)](#NotesCollection+create) ⇒ <code>Object</code>
* [.fetchURL(note)](#NotesCollection+fetchURL) ⇒ <code>Object</code>
* [.getDefaultSchema()](#NotesCollection+getDefaultSchema) ⇒ <code>object</code>

<a name="NotesCollection+get"></a>

### notesCollection.get(id) ⇒ <code>Object</code>
Fetches the note data

**Kind**: instance method of [<code>NotesCollection</code>](#NotesCollection)
**Returns**: <code>Object</code> - Information about the note

| Param | Type | Description |
| --- | --- | --- |
| id | <code>string</code> | Note id |

<a name="NotesCollection+all"></a>

### notesCollection.all() ⇒ <code>Object</code>
Expand Down
14 changes: 14 additions & 0 deletions packages/cozy-stack-client/src/NotesCollection.js
Expand Up @@ -23,6 +23,20 @@ class NotesCollection extends DocumentCollection {
super(NOTES_DOCTYPE, stackClient)
}

/**
* Fetches the note data
*
* @param {string} id Note id
* @returns {{data}} Information about the note
*/
async get(id) {
const resp = await this.stackClient.fetchJSON('GET', `/notes/${id}`)

return {
data: normalizeNote(resp.data)
}
}

/**
* Fetches all notes
*
Expand Down
31 changes: 31 additions & 0 deletions packages/cozy-stack-client/src/NotesCollection.spec.js
Expand Up @@ -17,6 +17,37 @@ describe('NotesCollection', () => {
jest.restoreAllMocks()
})

describe('get', () => {
const { stackClient, collection } = setup()

it('should call the appropriate route', async () => {
jest.spyOn(stackClient, 'fetchJSON').mockResolvedValue({
data: [],
links: {},
meta: { count: 0 }
})

await collection.get('id123')
expect(stackClient.fetchJSON).toHaveBeenCalledWith('GET', '/notes/id123')
})

it('should normalize document', async () => {
jest.spyOn(stackClient, 'fetchJSON').mockResolvedValue({
data: { _id: '1', attributes: { title: 'Test Note' } },
links: {},
meta: { count: 0 }
})
const result = await collection.get('id123')
expect(result.data).toEqual({
_id: '1',
_type: 'io.cozy.notes',
id: '1',
title: 'Test Note',
attributes: { title: 'Test Note' }
})
})
})

describe('all', () => {
const { stackClient, collection } = setup()

Expand Down

0 comments on commit 72574fb

Please sign in to comment.