Browse Source

Added more JS tests for services

Signed-off-by: Vincent Petry <vincent@nextcloud.com>
pull/5508/head
Vincent Petry 5 years ago
parent
commit
50d3292997
No known key found for this signature in database GPG Key ID: E055D6A4D513575C
  1. 1
      src/services/filesSharingServices.js
  2. 24
      src/services/filesSharingServices.spec.js
  3. 16
      src/services/messagesService.js
  4. 162
      src/services/messagesService.spec.js
  5. 1
      src/services/participantsService.js

1
src/services/filesSharingServices.js

@ -41,6 +41,7 @@ const shareFile = async function(path, token, referenceId) {
referenceId,
})
} catch (error) {
// FIXME: errors should be handled by called instead
if (error?.response?.data?.ocs?.meta?.message) {
console.error('Error while sharing file: ' + error.response.data.ocs.meta.message)
showError(error.response.data.ocs.meta.message)

24
src/services/filesSharingServices.spec.js

@ -0,0 +1,24 @@
import mockAxios from '../__mocks__/axios'
import { generateOcsUrl } from '@nextcloud/router'
import { shareFile } from './filesSharingServices'
describe('filesSharingServices', () => {
afterEach(() => {
// cleaning up the mess left behind the previous test
mockAxios.reset()
})
test('shareFile calls the sharing API endpoint', () => {
shareFile('path/to/file', 'XXTOKENXX', 'the-reference-id')
expect(mockAxios.post).toHaveBeenCalledWith(
generateOcsUrl('apps/files_sharing/api/v1', 2) + 'shares',
{
shareType: 10,
shareWith: 'XXTOKENXX',
path: 'path/to/file',
referenceId: 'the-reference-id',
}
)
})
})

16
src/services/messagesService.js

@ -31,7 +31,9 @@ import Hex from 'crypto-js/enc-hex'
* specified with its token.
*
* @param {string} token the conversation token;
* @param {object} options options
* @param {object} options options;
* @param {string} lastKnownMessageId last known message id;
* @param {int} includeLastKnown whether to include the last known message in the response;
*/
const fetchMessages = async function({ token, lastKnownMessageId, includeLastKnown }, options) {
const response = await axios.get(generateOcsUrl('apps/spreed/api/v1/chat', 2) + token, Object.assign(options, {
@ -39,10 +41,12 @@ const fetchMessages = async function({ token, lastKnownMessageId, includeLastKno
setReadMarker: 0,
lookIntoFuture: 0,
lastKnownMessageId,
// FIXME: change the function arg to boolean then convert to int for the API
includeLastKnown: includeLastKnown || 0,
},
}))
// TODO: move to action instead
if ('x-chat-last-common-read' in response.headers) {
const lastCommonReadMessage = parseInt(response.headers['x-chat-last-common-read'], 10)
store.dispatch('updateLastCommonReadMessage', {
@ -91,10 +95,14 @@ const lookForNewMessages = async({ token, lastKnownMessageId }, options) => {
* @param {string} param0.message The message object
* @param {string} param0.referenceId A reference id to identify the message later again
* @param {Number} param0.parent The id of the message to be replied to
* @param {object} options options
*/
const postNewMessage = async function({ token, message, actorDisplayName, referenceId, parent }, options) {
const response = await axios.post(generateOcsUrl('apps/spreed/api/v1/chat', 2) + token, { message, actorDisplayName, referenceId, replyTo: parent })
const postNewMessage = async function({ token, message, actorDisplayName, referenceId, parent }) {
const response = await axios.post(generateOcsUrl('apps/spreed/api/v1/chat', 2) + token, {
message,
actorDisplayName,
referenceId,
replyTo: parent,
})
if ('x-chat-last-common-read' in response.headers) {
const lastCommonReadMessage = parseInt(response.headers['x-chat-last-common-read'], 10)

162
src/services/messagesService.spec.js

@ -0,0 +1,162 @@
import mockAxios from '../__mocks__/axios'
import { generateOcsUrl } from '@nextcloud/router'
import {
fetchMessages,
lookForNewMessages,
postNewMessage,
deleteMessage,
postRichObjectToConversation,
updateLastReadMessage,
} from './messagesService'
describe('messagesService', () => {
afterEach(() => {
// cleaning up the mess left behind the previous test
mockAxios.reset()
})
test('fetchMessages calls the chat API endpoint excluding last known', () => {
fetchMessages({
token: 'XXTOKENXX',
lastKnownMessageId: 1234,
includeLastKnown: 0,
}, {
dummyOption: true,
})
expect(mockAxios.get).toHaveBeenCalledWith(
generateOcsUrl('apps/spreed/api/v1/chat', 2) + 'XXTOKENXX',
{
dummyOption: true,
params: {
setReadMarker: 0,
lookIntoFuture: 0,
lastKnownMessageId: 1234,
includeLastKnown: 0,
},
}
)
})
test('fetchMessages calls the chat API endpoint including last known', () => {
fetchMessages({
token: 'XXTOKENXX',
lastKnownMessageId: 1234,
includeLastKnown: 1,
}, {
dummyOption: true,
})
expect(mockAxios.get).toHaveBeenCalledWith(
generateOcsUrl('apps/spreed/api/v1/chat', 2) + 'XXTOKENXX',
{
dummyOption: true,
params: {
setReadMarker: 0,
lookIntoFuture: 0,
lastKnownMessageId: 1234,
includeLastKnown: 1,
},
}
)
})
test('lookForNewMessages calls the chat API endpoint excluding last known', () => {
lookForNewMessages({
token: 'XXTOKENXX',
lastKnownMessageId: 1234,
}, {
dummyOption: true,
})
expect(mockAxios.get).toHaveBeenCalledWith(
generateOcsUrl('apps/spreed/api/v1/chat', 2) + 'XXTOKENXX',
{
dummyOption: true,
params: {
setReadMarker: 0,
lookIntoFuture: 1,
lastKnownMessageId: 1234,
includeLastKnown: 0,
},
}
)
})
test('postNewMessage calls the chat API endpoint', () => {
postNewMessage({
token: 'XXTOKENXX',
message: 'hello world!',
actorDisplayName: 'actor-display-name',
referenceId: 'reference-id',
parent: 111,
})
expect(mockAxios.post).toHaveBeenCalledWith(
generateOcsUrl('apps/spreed/api/v1/chat', 2) + 'XXTOKENXX',
{
message: 'hello world!',
actorDisplayName: 'actor-display-name',
referenceId: 'reference-id',
replyTo: 111,
}
)
})
test('deleteMessage calls the chat API endpoint', () => {
deleteMessage({
token: 'XXTOKENXX',
id: 1234,
})
expect(mockAxios.delete).toHaveBeenCalledWith(
generateOcsUrl('apps/spreed/api/v1/chat', 2) + 'XXTOKENXX/1234'
)
})
test('postRichObjectToConversation calls the chat API endpoint', () => {
postRichObjectToConversation('XXTOKENXX', {
objectType: 'deck',
objectId: 999,
metaData: '{"x":1}',
referenceId: 'reference-id',
})
expect(mockAxios.post).toHaveBeenCalledWith(
generateOcsUrl('apps/spreed/api/v1/chat', 2) + 'XXTOKENXX/share',
{
objectType: 'deck',
objectId: 999,
metaData: '{"x":1}',
referenceId: 'reference-id',
}
)
})
test('postRichObjectToConversation without reference id will generate one', () => {
postRichObjectToConversation('XXTOKENXX', {
objectType: 'deck',
objectId: 999,
metaData: '{"x":1}',
})
const lastReq = mockAxios.lastReqGet()
expect(lastReq.url)
.toBe(generateOcsUrl('apps/spreed/api/v1/chat', 2) + 'XXTOKENXX/share')
expect(lastReq.data.objectType).toBe('deck')
expect(lastReq.data.objectId).toBe(999)
expect(lastReq.data.metaData).toBe('{"x":1}')
expect(lastReq.data.referenceId).toEqual(expect.stringMatching(/^[a-z0-9]{40}$/))
})
test('updateLastReadMessage calls the chat API endpoint', () => {
updateLastReadMessage('XXTOKENXX', 1234)
expect(mockAxios.post).toHaveBeenCalledWith(
generateOcsUrl('apps/spreed/api/v1/chat', 2) + 'XXTOKENXX/read',
{
lastReadMessage: 1234,
}
)
})
})

1
src/services/participantsService.js

@ -42,6 +42,7 @@ import store from '../store/index'
* @param {string} token The conversation token;
*/
const joinConversation = async(token) => {
// FIXME: move complex logic to a store action instead, a service should not call the store
// When the token is in the last joined conversation, the user is reloading or force joining
const forceJoin = SessionStorage.getItem('joined_conversation') === token

Loading…
Cancel
Save