summaryrefslogtreecommitdiffstats
path: root/tests/javascript/unit/components/AdminSettings.spec.ts
blob: 865b304161e9052b4d1e4f3cc94577e31cbff72a (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
import axios from '@nextcloud/axios'
import { createLocalVue, shallowMount, Wrapper } from '@vue/test-utils'
import { showError, showSuccess } from '@nextcloud/dialogs'
import { loadState } from '@nextcloud/initial-state'

import 'regenerator-runtime/runtime' // NOTE: Required for testing password-confirmation?
import AdminSettings from '../../../../src/components/AdminSettings.vue'

jest.mock('@nextcloud/axios')
jest.mock('@nextcloud/initial-state')
jest.mock('@nextcloud/router')
jest.mock('@nextcloud/dialogs')

describe('AdminSettings.vue', () => {
	'use strict'

	let wrapper: Wrapper<AdminSettings>

	beforeAll(() => {
		jest.useFakeTimers()
		const localVue = createLocalVue();
		(loadState as any).mockReturnValue('')
		wrapper = shallowMount(AdminSettings, { localVue })
	})

	it('should initialize and fetch settings from state', () => {
		expect(loadState).toBeCalledTimes(8)
	})

	it('should send post with updated settings', async () => {
		jest.spyOn(axios, 'post').mockResolvedValue({ data: {} });
		(wrapper.vm as any).handleResponse = jest.fn()

		await wrapper.vm.$options?.methods?.update.call(wrapper.vm, 'key', 'val')

		expect(axios.post).toBeCalledTimes(1)
	})

	it('should handle bad response', () => {
		(showError as any).mockClear()
		console.error = jest.fn()
		wrapper.vm.$options?.methods?.handleResponse.call(wrapper.vm, {
			error: true,
			errorMessage: 'FAIL',
		})

		expect(showError).toBeCalledTimes(1)
	})

	it('should handle success response', () => {
		wrapper.vm.$options?.methods?.handleResponse.call(wrapper.vm, {
			status: 'ok',
		});
		(global as any).t = jest.fn()
		jest.runAllTimers()

		expect(showSuccess).toBeCalledTimes(1)
	})

	afterAll(() => {
		jest.clearAllMocks()
		jest.useRealTimers()
	})
})