diff options
author | Maksim Sukharev <antreesy.web@gmail.com> | 2024-07-03 11:56:37 +0200 |
---|---|---|
committer | Maksim Sukharev <antreesy.web@gmail.com> | 2024-07-03 15:33:30 +0200 |
commit | 4b5daa2d45fb28fd01a038b3af2084665e2a0a9d (patch) | |
tree | 869a49d5588d3799595c457d6ad6ebde02b95949 | |
parent | 33b6c55311c6d4ed156298085327ee61183d2c66 (diff) |
fix(jest): centralise store mocks for testing
Signed-off-by: Maksim Sukharev <antreesy.web@gmail.com>
-rw-r--r-- | src/store/messagesStore.spec.js | 125 |
1 files changed, 46 insertions, 79 deletions
diff --git a/src/store/messagesStore.spec.js b/src/store/messagesStore.spec.js index e2ce0b247..7d230381d 100644 --- a/src/store/messagesStore.spec.js +++ b/src/store/messagesStore.spec.js @@ -68,9 +68,23 @@ jest.mock('@nextcloud/capabilities', () => ({ describe('messagesStore', () => { const TOKEN = 'XXTOKENXX' + const conversation = { + token: TOKEN, + lastMessage: { + id: 123, + }, + } + let localVue = null let testStoreConfig let store = null + let getActorIdMock + let getUserIdMock + let getActorTypeMock + let getDisplayNameMock + let conversationMock + let updateConversationLastMessageMock + let updateConversationLastReadMessageMock let updateConversationLastActiveAction let reactionsStore @@ -80,10 +94,26 @@ describe('messagesStore', () => { setActivePinia(createPinia()) reactionsStore = useReactionsStore() - testStoreConfig = cloneDeep(messagesStore) + testStoreConfig = cloneDeep(storeConfig) + getActorIdMock = jest.fn().mockReturnValue(() => 'actor-id-1') + getUserIdMock = jest.fn().mockReturnValue(() => 'actor-id-1') + getActorTypeMock = jest.fn().mockReturnValue(() => ATTENDEE.ACTOR_TYPE.USERS) + getDisplayNameMock = jest.fn().mockReturnValue(() => 'actor-display-name-1') + conversationMock = jest.fn().mockReturnValue(conversation) + updateConversationLastMessageMock = jest.fn() + updateConversationLastReadMessageMock = jest.fn() updateConversationLastActiveAction = jest.fn() - testStoreConfig.actions.updateConversationLastActive = updateConversationLastActiveAction + + testStoreConfig.modules.actorStore.getters.getActorId = getActorIdMock + testStoreConfig.modules.actorStore.getters.getUserId = getUserIdMock + testStoreConfig.modules.actorStore.getters.getActorType = getActorTypeMock + testStoreConfig.modules.actorStore.getters.getDisplayName = getDisplayNameMock + testStoreConfig.modules.conversationsStore.getters.conversation = jest.fn().mockReturnValue(conversationMock) + testStoreConfig.modules.conversationsStore.actions.updateConversationLastMessage = updateConversationLastMessageMock + testStoreConfig.modules.conversationsStore.actions.updateConversationLastReadMessage = updateConversationLastReadMessageMock + testStoreConfig.modules.conversationsStore.actions.updateConversationLastActive = updateConversationLastActiveAction + testStoreConfig.modules.pollStore.getters.debounceGetPollData = jest.fn() store = new Vuex.Store(testStoreConfig) }) @@ -104,11 +134,8 @@ describe('messagesStore', () => { }) test('doesn\'t add specific messages to the store', () => { - testStoreConfig = cloneDeep(storeConfig) - testStoreConfig.modules.pollStore.getters.debounceGetPollData = jest.fn() reactionsStore.resetReactions = jest.fn() reactionsStore.processReaction = jest.fn() - store = new Vuex.Store(testStoreConfig) const messages = [{ id: 2, @@ -229,9 +256,7 @@ describe('messagesStore', () => { let message beforeEach(() => { - testStoreConfig = cloneDeep(storeConfig) reactionsStore.resetReactions = jest.fn() - store = new Vuex.Store(testStoreConfig) message = { id: 10, @@ -361,24 +386,8 @@ describe('messagesStore', () => { describe('edit message', () => { let message - let conversationMock - let updateConversationLastMessageMock beforeEach(() => { - const conversation = { - token: TOKEN, - lastMessage: { - id: 10, - }, - } - - testStoreConfig = cloneDeep(messagesStore) - conversationMock = jest.fn().mockReturnValue(conversation) - updateConversationLastMessageMock = jest.fn() - testStoreConfig.getters.conversation = jest.fn().mockReturnValue(conversationMock) - testStoreConfig.actions.updateConversationLastMessage = updateConversationLastMessageMock - store = new Vuex.Store(testStoreConfig) - message = { id: 10, token: TOKEN, @@ -470,28 +479,13 @@ describe('messagesStore', () => { describe('temporary messages', () => { let mockDate - let getActorIdMock - let getActorTypeMock - let getDisplayNameMock let chatExtraStore beforeEach(() => { mockDate = new Date('2020-01-01 20:00:00') jest.spyOn(global, 'Date') .mockImplementation(() => mockDate) - - testStoreConfig = cloneDeep(messagesStore) chatExtraStore = useChatExtrasStore() - - getActorIdMock = jest.fn().mockReturnValue(() => 'actor-id-1') - getActorTypeMock = jest.fn().mockReturnValue(() => ATTENDEE.ACTOR_TYPE.USERS) - getDisplayNameMock = jest.fn().mockReturnValue(() => 'actor-display-name-1') - testStoreConfig.getters.getActorId = getActorIdMock - testStoreConfig.getters.getActorType = getActorTypeMock - testStoreConfig.getters.getDisplayName = getDisplayNameMock - testStoreConfig.actions.updateConversationLastActive = updateConversationLastActiveAction - - store = new Vuex.Store(testStoreConfig) }) test('creates temporary message', async () => { @@ -640,8 +634,10 @@ describe('messagesStore', () => { expect(updateConversationLastActiveAction).toHaveBeenCalledWith(expect.anything(), TOKEN) // add again just replaces it - temporaryMessage.message = 'replaced' - store.dispatch('addTemporaryMessage', { token: TOKEN, message: temporaryMessage }) + store.dispatch('addTemporaryMessage', { + token: TOKEN, + message: { ...temporaryMessage, message: 'replaced' } + }) expect(store.getters.messagesList(TOKEN)).toMatchObject([{ id: 'temp-1577908800000', @@ -757,31 +753,9 @@ describe('messagesStore', () => { }) describe('last read message markers', () => { - let conversationMock - let getUserIdMock - let updateConversationLastReadMessageMock - beforeEach(() => { - const conversation = { - lastMessage: { - id: 123, - }, - } - - testStoreConfig = cloneDeep(messagesStore) - - getUserIdMock = jest.fn() - conversationMock = jest.fn().mockReturnValue(conversation) - updateConversationLastReadMessageMock = jest.fn() - testStoreConfig.getters.conversation = jest.fn().mockReturnValue(conversationMock) - testStoreConfig.getters.getUserId = jest.fn().mockReturnValue(getUserIdMock) - testStoreConfig.actions.updateConversationLastReadMessage = updateConversationLastReadMessageMock - testStoreConfig.actions.addConversation = jest.fn() - const response = generateOCSResponse({ payload: conversation }) updateLastReadMessage.mockResolvedValue(response) - - store = new Vuex.Store(testStoreConfig) }) test('stores visual last read message id per token', () => { @@ -793,7 +767,7 @@ describe('messagesStore', () => { }) test('clears last read message', async () => { - getUserIdMock.mockReturnValue('user-1') + getUserIdMock.mockReturnValue(() => 'user-1') store.dispatch('setVisualLastReadMessageId', { token: TOKEN, id: 100 }) await store.dispatch('clearLastReadMessage', { @@ -813,7 +787,7 @@ describe('messagesStore', () => { }) test('clears last read message for federated conversation', async () => { - getUserIdMock.mockReturnValue('federated-user-1') + getUserIdMock.mockReturnValue(() => 'federated-user-1') conversationMock.mockReturnValue({ lastMessage: {}, remoteServer: 'nextcloud.com', @@ -835,7 +809,7 @@ describe('messagesStore', () => { }) test('clears last read message and update visually', async () => { - getUserIdMock.mockReturnValue('user-1') + getUserIdMock.mockReturnValue(() => 'user-1') store.dispatch('setVisualLastReadMessageId', { token: TOKEN, id: 100 }) await store.dispatch('clearLastReadMessage', { @@ -855,7 +829,7 @@ describe('messagesStore', () => { }) test('clears last read message for guests', async () => { - getUserIdMock.mockReturnValue(null) + getUserIdMock.mockReturnValue(() => null) store.dispatch('setVisualLastReadMessageId', { token: TOKEN, id: 100 }) await store.dispatch('clearLastReadMessage', { @@ -875,7 +849,7 @@ describe('messagesStore', () => { }) test('updates last read message', async () => { - getUserIdMock.mockReturnValue('user-1') + getUserIdMock.mockReturnValue(() => 'user-1') const response = generateOCSResponse({ payload: { unreadMessages: 0, @@ -903,7 +877,7 @@ describe('messagesStore', () => { }) test('updates last read message and update visually', async () => { - getUserIdMock.mockReturnValue('user-1') + getUserIdMock.mockReturnValue(() => 'user-1') const response = generateOCSResponse({ payload: { unreadMessages: 0, @@ -931,7 +905,7 @@ describe('messagesStore', () => { }) test('updates last read message for guests', async () => { - getUserIdMock.mockReturnValue(null) + getUserIdMock.mockReturnValue(() => null) store.dispatch('setVisualLastReadMessageId', { token: TOKEN, id: 100 }) await store.dispatch('updateLastReadMessage', { @@ -1790,7 +1764,7 @@ describe('messagesStore', () => { lastMessage: { id: 100 }, lastReadMessage: 50, }) - getUserIdMock.mockReturnValue('current-user') + getUserIdMock.mockReturnValue(() => 'current-user') const temporaryMessage = { id: 'temp-123', @@ -1985,11 +1959,10 @@ describe('messagesStore', () => { */ function setupWithValues(lastKnownMessageId, lastConversationMessageId) { store.dispatch('setLastKnownMessageId', { token: TOKEN, id: 123 }) - const conversationMock = jest.fn().mockReturnValue({ + conversationMock.mockReturnValue({ + token: TOKEN, lastMessage: { id: lastConversationMessageId }, }) - testStoreConfig.getters.conversation = jest.fn().mockReturnValue(conversationMock) - store = new Vuex.Store(testStoreConfig) store.dispatch('setLastKnownMessageId', { token: TOKEN, id: lastKnownMessageId }) } @@ -2015,12 +1988,6 @@ describe('messagesStore', () => { let messageExpected beforeEach(() => { - localVue = createLocalVue() - localVue.use(Vuex) - - testStoreConfig = cloneDeep(storeConfig) - store = new Vuex.Store(testStoreConfig) - message1 = { id: 1, token: TOKEN, |