summaryrefslogtreecommitdiffstats
path: root/tests/javascript/unit/store/folder.spec.ts
blob: 0350bed34a59ddf6fee04a4cc19f59f1b04cd674 (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 { generateUrl } from '@nextcloud/router'
import { Folder } from '../../../../src/types/Folder'
import { AppState } from '../../../../src/store'
import { FOLDER_ACTION_TYPES, FOLDER_MUTATION_TYPES, mutations, actions } from '../../../../src/store/folder'

jest.mock('@nextcloud/axios')
jest.mock('@nextcloud/router')

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

	describe('actions', () => {
		it('FETCH_FOLDERS should send GET and then commit folders returned to state', async () => {
			(generateUrl as any).mockReturnValue('');
			(axios.get as any).mockResolvedValue({ data: { folders: [] } })

			const commit = jest.fn()

		  await (actions[FOLDER_ACTION_TYPES.FETCH_FOLDERS] as any)({ commit })

			expect(axios.get).toBeCalled()
			expect(commit).toBeCalled()
		})

		it('ADD_FOLDERS should send POST and then commit the folders returned to state', async () => {
			(axios.post as any).mockResolvedValue({ data: { folders: [] } })

			const folder = {} as Folder
			const commit = jest.fn()
		  await actions[FOLDER_ACTION_TYPES.ADD_FOLDERS]({ commit }, { folder })
			expect(axios.post).toBeCalled()
			expect(commit).toBeCalled()
		})

		it('DELETE_FOLDER should send DELETE and then commit deleted folder to state', async () => {
			(axios.delete as any).mockResolvedValue()

			const folder = {} as Folder
			const commit = jest.fn()
		  await actions[FOLDER_ACTION_TYPES.DELETE_FOLDER]({ commit }, { folder })
			expect(axios.delete).toBeCalled()
			expect(commit).toBeCalled()
		})
	})

	describe('mutations', () => {
		it('SET_FOLDERS should add the passed in folders to the state', () => {
			const state = { folders: [] as Folder[] } as AppState
			let folders = [] as Folder[]
			(mutations[FOLDER_MUTATION_TYPES.SET_FOLDERS] as any)(state, folders)

			expect(state.folders.length).toEqual(0)

			folders = [{ name: 'test' }] as Folder[]
			(mutations[FOLDER_MUTATION_TYPES.SET_FOLDERS] as any)(state, folders)

			expect(state.folders.length).toEqual(1)
			expect(state.folders[0]).toEqual(folders[0])
		})

		it('DELETE_FOLDER should remove the passed in folder from the state', () => {
			const state = { folders: [{ name: 'test' }] as Folder[] } as AppState
			const folders = [state.folders[0]] as Folder[]
			(mutations[FOLDER_MUTATION_TYPES.DELETE_FOLDER] as any)(state, folders)

			expect(state.folders.length).toEqual(0)
		})
	})
})