-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2cd2791
commit 1698213
Showing
1 changed file
with
113 additions
and
0 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
packages/web-pkg/tests/unit/components/Modals/SaveAsModal.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import SaveAsModal from '../../../../src/components/Modals/SaveAsModal.vue' | ||
import { defaultComponentMocks, defaultPlugins, nextTicks, shallowMount } from 'web-test-helpers' | ||
import { mock, mockDeep } from 'vitest-mock-extended' | ||
import { Resource, SpaceResource } from '@ownclouders/web-client' | ||
import { ListFilesResult } from '@ownclouders/web-client/src' | ||
import { Modal, useMessages, useModals } from '../../../../src/composables/piniaStores' | ||
import { ClientService } from '../../../../src' | ||
|
||
describe('SaveAsModal', () => { | ||
describe('iframe', () => { | ||
it('sets the iframe src correctly', () => { | ||
const { wrapper } = getWrapper() | ||
expect(wrapper.vm.iframeSrc).toEqual( | ||
'http://localhost:3000/files-spaces-generic?hide-logo=true&embed=true&embed-target=location&embed-choose-file-name=true&embed-choose-file-name-suggestion=test.txt' | ||
) | ||
}) | ||
it('sets the iframe title correctly', () => { | ||
const { wrapper } = getWrapper() | ||
expect(wrapper.vm.iframeTitle).toEqual('ownCloud') | ||
}) | ||
}) | ||
describe('method "onLocationPick"', () => { | ||
it('does nothing if the event message does not equal "owncloud-embed:select"', () => { | ||
const { mocks } = getWrapper() | ||
|
||
expect(mocks.$clientService.webdav.listFiles).not.toHaveBeenCalled() | ||
expect(mocks.$clientService.webdav.putFileContents).not.toHaveBeenCalled() | ||
}) | ||
it('saves the file when message does equal "owncloud-embed:select"', async () => { | ||
const { wrapper, mocks } = getWrapper() | ||
const modalStore = useModals() | ||
const messageStore = useMessages() | ||
|
||
mocks.$clientService.webdav.putFileContents.mockResolvedValue(mock<Resource>()) | ||
|
||
wrapper.vm.onLocationPick( | ||
mock<MessageEvent>({ | ||
data: { | ||
name: 'owncloud-embed:select', | ||
data: { | ||
resources: [mock<Resource>({ storageId: '1' })], | ||
fileName: 'test with new name.txt' | ||
} | ||
} | ||
}) | ||
) | ||
|
||
await nextTicks(2) | ||
expect(messageStore.showMessage).toHaveBeenCalled() | ||
expect(modalStore.removeModal).toHaveBeenCalled() | ||
}) | ||
it('shows an error message when the file when message does equal "owncloud-embed:select and request fails"', async () => { | ||
console.error = vi.fn() | ||
const { wrapper, mocks } = getWrapper() | ||
const modalStore = useModals() | ||
const messageStore = useMessages() | ||
|
||
mocks.$clientService.webdav.putFileContents.mockRejectedValue(new Error('')) | ||
|
||
wrapper.vm.onLocationPick( | ||
mock<MessageEvent>({ | ||
data: { | ||
name: 'owncloud-embed:select', | ||
data: { | ||
resources: [mock<Resource>({ storageId: '1' })], | ||
fileName: 'test with new name.txt' | ||
} | ||
} | ||
}) | ||
) | ||
|
||
await nextTicks(2) | ||
expect(messageStore.showErrorMessage).toHaveBeenCalled() | ||
expect(modalStore.removeModal).toHaveBeenCalled() | ||
}) | ||
}) | ||
}) | ||
|
||
function getWrapper() { | ||
const $clientService = mockDeep<ClientService>() | ||
const mocks = { ...defaultComponentMocks(), $clientService } | ||
mocks.$clientService.webdav.listFiles.mockResolvedValue(mock<ListFilesResult>({ children: [] })) | ||
|
||
return { | ||
mocks, | ||
wrapper: shallowMount(SaveAsModal, { | ||
props: { | ||
modal: mock<Modal>(), | ||
content: 'some text', | ||
originalResource: { id: '1', path: '/test.txt', name: 'test.txt', extension: 'txt' }, | ||
parentFolderLink: { | ||
name: 'files-spaces-generic', | ||
params: { | ||
driveAliasAndItem: 'personal/admin' | ||
}, | ||
query: { | ||
fileId: | ||
'61dcd768-0bc4-4dd5-975a-2fe2bc9bc664$f1e4f3ec-1f24-460d-9f9a-4416ab6ddb6b!36cce768-8c9d-45e4-9c7d-4c9611962a75' | ||
} | ||
} | ||
}, | ||
global: { | ||
plugins: [ | ||
...defaultPlugins({ | ||
piniaOptions: { spacesState: { spaces: [mock<SpaceResource>({ id: '1' })] } } | ||
}) | ||
], | ||
mocks, | ||
provide: mocks | ||
} | ||
}) | ||
} | ||
} |