Skip to content

Commit

Permalink
feat: Favorites update optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Merkur39 committed Oct 30, 2024
1 parent 46c3400 commit bad2b81
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 26 deletions.
19 changes: 7 additions & 12 deletions src/components/Actions/favorite.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import minilog from 'cozy-minilog'

import { updateContact } from '../../connections/allContacts'
import { mergeContact } from '../../helpers/mergeContact'

const log = minilog('connections/allContacts')

Expand All @@ -27,19 +27,14 @@ export const favorite = ({ client, selection, clearSelection, t }) => {
action: async () => {
const contactToUpdate = isAllFavorite ? selection : noFavoriteSelected
const favorite = isAllFavorite ? false : true
const contactsToSave = contactToUpdate.map(contact =>
mergeContact(contact, {
cozyMetadata: { favorite }
})
)

try {
await Promise.all(
contactToUpdate.map(contact =>
updateContact({
client,
contact,
attributes: {
cozyMetadata: { favorite }
}
})
)
)
await client.saveAll(contactsToSave)
} catch (error) {
log.error('Error updating contact', error)
} finally {
Expand Down
46 changes: 32 additions & 14 deletions src/components/Actions/favorite.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { favorite } from './favorite'
import { updateContact } from '../../connections/allContacts'

jest.mock('../../connections/allContacts', () => ({
updateContact: jest.fn()
}))

describe('favorite', () => {
const clearSelection = jest.fn()
const client = {
saveAll: jest.fn()
}

const favoriteSelection = [
{
Expand All @@ -22,7 +20,7 @@ describe('favorite', () => {
}
}
]
const unfavoriteSelection = [
const notfavoriteSelection = [
{
_id: '100',
cozyMetadata: {
Expand All @@ -36,11 +34,17 @@ describe('favorite', () => {
}
}
]
const mixedSelection = [favoriteSelection[0], unfavoriteSelection[0]]
const mixedFavoriteSelection = [favoriteSelection[0], notfavoriteSelection[0]]

beforeEach(() => {
clearSelection.mockClear()
client.saveAll.mockClear()
})

it('should return action with "add_favorite" label & "star-outline" icon', async () => {
const action = favorite({
selection: unfavoriteSelection,
client,
selection: notfavoriteSelection,
clearSelection,
t: jest.fn(k => k)
})
Expand All @@ -49,7 +53,8 @@ describe('favorite', () => {
expect(action.icon).toBe('star-outline')

const action2 = favorite({
selection: mixedSelection,
client,
selection: mixedFavoriteSelection,
clearSelection,
t: jest.fn(k => k)
})
Expand All @@ -60,6 +65,7 @@ describe('favorite', () => {

it('should return action with "remove_favorite" label & "star" icon', async () => {
const action = favorite({
client,
selection: favoriteSelection,
clearSelection,
t: jest.fn(k => k)
Expand All @@ -69,17 +75,29 @@ describe('favorite', () => {
expect(action.icon).toBe('star')
})

it('Should only update not favorite contacts', async () => {
it('Should call "saveAll" & "clearSelection" one time with "mixedFavoriteSelection"', async () => {
const action = favorite({
selection: mixedSelection,
client,
selection: mixedFavoriteSelection,
clearSelection,
t: jest.fn(k => k)
})

await action.action()
expect(client.saveAll).toHaveBeenCalledTimes(1)
expect(clearSelection).toHaveBeenCalledTimes(1)
})

it('Should call "saveAll" & "clearSelection" one time with "notfavoriteSelection"', async () => {
const action = favorite({
client,
selection: notfavoriteSelection,
clearSelection,
t: jest.fn(k => k)
})

expect(action.label).toBe('SelectionBar.add_favorite')
expect(action.icon).toBe('star-outline')
await action.action()
expect(updateContact).toHaveBeenCalledTimes(1)
expect(client.saveAll).toHaveBeenCalledTimes(1)
expect(clearSelection).toHaveBeenCalledTimes(1)
})
})

0 comments on commit bad2b81

Please sign in to comment.