Skip to content

Commit

Permalink
Merge pull request #2626 from cozy/chore/remove-direct-dependency-to-…
Browse files Browse the repository at this point in the history
…cozy-stack-client

Backport of #2625

`cozy-stack-client` comes with `cozy-client` and declaring a direct
dependency to it means we need to keep it up-to-date with
`cozy-client`.
Removing this dependency simplifies `cozy-client` upgrades.

To this end, we need to remove all direct usage of `cozy-stack-client`
so we replace our own `createClientWithData` with `createMockClient`
from `cozy-client` as they serve the same purpose.

We take the opportunity to bump `cozy-pouch-link` to keep it up-to-date
and remove outdated versions of `cozy-client` and `cozy-stack-client`
from the bundle.

This should also fix an issue in the export page which requires 
`cozy-stack-client` v35.6.0.

```
### 🐛 Bug Fixes

* The export page can now request queued and running export service jobs

### 🔧 Tech

* Remove direct dependency to `cozy-stack-client`
* Bump `cozy-stack-client` from 35.3.1 to 35.6.0
* Bump `cozy-pouch-link` from 35.3.1 to 35.6.0
```
  • Loading branch information
taratatach authored Mar 9, 2023
2 parents 10431ca + e122640 commit 275fde2
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 123 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,10 @@
"cozy-konnector-libs": "4.44.0",
"cozy-logger": "^1.9.0",
"cozy-notifications": "0.12.0",
"cozy-pouch-link": "35.3.1",
"cozy-pouch-link": "35.6.0",
"cozy-realtime": "4.2.9",
"cozy-scripts": "6.3.0",
"cozy-sharing": "3.12.2",
"cozy-stack-client": "35.3.1",
"cozy-ui": "^79.0.0",
"d3": "5.11.0",
"date-fns": "1.30.1",
Expand Down
3 changes: 1 addition & 2 deletions renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
{
"packagePatterns": [
"cozy-client",
"cozy-pouch-link",
"cozy-stack-client"
"cozy-pouch-link"
],
"groupName": "cozy-client packages"
},
Expand Down
18 changes: 9 additions & 9 deletions src/ducks/account/services.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createMockClient } from 'cozy-client'
import { linkMyselfToAccounts, unlinkMyselfFromAccounts } from './services'
import { createClientWithData } from 'test/client'
import { ACCOUNT_DOCTYPE, CONTACT_DOCTYPE } from 'doctypes'
import { fetchSettings } from 'ducks/settings/helpers'
import get from 'lodash/get'
Expand Down Expand Up @@ -34,8 +34,8 @@ describe('linkMyselfToAccounts', () => {
}
]

