summaryrefslogtreecommitdiffstats
path: root/tests/javascript/unit/store/feed.spec.ts
blob: 928820085248d90232889d061a10051041a24bdf (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
import axios from '@nextcloud/axios'
import { Feed } from '../../../../src/types/Feed'
import { AppState } from '../../../../src/store'
import { FEED_ACTION_TYPES, FEED_MUTATION_TYPES, mutations, actions } from '../../../../src/store/feed'

jest.mock('@nextcloud/axios')

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

	describe('actions', () => {
		describe('ADD_FEED', () => {
			it('should call POST and commit feed to state', async () => {
				(axios as any).post.mockResolvedValue({ data: { feeds: [] }})
				const commit = jest.fn()
				await actions[FEED_ACTION_TYPES.ADD_FEED]({ commit }, { feedReq: { url: '' } })
				expect(axios.post).toBeCalled()
				expect(commit).toBeCalled()
			})

			it('should call POST and not call commit if error', async () => {
				(axios as any).post.mockRejectedValue()
				const commit = jest.fn()
				await actions[FEED_ACTION_TYPES.ADD_FEED]({ commit }, { feedReq: { url: '' } })
				expect(axios.post).toBeCalled()

				expect(commit).not.toBeCalled()
			})
		})

		

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

	describe('mutations', () => {
		describe('SET_FEEDS', () => {
			it('should add feeds to state', () => {
				const state = { feeds: [] as Feed[], folders: [] as any[] } as AppState
				let feeds = [] as any
	
				mutations[FEED_MUTATION_TYPES.SET_FEEDS](state, feeds)
				expect(state.feeds.length).toEqual(0)
	
				feeds = [{ title: 'test' }] as Feed[]
	
				mutations[FEED_MUTATION_TYPES.SET_FEEDS](state, feeds)
				expect(state.feeds.length).toEqual(1)
				expect(state.feeds[0]).toEqual(feeds[0])
			})
		})
		
		describe('ADD_FEED', () => {
			it('should add a single feed to state', () => {
				const state = { feeds: [] as Feed[], folders: [] as any[] } as AppState
				let feed = { title: 'test' } as any;
	
				mutations[FEED_MUTATION_TYPES.ADD_FEED](state, feed)
				expect(state.feeds.length).toEqual(1)
				expect(state.feeds[0]).toEqual(feed)
			})
		})
	})
})