Files
ocis/web/packages/web-pkg/tests/unit/components/CreateLinkModal.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

198 lines
7.0 KiB
TypeScript

import CreateLinkModal from '../../../src/components/CreateLinkModal.vue'
import { ComponentProps, defaultComponentMocks, defaultPlugins, mount } from '../../../src/testing'
import { mock } from 'vitest-mock-extended'
import { PasswordPolicyService } from '../../../src/services'
import { usePasswordPolicyService } from '../../../src/composables/passwordPolicyService'
import { AbilityRule, LinkShare, Resource, ShareRole, SpaceResource } from '@ownclouders/web-client'
import { PasswordPolicy } from '@ownclouders/design-system/helpers'
import { useEmbedMode } from '../../../src/composables/embedMode'
import { useLinkTypes } from '../../../src/composables/links'
import { ref } from 'vue'
import { CapabilityStore, Modal, useSharesStore } from '../../../src/composables/piniaStores'
import { SharingLinkType } from '@ownclouders/web-client/graph/generated'
import { describe } from 'vitest'
vi.mock('../../../src/composables/embedMode')
vi.mock('../../../src/composables/passwordPolicyService')
vi.mock('../../../src/composables/links', async (importOriginal) => ({
...(await importOriginal<any>()),
useLinkTypes: vi.fn()
}))
const selectors = {
passwordInput: '.link-modal-password-input',
roleElements: '.role-dropdown-list li',
contextMenuToggle: '#link-modal-context-menu-toggle',
confirmBtn: '.link-modal-confirm',
cancelBtn: '.link-modal-cancel',
linkRoleDropDownToggle: '.link-role-dropdown-toggle'
}
describe('CreateLinkModal', () => {
describe('password input', () => {
it('should be rendered', () => {
const { wrapper } = getWrapper()
expect(wrapper.find(selectors.passwordInput).exists()).toBeTruthy()
})
})
describe('datepicker', () => {
it('should be rendered', () => {
const { wrapper } = getWrapper()
expect(wrapper.findComponent({ name: 'oc-datepicker' }).exists()).toBeTruthy()
})
})
describe('link role drop', () => {
it('lists all types as roles', async () => {
const availableLinkTypes = [SharingLinkType.View, SharingLinkType.Edit]
const { wrapper } = getWrapper({ availableLinkTypes })
await wrapper.find(selectors.linkRoleDropDownToggle).trigger('click')
expect(wrapper.findAll(selectors.roleElements).length).toBe(availableLinkTypes.length)
})
})
describe('method "confirm"', () => {
it('creates links for all resources', async () => {
const resources = [mock<Resource>({ isFolder: false }), mock<Resource>({ isFolder: false })]
const { wrapper } = getWrapper({ resources })
await wrapper.vm.onConfirm()
const { addLink } = useSharesStore()
expect(addLink).toHaveBeenCalledTimes(resources.length)
})
it('emits event in embed mode including the created links', async () => {
const resources = [mock<Resource>({ isFolder: false })]
const { wrapper, mocks } = getWrapper({ resources, embedModeEnabled: true })
const link = mock<LinkShare>({ webUrl: 'someurl' })
const { addLink } = useSharesStore()
vi.mocked(addLink).mockResolvedValue(link)
await wrapper.vm.onConfirm()
expect(mocks.postMessageMock).toHaveBeenCalledWith('owncloud-embed:share', [link.webUrl])
})
it('shows error messages for links that failed to be created', async () => {
const consoleMock = vi.fn(() => undefined)
vi.spyOn(console, 'error').mockImplementation(consoleMock)
const resources = [mock<Resource>({ isFolder: false })]
const { wrapper } = getWrapper({ resources })
const { addLink } = useSharesStore()
vi.mocked(addLink).mockRejectedValue({ response: {} })
await wrapper.vm.onConfirm()
expect(consoleMock).toHaveBeenCalledTimes(1)
})
it('calls clipboard after link creation', async () => {
global.ClipboardItem = vi.fn().mockImplementation(function (data) {
return { data }
}) as any
global.navigator = {
...global.navigator,
clipboard: {
write: vi.fn().mockResolvedValue(undefined)
}
} as any
const resources = [mock<Resource>({ isFolder: false })]
const { wrapper } = getWrapper({ resources })
const link = mock<LinkShare>({ webUrl: 'https://example.com/link' })
const { addLink } = useSharesStore()
vi.mocked(addLink).mockResolvedValue(link)
await wrapper.vm.onConfirm()
expect(global.navigator.clipboard.write).toHaveBeenCalled()
expect(global.ClipboardItem).toHaveBeenCalled()
})
})
describe('action buttons', () => {
describe('confirm button', () => {
it('is disabled when password policy is not fulfilled', async () => {
const { wrapper } = getWrapper({ passwordPolicyFulfilled: false })
expect(wrapper.find(selectors.confirmBtn).attributes('disabled')).toBeTruthy()
})
})
})
})
function getWrapper({
resources = [],
space = undefined,
defaultLinkType = SharingLinkType.View,
userCanCreatePublicLinks = true,
passwordEnforced = false,
passwordPolicyFulfilled = true,
embedModeEnabled = false,
callbackFn = undefined,
availableLinkTypes = [SharingLinkType.View]
}: {
resources?: Resource[]
space?: SpaceResource
defaultLinkType?: SharingLinkType
userCanCreatePublicLinks?: boolean
passwordEnforced?: boolean
passwordPolicyFulfilled?: boolean
embedModeEnabled?: boolean
callbackFn?: ComponentProps<typeof CreateLinkModal>['callbackFn']
availableLinkTypes?: SharingLinkType[]
} = {}) {
vi.mocked(usePasswordPolicyService).mockReturnValue(
mock<PasswordPolicyService>({
getPolicy: () => mock<PasswordPolicy>({ check: () => passwordPolicyFulfilled })
})
)
vi.mocked(useLinkTypes).mockReturnValue(
mock<ReturnType<typeof useLinkTypes>>({
defaultLinkType: ref(defaultLinkType),
getAvailableLinkTypes: () => availableLinkTypes,
getLinkRoleByType: () => mock<ShareRole>({ description: 'role', displayName: 'role' }),
isPasswordEnforcedForLinkType: () => passwordEnforced
})
)
const postMessageMock = vi.fn()
vi.mocked(useEmbedMode).mockReturnValue(
mock<ReturnType<typeof useEmbedMode>>({
isEnabled: ref(embedModeEnabled),
postMessage: postMessageMock
})
)
const mocks = { ...defaultComponentMocks(), postMessageMock }
const abilities = [] as AbilityRule[]
if (userCanCreatePublicLinks) {
abilities.push({ action: 'create-all', subject: 'PublicLink' })
}
const capabilities = {
files_sharing: {
public: {
can_edit: true,
can_contribute: true,
alias: true,
password: { enforced_for: { read_only: passwordEnforced } }
}
}
} satisfies Partial<CapabilityStore['capabilities']>
return {
mocks,
wrapper: mount(CreateLinkModal, {
props: {
space,
resources,
callbackFn,
modal: mock<Modal>()
},
global: {
plugins: [
...defaultPlugins({ abilities, piniaOptions: { capabilityState: { capabilities } } })
],
mocks,
provide: mocks,
stubs: { OcTextInput: true, OcDatepicker: true, OcButton: true }
}
})
}
}