const client = createClientWithData({
data: {
const client = createMockClient({
remote: {
[ACCOUNT_DOCTYPE]: accounts
}
})
Expand All @@ -62,8 +62,8 @@ describe('linkMyselfToAccounts', () => {
}
]

const client = createClientWithData({
data: {
const client = createMockClient({
remote: {
[ACCOUNT_DOCTYPE]: accounts
}
})
Expand Down Expand Up @@ -100,8 +100,8 @@ describe('unlinkMyselfToAccounts', () => {
}
]

const client = createClientWithData({
data: {
const client = createMockClient({
remote: {
[ACCOUNT_DOCTYPE]: accounts
}
})
Expand Down Expand Up @@ -136,8 +136,8 @@ describe('unlinkMyselfToAccounts', () => {
}
]

const client = createClientWithData({
data: {
const client = createMockClient({
remote: {
[ACCOUNT_DOCTYPE]: accounts
}
})
Expand Down
10 changes: 5 additions & 5 deletions src/ducks/budgetAlerts/service.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import CozyClient from 'cozy-client'
import { createClientWithData } from 'test/client'
import CozyClient, { createMockClient } from 'cozy-client'
import { TRANSACTION_DOCTYPE } from 'doctypes'
import fixture from 'test/fixtures/unit-tests.json'
import { runCategoryBudgetService } from './service'
import {
Expand Down Expand Up @@ -42,9 +42,9 @@ beforeEach(() => {
describe('service', () => {
const setup = ({ budgetAlerts, expenses }) => {
fetchCategoryAlerts.mockReturnValue(budgetAlerts)
const client = createClientWithData({
data: {
'io.cozy.bank.operations': expenses
const client = createMockClient({
remote: {
[TRANSACTION_DOCTYPE]: expenses
},
clientOptions: {
uri: 'https://test.mycozy.cloud'
Expand Down
2 changes: 1 addition & 1 deletion src/ducks/filters/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ describe('filter selectors', () => {
getTransactions.mockReturnValue(docStore['io.cozy.bank.operations'])
getAccounts.mockReturnValue(docStore[ACCOUNT_DOCTYPE])
getAllGroups.mockReturnValue(docStore['io.cozy.bank.groups'])
// TODO use createClientWithData instead of mocked selectors
// TODO use createMockClient instead of mocked selectors
getGroupsById.mockReturnValue(keyBy(docStore['io.cozy.bank.groups'], '_id'))
getAccountsById.mockReturnValue(
keyBy(docStore['io.cozy.bank.accounts'], '_id')
Expand Down
14 changes: 7 additions & 7 deletions src/ducks/groups/services.spec.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { createAutoGroups, removeDuplicateAccountsFromGroups } from './services'
import { createClientWithData } from 'test/client'
import { createMockClient } from 'cozy-client'
import { GROUP_DOCTYPE, ACCOUNT_DOCTYPE, schema } from 'doctypes'
import { fetchSettings } from 'ducks/settings/helpers'

jest.mock('ducks/settings/helpers')

describe('createAutoGroups', () => {
const setup = ({ data, processedAccounts }) => {
const client = createClientWithData({
data,
const setup = ({ remote, processedAccounts }) => {
const client = createMockClient({
remote,
clientOptions: { schema }
})

Expand All @@ -27,7 +27,7 @@ describe('createAutoGroups', () => {
describe('when the automatic group does not exist', () => {
it('should create a new automatic group', async () => {
const { client, settings } = setup({
data: {
remote: {
[ACCOUNT_DOCTYPE]: [{ _id: 'a1', type: 'Checkings' }]
},
processedAccounts: []
Expand All @@ -51,7 +51,7 @@ describe('createAutoGroups', () => {
describe('when the automatic group already exists', () => {
it('should add accounts to the existing group', async () => {
const { client, settings } = setup({
data: {
remote: {
[GROUP_DOCTYPE]: [
{
_id: 'checkings_auto',
Expand Down Expand Up @@ -85,7 +85,7 @@ describe('createAutoGroups', () => {
describe('duplicate accounts removal', () => {
it('should remove duplicate accounts from an existing group', async () => {
const { client } = setup({
data: {
remote: {
[GROUP_DOCTYPE]: [
{
_id: 'checkings_auto',
Expand Down
4 changes: 2 additions & 2 deletions src/ducks/settings/CategoryAlerts/CategoryAlertCard.spec.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { mount } from 'enzyme'
import React from 'react'
import { createMockClient } from 'cozy-client'
import AppLike from 'test/AppLike'
import { createClientWithData } from 'test/client'
import CategoryAlertCard from './CategoryAlertCard'
import getClient from 'selectors/getClient'
import { GROUP_DOCTYPE } from 'doctypes'
Expand All @@ -18,7 +18,7 @@ describe('category alert card', () => {
_id: 'group1337'
}
}
const client = createClientWithData({
const client = createMockClient({
queries: {
groups: {
doctype: GROUP_DOCTYPE,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { mount } from 'enzyme'
import React from 'react'
import { createMockClient } from 'cozy-client'
import CategoryAlertEditModal from './CategoryAlertEditModal'
import AccountIcon from 'components/AccountIcon'
import fixtures from 'test/fixtures/unit-tests.json'
import getClient from 'selectors/getClient'
import { createClientWithData } from 'test/client'
import Input from 'cozy-ui/transpiled/react/Input'
import { ACCOUNT_DOCTYPE } from 'doctypes'
import AppLike from 'test/AppLike'
Expand All @@ -16,7 +16,7 @@ jest.mock('components/AccountIcon', () => () => null)
describe('category alert edition modal', () => {
let client
beforeEach(() => {
client = createClientWithData({
client = createMockClient({
queries: {
accounts: {
data: fixtures[ACCOUNT_DOCTYPE],
Expand Down
5 changes: 2 additions & 3 deletions src/ducks/settings/helpers.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { mount } from 'enzyme'
import fixtures from 'test/fixtures/unit-tests.json'
import { ACCOUNT_DOCTYPE, GROUP_DOCTYPE, SETTINGS_DOCTYPE } from 'doctypes'
import AppLike from 'test/AppLike'
import CozyClient from 'cozy-client'
import { createClientWithData } from 'test/client'
import CozyClient, { createMockClient } from 'cozy-client'

import {
fetchSettings,
Expand Down Expand Up @@ -44,7 +43,7 @@ describe('defaulted settings', () => {

describe('withAccountOrGroupLabeller', () => {
const setup = ({ accountOrGroup }) => {
const client = createClientWithData({
const client = createMockClient({
queries: {
groups: {
doctype: GROUP_DOCTYPE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import React from 'react'
import { render, fireEvent, within } from '@testing-library/react'
import Alerter from 'cozy-ui/transpiled/react/Alerter'
import { format } from 'date-fns'
import { createMockClient } from 'cozy-client'

import AppLike from 'test/AppLike'
import { createClientWithData } from 'test/client'
import { ACCOUNT_DOCTYPE, TRANSACTION_DOCTYPE } from 'doctypes'
import { TrackerProvider, trackPage } from 'ducks/tracking/browser'
import { getTracker } from 'ducks/tracking/tracker'
Expand Down Expand Up @@ -44,7 +44,7 @@ jest.mock('ducks/transactions/helpers', () => ({
describe('transaction modal', () => {
let client
beforeEach(() => {
client = createClientWithData({
client = createMockClient({
queries: {
transactions: {
doctype: TRANSACTION_DOCTYPE,
Expand Down
5 changes: 2 additions & 3 deletions src/ducks/transactions/helpers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ import {
schema
} from 'doctypes'
import MockDate from 'mockdate'
import CozyClient from 'cozy-client'
import CozyClient, { createMockClient } from 'cozy-client'
import logger from 'cozy-client/dist/logger'

import { createClientWithData } from 'test/client'
import { getCategoryIdFromName } from 'ducks/categories/helpers'

// eslint-disable-next-line no-console
Expand Down Expand Up @@ -55,7 +54,7 @@ describe('reimbursements', () => {
transaction.reimbursements.data[0].bill

beforeEach(() => {
client = createClientWithData({
client = createMockClient({
queries: {
bills: {
doctype: BILLS_DOCTYPE,
Expand Down
27 changes: 14 additions & 13 deletions src/targets/services/helpers/helpers.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import CozyStackClient from 'cozy-stack-client'
import DocumentCollection from 'cozy-stack-client/dist/DocumentCollection'
import CozyClient from 'cozy-client'
import { getOptions, fetchChangesOrAll } from './helpers'

describe('getOptions', () => {
Expand Down Expand Up @@ -101,32 +100,34 @@ describe('getOptions', () => {
})

describe('fetchChangesOrAll', () => {
const client = new CozyStackClient()
const client = new CozyClient()
const documentCollection = jest.fn()

beforeEach(() => {
jest
.spyOn(DocumentCollection.prototype, 'fetchChanges')
documentCollection.fetchChanges = jest
.fn()
.mockResolvedValue({ documents: [], newLastSeq: '1234' })
jest
.spyOn(DocumentCollection.prototype, 'all')
documentCollection.all = jest
.fn()
.mockResolvedValue({ data: [], newLastSeq: '1234' })
jest.spyOn(client, 'collection').mockReturnValue(documentCollection)
})

afterEach(() => {
DocumentCollection.prototype.fetchChanges.mockClear()
DocumentCollection.prototype.all.mockClear()
documentCollection.fetchChanges.mockClear()
documentCollection.all.mockClear()
})

it('should return all documents if lastSeq is "0"', async () => {
const res = await fetchChangesOrAll(client, 'io.cozy.todos', '0')

expect(DocumentCollection.prototype.fetchChanges).toHaveBeenCalledWith({
expect(documentCollection.fetchChanges).toHaveBeenCalledWith({
since: '',
descending: true,
limit: 1
})

expect(DocumentCollection.prototype.all).toHaveBeenCalledWith({
expect(documentCollection.all).toHaveBeenCalledWith({
limit: null
})

Expand All @@ -136,11 +137,11 @@ describe('fetchChangesOrAll', () => {
it('should return changes if lastSeq is not "0"', async () => {
const res = await fetchChangesOrAll(client, 'io.cozy.todos', 'abcd')

expect(DocumentCollection.prototype.fetchChanges).toHaveBeenCalledWith({
expect(documentCollection.fetchChanges).toHaveBeenCalledWith({
since: 'abcd',
include_docs: true
})
expect(DocumentCollection.prototype.all).not.toHaveBeenCalled()
expect(documentCollection.all).not.toHaveBeenCalled()
expect(res).toStrictEqual({ documents: [], newLastSeq: '1234' })
})
})
33 changes: 1 addition & 32 deletions test/client.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import CozyClient, { Q } from 'cozy-client'
import CozyClient from 'cozy-client'
import { schema } from 'doctypes'
import { receiveQueryResult, initQuery } from 'cozy-client/dist/store'
import { normalizeDoc } from 'cozy-stack-client/dist/DocumentCollection'

const defaultOptions = {
uri: 'http://cozy.works:8080',
Expand Down Expand Up @@ -33,33 +31,4 @@ export const getClient = ({ uri, token, fetchJSONReturn } = defaultOptions) => {
return client
}

export const createClientWithData = ({ queries, data, clientOptions }) => {
const client = new CozyClient(clientOptions || {})
client.ensureStore()
for (let [queryName, queryOptions] of Object.entries(queries || {})) {
client.store.dispatch(
initQuery(queryName, queryOptions.definition || Q(queryOptions.doctype))
)
client.store.dispatch(
receiveQueryResult(queryName, {
data: queryOptions.data.map(doc =>
normalizeDoc(doc, queryOptions.doctype)
)
})
)
}
client.query = jest.fn().mockImplementation(qdef => {
if (data[qdef.doctype]) {
return { data: data[qdef.doctype] }
} else {
return { data: [] }
}
})

client.save = jest.fn()
client.stackClient.fetchJSON = jest.fn()

return client
}

export default getClient
Loading

0 comments on commit 275fde2

Please sign in to comment.