Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Favorites update optimization #1017

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }
})
Merkur39 marked this conversation as resolved.
Show resolved Hide resolved
)

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)
})
})
Loading