From d6bd3a9677822570a9e2c5240dc42e74da6dc376 Mon Sep 17 00:00:00 2001 From: Maksim Sukharev Date: Thu, 22 Feb 2024 11:57:37 +0100 Subject: [PATCH] fix(jest): use nextcloud/l10n library in tests, omit some console logs Signed-off-by: Maksim Sukharev --- .../Message/MessagePart/Reactions.spec.js | 1 + src/stores/__tests__/guestName.spec.js | 11 ++--------- src/stores/__tests__/reactions.spec.js | 9 ++++++++- src/stores/guestName.js | 2 +- src/test-setup.js | 7 ++++--- 5 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/components/MessagesList/MessagesGroup/Message/MessagePart/Reactions.spec.js b/src/components/MessagesList/MessagesGroup/Message/MessagePart/Reactions.spec.js index 18872c34eb3..5975bae6307 100644 --- a/src/components/MessagesList/MessagesGroup/Message/MessagePart/Reactions.spec.js +++ b/src/components/MessagesList/MessagesGroup/Message/MessagePart/Reactions.spec.js @@ -282,6 +282,7 @@ describe('Reactions.vue', () => { test('fetches reactions details when they are not available', async () => { // Arrange reactionsStore.resetReactions(token, messageId) + console.debug = jest.fn() jest.spyOn(reactionsStore, 'fetchReactions') const wrapper = shallowMount(Reactions, { diff --git a/src/stores/__tests__/guestName.spec.js b/src/stores/__tests__/guestName.spec.js index e797deec17a..30afa6872c8 100644 --- a/src/stores/__tests__/guestName.spec.js +++ b/src/stores/__tests__/guestName.spec.js @@ -106,12 +106,6 @@ describe('guestNameStore', () => { expect(store.getGuestName('token-1', 'actor-id1')).toBe('Guest') }) - test('translates default guest name', () => { - - expect(store.getGuestName('token-1', 'actor-id0')).toBe('Guest') - expect(global.t).toHaveBeenCalledWith('spreed', 'Guest') - }) - test('gets suffix with guest display name', () => { // Arrange const actor1 = { @@ -123,8 +117,7 @@ describe('guestNameStore', () => { store.addGuestName(actor1, { noUpdate: false }) // Assert - expect(store.getGuestNameWithGuestSuffix('token-1', 'actor-id1')).toBe('{guest} (guest)') - expect(global.t).toHaveBeenCalledWith('spreed', '{guest} (guest)', { guest: 'actor-display-name-one' }) + expect(store.getGuestNameWithGuestSuffix('token-1', 'actor-id1')).toBe('actor-display-name-one (guest)') }) test('does not get suffix for translatable default guest name', () => { @@ -139,7 +132,6 @@ describe('guestNameStore', () => { // Assert expect(store.getGuestNameWithGuestSuffix('token-1', 'actor-id1')).toBe('Guest') - expect(global.t).toHaveBeenCalledWith('spreed', 'Guest') }) test('stores the display name when guest submits it', async () => { @@ -196,6 +188,7 @@ describe('guestNameStore', () => { actorId: 'actor-id1', actorDisplayName: 'old actor 1', } + console.error = jest.fn() vuexStore.dispatch('setCurrentUser', { uid: 'actor-id1' }) store.addGuestName(actor1, { noUpdate: false }) diff --git a/src/stores/__tests__/reactions.spec.js b/src/stores/__tests__/reactions.spec.js index 367219cd192..c41b49efdc3 100644 --- a/src/stores/__tests__/reactions.spec.js +++ b/src/stores/__tests__/reactions.spec.js @@ -67,6 +67,7 @@ describe('reactionsStore', () => { // Arrange const response = generateOCSResponse({ payload: reactions }) getReactionsDetails.mockResolvedValue(response) + console.debug = jest.fn() // Act reactionsStore.fetchReactions() @@ -74,7 +75,7 @@ describe('reactionsStore', () => { // Assert expect(reactionsStore.getReactions(token, messageId)).toEqual(reactions) }) - it('updates reactions from the store', () => { + it('does not update reactions in the store twice', () => { // Arrange const newReactions = { '🎄': [ @@ -95,6 +96,11 @@ describe('reactionsStore', () => { messageId, reactionsDetails: newReactions }) + reactionsStore.updateReactions({ + token, + messageId, + reactionsDetails: newReactions + }) // Assert expect(reactionsStore.getReactions(token, messageId)).toEqual(newReactions) @@ -183,6 +189,7 @@ describe('reactionsStore', () => { getReactionsDetails.mockResolvedValue(response) jest.spyOn(reactionsStore, 'removeReaction') jest.spyOn(reactionsStore, 'fetchReactions') + console.debug = jest.fn() // Act await reactionsStore.processReaction(token, message) diff --git a/src/stores/guestName.js b/src/stores/guestName.js index a9ebe088681..1ea3d2523a5 100644 --- a/src/stores/guestName.js +++ b/src/stores/guestName.js @@ -123,7 +123,7 @@ export const useGuestNameStore = defineStore('guestName', { actorId, actorDisplayName: previousName, }, { noUpdate: false }) - console.debug(error) + console.error(error) } }, }, diff --git a/src/test-setup.js b/src/test-setup.js index 0fb52d7ba6f..da1bf3dc832 100644 --- a/src/test-setup.js +++ b/src/test-setup.js @@ -24,6 +24,8 @@ import 'regenerator-runtime/runtime' import Vue from 'vue' +import { translate, translatePlural } from '@nextcloud/l10n' + jest.mock('extendable-media-recorder', () => ({ MediaRecorder: jest.fn(), register: jest.fn(), @@ -125,9 +127,8 @@ global.BroadcastChannel = jest.fn(() => ({ window.URL.createObjectURL = jest.fn() window.URL.revokeObjectURL = jest.fn() -// TODO: use nextcloud-l10n lib once https://github.com/nextcloud/nextcloud-l10n/issues/271 is solved -global.t = jest.fn().mockImplementation((app, text) => text) -global.n = jest.fn().mockImplementation((app, text) => text) +global.t = translate +global.n = translatePlural Vue.prototype.t = global.t Vue.prototype.n = global.n