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

193 lines
6.7 KiB
TypeScript

import { ref, unref } from 'vue'
import {
defaultComponentMocks,
defaultPlugins,
mount,
PartialComponentProps,
RouteLocation
} from '../../../src/testing'
import { mock } from 'vitest-mock-extended'
import ViewOptions from '../../../src/components/ViewOptions.vue'
import {
FolderViewModeConstants,
useResourcesStore,
useRouteQuery,
useRouteQueryPersisted
} from '../../../src/composables'
import { FolderView } from '../../../src'
import { OcPageSize, OcSwitch } from '@ownclouders/design-system/components'
vi.mock('../../../src/composables/router', async (importOriginal) => ({
...(await importOriginal<any>()),
useRouteQueryPersisted: vi.fn(),
useRouteQuery: vi.fn()
}))
const selectors = {
pageSizeSelect: '.oc-page-size',
hiddenFilesSwitch: '[data-testid="files-switch-hidden-files"]',
flatListSwitch: '[data-testid="files-switch-flat-list"]',
fileExtensionsSwitch: '[data-testid="files-switch-files-extensions-files"]',
viewModeSwitchBtns: '.viewmode-switch-buttons',
tileSizeSlider: '[data-testid="files-tiles-size-slider"]'
}
describe('ViewOptions component', () => {
describe('pagination', () => {
it('does not show when disabled', () => {
const { wrapper } = getWrapper({ props: { hasPagination: false } })
expect(wrapper.find(selectors.pageSizeSelect).exists()).toBeFalsy()
})
it('sets the correct initial files page limit', () => {
const perPage = '100'
const { wrapper } = getWrapper({ perPage })
expect(
wrapper.findComponent<typeof OcPageSize>(selectors.pageSizeSelect).props().selected
).toBe(perPage)
})
it('sets the correct files page limit', () => {
const perPage = '100'
const newItemsPerPage = '500'
const { wrapper, mocks } = getWrapper({ perPage }) as any
wrapper.vm.setItemsPerPage(newItemsPerPage)
expect(mocks.$router.replace).toHaveBeenCalledWith(
expect.objectContaining({
query: expect.objectContaining({ 'items-per-page': newItemsPerPage })
})
)
})
it('resets the page to 1 if current page is > 1', () => {
const perPage = '100'
const newItemsPerPage = '500'
const { wrapper, mocks } = getWrapper({ perPage, currentPage: '2' }) as any
wrapper.vm.setItemsPerPage(newItemsPerPage)
expect(mocks.$router.replace).toHaveBeenCalledWith(
expect.objectContaining({
query: expect.objectContaining({ 'items-per-page': newItemsPerPage, page: '1' })
})
)
})
})
describe('hidden files toggle', () => {
it('does not show when disabled', () => {
const { wrapper } = getWrapper({ props: { hasHiddenFiles: false } })
expect(wrapper.find(selectors.hiddenFilesSwitch).exists()).toBeFalsy()
})
it('toggles the setting to show/hide hidden files', () => {
const { wrapper } = getWrapper()
wrapper
.findComponent<typeof OcSwitch>(selectors.hiddenFilesSwitch)
.vm.$emit('update:checked', false)
const { setAreHiddenFilesShown } = useResourcesStore()
expect(setAreHiddenFilesShown).toHaveBeenCalled()
})
})
describe('flat list toggle', () => {
it('does not show when disabled', () => {
const { wrapper } = getWrapper({ props: { shouldShowFlatListToggle: false } })
expect(wrapper.find(selectors.flatListSwitch).exists()).toBeFalsy()
})
it('toggles the setting to show/hide flat list', () => {
const { wrapper } = getWrapper()
wrapper
.findComponent<typeof OcSwitch>(selectors.flatListSwitch)
.vm.$emit('update:checked', false)
const { setShouldShowFlatList } = useResourcesStore()
expect(setShouldShowFlatList).toHaveBeenCalled()
})
})
describe('file extension toggle', () => {
it('does not show when disabled', () => {
const { wrapper } = getWrapper({ props: { hasFileExtensions: false } })
expect(wrapper.find(selectors.fileExtensionsSwitch).exists()).toBeFalsy()
})
it('toggles the setting to show/hide file extensions', () => {
const { wrapper } = getWrapper()
wrapper
.findComponent<typeof OcSwitch>(selectors.fileExtensionsSwitch)
.vm.$emit('update:checked', false)
const { setAreFileExtensionsShown } = useResourcesStore()
expect(setAreFileExtensionsShown).toHaveBeenCalled()
})
})
describe('view mode switcher', () => {
it('does not show initially', () => {
const { wrapper } = getWrapper()
expect(wrapper.find(selectors.viewModeSwitchBtns).exists()).toBeFalsy()
})
it('shows if more than one viewModes are passed', () => {
const { wrapper } = getWrapper({
props: {
viewModes: [
mock<FolderView>({ name: '1', label: '' }),
mock<FolderView>({ name: '2', label: '' })
]
}
})
expect(wrapper.find(selectors.viewModeSwitchBtns).exists()).toBeTruthy()
})
})
describe('tile size slider', () => {
it('does not show initially', () => {
const { wrapper } = getWrapper()
expect(wrapper.find(selectors.tileSizeSlider).exists()).toBeFalsy()
})
it('shows if the viewModes include "resource-tiles"', () => {
const { wrapper } = getWrapper({
props: { viewModes: [mock<FolderView>({ name: FolderViewModeConstants.name.tiles })] }
})
expect(wrapper.find(selectors.tileSizeSlider).exists()).toBeTruthy()
})
it.each([1, 2, 3, 4, 5, 6])('applies the correct size step', (tileSize) => {
const { mocks } = getWrapper({
tileSize: tileSize.toString(),
props: { viewModes: [mock<FolderView>({ name: FolderViewModeConstants.name.tiles })] }
})
expect(unref(mocks.tileSizeQueryMock)).toBe(tileSize.toString())
})
})
})
function getWrapper({
perPage = '100',
viewMode = FolderViewModeConstants.name.table,
tileSize = '1',
props = {},
currentPage = '1'
}: {
perPage?: string
viewMode?: string
tileSize?: string
props?: PartialComponentProps<typeof ViewOptions>
currentPage?: string
} = {}) {
vi.mocked(useRouteQueryPersisted).mockImplementationOnce(() => ref(perPage))
vi.mocked(useRouteQueryPersisted).mockImplementationOnce(() => ref(viewMode))
const tileSizeQueryMock = ref(tileSize)
vi.mocked(useRouteQueryPersisted).mockImplementationOnce(() => tileSizeQueryMock)
vi.mocked(useRouteQuery).mockImplementationOnce(() => ref(currentPage))
const mocks = {
...defaultComponentMocks({ currentRoute: mock<RouteLocation>({ path: '/files' }) }),
tileSizeQueryMock
}
return {
mocks,
wrapper: mount(ViewOptions, {
props: {
perPageStoragePrefix: '',
...props
},
global: {
mocks,
provide: mocks,
stubs: { OcButton: true, OcPageSize: false, OcSelect: true },
plugins: [...defaultPlugins()]
}
})
}
}