Skip to content

Commit

Permalink
fix(MessagesList): restore the state if context request was canceled,…
Browse files Browse the repository at this point in the history
… abort lookForNewMessages request if query param is missing

Signed-off-by: Maksim Sukharev <antreesy.web@gmail.com>
  • Loading branch information
Antreesy committed Jun 10, 2024
1 parent d264e88 commit 1c6e934
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/components/MessagesList/MessagesList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,14 @@ export default {
this.$store.dispatch('setLastKnownMessageId', { token, id: startingMessageId })

// Get chat messages before last read message and after it
await this.getMessageContext(token, startingMessageId)
try {
await this.getMessageContext(token, startingMessageId)
} catch (exception) {
// Request was cancelled, stop getting preconditions and restore initial state
this.$store.dispatch('setFirstKnownMessageId', { token, id: null })
this.$store.dispatch('setLastKnownMessageId', { token, id: null })
return
}
}

this.$nextTick(() => {
Expand Down Expand Up @@ -686,9 +693,12 @@ export default {
messageId,
minimumVisible: CHAT.MINIMUM_VISIBLE,
})
this.loadingOldMessages = false
} catch (exception) {
if (Axios.isCancel(exception)) {
console.debug('The request has been canceled', exception)
this.loadingOldMessages = false
throw exception
}

if (exception?.response?.status === 304 && exception?.response?.data === '') {
Expand Down
6 changes: 6 additions & 0 deletions src/store/messagesStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,12 @@ const actions = {
async lookForNewMessages(context, { token, lastKnownMessageId, requestId, requestOptions }) {
context.dispatch('cancelLookForNewMessages', { requestId })

if (!lastKnownMessageId) {
// if param is null | undefined, it won't be included in the request query
console.warn('Trying to load messages without the required parameter')
return
}

// Get a new cancelable request function and cancel function pair
const { request, cancel } = CancelableRequest(lookForNewMessages)

Expand Down
23 changes: 23 additions & 0 deletions src/store/messagesStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1300,6 +1300,10 @@ describe('messagesStore', () => {
store = new Vuex.Store(testStoreConfig)
})

afterEach(() => {
jest.clearAllMocks()
})

test('looks for new messages', async () => {
const messages = [{
id: 1,
Expand Down Expand Up @@ -1389,6 +1393,25 @@ describe('messagesStore', () => {
expect(store.getters.getLastKnownMessageId(TOKEN)).toBe(null)
})

test('does not look for new messages if lastKnownMessageId is falsy', async () => {
// Arrange: prepare cancelable request from previous call of the function
const cancelFunctionMock = jest.fn()
cancelFunctionMocks.push(cancelFunctionMock)
store.commit('setCancelLookForNewMessages', { cancelFunction: cancelFunctionMock, requestId: 'request1' })
console.warn = jest.fn()

// Act
store.dispatch('lookForNewMessages', {
token: TOKEN,
requestId: 'request1',
lastKnownMessageId: null,
})

// Assert
expect(cancelFunctionMocks[0]).toHaveBeenCalledWith('canceled')
expect(lookForNewMessages).not.toHaveBeenCalled()
})

test('cancels look for new messages', async () => {
store.dispatch('lookForNewMessages', {
token: TOKEN,
Expand Down

0 comments on commit 1c6e934

Please sign in to comment.