summaryrefslogtreecommitdiffstats
path: root/tests/javascript/unit/store/folder.spec.ts
blob: 8fd2ad34ff8982e984976feda37bd09997b02e30 (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
import { Folder } from '../../../../src/types/Folder'
import { AppState } from '../../../../src/store'
import { FOLDER_ACTION_TYPES, mutations, actions } from '../../../../src/store/folder'
import { FOLDER_MUTATION_TYPES } from '../../../../src/types/MutationTypes'
import { FolderService } from '../../../../src/dataservices/folder.service'

jest.mock('@nextcloud/router')

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

	describe('actions', () => {
		it('FETCH_FOLDERS should call FolderService.fetchAllFolders and then commit folders returned to state', async () => {
			FolderService.fetchAllFolders = jest.fn();
			(FolderService.fetchAllFolders as any).mockResolvedValue({ data: { folders: [] } })

			const commit = jest.fn()

		  await (actions[FOLDER_ACTION_TYPES.FETCH_FOLDERS] as any)({ commit })
			expect(FolderService.fetchAllFolders).toBeCalled()
			expect(commit).toBeCalled()
		})

		it('ADD_FOLDERS should call FolderService.createFolder and then commit the folders returned to state', async () => {
			FolderService.createFolder = jest.fn();
			(FolderService.createFolder as any).mockResolvedValue({ data: { folders: [] } })

			const folder = {} as Folder
			const commit = jest.fn()

		  await actions[FOLDER_ACTION_TYPES.ADD_FOLDERS]({ commit }, { folder })
			expect(FolderService.createFolder).toBeCalled()
			expect(commit).toBeCalled()
		})

		it('DELETE_FOLDER should call FolderService.deleteFolder and then commit deleted folder to state', async () => {
			FolderService.deleteFolder = jest.fn();
			(FolderService.deleteFolder as any).mockResolvedValue()

			const folder = {} as Folder
			const commit = jest.fn()

		  await actions[FOLDER_ACTION_TYPES.DELETE_FOLDER]({ commit }, { folder })
			expect(FolderService.deleteFolder).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)
		})
	})
})