Add unit tests for function and fix a problem revealed by it

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2022-04-13 15:04:10 +02:00
Родитель 8a749b97ec
Коммит 09731a4ac6
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 7076EA9751AACDDA
3 изменённых файлов: 236 добавлений и 84 удалений

Просмотреть файл

@ -130,20 +130,17 @@ describe('Conversation.vue', () => {
describe('author name', () => {
test('displays last chat message with shortened author name', () => {
testConversationLabel(item, 'Alice: hello')
expect(messagesMock).toHaveBeenCalledWith(TOKEN)
})
test('displays last chat message with author name if no space in name', () => {
item.lastMessage.actorDisplayName = 'Bob'
testConversationLabel(item, 'Bob: hello')
expect(messagesMock).toHaveBeenCalledWith(TOKEN)
})
test('displays own last chat message with "You" as author', () => {
item.lastMessage.actorId = 'user-id-self'
testConversationLabel(item, 'You: hello')
expect(messagesMock).toHaveBeenCalledWith(TOKEN)
})
test('displays last system message without author', () => {
@ -151,13 +148,11 @@ describe('Conversation.vue', () => {
item.lastMessage.systemMessage = 'call_joined'
testConversationLabel(item, 'Alice has joined the call')
expect(messagesMock).toHaveBeenCalledWith(TOKEN)
})
test('displays last message without author in one to one conversations', () => {
item.type = CONVERSATION.TYPE.ONE_TO_ONE
testConversationLabel(item, 'hello')
expect(messagesMock).toHaveBeenCalledWith(TOKEN)
})
test('displays own last message with "You" author in one to one conversations', () => {
@ -165,7 +160,6 @@ describe('Conversation.vue', () => {
item.lastMessage.actorId = 'user-id-self'
testConversationLabel(item, 'You: hello')
expect(messagesMock).toHaveBeenCalledWith(TOKEN)
})
test('displays last guest message with default author when none set', () => {
@ -174,14 +168,12 @@ describe('Conversation.vue', () => {
item.lastMessage.actorType = ATTENDEE.ACTOR_TYPE.GUESTS
testConversationLabel(item, 'Guest: hello')
expect(messagesMock).toHaveBeenCalledWith(TOKEN)
})
test('displays last message for search results', () => {
// search results have no actor id
item.actorId = null
testConversationLabel(item, 'Alice: hello', true)
expect(messagesMock).toHaveBeenCalledWith(TOKEN)
})
})
@ -194,80 +186,6 @@ describe('Conversation.vue', () => {
}
testConversationLabel(item, 'Alice: filename.jpg')
expect(messagesMock).toHaveBeenCalledWith(TOKEN)
})
describe('last message from messages store', () => {
// see Conversation.lastChatMessage() description for the reasoning
let displayedLastStoreMessage
let lastMessageFromConversation
beforeEach(() => {
displayedLastStoreMessage = {
id: 100,
actorId: 'user-id-alice',
actorDisplayName: 'Alice Wonderland',
actorType: ATTENDEE.ACTOR_TYPE.USERS,
message: 'hello from store',
messageParameters: {},
systemMessage: '',
timestamp: 100,
}
lastMessageFromConversation = {
id: 110,
actorId: 'user-id-alice',
actorDisplayName: 'Alice Wonderland',
actorType: ATTENDEE.ACTOR_TYPE.USERS,
message: 'hello from conversation',
messageParameters: {},
systemMessage: '',
timestamp: 100,
}
item.lastMessage = lastMessageFromConversation
messagesMock.mockClear().mockReturnValue({
100: displayedLastStoreMessage,
})
})
test('displays store message when more recent', () => {
displayedLastStoreMessage.timestamp = 2000
testConversationLabel(item, 'Alice: hello from store')
expect(messagesMock).toHaveBeenCalledWith(TOKEN)
})
test('displays conversation message when last one is a temporary command message', () => {
messagesMock.mockClear().mockReturnValue({
'temp-100': displayedLastStoreMessage,
})
displayedLastStoreMessage.timestamp = 2000
displayedLastStoreMessage.id = 'temp-100'
displayedLastStoreMessage.message = '/me doing things'
testConversationLabel(item, 'Alice: hello from conversation')
expect(messagesMock).toHaveBeenCalledWith(TOKEN)
})
test('displays conversation message when last one is a bot message', () => {
displayedLastStoreMessage.timestamp = 2000
displayedLastStoreMessage.actorType = ATTENDEE.ACTOR_TYPE.BOTS
testConversationLabel(item, 'Alice: hello from conversation')
expect(messagesMock).toHaveBeenCalledWith(TOKEN)
})
test('displays store message when last one is a changelog bot message', () => {
displayedLastStoreMessage.timestamp = 2000
displayedLastStoreMessage.actorType = ATTENDEE.ACTOR_TYPE.BOTS
displayedLastStoreMessage.actorId = ATTENDEE.CHANGELOG_BOT_ID
testConversationLabel(item, 'Alice: hello from store')
expect(messagesMock).toHaveBeenCalledWith(TOKEN)
})
})
})

Просмотреть файл

@ -419,8 +419,9 @@ const actions = {
&& lastMessage.systemMessage !== 'reaction_deleted'
&& lastMessage.systemMessage !== 'reaction_revoked'
&& lastMessage.systemMessage !== 'message_deleted'
&& ((typeof lastMessage.id.startsWith === 'function' && !lastMessage.id.startsWith('temp-'))
|| !lastMessage.message.startsWith('/'))) {
&& !(typeof lastMessage.id.startsWith === 'function'
&& lastMessage.id.startsWith('temp-')
&& lastMessage.message.startsWith('/'))) {
commit('updateConversationLastMessage', { token, lastMessage })
}
},

Просмотреть файл

@ -50,6 +50,13 @@ jest.mock('../services/conversationsService', () => ({
describe('conversationsStore', () => {
const testToken = 'XXTOKENXX'
const previousLastMessage = {
actorType: 'users',
actorId: 'admin',
systemMessage: '',
id: 31,
message: 'Message 1',
}
let testStoreConfig = null
let testConversation
let localVue = null
@ -592,6 +599,232 @@ describe('conversationsStore', () => {
})
})
describe('update last message', () => {
beforeEach(() => {
store = new Vuex.Store(testStoreConfig)
})
test('successful update from user', () => {
const testLastMessage = {
actorType: 'users',
actorId: 'admin',
systemMessage: '',
id: 42,
message: 'Message 2',
}
testConversation.lastMessage = previousLastMessage
store.dispatch('addConversation', testConversation)
store.dispatch('updateConversationLastMessage', {
token: testToken,
lastMessage: testLastMessage,
})
const changedConversation = store.getters.conversation(testToken)
expect(changedConversation.lastMessage).toBe(testLastMessage)
})
test('ignore update from bot', () => {
const testLastMessage = {
actorType: 'bots',
actorId: 'selfmade',
systemMessage: '',
id: 42,
message: 'Message 2',
}
testConversation.lastMessage = previousLastMessage
store.dispatch('addConversation', testConversation)
store.dispatch('updateConversationLastMessage', {
token: testToken,
lastMessage: testLastMessage,
})
const changedConversation = store.getters.conversation(testToken)
expect(changedConversation.lastMessage).toBe(previousLastMessage)
})
test('ignore update from bot but not from changelog', () => {
const testLastMessage = {
actorType: 'bots',
actorId: 'changelog',
systemMessage: '',
id: 42,
message: 'Message 2',
}
testConversation.lastMessage = previousLastMessage
store.dispatch('addConversation', testConversation)
store.dispatch('updateConversationLastMessage', {
token: testToken,
lastMessage: testLastMessage,
})
const changedConversation = store.getters.conversation(testToken)
expect(changedConversation.lastMessage).toBe(testLastMessage)
})
test('ignore update reactions', () => {
const testLastMessage = {
actorType: 'users',
actorId: 'admin',
systemMessage: 'reaction',
id: 42,
message: '👍',
}
testConversation.lastMessage = previousLastMessage
store.dispatch('addConversation', testConversation)
store.dispatch('updateConversationLastMessage', {
token: testToken,
lastMessage: testLastMessage,
})
const changedConversation = store.getters.conversation(testToken)
expect(changedConversation.lastMessage).toBe(previousLastMessage)
})
test('ignore update from the action of deleting reactions', () => {
const testLastMessage = {
actorType: 'users',
actorId: 'admin',
systemMessage: 'reaction_revoked',
id: 42,
message: 'Admin deleted a reaction',
}
testConversation.lastMessage = previousLastMessage
store.dispatch('addConversation', testConversation)
store.dispatch('updateConversationLastMessage', {
token: testToken,
lastMessage: testLastMessage,
})
const changedConversation = store.getters.conversation(testToken)
expect(changedConversation.lastMessage).toBe(previousLastMessage)
})
test('ignore update deleted reactions (only theory as the action of deleting would come after it anyway)', () => {
const testLastMessage = {
actorType: 'users',
actorId: 'admin',
systemMessage: 'reaction_deleted',
id: 42,
message: 'Reaction deleted by author',
}
testConversation.lastMessage = previousLastMessage
store.dispatch('addConversation', testConversation)
store.dispatch('updateConversationLastMessage', {
token: testToken,
lastMessage: testLastMessage,
})
const changedConversation = store.getters.conversation(testToken)
expect(changedConversation.lastMessage).toBe(previousLastMessage)
})
test('ignore update from deleting a message', () => {
const testLastMessage = {
actorType: 'users',
actorId: 'admin',
systemMessage: 'message_deleted',
id: 42,
message: 'Admin deleted a message',
}
testConversation.lastMessage = previousLastMessage
store.dispatch('addConversation', testConversation)
store.dispatch('updateConversationLastMessage', {
token: testToken,
lastMessage: testLastMessage,
})
const changedConversation = store.getters.conversation(testToken)
expect(changedConversation.lastMessage).toBe(previousLastMessage)
})
test('successfully update temporary messages', () => {
const testLastMessage = {
actorType: 'users',
actorId: 'admin',
systemMessage: '',
id: 'temp-42',
message: 'quit',
}
testConversation.lastMessage = previousLastMessage
store.dispatch('addConversation', testConversation)
store.dispatch('updateConversationLastMessage', {
token: testToken,
lastMessage: testLastMessage,
})
const changedConversation = store.getters.conversation(testToken)
expect(changedConversation.lastMessage).toBe(testLastMessage)
})
test('successfully update also posted messages which start with a slash', () => {
const testLastMessage = {
actorType: 'users',
actorId: 'admin',
systemMessage: '',
id: 42,
message: '/quit',
}
testConversation.lastMessage = previousLastMessage
store.dispatch('addConversation', testConversation)
store.dispatch('updateConversationLastMessage', {
token: testToken,
lastMessage: testLastMessage,
})
const changedConversation = store.getters.conversation(testToken)
expect(changedConversation.lastMessage).toBe(testLastMessage)
})
test('ignore update from temporary if posting a command', () => {
const testLastMessage = {
actorType: 'users',
actorId: 'admin',
systemMessage: '',
id: 'temp-42',
message: '/quit',
}
testConversation.lastMessage = previousLastMessage
store.dispatch('addConversation', testConversation)
store.dispatch('updateConversationLastMessage', {
token: testToken,
lastMessage: testLastMessage,
})
const changedConversation = store.getters.conversation(testToken)
expect(changedConversation.lastMessage).toBe(previousLastMessage)
})
})
describe('creating conversations', () => {
test('creates one to one conversation', async () => {
const newConversation = {