summaryrefslogtreecommitdiffstats
path: root/tests/javascript/unit/services/item.service.spec.ts
blob: 0052487090706cbb85188de05c151f55c2d3ed4b (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
import { ITEM_TYPES, ItemService } from '../../../../src/dataservices/item.service'
import axios from '@nextcloud/axios'

jest.mock('@nextcloud/axios')

describe('item.service.ts', () => {
	'use strict'

	beforeEach(() => {
		(axios.get as any).mockReset();
		(axios.post as any).mockReset()
	})

	describe('fetchStarred', () => {
		it('should call GET with offset set to start param and STARRED item type', async () => {
			(axios as any).get.mockResolvedValue({ data: { feeds: [] } })

			await ItemService.fetchStarred(0)

			expect(axios.get).toBeCalled()
			const queryParams = (axios.get as any).mock.calls[0][1].params

			expect(queryParams.offset).toEqual(0)
			expect(queryParams.type).toEqual(ITEM_TYPES.STARRED)
		})
	})

	describe('fetchUnread', () => {
		it('should call GET with offset set to start param and UNREAD item type', async () => {
			(axios as any).get.mockResolvedValue({ data: { feeds: [] } })

			await ItemService.fetchUnread(2)

			expect(axios.get).toBeCalled()
			const queryParams = (axios.get as any).mock.calls[0][1].params

			expect(queryParams.offset).toEqual(2)
			expect(queryParams.type).toEqual(ITEM_TYPES.UNREAD)
		})
	})

	describe('fetchFeedItems', () => {
		it('should call GET with offset set to start param, UNREAD item type, and id set to feedId', async () => {
			(axios as any).get.mockResolvedValue({ data: { feeds: [] } })

			await ItemService.fetchFeedItems(123, 0)

			expect(axios.get).toBeCalled()
			const queryParams = (axios.get as any).mock.calls[0][1].params

			expect(queryParams.id).toEqual(123)
			expect(queryParams.offset).toEqual(0)
			expect(queryParams.type).toEqual(ITEM_TYPES.ALL)
		})
	})

	describe('markRead', () => {
		it('should call POST with item id in URL and read param', async () => {
			await ItemService.markRead({ id: 123 } as any, true)

			expect(axios.post).toBeCalled()
			const args = (axios.post as any).mock.calls[0]

			expect(args[0]).toContain('123')
			expect(args[1].isRead).toEqual(true)
		})
	})

	describe('markStarred', () => {
		it('should call POST with item feedId and guidHash in URL and read param', async () => {
			await ItemService.markStarred({ feedId: 1, guidHash: 'abc' } as any, false)

			expect(axios.post).toBeCalled()
			const args = (axios.post as any).mock.calls[0]

			expect(args[0]).toContain('1')
			expect(args[0]).toContain('abc')
			expect(args[1].isStarred).toEqual(false)
		})
	})
})