Skip to content

Commit 09731a4

Browse files
committed
Add unit tests for function and fix a problem revealed by it
Signed-off-by: Joas Schilling <[email protected]>
1 parent 8a749b9 commit 09731a4

File tree

3 files changed

+236
-84
lines changed

3 files changed

+236
-84
lines changed

src/components/LeftSidebar/ConversationsList/Conversation.spec.js

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -130,42 +130,36 @@ describe('Conversation.vue', () => {
130130
describe('author name', () => {
131131
test('displays last chat message with shortened author name', () => {
132132
testConversationLabel(item, 'Alice: hello')
133-
expect(messagesMock).toHaveBeenCalledWith(TOKEN)
134133
})
135134

136135
test('displays last chat message with author name if no space in name', () => {
137136
item.lastMessage.actorDisplayName = 'Bob'
138137
testConversationLabel(item, 'Bob: hello')
139-
expect(messagesMock).toHaveBeenCalledWith(TOKEN)
140138
})
141139

142140
test('displays own last chat message with "You" as author', () => {
143141
item.lastMessage.actorId = 'user-id-self'
144142

145143
testConversationLabel(item, 'You: hello')
146-
expect(messagesMock).toHaveBeenCalledWith(TOKEN)
147144
})
148145

149146
test('displays last system message without author', () => {
150147
item.lastMessage.message = 'Alice has joined the call'
151148
item.lastMessage.systemMessage = 'call_joined'
152149

153150
testConversationLabel(item, 'Alice has joined the call')
154-
expect(messagesMock).toHaveBeenCalledWith(TOKEN)
155151
})
156152

157153
test('displays last message without author in one to one conversations', () => {
158154
item.type = CONVERSATION.TYPE.ONE_TO_ONE
159155
testConversationLabel(item, 'hello')
160-
expect(messagesMock).toHaveBeenCalledWith(TOKEN)
161156
})
162157

163158
test('displays own last message with "You" author in one to one conversations', () => {
164159
item.type = CONVERSATION.TYPE.ONE_TO_ONE
165160
item.lastMessage.actorId = 'user-id-self'
166161

167162
testConversationLabel(item, 'You: hello')
168-
expect(messagesMock).toHaveBeenCalledWith(TOKEN)
169163
})
170164

171165
test('displays last guest message with default author when none set', () => {
@@ -174,14 +168,12 @@ describe('Conversation.vue', () => {
174168
item.lastMessage.actorType = ATTENDEE.ACTOR_TYPE.GUESTS
175169

176170
testConversationLabel(item, 'Guest: hello')
177-
expect(messagesMock).toHaveBeenCalledWith(TOKEN)
178171
})
179172

180173
test('displays last message for search results', () => {
181174
// search results have no actor id
182175
item.actorId = null
183176
testConversationLabel(item, 'Alice: hello', true)
184-
expect(messagesMock).toHaveBeenCalledWith(TOKEN)
185177
})
186178
})
187179

@@ -194,80 +186,6 @@ describe('Conversation.vue', () => {
194186
}
195187

196188
testConversationLabel(item, 'Alice: filename.jpg')
197-
expect(messagesMock).toHaveBeenCalledWith(TOKEN)
198-
})
199-
200-
describe('last message from messages store', () => {
201-
// see Conversation.lastChatMessage() description for the reasoning
202-
let displayedLastStoreMessage
203-
let lastMessageFromConversation
204-
205-
beforeEach(() => {
206-
displayedLastStoreMessage = {
207-
id: 100,
208-
actorId: 'user-id-alice',
209-
actorDisplayName: 'Alice Wonderland',
210-
actorType: ATTENDEE.ACTOR_TYPE.USERS,
211-
message: 'hello from store',
212-
messageParameters: {},
213-
systemMessage: '',
214-
timestamp: 100,
215-
}
216-
217-
lastMessageFromConversation = {
218-
id: 110,
219-
actorId: 'user-id-alice',
220-
actorDisplayName: 'Alice Wonderland',
221-
actorType: ATTENDEE.ACTOR_TYPE.USERS,
222-
message: 'hello from conversation',
223-
messageParameters: {},
224-
systemMessage: '',
225-
timestamp: 100,
226-
}
227-
228-
item.lastMessage = lastMessageFromConversation
229-
230-
messagesMock.mockClear().mockReturnValue({
231-
100: displayedLastStoreMessage,
232-
})
233-
})
234-
235-
test('displays store message when more recent', () => {
236-
displayedLastStoreMessage.timestamp = 2000
237-
238-
testConversationLabel(item, 'Alice: hello from store')
239-
expect(messagesMock).toHaveBeenCalledWith(TOKEN)
240-
})
241-
242-
test('displays conversation message when last one is a temporary command message', () => {
243-
messagesMock.mockClear().mockReturnValue({
244-
'temp-100': displayedLastStoreMessage,
245-
})
246-
247-
displayedLastStoreMessage.timestamp = 2000
248-
displayedLastStoreMessage.id = 'temp-100'
249-
displayedLastStoreMessage.message = '/me doing things'
250-
251-
testConversationLabel(item, 'Alice: hello from conversation')
252-
expect(messagesMock).toHaveBeenCalledWith(TOKEN)
253-
})
254-
255-
test('displays conversation message when last one is a bot message', () => {
256-
displayedLastStoreMessage.timestamp = 2000
257-
displayedLastStoreMessage.actorType = ATTENDEE.ACTOR_TYPE.BOTS
258-
259-
testConversationLabel(item, 'Alice: hello from conversation')
260-
expect(messagesMock).toHaveBeenCalledWith(TOKEN)
261-
})
262-
263-
test('displays store message when last one is a changelog bot message', () => {
264-
displayedLastStoreMessage.timestamp = 2000
265-
displayedLastStoreMessage.actorType = ATTENDEE.ACTOR_TYPE.BOTS
266-
displayedLastStoreMessage.actorId = ATTENDEE.CHANGELOG_BOT_ID
267-
268-
testConversationLabel(item, 'Alice: hello from store')
269-
expect(messagesMock).toHaveBeenCalledWith(TOKEN)
270-
})
271189
})
272190
})
273191

src/store/conversationsStore.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,9 @@ const actions = {
419419
&& lastMessage.systemMessage !== 'reaction_deleted'
420420
&& lastMessage.systemMessage !== 'reaction_revoked'
421421
&& lastMessage.systemMessage !== 'message_deleted'
422-
&& ((typeof lastMessage.id.startsWith === 'function' && !lastMessage.id.startsWith('temp-'))
423-
|| !lastMessage.message.startsWith('/'))) {
422+
&& !(typeof lastMessage.id.startsWith === 'function'
423+
&& lastMessage.id.startsWith('temp-')
424+
&& lastMessage.message.startsWith('/'))) {
424425
commit('updateConversationLastMessage', { token, lastMessage })
425426
}
426427
},

