Skip to content

Commit

Permalink
Refactor resources to named vars in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kulmann committed Aug 18, 2021
1 parent bf6080b commit 3ac9287
Showing 1 changed file with 56 additions and 52 deletions.
108 changes: 56 additions & 52 deletions packages/web-app-files/tests/unit/views/Personal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,46 +60,48 @@ const stubs = {
'list-info': true
}

const resources = [
{
id: 'forest',
name: 'forest.jpg',
path: 'images/nature/forest.jpg',
thumbnail: 'https://cdn.pixabay.com/photo/2015/09/09/16/05/forest-931706_960_720.jpg',
type: 'file',
size: '111000234',
mdate: 'Thu, 01 Jul 2021 08:34:04 GMT'
},
{
id: 'notes',
name: 'notes.txt',
path: '/Documents/notes.txt',
icon: 'text',
type: 'file',
size: '1245',
mdate: 'Thu, 01 Jul 2021 08:45:04 GMT'
},
{
id: 'documents',
name: 'Documents',
path: '/Documents',
icon: 'folder',
type: 'folder',
size: '5324435',
mdate: 'Sat, 09 Jan 2021 14:34:04 GMT'
},
{
id: 'pdfs',
name: 'Pdfs',
path: '/pdfs',
icon: 'folder',
type: 'folder',
size: '53244',
mdate: 'Sat, 09 Jan 2021 14:34:04 GMT'
}
]
const resourceForestJpg = {
id: 'forest',
name: 'forest.jpg',
path: 'images/nature/forest.jpg',
thumbnail: 'https://cdn.pixabay.com/photo/2015/09/09/16/05/forest-931706_960_720.jpg',
type: 'file',
size: '111000234',
mdate: 'Thu, 01 Jul 2021 08:34:04 GMT'
}
const resourceNotesTxt = {
id: 'notes',
name: 'notes.txt',
path: '/Documents/notes.txt',
icon: 'text',
type: 'file',
size: '1245',
mdate: 'Thu, 01 Jul 2021 08:45:04 GMT'
}
const resourceDocumentsFolder = {
id: 'documents',
name: 'Documents',
path: '/Documents',
icon: 'folder',
type: 'folder',
size: '5324435',
mdate: 'Sat, 09 Jan 2021 14:34:04 GMT'
}
const resourcePdfsFolder = {
id: 'pdfs',
name: 'Pdfs',
path: '/pdfs',
icon: 'folder',
type: 'folder',
size: '53244',
mdate: 'Sat, 09 Jan 2021 14:34:04 GMT'
}

const resourcesFiles = [resourceForestJpg, resourceNotesTxt]
const resourcesFolders = [resourceDocumentsFolder, resourcePdfsFolder]
const resources = [...resourcesFiles, ...resourcesFolders]

function createWrapper(selectedFiles = [resources[0]]) {
function createWrapper(selectedFiles = [resourceForestJpg]) {
jest.spyOn(Personal.methods, 'loadResources').mockImplementation()
jest
.spyOn(MixinAccessibleBreadcrumb.methods, 'accessibleBreadcrumb_focusAndAnnounceBreadcrumb')
Expand Down Expand Up @@ -128,12 +130,12 @@ function createWrapper(selectedFiles = [resources[0]]) {
currentPage: 1
},
getters: {
activeFiles: () => resources,
activeFiles: () => [...resources],
inProgress: () => [null],
currentFolder: () => '/',
pages: () => 4,
selectedFiles: () => selectedFiles,
highlightedFile: () => resources[0]
selectedFiles: () => [...selectedFiles],
highlightedFile: () => resourceForestJpg
},
actions: {
loadIndicators: () => {}
Expand All @@ -144,7 +146,9 @@ function createWrapper(selectedFiles = [resources[0]]) {
SET_CURRENT_FOLDER: () => {},
LOAD_FILES: () => {},
CLEAR_CURRENT_FILES_LIST: () => {},
REMOVE_FILE_FROM_SEARCHED: () => {}
REMOVE_FILE: () => {},
REMOVE_FILE_FROM_SEARCHED: () => {},
REMOVE_FILE_SELECTION: () => {}
},
namespaced: true
}
Expand Down Expand Up @@ -173,26 +177,26 @@ describe('Personal view', () => {
describe('file move with drag & drop', () => {
it('should exit if target is also selected', async () => {
const spyOnGetFolderItems = jest.spyOn(Personal.methods, 'fetchResources')
const wrapper = createWrapper([resources[0], resources[3]])
await wrapper.vm.fileDropped(resources[3].id)
const wrapper = createWrapper([resourceForestJpg, resourcePdfsFolder])
await wrapper.vm.fileDropped(resourcePdfsFolder.id)
expect(spyOnGetFolderItems).not.toBeCalled()
spyOnGetFolderItems.mockReset()
})
it('should exit if target is not a folder', async () => {
const spyOnGetFolderItems = jest.spyOn(Personal.methods, 'fetchResources')
const wrapper = createWrapper([resources[2]])
await wrapper.vm.fileDropped(resources[0].id)
const wrapper = createWrapper([resourceDocumentsFolder])
await wrapper.vm.fileDropped(resourceForestJpg.id)
expect(spyOnGetFolderItems).not.toBeCalled()
spyOnGetFolderItems.mockReset()
})
it('should not move file if resource is already in target', async () => {
const spyOnGetFolderItems = jest
.spyOn(Personal.methods, 'fetchResources')
.mockResolvedValueOnce([resources[2]])
.mockResolvedValueOnce([resourceDocumentsFolder])
const spyOnMoveFiles = jest.spyOn(localVue.prototype.$client.files, 'move')

const wrapper = createWrapper([resources[2]])
await wrapper.vm.fileDropped(resources[3].id)
const wrapper = createWrapper([resourceDocumentsFolder])
await wrapper.vm.fileDropped(resourcePdfsFolder.id)
expect(spyOnMoveFiles).not.toBeCalled()

spyOnMoveFiles.mockReset()
Expand All @@ -206,8 +210,8 @@ describe('Personal view', () => {
.spyOn(localVue.prototype.$client.files, 'move')
.mockImplementation()

const wrapper = createWrapper([resources[2]])
await wrapper.vm.fileDropped(resources[3].id)
const wrapper = createWrapper([resourceDocumentsFolder])
await wrapper.vm.fileDropped(resourcePdfsFolder.id)
expect(spyOnMoveFilesMove).toBeCalled()

spyOnMoveFilesMove.mockReset()
Expand Down

0 comments on commit 3ac9287

Please sign in to comment.