summaryrefslogtreecommitdiffstats
path: root/tests/javascript/unit/store/item.spec.ts
blob: 614e47c1dacae78f26f3a1ba3beb038d8eee84a7 (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
import axios from '@nextcloud/axios'
import { AppState } from '../../../../src/store'
import { FEED_ITEM_ACTION_TYPES, mutations, actions } from '../../../../src/store/item'

import { FEED_ITEM_MUTATION_TYPES } from '../../../../src/types/MutationTypes'
import { FeedItem } from '../../../../src/types/FeedItem'

jest.mock('@nextcloud/axios')

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

	describe('actions', () => {
		describe('FETCH_STARRED', () => {
			it('should call GET and commit items and starred count to state', async () => {
				(axios as any).get.mockResolvedValue({ data: { items: [], starred: 3 } })
				const commit = jest.fn()
				await (actions[FEED_ITEM_ACTION_TYPES.FETCH_STARRED] as any)({ commit })
				expect(axios.get).toBeCalled()
				expect(commit).toBeCalledWith(FEED_ITEM_MUTATION_TYPES.SET_ITEMS, [])
				expect(commit).toBeCalledWith(FEED_ITEM_MUTATION_TYPES.SET_STARRED_COUNT, 3)
			})
		})

		it('MARK_READ should call GET and commit returned feeds to state', async () => {
			const item = { id: 1 }
			const commit = jest.fn()
			await (actions[FEED_ITEM_ACTION_TYPES.MARK_READ] as any)({ commit }, { item })
			expect(axios.post).toBeCalled()
			expect(commit).toBeCalled()
		})

		it('MARK_UNREAD should call GET and commit returned feeds to state', async () => {
			const item = { id: 1 }
			const commit = jest.fn()
			await (actions[FEED_ITEM_ACTION_TYPES.MARK_UNREAD] as any)({ commit }, { item })
			expect(axios.post).toBeCalled()
			expect(commit).toBeCalledWith(FEED_ITEM_MUTATION_TYPES.UPDATE_ITEM, { item })
		})

		it('STAR_ITEM should call GET and commit returned feeds to state', async () => {
			const item = { id: 1 };
			(axios as any).get.mockResolvedValue({ data: { feeds: [] } })
			const commit = jest.fn()
			await (actions[FEED_ITEM_ACTION_TYPES.STAR_ITEM] as any)({ commit }, { item })
			expect(axios.post).toBeCalled()
			expect(commit).toBeCalled()
		})

		it('UNSTAR_ITEM should call GET and commit returned feeds to state', async () => {
			const item = { id: 1 };
			(axios as any).get.mockResolvedValue({ data: { feeds: [] } })
			const commit = jest.fn()
			await (actions[FEED_ITEM_ACTION_TYPES.UNSTAR_ITEM] as any)({ commit }, { item })
			expect(axios.post).toBeCalled()
			expect(commit).toBeCalled()
		})
	})

	describe('mutations', () => {
		describe('SET_ITEMS', () => {
			it('should add feeds to state', () => {
				const state = { allItems: [] as any } as AppState
				let items = [] as any

				mutations[FEED_ITEM_MUTATION_TYPES.SET_ITEMS](state, items)
				expect(state.allItems.length).toEqual(0)

				items = [{ title: 'test' }] as FeedItem[]

				mutations[FEED_ITEM_MUTATION_TYPES.SET_ITEMS](state, items)
				expect(state.allItems.length).toEqual(1)
				expect(state.allItems[0]).toEqual(items[0])
			})
		})

		describe('SET_STARRED_COUNT', () => {
			it('should add a single feed to state', () => {
				const state = { } as AppState

				(mutations[FEED_ITEM_MUTATION_TYPES.SET_STARRED_COUNT] as any)(state, 13)
				expect(state.starredCount).toEqual(13)
			})
		})

		describe('UPDATE_ITEM', () => {
			it('should add a single feed to state', () => {
				const state = { allItems: [{ id: 1, title: 'abc' }] as any } as AppState
				const item = { title: 'test', id: 1 } as any

				(mutations[FEED_ITEM_MUTATION_TYPES.UPDATE_ITEM] as any)(state, { item })
				expect(state.allItems[0]).toEqual(item)
			})
		})
	})
})