src/store/conversationsStore.spec.js

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ jest.mock('../services/conversationsService', () => ({
5050

5151
describe('conversationsStore', () => {
5252
const testToken = 'XXTOKENXX'
53+
const previousLastMessage = {
54+
actorType: 'users',
55+
actorId: 'admin',
56+
systemMessage: '',
57+
id: 31,
58+
message: 'Message 1',
59+
}
5360
let testStoreConfig = null
5461
let testConversation
5562
let localVue = null
@@ -592,6 +599,232 @@ describe('conversationsStore', () => {
592599
})
593600
})
594601

602+
describe('update last message', () => {
603+
beforeEach(() => {
604+
store = new Vuex.Store(testStoreConfig)
605+
})
606+
607+
test('successful update from user', () => {
608+
const testLastMessage = {
609+
actorType: 'users',
610+
actorId: 'admin',
611+
systemMessage: '',
612+
id: 42,
613+
message: 'Message 2',
614+
}
615+
616+
testConversation.lastMessage = previousLastMessage
617+
618+
store.dispatch('addConversation', testConversation)
619+
620+
store.dispatch('updateConversationLastMessage', {
621+
token: testToken,
622+
lastMessage: testLastMessage,
623+
})
624+
625+
const changedConversation = store.getters.conversation(testToken)
626+
expect(changedConversation.lastMessage).toBe(testLastMessage)
627+
})
628+
629+
test('ignore update from bot', () => {
630+
const testLastMessage = {
631+
actorType: 'bots',
632+
actorId: 'selfmade',
633+
systemMessage: '',
634+
id: 42,
635+
message: 'Message 2',
636+
}
637+
638+
testConversation.lastMessage = previousLastMessage
639+
640+
store.dispatch('addConversation', testConversation)
641+
642+
store.dispatch('updateConversationLastMessage', {
643+
token: testToken,
644+
lastMessage: testLastMessage,
645+
})
646+
647+
const changedConversation = store.getters.conversation(testToken)
648+
expect(changedConversation.lastMessage).toBe(previousLastMessage)
649+
})
650+
651+
test('ignore update from bot but not from changelog', () => {
652+
const testLastMessage = {
653+
actorType: 'bots',
654+
actorId: 'changelog',
655+
systemMessage: '',
656+
id: 42,
657+
message: 'Message 2',
658+
}
659+
660+
testConversation.lastMessage = previousLastMessage
661+
662+
store.dispatch('addConversation', testConversation)
663+
664+
store.dispatch('updateConversationLastMessage', {
665+
token: testToken,
666+
lastMessage: testLastMessage,
667+
})
668+
669+
const changedConversation = store.getters.conversation(testToken)
670+
expect(changedConversation.lastMessage).toBe(testLastMessage)
671+
})
672+
673+
test('ignore update reactions', () => {
674+
const testLastMessage = {
675+
actorType: 'users',
676+
actorId: 'admin',
677+
systemMessage: 'reaction',
678+
id: 42,
679+
message: '👍',
680+
}
681+
682+
testConversation.lastMessage = previousLastMessage
683+
684+
store.dispatch('addConversation', testConversation)
685+
686+
store.dispatch('updateConversationLastMessage', {
687+
token: testToken,
688+
lastMessage: testLastMessage,
689+
})
690+
691+
const changedConversation = store.getters.conversation(testToken)
692+
expect(changedConversation.lastMessage).toBe(previousLastMessage)
693+
})
694+
695+
test('ignore update from the action of deleting reactions', () => {
696+
const testLastMessage = {
697+
actorType: 'users',
698+
actorId: 'admin',
699+
systemMessage: 'reaction_revoked',
700+
id: 42,
701+
message: 'Admin deleted a reaction',
702+
}
703+
704+
testConversation.lastMessage = previousLastMessage
705+
706+
store.dispatch('addConversation', testConversation)
707+
708+
store.dispatch('updateConversationLastMessage', {
709+
token: testToken,
710+
lastMessage: testLastMessage,
711+
})
712+
713+
const changedConversation = store.getters.conversation(testToken)
714+
expect(changedConversation.lastMessage).toBe(previousLastMessage)
715+
})
716+
717+
test('ignore update deleted reactions (only theory as the action of deleting would come after it anyway)', () => {
718+
const testLastMessage = {
719+
actorType: 'users',
720+
actorId: 'admin',
721+
systemMessage: 'reaction_deleted',
722+
id: 42,
723+
message: 'Reaction deleted by author',
724+
}
725+
726+
testConversation.lastMessage = previousLastMessage
727+
728+
store.dispatch('addConversation', testConversation)
729+
730+
store.dispatch('updateConversationLastMessage', {
731+
token: testToken,
732+
lastMessage: testLastMessage,
733+
})
734+
735+
const changedConversation = store.getters.conversation(testToken)
736+
expect(changedConversation.lastMessage).toBe(previousLastMessage)
737+
})
738+
739+
test('ignore update from deleting a message', () => {
740+
const testLastMessage = {
741+
actorType: 'users',
742+
actorId: 'admin',
743+
systemMessage: 'message_deleted',
744+
id: 42,
745+
message: 'Admin deleted a message',
746+
}
747+
748+
testConversation.lastMessage = previousLastMessage
749+
750+
store.dispatch('addConversation', testConversation)
751+
752+
store.dispatch('updateConversationLastMessage', {
753+
token: testToken,
754+
lastMessage: testLastMessage,
755+
})
756+
757+
const changedConversation = store.getters.conversation(testToken)
758+
expect(changedConversation.lastMessage).toBe(previousLastMessage)
759+
})
760+
761+
test('successfully update temporary messages', () => {
762+
const testLastMessage = {
763+
actorType: 'users',
764+
actorId: 'admin',
765+
systemMessage: '',
766+
id: 'temp-42',
767+
message: 'quit',
768+
}
769+
770+
testConversation.lastMessage = previousLastMessage
771+
772+
store.dispatch('addConversation', testConversation)
773+
774+
store.dispatch('updateConversationLastMessage', {
775+
token: testToken,
776+
lastMessage: testLastMessage,
777+
})
778+
779+
const changedConversation = store.getters.conversation(testToken)
780+
expect(changedConversation.lastMessage).toBe(testLastMessage)
781+
})
782+
783+
test('successfully update also posted messages which start with a slash', () => {
784+
const testLastMessage = {
785+
actorType: 'users',
786+
actorId: 'admin',
787+
systemMessage: '',
788+
id: 42,
789+
message: '/quit',
790+
}
791+
792+
testConversation.lastMessage = previousLastMessage
793+
794+
store.dispatch('addConversation', testConversation)
795+
796+
store.dispatch('updateConversationLastMessage', {
797+
token: testToken,
798+
lastMessage: testLastMessage,
799+
})
800+
801+
const changedConversation = store.getters.conversation(testToken)
802+
expect(changedConversation.lastMessage).toBe(testLastMessage)
803+
})
804+
805+
test('ignore update from temporary if posting a command', () => {
806+
const testLastMessage = {
807+
actorType: 'users',
808+
actorId: 'admin',
809+
systemMessage: '',
810+
id: 'temp-42',
811+
message: '/quit',
812+
}
813+
814+
testConversation.lastMessage = previousLastMessage
815+
816+
store.dispatch('addConversation', testConversation)
817+
818+
store.dispatch('updateConversationLastMessage', {
819+
token: testToken,
820+
lastMessage: testLastMessage,
821+
})
822+
823+
const changedConversation = store.getters.conversation(testToken)
824+
expect(changedConversation.lastMessage).toBe(previousLastMessage)
825+
})
826+
})
827+
595828
describe('creating conversations', () => {
596829
test('creates one to one conversation', async () => {
597830
const newConversation = {

0 commit comments

Comments
 (0)