Skip to content

Commit

Permalink
fix(tests): use shallowMount instead of mount to only unit test one c…
Browse files Browse the repository at this point in the history
…omponent

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux committed Oct 13, 2023
1 parent d5a7688 commit b6d95c6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
27 changes: 20 additions & 7 deletions lib/components/FilePicker/FileList.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@
*
*/

import { mount } from '@vue/test-utils'
import { mount, shallowMount } from '@vue/test-utils'
import { beforeAll, describe, expect, it, vi } from 'vitest'

import FileList from './FileList.vue'
import { File, Folder } from '@nextcloud/files'
import { nextTick } from 'vue'

const axios = vi.hoisted(() => ({
get: vi.fn(() => new Promise(() => {})),
}))
vi.mock('axios', () => axios)
vi.mock('@nextcloud/axios', () => ({ default: axios }))

const exampleNodes = [
new File({
Expand Down Expand Up @@ -77,7 +78,7 @@ describe('FilePicker FileList', () => {
const consoleError = vi.spyOn(console, 'error')
const consoleWarning = vi.spyOn(console, 'warn')

const wrapper = mount(FileList, {
const wrapper = shallowMount(FileList, {
propsData: {
currentView: 'files',
multiselect: false,
Expand All @@ -95,7 +96,7 @@ describe('FilePicker FileList', () => {
})

it('header checkbox is not shown if multiselect is `false`', () => {
const wrapper = mount(FileList, {
const wrapper = shallowMount(FileList, {
propsData: {
currentView: 'files',
multiselect: false,
Expand All @@ -110,7 +111,7 @@ describe('FilePicker FileList', () => {
})

it('header checkbox is shown if multiselect is `true`', () => {
const wrapper = mount(FileList, {
const wrapper = shallowMount(FileList, {
propsData: {
currentView: 'files',
multiselect: true,
Expand All @@ -131,7 +132,7 @@ describe('FilePicker FileList', () => {

it('header checkbox is checked when all nodes are selected', async () => {
const nodes = [...exampleNodes]
const wrapper = mount(FileList, {
const wrapper = shallowMount(FileList, {
propsData: {
currentView: 'files',
multiselect: true,
Expand Down Expand Up @@ -160,8 +161,13 @@ describe('FilePicker FileList', () => {
selectedFiles: [],
path: '/',
},
stubs: {
FilePreview: true,
},
})

await nextTick()

const rows = wrapper.findAll('[data-testid="file-list-row"]')
// all nodes are shown
expect(rows.length).toBe(nodes.length)
Expand All @@ -186,9 +192,16 @@ describe('FilePicker FileList', () => {
selectedFiles: [],
path: '/',
},
stubs: {
FilePreview: true,
},
})

await wrapper.find('[data-test="file-picker_sort-name"]').trigger('click')
await nextTick()

wrapper.find('[data-test="file-picker_sort-name"]').trigger('click')

await nextTick()

const rows = wrapper.findAll('.file-picker__row')
// all nodes are shown
Expand Down
18 changes: 9 additions & 9 deletions lib/components/FilePicker/FileListRow.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/

import { afterEach, describe, expect, it, vi } from 'vitest'
import { mount } from '@vue/test-utils'
import { shallowMount } from '@vue/test-utils'
import { File } from '@nextcloud/files'

import FileListRow from './FileListRow.vue'
Expand All @@ -39,11 +39,11 @@ describe('FilePicker: FileListRow', () => {
vi.restoreAllMocks()
})

it('is mountable', () => {
it('is shallowMountable', () => {
const consoleWarn = vi.spyOn(console, 'warn')
const consoleError = vi.spyOn(console, 'error')

const wrapper = mount(FileListRow, {
const wrapper = shallowMount(FileListRow, {
propsData: {
allowPickDirectory: true,
selected: false,
Expand All @@ -56,14 +56,14 @@ describe('FilePicker: FileListRow', () => {
// No console errors
expect(consoleWarn).not.toBeCalled()
expect(consoleError).not.toBeCalled()
// mounted
// shallowMounted
expect(wrapper.exists()).toBe(true)
expect(wrapper.element.tagName.toLowerCase()).toBe('tr')
expect(wrapper.find('[data-testid="file-list-row"]').isEmpty()).toBe(false)
})

it('shows checkbox based on `showCheckbox` property', async () => {
const wrapper = mount(FileListRow, {
const wrapper = shallowMount(FileListRow, {
propsData: {
allowPickDirectory: true,
selected: false,
Expand All @@ -79,7 +79,7 @@ describe('FilePicker: FileListRow', () => {
})

it('Click checkbox triggers select', async () => {
const wrapper = mount(FileListRow, {
const wrapper = shallowMount(FileListRow, {
propsData: {
allowPickDirectory: false,
selected: false,
Expand All @@ -96,7 +96,7 @@ describe('FilePicker: FileListRow', () => {
})

it('Click element triggers select', async () => {
const wrapper = mount(FileListRow, {
const wrapper = shallowMount(FileListRow, {
propsData: {
allowPickDirectory: false,
selected: false,
Expand All @@ -113,7 +113,7 @@ describe('FilePicker: FileListRow', () => {
})

it('Click element without checkbox triggers select', async () => {
const wrapper = mount(FileListRow, {
const wrapper = shallowMount(FileListRow, {
propsData: {
allowPickDirectory: false,
selected: false,
Expand All @@ -130,7 +130,7 @@ describe('FilePicker: FileListRow', () => {
})

it('Enter triggers select', async () => {
const wrapper = mount(FileListRow, {
const wrapper = shallowMount(FileListRow, {
propsData: {
allowPickDirectory: false,
selected: false,
Expand Down
6 changes: 6 additions & 0 deletions lib/components/FilePicker/FilePreview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,9 @@ watch(previewURL, () => {
}
}, { immediate: true })
</script>

<script lang="ts">
export default {
name: 'FilePreview',
}
</script>

0 comments on commit b6d95c6

Please sign in to comment.