Files
ocis/web/packages/web-pkg/tests/unit/components/Modals/SaveAsModal.spec.ts
Matteo 6069640d49 [full-ci] fix(web): break web-pkg/design-system/web-test-helpers dependency cycle (#13038)
fix(web): break web-pkg/design-system/web-test-helpers dependency cycle

web-pkg, design-system and web-test-helpers formed a cycle: the mount
helpers have to install the design system (its components are registered
globally) and web-pkg's mocked pinia stores, so web-test-helpers was
imported by both packages that imported it back.

Resolved by layering the helpers onto the packages that already need
them, instead of adding another one:

  design-system/testing  mount/shallowMount, prop types, design system +
                         gettext + router-link stub plugins
  web-pkg/src/testing    the above, extended with casl abilities, mocked
                         pinia stores and component mocks
  web-test-helpers       re-export of web-pkg/src/testing

design-system sits at the bottom of the workspace graph, so it cannot
depend on a package that installs it - hence the generic half lives
there. web-pkg composes those plugins rather than duplicating them, so
every helper is still defined exactly once, and no package depends on
one above it.

The public API of `@ownclouders/web-test-helpers` is unchanged, so
external extensions are unaffected.

Also:
- pin `typescript` via `pnpm.overrides` - without it, a second peer-
  resolved typescript version forks `vue`, which forks `vue3-gettext`
  into two module instances, breaking `inject(GetTextSymbol)` for any
  component mounted through these helpers.
- keep `@ownclouders/design-system/testing` external in web-pkg's lib
  build. rollup matches `external` strings exactly, so the subpath was
  inlined, shipping a second copy of the design system and of
  @vue/test-utils into the published bundle - components and wrappers
  were then not identity-equal to the consumer's own, and gettext
  injection broke.
- name the web-test-helpers umd output `.cjs`. The package is ESM, so the
  `.js` file that publishConfig.exports pointed `require` at could not be
  required.

Verified: `pnpm install --frozen-lockfile` clean, zero cyclic-workspace
warnings; `pnpm test:unit` 380 files / 3129 tests pass; `pnpm check:types`
0 errors; `pnpm lint`/`check:format` 0 errors; `pnpm build` succeeds;
published artifacts checked by mounting through the built `dist/` bundles
the way an external extension resolves them via publishConfig.exports.
2026-10-02 08:13:31 +02:00

150 lines
5.0 KiB
TypeScript

import SaveAsModal from '../../../../src/components/Modals/SaveAsModal.vue'
import {
defaultComponentMocks,
defaultPlugins,
nextTicks,
shallowMount
} from '../../../../src/testing'
import { mock, mockDeep } from 'vitest-mock-extended'
import { Resource, SpaceResource } from '@ownclouders/web-client'
import { ListFilesResult } from '@ownclouders/web-client/webdav'
import { Modal, useMessages, useModals } from '../../../../src/composables/piniaStores'
import { ClientService } from '../../../../src'
window.open = vi.fn()
describe('SaveAsModal', () => {
describe('iframe', () => {
it('sets the iframe src correctly', () => {
const { wrapper } = getWrapper()
expect((wrapper.vm as any).iframeSrc).toEqual(
'http://localhost:3000/files-spaces-generic?hide-logo=true&embed=true&embed-target=location&embed-choose-file-name=true&embed-delegate-authentication=false&embed-choose-file-name-suggestion=test.txt'
)
})
it('sets the iframe title correctly', () => {
const { wrapper } = getWrapper()
expect((wrapper.vm as any).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()
expect(window.open).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 as any).onLocationPick(
mock<MessageEvent>({
origin: window.location.origin,
data: {
name: 'owncloud-embed:select',
data: {
resources: [mock<Resource>({ storageId: '1', spaceId: '1' })],
fileName: 'test with new name.txt'
}
}
})
)
await nextTicks(4)
expect(messageStore.showMessage).toHaveBeenCalled()
expect(modalStore.removeModal).toHaveBeenCalled()
expect(window.open).toHaveBeenCalled()
})
it('does nothing when the message originates from an untrusted origin', async () => {
const { wrapper, mocks } = getWrapper()
;(wrapper.vm as any).onLocationPick(
mock<MessageEvent>({
origin: 'https://attacker.example.com',
data: {
name: 'owncloud-embed:select',
data: {
resources: [mock<Resource>({ storageId: '1', spaceId: '1' })],
fileName: 'test with new name.txt'
}
}
})
)
await nextTicks(4)
expect(mocks.$clientService.webdav.putFileContents).not.toHaveBeenCalled()
expect(window.open).not.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 as any).onLocationPick(
mock<MessageEvent>({
origin: window.location.origin,
data: {
name: 'owncloud-embed:select',
data: {
resources: [mock<Resource>({ storageId: '1', spaceId: '1' })],
fileName: 'test with new name.txt'
}
}
})
)
await nextTicks(4)
expect(messageStore.showErrorMessage).toHaveBeenCalled()
expect(modalStore.removeModal).toHaveBeenCalled()
expect(window.open).not.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',
spaceId: '1'
},
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
}
})
}
}