Skip to content

Commit

Permalink
fixup! Add tests for reactions store
Browse files Browse the repository at this point in the history
  • Loading branch information
DorraJaouad committed Dec 30, 2023
1 parent 8919075 commit 6e95640
Showing 1 changed file with 114 additions and 4 deletions.
118 changes: 114 additions & 4 deletions src/stores/__tests__/reactions.spec.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
import { createLocalVue } from '@vue/test-utils'
import { setActivePinia, createPinia } from 'pinia'
import Vuex from 'vuex'

import { getReactionsDetails } from '../../services/messagesService.js'
import { showError } from '@nextcloud/dialogs'

import { getReactionsDetails, addReactionToMessage, removeReactionFromMessage } from '../../services/messagesService.js'
import vuexStore from '../../store/index.js'
import { useReactionsStore } from '../reactions.js'

jest.mock('../../services/messagesService', () => ({
getReactionsDetails: jest.fn(),
addReactionToMessage: jest.fn(),
removeReactionFromMessage: jest.fn(),
}))

jest.mock('@nextcloud/dialogs', () => ({
showSuccess: jest.fn(),
showError: jest.fn(),
}))

describe('reactionsStore', () => {
let reactionsStore
let token
let messageId
let reactions
let localVue

beforeEach(() => {
setActivePinia(createPinia())
reactionsStore = useReactionsStore()

localVue = createLocalVue()
localVue.use(Vuex)

token = 'token1'
messageId = 'parent-id'
reactions = {
Expand Down Expand Up @@ -143,7 +159,7 @@ describe('reactionsStore', () => {
const actors = reactionsStore.getReactions(token, messageId)['πŸŽ„']
expect(actors.length).toEqual(2) // should not add a new actor
})
it('removes a reaction to the store', async () => {
it('removes a reaction from the store', async () => {
// Arrange
const message = {
systemMessage: 'reaction_revoked',
Expand Down Expand Up @@ -181,9 +197,103 @@ describe('reactionsStore', () => {
await reactionsStore.processReaction(message)

// Assert
expect(reactionsStore.fetchReactions).toHaveBeenCalled()
expect(getReactionsDetails).toHaveBeenCalled()
expect(reactionsStore.removeReaction).toHaveBeenCalled()
expect(Object.keys(reactionsStore.reactions[token][messageId])).toEqual(['πŸŽ„', 'πŸ”₯'])
})
it('does not fetch reactions when receiving a reaction_deleted system message', async () => {
// Arrange
const message = {
systemMessage: 'reaction_deleted',
actorDisplayName: 'Test Actor',
actorId: '123',
actorType: 'user',
timestamp: Date.now(),
token,
parent: {
id: messageId
},
message: 'reaction removed'
}

// Act
await reactionsStore.processReaction(message)

// Assert
expect(getReactionsDetails).not.toHaveBeenCalled()
})

describe('error handling', () => {
it('does not add a reaction when the API call fails', async () => {
// Arrange
addReactionToMessage.mockRejectedValue(new Error('API call failed'))
jest.spyOn(vuexStore, 'commit')

const message = {
actorId: 'admin',
actorType: 'users',
id: messageId,
markdown: true,
message: 'This is a message to have reactions on',
reactions: { 'πŸŽ„': 2, 'πŸ”₯': 2, 'πŸ”’': 2 },
reactionsSelf: ['πŸ”₯'],
timestamp: 1703668230,
token
}
vuexStore.commit('addMessage', message) // add a message to the store

// Act
await reactionsStore.addReactionToMessage({ token, messageId, selectedEmoji: 'πŸ˜…' })

// Assert
expect(vuexStore.commit).toHaveBeenCalledWith('addReactionToMessage', {
token,
messageId,
reaction: 'πŸ˜…'
})
expect(showError).toHaveBeenCalled()
expect(Object.keys(reactionsStore.getReactions(token, messageId))).toEqual(['πŸŽ„', 'πŸ”₯', 'πŸ”’']) // no reaction added
})
it('does not remove a reaction when the API call fails', async () => {
// Arrange
removeReactionFromMessage.mockRejectedValue(new Error('API call failed'))
jest.spyOn(vuexStore, 'commit')

const message = {
actorId: 'admin',
actorType: 'users',
id: messageId,
markdown: true,
message: 'This is a message to have reactions on',
reactions: { 'πŸŽ„': 2, 'πŸ”₯': 2, 'πŸ”’': 2 },
reactionsSelf: ['πŸ”₯'],
timestamp: 1703668230,
token
}

vuexStore.commit('addMessage', message) // add a message to the store

// Act
await reactionsStore.removeReactionFromMessage({ token, messageId, selectedEmoji: 'πŸŽ„' })

// Assert
expect(vuexStore.commit).toHaveBeenCalledWith('removeReactionFromMessage', {
token,
messageId,
reaction: 'πŸŽ„'
})
expect(showError).toHaveBeenCalled()
expect(reactionsStore.getReactions(token, messageId)['πŸŽ„'].length).toEqual(2) // no reaction removed
})
it('shows an error when the API call of fetching reactions fails', async () => {
// Arrange
getReactionsDetails.mockRejectedValue(new Error('API call failed'))
console.debug = jest.fn()

// Act
await reactionsStore.fetchReactions({ token, messageId })

// Assert
expect(console.debug).toHaveBeenCalled()
})
})
})

0 comments on commit 6e95640

Please sign in to comment.