Skip to content

Commit

Permalink
refactor: Use parameter destructuring for jsonapi.fromPouchResult
Browse files Browse the repository at this point in the history
This replies to #1506 (comment)
  • Loading branch information
Ldoppea committed Sep 5, 2024
1 parent 821bd90 commit 29a31bc
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
19 changes: 12 additions & 7 deletions packages/cozy-pouch-link/src/CozyPouchLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,12 @@ class PouchLink extends CozyLink {
res.limit = limit
withRows = true
}
return jsonapi.fromPouchResult(res, withRows, doctype, this.client)
return jsonapi.fromPouchResult({
res,
withRows,
doctype,
client: this.client
})
}

async executeMutation(mutation, result, forward) {
Expand All @@ -620,12 +625,12 @@ class PouchLink extends CozyLink {
return forward(mutation, result)
}

return jsonapi.fromPouchResult(
pouchRes,
false,
getDoctypeFromOperation(mutation),
this.client
)
return jsonapi.fromPouchResult({
res: pouchRes,
withRows: false,
doctype: getDoctypeFromOperation(mutation),
client: this.client
})
}

async createDocument(mutation) {
Expand Down
2 changes: 1 addition & 1 deletion packages/cozy-pouch-link/src/jsonapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const normalizeLinks = (docRef, doctype, client) => {

const filterDeletedDocumentsFromRows = doc => !!doc

export const fromPouchResult = (res, withRows, doctype, client) => {
export const fromPouchResult = ({ res, withRows, doctype, client }) => {
// Sometimes, queries are transformed by Collections and they call a dedicated
// cozy-stack route. When this is the case, we want to be able to replicate the same
// query from cozy-pouch-link. It is not possible as-is because the received data
Expand Down
48 changes: 42 additions & 6 deletions packages/cozy-pouch-link/src/jsonapi.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import CozyClient from 'cozy-client'

import { fromPouchResult, normalizeDoc } from './jsonapi'

const BART_FIXTURE = {
Expand Down Expand Up @@ -26,6 +28,10 @@ const DELETED_DOC_FIXTURE = {
delete: true
}

const token = 'fake_token'
const uri = 'https://claude.mycozy.cloud'
const client = new CozyClient({ token, uri })

describe('doc normalization', () => {
it('keeps the highest between rev and _rev and removes the rev attribute', () => {
const normalized = normalizeDoc(
Expand Down Expand Up @@ -58,7 +64,12 @@ describe('jsonapi', () => {
const res = {
rows: [BART_FIXTURE, LISA_FIXTURE, MARGE_FIXTURE, DELETED_DOC_FIXTURE]
}
const normalized = fromPouchResult(res, true, 'io.cozy.simpsons')
const normalized = fromPouchResult({
res,
withRows: true,
doctype: 'io.cozy.simpsons',
client
})
expect(normalized.data[0].name).toBe('Bart')
expect(normalized.data[0].id).toBe(1)
expect(normalized.data[0]._id).toBe(1)
Expand All @@ -76,13 +87,23 @@ describe('jsonapi', () => {
describe('pagination', () => {
it('has no next when there is no pagination information', () => {
const res = { rows: [BART_FIXTURE] }
const normalized = fromPouchResult(res, true, 'io.cozy.simpsons')
const normalized = fromPouchResult({
res,
withRows: true,
doctype: 'io.cozy.simpsons',
client
})
expect(normalized.next).toBe(false)
})

it('paginates when there is a total_rows field greater than the rows number', () => {
const res = { rows: [BART_FIXTURE], total_rows: 3 }
const normalized = fromPouchResult(res, true, 'io.cozy.simpsons')
const normalized = fromPouchResult({
res,
withRows: true,
doctype: 'io.cozy.simpsons',
client
})
expect(normalized.next).toBe(true)
})

Expand All @@ -91,17 +112,32 @@ describe('jsonapi', () => {
rows: [BART_FIXTURE, MARGE_FIXTURE, LISA_FIXTURE],
total_rows: 3
}
const normalized = fromPouchResult(res, true, 'io.cozy.simpsons')
const normalized = fromPouchResult({
res,
withRows: true,
doctype: 'io.cozy.simpsons',
client
})
expect(normalized.next).toBe(false)
})

it('paginates when there is a limit field', () => {
const res = { rows: [BART_FIXTURE, LISA_FIXTURE], limit: 2 }
const normalized = fromPouchResult(res, true, 'io.cozy.simpsons')
const normalized = fromPouchResult({
res,
withRows: true,
doctype: 'io.cozy.simpsons',
client
})
expect(normalized.next).toBe(true)

const lastRes = { rows: [MARGE_FIXTURE], limit: 2 }
const lastNormalized = fromPouchResult(lastRes, true, 'io.cozy.simpsons')
const lastNormalized = fromPouchResult({
res: lastRes,
withRows: true,
doctype: 'io.cozy.simpsons',
client
})
expect(lastNormalized.next).toBe(false)
})
})
Expand Down

0 comments on commit 29a31bc

Please sign in to comment.