Skip to content

Commit

Permalink
[qa] replace callback by promise
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoPennec committed Nov 27, 2024
1 parent f67cd9c commit 2d88ecb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
7 changes: 3 additions & 4 deletions src/store/api/productions.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,10 @@ export default {
return client.pput(`/api/data/projects/${production.id}`, data)
},

postAvatar(productionId, formData, callback) {
client.post(
postAvatar(productionId, formData) {
return client.ppost(
`/api/pictures/thumbnails/projects/${productionId}`,
formData,
callback
formData
)
},

Expand Down
18 changes: 6 additions & 12 deletions src/store/modules/productions.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,18 +397,12 @@ const actions = {
commit(PRODUCTION_PICTURE_FILE_SELECTED, formData)
},

uploadProductionAvatar({ commit, state }, productionId) {
return new Promise((resolve, reject) => {
productionsApi.postAvatar(
productionId,
state.productionAvatarFormData,
err => {
commit(PRODUCTION_AVATAR_UPLOADED, productionId)
if (err) reject(err)
else resolve()
}
)
})
async uploadProductionAvatar({ commit, state }, productionId) {
await productionsApi.postAvatar(
productionId,
state.productionAvatarFormData
)
commit(PRODUCTION_AVATAR_UPLOADED, productionId)
},

addPersonToTeam({ commit, state }, person) {
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/store/productions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,18 +440,18 @@ describe('Productions store', () => {
const state = {
productionAvatarFormData: 'form-data'
}
productionApi.postAvatar = vi.fn((_, __, callback) => callback(null))
productionApi.postAvatar = vi.fn(() => Promise.resolve())
await store.actions.uploadProductionAvatar({ commit: mockCommit, state }, 'production-id')
expect(mockCommit).toBeCalledTimes(1)
expect(mockCommit).toHaveBeenNthCalledWith(1, PRODUCTION_AVATAR_UPLOADED, 'production-id')

mockCommit = vi.fn()
productionApi.postAvatar = vi.fn((_, __, callback) => callback(new Error('error')))
productionApi.postAvatar = vi.fn(() => Promise.reject())
try {
await store.actions.uploadProductionAvatar({ commit: mockCommit, state }, 'production-id')
} catch (e) {
expect(mockCommit).toBeCalledTimes(1)
expect(mockCommit).toHaveBeenNthCalledWith(1, PRODUCTION_AVATAR_UPLOADED, 'production-id')
expect(productionApi.postAvatar).toBeCalledTimes(1)
expect(mockCommit).toBeCalledTimes(0)
}
})

Expand Down

0 comments on commit 2d88ecb

Please sign in to comment.