From 3ec33f81dd80300540a7a553df597184d1bf72d3 Mon Sep 17 00:00:00 2001 From: DorraJaouad Date: Fri, 8 Sep 2023 14:23:30 +0200 Subject: [PATCH] Remove the duplicated actions in guestName store and fix naming Signed-off-by: DorraJaouad --- src/components/CallView/shared/LocalVideo.vue | 2 +- .../CallView/shared/ReactionToaster.vue | 4 +- src/components/CallView/shared/Screen.vue | 2 +- .../CallView/shared/VideoVue.spec.js | 4 +- src/components/CallView/shared/VideoVue.vue | 2 +- .../MediaSettings/MediaSettings.vue | 2 +- .../MessagesGroup/Message/Message.spec.js | 4 +- .../MessagesGroup/Message/Message.vue | 2 +- .../MessagesGroup/MessagesGroup.spec.js | 22 +-- .../MessagesGroup/MessagesGroup.vue | 2 +- .../NewMessage/NewMessageTypingIndicator.vue | 2 +- src/components/SetGuestUsername.vue | 10 +- src/mixins/getParticipants.js | 6 +- src/store/messagesStore.js | 8 +- src/store/messagesStore.spec.js | 34 ++--- src/stores/__tests__/guestName.spec.js | 138 ++++++++++++++++++ .../{guestNameStore.js => guestName.js} | 54 +++---- src/stores/guestNameStore.spec.js | 93 ------------ 18 files changed, 208 insertions(+), 183 deletions(-) create mode 100644 src/stores/__tests__/guestName.spec.js rename src/stores/{guestNameStore.js => guestName.js} (62%) delete mode 100644 src/stores/guestNameStore.spec.js diff --git a/src/components/CallView/shared/LocalVideo.vue b/src/components/CallView/shared/LocalVideo.vue index 8b902c1842e..f88b4e8af1d 100644 --- a/src/components/CallView/shared/LocalVideo.vue +++ b/src/components/CallView/shared/LocalVideo.vue @@ -82,7 +82,7 @@ import NcButton from '@nextcloud/vue/dist/Components/NcButton.js' import VideoBackground from './VideoBackground.vue' import video from '../../../mixins/video.js' -import { useGuestNameStore } from '../../../stores/guestNameStore.js' +import { useGuestNameStore } from '../../../stores/guestName.js' import { ConnectionState } from '../../../utils/webrtc/models/CallParticipantModel.js' export default { diff --git a/src/components/CallView/shared/ReactionToaster.vue b/src/components/CallView/shared/ReactionToaster.vue index bcef4063e8e..9050607b8e4 100644 --- a/src/components/CallView/shared/ReactionToaster.vue +++ b/src/components/CallView/shared/ReactionToaster.vue @@ -48,7 +48,7 @@ import usernameToColor from '@nextcloud/vue/dist/Functions/usernameToColor.js' import TransitionWrapper from '../../TransitionWrapper.vue' -import { useGuestNameStore } from '../../../stores/guestNameStore.js' +import { useGuestNameStore } from '../../../stores/guestName.js' export default { name: 'ReactionToaster', @@ -154,7 +154,7 @@ export default { id: model.attributes.peerId, reaction, name: isLocalModel - ? this.$store.getters.getDisplayName() || this.guestNameStore.getGuestName() + ? this.$store.getters.getDisplayName() || t('spreed', 'Guest') : this.getParticipantName(model), seed: Math.random(), }) diff --git a/src/components/CallView/shared/Screen.vue b/src/components/CallView/shared/Screen.vue index 00a1deb3e01..4b8bf4ea94f 100644 --- a/src/components/CallView/shared/Screen.vue +++ b/src/components/CallView/shared/Screen.vue @@ -41,7 +41,7 @@ import SHA1 from 'crypto-js/sha1.js' import VideoBottomBar from './VideoBottomBar.vue' -import { useGuestNameStore } from '../../../stores/guestNameStore.js' +import { useGuestNameStore } from '../../../stores/guestName.js' export default { diff --git a/src/components/CallView/shared/VideoVue.spec.js b/src/components/CallView/shared/VideoVue.spec.js index f411256e99c..a7ab4852fe0 100644 --- a/src/components/CallView/shared/VideoVue.spec.js +++ b/src/components/CallView/shared/VideoVue.spec.js @@ -40,7 +40,6 @@ describe('VideoVue.vue', () => { let localVue let store let testStoreConfig - let pinia let callParticipantModel @@ -89,8 +88,7 @@ describe('VideoVue.vue', () => { beforeEach(() => { localVue = createLocalVue() localVue.use(Vuex) - pinia = createPinia() - setActivePinia(pinia) + setActivePinia(createPinia()) testStoreConfig = cloneDeep(storeConfig) // eslint-disable-next-line import/no-named-as-default-member diff --git a/src/components/CallView/shared/VideoVue.vue b/src/components/CallView/shared/VideoVue.vue index 1cc8b40fa93..2342428074d 100644 --- a/src/components/CallView/shared/VideoVue.vue +++ b/src/components/CallView/shared/VideoVue.vue @@ -111,7 +111,7 @@ import VideoBottomBar from './VideoBottomBar.vue' import { ATTENDEE } from '../../../constants.js' import video from '../../../mixins/video.js' import { EventBus } from '../../../services/EventBus.js' -import { useGuestNameStore } from '../../../stores/guestNameStore.js' +import { useGuestNameStore } from '../../../stores/guestName.js' import { ConnectionState } from '../../../utils/webrtc/models/CallParticipantModel.js' export default { diff --git a/src/components/MediaSettings/MediaSettings.vue b/src/components/MediaSettings/MediaSettings.vue index 471f885458a..157b2ab720d 100644 --- a/src/components/MediaSettings/MediaSettings.vue +++ b/src/components/MediaSettings/MediaSettings.vue @@ -208,7 +208,7 @@ import { CALL, VIRTUAL_BACKGROUND } from '../../constants.js' import { devices } from '../../mixins/devices.js' import isInLobby from '../../mixins/isInLobby.js' import BrowserStorage from '../../services/BrowserStorage.js' -import { useGuestNameStore } from '../../stores/guestNameStore.js' +import { useGuestNameStore } from '../../stores/guestName.js' import { localMediaModel } from '../../utils/webrtc/index.js' export default { diff --git a/src/components/MessagesList/MessagesGroup/Message/Message.spec.js b/src/components/MessagesList/MessagesGroup/Message/Message.spec.js index 7a4ed3121fd..a96c6a5515a 100644 --- a/src/components/MessagesList/MessagesGroup/Message/Message.spec.js +++ b/src/components/MessagesList/MessagesGroup/Message/Message.spec.js @@ -52,15 +52,13 @@ describe('Message.vue', () => { let conversationProps let injected let getActorTypeMock - let pinia const getVisualLastReadMessageIdMock = jest.fn() beforeEach(() => { localVue = createLocalVue() localVue.use(vOutsideEvents) localVue.use(Vuex) - pinia = createPinia() - setActivePinia(pinia) + setActivePinia(createPinia()) conversationProps = { token: TOKEN, diff --git a/src/components/MessagesList/MessagesGroup/Message/Message.vue b/src/components/MessagesList/MessagesGroup/Message/Message.vue index ee0fb58a505..9db4d8505cf 100644 --- a/src/components/MessagesList/MessagesGroup/Message/Message.vue +++ b/src/components/MessagesList/MessagesGroup/Message/Message.vue @@ -263,7 +263,7 @@ import { useIsInCall } from '../../../../composables/useIsInCall.js' import { ATTENDEE, CONVERSATION, PARTICIPANT } from '../../../../constants.js' import participant from '../../../../mixins/participant.js' import { EventBus } from '../../../../services/EventBus.js' -import { useGuestNameStore } from '../../../../stores/guestNameStore.js' +import { useGuestNameStore } from '../../../../stores/guestName.js' const isTranslationAvailable = getCapabilities()?.spreed?.config?.chat?.translations?.length > 0 diff --git a/src/components/MessagesList/MessagesGroup/MessagesGroup.spec.js b/src/components/MessagesList/MessagesGroup/MessagesGroup.spec.js index 1387452a8ca..bc00b2f4cf0 100644 --- a/src/components/MessagesList/MessagesGroup/MessagesGroup.spec.js +++ b/src/components/MessagesList/MessagesGroup/MessagesGroup.spec.js @@ -8,27 +8,23 @@ import MessagesSystemGroup from './MessagesSystemGroup.vue' import { ATTENDEE } from '../../../constants.js' import storeConfig from '../../../store/storeConfig.js' -import { useGuestNameStore } from '../../../stores/guestNameStore.js' +import { useGuestNameStore } from '../../../stores/guestName.js' describe('MessagesGroup.vue', () => { const TOKEN = 'XXTOKENXX' let store let localVue let testStoreConfig - let getGuestNameMock - let pinia + let guestNameStore beforeEach(() => { localVue = createLocalVue() localVue.use(Vuex) - pinia = createPinia() - setActivePinia(pinia) + setActivePinia(createPinia()) - const guestNameStore = useGuestNameStore() + guestNameStore = useGuestNameStore() testStoreConfig = cloneDeep(storeConfig) - getGuestNameMock = jest.fn() - guestNameStore.getGuestName = getGuestNameMock // eslint-disable-next-line import/no-named-as-default-member store = new Vuex.Store(testStoreConfig) }) @@ -186,7 +182,13 @@ describe('MessagesGroup.vue', () => { }) test('renders guest display name', () => { - getGuestNameMock.mockReturnValue('guest-one-display-name') + // Arrange + guestNameStore.addGuestName({ + token: TOKEN, + actorId: 'actor-1', + actorDisplayName: 'guest-one-display-name', + }, { noUpdate: false }) + const wrapper = shallowMount(MessagesGroup, { localVue, store, @@ -239,8 +241,6 @@ describe('MessagesGroup.vue', () => { message = messagesEl.at(1) expect(message.attributes('id')).toBe('110') expect(message.attributes('actorid')).toBe('actor-1') - - expect(getGuestNameMock).toHaveBeenCalledWith(TOKEN, 'actor-1') }) test('renders deleted guest display name', () => { diff --git a/src/components/MessagesList/MessagesGroup/MessagesGroup.vue b/src/components/MessagesList/MessagesGroup/MessagesGroup.vue index e44ea47da88..0a1a71ba8b0 100644 --- a/src/components/MessagesList/MessagesGroup/MessagesGroup.vue +++ b/src/components/MessagesList/MessagesGroup/MessagesGroup.vue @@ -48,7 +48,7 @@ import AuthorAvatar from './AuthorAvatar.vue' import Message from './Message/Message.vue' import { ATTENDEE } from '../../../constants.js' -import { useGuestNameStore } from '../../../stores/guestNameStore.js' +import { useGuestNameStore } from '../../../stores/guestName.js' export default { name: 'MessagesGroup', diff --git a/src/components/NewMessage/NewMessageTypingIndicator.vue b/src/components/NewMessage/NewMessageTypingIndicator.vue index 008e08522f0..61b4813ec03 100644 --- a/src/components/NewMessage/NewMessageTypingIndicator.vue +++ b/src/components/NewMessage/NewMessageTypingIndicator.vue @@ -45,7 +45,7 @@ import escapeHtml from 'escape-html' import AvatarWrapper from '../AvatarWrapper/AvatarWrapper.vue' -import { useGuestNameStore } from '../../stores/guestNameStore.js' +import { useGuestNameStore } from '../../stores/guestName.js' export default { name: 'NewMessageTypingIndicator', diff --git a/src/components/SetGuestUsername.vue b/src/components/SetGuestUsername.vue index 5621e743d72..d4e780d5555 100644 --- a/src/components/SetGuestUsername.vue +++ b/src/components/SetGuestUsername.vue @@ -55,7 +55,7 @@ import NcButton from '@nextcloud/vue/dist/Components/NcButton.js' import NcTextField from '@nextcloud/vue/dist/Components/NcTextField.js' import { setGuestUserName } from '../services/participantsService.js' -import { useGuestNameStore } from '../stores/guestNameStore.js' +import { useGuestNameStore } from '../stores/guestName.js' export default { name: 'SetGuestUsername', @@ -127,11 +127,11 @@ export default { const previousName = this.$store.getters.getDisplayName() try { this.$store.dispatch('setDisplayName', this.guestUserName) - this.guestNameStore.forceGuestName({ + this.guestNameStore.addGuestName({ token: this.token, actorId: this.$store.getters.getActorId(), actorDisplayName: this.guestUserName, - }) + }, { noUpdate: false }) await setGuestUserName(this.token, this.guestUserName) if (this.guestUserName !== '') { localStorage.setItem('nick', this.guestUserName) @@ -141,11 +141,11 @@ export default { this.isEditingUsername = false } catch (exception) { this.$store.dispatch('setDisplayName', previousName) - this.guestNameStore.forceGuestName({ + this.guestNameStore.addGuestName({ token: this.token, actorId: this.$store.getters.getActorId(), actorDisplayName: previousName, - }) + }, { noUpdate: false }) console.debug(exception) } }, diff --git a/src/mixins/getParticipants.js b/src/mixins/getParticipants.js index bcd891dfee1..ea5df10154c 100644 --- a/src/mixins/getParticipants.js +++ b/src/mixins/getParticipants.js @@ -31,7 +31,7 @@ import { emit } from '@nextcloud/event-bus' import { PARTICIPANT } from '../constants.js' import { EventBus } from '../services/EventBus.js' import { fetchParticipants } from '../services/participantsService.js' -import { useGuestNameStore } from '../stores/guestNameStore.js' +import { useGuestNameStore } from '../stores/guestName.js' import CancelableRequest from '../utils/cancelableRequest.js' import isInLobby from './isInLobby.js' @@ -143,11 +143,11 @@ const getParticipants = { }) if (participant.participantType === PARTICIPANT.TYPE.GUEST || participant.participantType === PARTICIPANT.TYPE.GUEST_MODERATOR) { - this.guestNameStore.forceGuestName({ + this.guestNameStore.addGuestName({ token, actorId: Hex.stringify(SHA1(participant.sessionIds[0])), actorDisplayName: participant.displayName, - }) + }, { noUpdate: false }) } else if (participant.actorType === 'users' && hasUserStatuses) { emit('user_status:status.updated', { status: participant.status, diff --git a/src/store/messagesStore.js b/src/store/messagesStore.js index 84f2f91420f..66022eb10ea 100644 --- a/src/store/messagesStore.js +++ b/src/store/messagesStore.js @@ -40,7 +40,7 @@ import { addReactionToMessage, removeReactionFromMessage, } from '../services/messagesService.js' -import { useGuestNameStore } from '../stores/guestNameStore.js' +import { useGuestNameStore } from '../stores/guestName.js' import CancelableRequest from '../utils/cancelableRequest.js' /** @@ -821,7 +821,7 @@ const actions = { if (message.actorType === ATTENDEE.ACTOR_TYPE.GUESTS) { // update guest display names cache const guestNameStore = useGuestNameStore() - guestNameStore.setGuestNameIfEmpty(message) + guestNameStore.addGuestName(message, { noUpdate: true }) } context.dispatch('processMessage', message) newestKnownMessageId = Math.max(newestKnownMessageId, message.id) @@ -912,7 +912,7 @@ const actions = { if (message.actorType === ATTENDEE.ACTOR_TYPE.GUESTS) { // update guest display names cache const guestNameStore = useGuestNameStore() - guestNameStore.setGuestNameIfEmpty(message) + guestNameStore.addGuestName(message, { noUpdate: true }) } context.dispatch('processMessage', message) newestKnownMessageId = Math.max(newestKnownMessageId, message.id) @@ -1038,7 +1038,7 @@ const actions = { // force in case the display name has changed since // the last fetch const guestNameStore = useGuestNameStore() - guestNameStore.forceGuestName(message) + guestNameStore.addGuestName(message, { noUpdate: false }) } context.dispatch('processMessage', message) if (!lastMessage || message.id > lastMessage.id) { diff --git a/src/store/messagesStore.spec.js b/src/store/messagesStore.spec.js index c6f36adf919..1c0514dd21d 100644 --- a/src/store/messagesStore.spec.js +++ b/src/store/messagesStore.spec.js @@ -18,7 +18,7 @@ import { lookForNewMessages, postNewMessage, } from '../services/messagesService.js' -import { useGuestNameStore } from '../stores/guestNameStore.js' +import { useGuestNameStore } from '../stores/guestName.js' import { generateOCSErrorResponse, generateOCSResponse } from '../test-helpers.js' import CancelableRequest from '../utils/cancelableRequest.js' import messagesStore from './messagesStore.js' @@ -44,13 +44,11 @@ describe('messagesStore', () => { let testStoreConfig let store = null let updateConversationLastActiveAction - let pinia beforeEach(() => { localVue = createLocalVue() localVue.use(Vuex) - pinia = createPinia() - setActivePinia(pinia) + setActivePinia(createPinia()) testStoreConfig = cloneDeep(messagesStore) @@ -769,7 +767,7 @@ describe('messagesStore', () => { describe('fetchMessages', () => { let updateLastCommonReadMessageAction - let setGuestNameIfEmptyAction + let addGuestNameAction let cancelFunctionMock beforeEach(() => { @@ -777,9 +775,9 @@ describe('messagesStore', () => { const guestNameStore = useGuestNameStore() updateLastCommonReadMessageAction = jest.fn() - setGuestNameIfEmptyAction = jest.fn() + addGuestNameAction = jest.fn() testStoreConfig.actions.updateLastCommonReadMessage = updateLastCommonReadMessageAction - guestNameStore.setGuestNameIfEmpty = setGuestNameIfEmptyAction + guestNameStore.addGuestName = addGuestNameAction cancelFunctionMock = jest.fn() CancelableRequest.mockImplementation((request) => { @@ -833,7 +831,7 @@ describe('messagesStore', () => { expect(updateLastCommonReadMessageAction) .toHaveBeenCalledWith(expect.anything(), { token: TOKEN, lastCommonReadMessage: 123 }) - expect(setGuestNameIfEmptyAction).toHaveBeenCalledWith(messages[1]) + expect(addGuestNameAction).toHaveBeenCalledWith(messages[1], { noUpdate: true }) expect(store.getters.messagesList(TOKEN)).toStrictEqual(messages) expect(store.getters.getFirstKnownMessageId(TOKEN)).toBe(100) @@ -881,7 +879,7 @@ describe('messagesStore', () => { expect(updateLastCommonReadMessageAction) .toHaveBeenCalledWith(expect.anything(), { token: TOKEN, lastCommonReadMessage: 123 }) - expect(setGuestNameIfEmptyAction).toHaveBeenCalledWith(messages[1]) + expect(addGuestNameAction).toHaveBeenCalledWith(messages[1], { noUpdate: true }) expect(store.getters.messagesList(TOKEN)).toStrictEqual(messages) expect(store.getters.getFirstKnownMessageId(TOKEN)).toBe(100) @@ -924,7 +922,7 @@ describe('messagesStore', () => { describe('get message context', () => { let updateLastCommonReadMessageAction - let setGuestNameIfEmptyAction + let addGuestNameAction let cancelFunctionMock beforeEach(() => { @@ -932,9 +930,9 @@ describe('messagesStore', () => { const guestNameStore = useGuestNameStore() updateLastCommonReadMessageAction = jest.fn() - setGuestNameIfEmptyAction = jest.fn() + addGuestNameAction = jest.fn() testStoreConfig.actions.updateLastCommonReadMessage = updateLastCommonReadMessageAction - guestNameStore.setGuestNameIfEmpty = setGuestNameIfEmptyAction + guestNameStore.addGuestName = addGuestNameAction cancelFunctionMock = jest.fn() CancelableRequest.mockImplementation((request) => { @@ -986,7 +984,7 @@ describe('messagesStore', () => { expect(updateLastCommonReadMessageAction) .toHaveBeenCalledWith(expect.anything(), { token: TOKEN, lastCommonReadMessage: 1 }) - expect(setGuestNameIfEmptyAction).toHaveBeenCalledWith(messages[1]) + expect(addGuestNameAction).toHaveBeenCalledWith(messages[1], { noUpdate: true }) expect(store.getters.messagesList(TOKEN)).toStrictEqual(messages) expect(store.getters.getFirstKnownMessageId(TOKEN)).toBe(1) @@ -1057,7 +1055,7 @@ describe('messagesStore', () => { expect(updateLastCommonReadMessageAction).toHaveBeenNthCalledWith(1, expect.anything(), { token: TOKEN, lastCommonReadMessage: 2 }) expect(updateLastCommonReadMessageAction).toHaveBeenNthCalledWith(2, expect.anything(), { token: TOKEN, lastCommonReadMessage: 2 }) - expect(setGuestNameIfEmptyAction).toHaveBeenCalledWith(messagesContext[1]) + expect(addGuestNameAction).toHaveBeenCalledWith(messagesContext[1], { noUpdate: true }) expect(store.getters.messagesList(TOKEN)).toStrictEqual([...messagesFetch, ...messagesContext]) expect(store.getters.getFirstKnownMessageId(TOKEN)).toBe(1) @@ -1069,7 +1067,7 @@ describe('messagesStore', () => { let updateLastCommonReadMessageAction let updateConversationLastMessageAction let updateUnreadMessagesMutation - let forceGuestNameAction + let addGuestNameAction let cancelFunctionMocks let conversationMock let getActorIdMock @@ -1092,10 +1090,10 @@ describe('messagesStore', () => { updateConversationLastMessageAction = jest.fn() updateLastCommonReadMessageAction = jest.fn() updateUnreadMessagesMutation = jest.fn() - forceGuestNameAction = jest.fn() + addGuestNameAction = jest.fn() testStoreConfig.actions.updateConversationLastMessage = updateConversationLastMessageAction testStoreConfig.actions.updateLastCommonReadMessage = updateLastCommonReadMessageAction - guestNameStore.forceGuestName = forceGuestNameAction + guestNameStore.addGuestName = addGuestNameAction testStoreConfig.mutations.updateUnreadMessages = updateUnreadMessagesMutation cancelFunctionMocks = [] @@ -1157,7 +1155,7 @@ describe('messagesStore', () => { expect(updateLastCommonReadMessageAction) .toHaveBeenCalledWith(expect.anything(), { token: TOKEN, lastCommonReadMessage: 123 }) - expect(forceGuestNameAction).toHaveBeenCalledWith(messages[1]) + expect(addGuestNameAction).toHaveBeenCalledWith(messages[1], { noUpdate: false }) expect(store.getters.messagesList(TOKEN)).toStrictEqual(messages) expect(store.getters.getLastKnownMessageId(TOKEN)).toBe(100) diff --git a/src/stores/__tests__/guestName.spec.js b/src/stores/__tests__/guestName.spec.js new file mode 100644 index 00000000000..48a7625ab79 --- /dev/null +++ b/src/stores/__tests__/guestName.spec.js @@ -0,0 +1,138 @@ +import { createPinia, setActivePinia } from 'pinia' + +import { useGuestNameStore } from '../guestName.js' + +describe('guestNameStore', () => { + let store + + beforeEach(() => { + setActivePinia(createPinia()) + store = useGuestNameStore() + }) + + afterEach(() => { + jest.clearAllMocks() + }) + + test('sets guest name if empty', () => { + // Arrange + const actor1 = { + token: 'token-1', + actorId: 'actor-id1', + actorDisplayName: 'actor-display-name-one', + } + + // Act + store.addGuestName(actor1, { noUpdate: true }) + + // Assert + expect(store.getGuestName('token-1', 'actor-id1')).toBe('actor-display-name-one') + // non-existing token + expect(store.getGuestName('token-2', 'actor-id1')).toBe('Guest') + // non-existing actorId + expect(store.getGuestName('token-1', 'actor-id2')).toBe('Guest') + }) + + test('does not overwrite guest name if not empty', () => { + // Arrange + const actor1 = { + token: 'token-1', + actorId: 'actor-id1', + actorDisplayName: 'actor-display-name-one', + } + const actor1Altered = { + token: 'token-1', + actorId: 'actor-id1', + actorDisplayName: 'actor-display-name-another', + } + + // Act + store.addGuestName(actor1, { noUpdate: true }) + // attempt overwriting + store.addGuestName(actor1Altered, { noUpdate: true }) + + // Assert + expect(store.getGuestName('token-1', 'actor-id1')).toBe('actor-display-name-one') + }) + + test('forces overwriting guest name', () => { + // Arrange + const actor1 = { + token: 'token-1', + actorId: 'actor-id1', + actorDisplayName: 'actor-display-name-one', + } + const actor1Altered = { + token: 'token-1', + actorId: 'actor-id1', + actorDisplayName: 'actor-display-name-another', + } + + // Act + store.addGuestName(actor1, { noUpdate: false }) + // attempt overwriting + store.addGuestName(actor1Altered, { noUpdate: false }) + + // Assert + expect(store.getGuestName('token-1', 'actor-id1')).toBe('actor-display-name-another') + }) + + test('clear guest name', () => { + // Arrange + const actor1 = { + token: 'token-1', + actorId: 'actor-id1', + actorDisplayName: 'actor-display-name-one', + } + + const actor1Altered = { + token: 'token-1', + actorId: 'actor-id1', + actorDisplayName: '', + } + + // Act + store.addGuestName(actor1, { noUpdate: true }) + store.addGuestName(actor1Altered, { noUpdate: false }) + + // Assert + 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 = { + token: 'token-1', + actorId: 'actor-id1', + actorDisplayName: 'actor-display-name-one', + } + + 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' }) + }) + + test('does not get suffix for translatable default guest name', () => { + // Arrange + const actor1 = { + token: 'token-1', + actorId: 'actor-id1', + actorDisplayName: t('spreed', 'Guest'), + } + + store.addGuestName(actor1, { noUpdate: false }) + + // Assert + expect(store.getGuestNameWithGuestSuffix('token-1', 'actor-id1')).toBe('Guest') + expect(global.t).toHaveBeenCalledWith('spreed', 'Guest') + }) + +}) diff --git a/src/stores/guestNameStore.js b/src/stores/guestName.js similarity index 62% rename from src/stores/guestNameStore.js rename to src/stores/guestName.js index e1ec4abf2e4..8c986136e3c 100644 --- a/src/stores/guestNameStore.js +++ b/src/stores/guestName.js @@ -2,6 +2,7 @@ * @copyright Copyright (c) 2019 Joas Schilling * * @author Joas Schilling + * @author Dorra Jaouad * * @license AGPL-3.0-or-later * @@ -28,17 +29,25 @@ export const useGuestNameStore = defineStore('guestName', { }), actions: { - /** - * Gets the participant display name - * - * @param {string} token the conversation's token - * @param {string} actorId the participant actorId - * @return {string} the participant name - */ + /** + * Gets the participant display name + * + * @param {string} token the conversation's token + * @param {string} actorId the participant actorId + * @return {string} the participant name + */ getGuestName(token, actorId) { return this.guestNames[token]?.[actorId] ?? t('spreed', 'Guest') }, + /** + * Gets the participant display name with suffix + * if the display name is not default translatable Guest + * + * @param {string} token the conversation's token + * @param {string} actorId the participant actorId + * @return {string} the participant name with/without suffix + */ getGuestNameWithGuestSuffix(token, actorId) { const displayName = this.getGuestName(token, actorId) if (displayName === t('spreed', 'Guest')) { @@ -52,13 +61,14 @@ export const useGuestNameStore = defineStore('guestName', { /** * Adds a guest name to the store * - * @param {object} data the wrapping object; - * @param {boolean} data.noUpdate Only set the guest name if it was not set before + * @param {object} data the wrapping object * @param {string} data.token the token of the conversation * @param {string} data.actorId the guest * @param {string} data.actorDisplayName the display name to set + * @param {object} options options + * @param {boolean} options.noUpdate Override the display name or set it if it is empty */ - addGuestName({ noUpdate, token, actorId, actorDisplayName }) { + addGuestName({ token, actorId, actorDisplayName }, { noUpdate }) { if (!this.guestNames[token]) { Vue.set(this.guestNames, token, {}) @@ -73,29 +83,5 @@ export const useGuestNameStore = defineStore('guestName', { Vue.set(this.guestNames[token], actorId, actorDisplayName) } }, - - /** - * Add guest name of a chat message to the store - * - * @param {object} data the wrapping object; - * @param {string} data.token the token of the conversation - * @param {string} data.actorId the guest - * @param {string} data.actorDisplayName the display name to set - */ - setGuestNameIfEmpty({ token, actorId, actorDisplayName }) { - this.addGuestName({ noUpdate: true, token, actorId, actorDisplayName }) - }, - - /** - * Add guest name of a chat message to the store - * - * @param {object} data the wrapping object; - * @param {string} data.token the token of the conversation - * @param {string} data.actorId the guest - * @param {string} data.actorDisplayName the display name to set - */ - forceGuestName({ token, actorId, actorDisplayName }) { - this.addGuestName({ noUpdate: false, token, actorId, actorDisplayName }) - }, }, }) diff --git a/src/stores/guestNameStore.spec.js b/src/stores/guestNameStore.spec.js deleted file mode 100644 index fe9b2438b55..00000000000 --- a/src/stores/guestNameStore.spec.js +++ /dev/null @@ -1,93 +0,0 @@ -import { createPinia, setActivePinia } from 'pinia' - -import { useGuestNameStore } from './guestNameStore.js' - -describe('guestNameStore', () => { - let store = null - let pinia - - beforeEach(() => { - pinia = createPinia() - setActivePinia(pinia) - store = useGuestNameStore() - }) - - afterEach(() => { - jest.clearAllMocks() - }) - - test('sets guest name if empty', () => { - store.setGuestNameIfEmpty({ - token: 'token-1', - actorId: 'actor-id1', - actorDisplayName: 'actor-display-name-one', - }) - store.setGuestNameIfEmpty({ - token: 'token-1', - actorId: 'actor-id2', - actorDisplayName: 'actor-display-name-two', - }) - - expect(store.getGuestName('token-1', 'actor-id1')).toBe('actor-display-name-one') - expect(store.getGuestName('token-1', 'actor-id2')).toBe('actor-display-name-two') - expect(store.getGuestName('token-2', 'actor-id1')).toBe('Guest') - expect(store.getGuestName('token-1', 'actor-id3')).toBe('Guest') - }) - - test('does not override guest name if not empty', () => { - store.setGuestNameIfEmpty({ - token: 'token-1', - actorId: 'actor-id1', - actorDisplayName: 'actor-display-name-one', - }) - - // attempt overwriting - store.setGuestNameIfEmpty({ - token: 'token-1', - actorId: 'actor-id1', - actorDisplayName: 'actor-display-name-another', - }) - - expect(store.getGuestName('token-1', 'actor-id1')).toBe('actor-display-name-one') - }) - - test('force override guest name', () => { - store.setGuestNameIfEmpty({ - token: 'token-1', - actorId: 'actor-id1', - actorDisplayName: 'actor-display-name-one', - }) - - // attempt overwriting - store.forceGuestName({ - token: 'token-1', - actorId: 'actor-id1', - actorDisplayName: 'actor-display-name-another', - }) - - expect(store.getGuestName('token-1', 'actor-id1')).toBe('actor-display-name-another') - }) - - test('clear guest name', () => { - store.setGuestNameIfEmpty({ - token: 'token-1', - actorId: 'actor-id1', - actorDisplayName: 'actor-display-name-one', - }) - - store.forceGuestName({ - token: 'token-1', - actorId: 'actor-id1', - actorDisplayName: '', - }) - - 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') - }) - -})