Welcome to mirror list, hosted at ThFree Co, Russian Federation.

messagesService.spec.js « services « src - github.com/nextcloud/spreed.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 31098edaf3ee41bf91a0bd496f12ff9a66fc7f44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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/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/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/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,
		}, {
			dummyOption: true,
		})

		expect(mockAxios.post).toHaveBeenCalledWith(
			generateOcsUrl('apps/spreed/api/v1/chat/XXTOKENXX'),
			{
				message: 'hello world!',
				actorDisplayName: 'actor-display-name',
				referenceId: 'reference-id',
				replyTo: 111,
			},
			{
				dummyOption: true,
			}
		)
	})

	test('deleteMessage calls the chat API endpoint', () => {
		deleteMessage({
			token: 'XXTOKENXX',
			id: 1234,
		})

		expect(mockAxios.delete).toHaveBeenCalledWith(
			generateOcsUrl('apps/spreed/api/v1/chat/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/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/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]{64}$/))
	})

	test('updateLastReadMessage calls the chat API endpoint', () => {
		updateLastReadMessage('XXTOKENXX', 1234)

		expect(mockAxios.post).toHaveBeenCalledWith(
			generateOcsUrl('apps/spreed/api/v1/chat/XXTOKENXX/read'),
			{
				lastReadMessage: 1234,
			}
		)
	})
})