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.
105 lines
3.7 KiB
TypeScript
105 lines
3.7 KiB
TypeScript
import { HttpClient } from '../../../src/http'
|
|
import { ClientService, useAuthStore, useConfigStore } from '../../../src/'
|
|
import { Language } from 'vue3-gettext'
|
|
import { graph, ocs, webdav } from '@ownclouders/web-client'
|
|
import { Graph } from '@ownclouders/web-client/graph'
|
|
import { OCS } from '@ownclouders/web-client/ocs'
|
|
import { WebDAV } from '@ownclouders/web-client/webdav'
|
|
import { createTestingPinia, writable } from '../../../src/testing'
|
|
import { FetchClient } from '@ownclouders/web-client'
|
|
import { mock } from 'vitest-mock-extended'
|
|
|
|
const language = { current: 'en' }
|
|
const serverUrl = 'someUrl'
|
|
|
|
const getClientServiceMock = () => {
|
|
const authStore = useAuthStore()
|
|
const configStore = useConfigStore()
|
|
writable(configStore).serverUrl = serverUrl
|
|
|
|
return new ClientService({
|
|
configStore,
|
|
language: language as Language,
|
|
authStore
|
|
})
|
|
}
|
|
const v4uuid = '00000000-0000-0000-0000-000000000000'
|
|
vi.mock('uuid', () => ({ v4: () => v4uuid }))
|
|
vi.mock('@ownclouders/web-client', async (importOriginal) => ({
|
|
...(await importOriginal<any>()),
|
|
graph: vi.fn(),
|
|
ocs: vi.fn(),
|
|
webdav: vi.fn()
|
|
}))
|
|
|
|
describe('ClientService', () => {
|
|
beforeEach(() => {
|
|
createTestingPinia({ initialState: { auth: { accessToken: 'token' } } })
|
|
})
|
|
describe('http authenticated', () => {
|
|
it('initializes an http client', () => {
|
|
const clientService = getClientServiceMock()
|
|
expect(clientService.httpAuthenticated).toBeInstanceOf(HttpClient)
|
|
})
|
|
it('initializes the http client with baseUrl and static headers', () => {
|
|
vi.mock('../../../src/http')
|
|
const mocky = vi.mocked(HttpClient)
|
|
getClientServiceMock()
|
|
|
|
expect(mocky).toHaveBeenCalledWith({
|
|
baseUrl: serverUrl,
|
|
staticHeaders: { 'Initiator-ID': v4uuid, 'X-Requested-With': 'XMLHttpRequest' },
|
|
headers: expect.any(Function),
|
|
onResponse: expect.any(Function)
|
|
})
|
|
})
|
|
})
|
|
describe('http unauthenticated', () => {
|
|
it('initializes an http client', () => {
|
|
const clientService = getClientServiceMock()
|
|
expect(clientService.httpUnAuthenticated).toBeInstanceOf(HttpClient)
|
|
})
|
|
it('initializes the http client with baseUrl and static headers', () => {
|
|
vi.mock('../../../src/http')
|
|
const mocky = vi.mocked(HttpClient)
|
|
getClientServiceMock()
|
|
|
|
expect(mocky).toHaveBeenCalledWith({
|
|
baseUrl: serverUrl,
|
|
staticHeaders: { 'Initiator-ID': v4uuid, 'X-Requested-With': 'XMLHttpRequest' },
|
|
headers: expect.any(Function),
|
|
onResponse: expect.any(Function)
|
|
})
|
|
})
|
|
})
|
|
describe('graph', () => {
|
|
it('initializes a fetch client with static headers', () => {
|
|
const graphMock = mock<Graph>()
|
|
const graphSpy = vi.mocked(graph).mockReturnValue(graphMock)
|
|
const clientService = getClientServiceMock()
|
|
|
|
expect(graphSpy).toHaveBeenCalledWith(serverUrl, expect.any(FetchClient))
|
|
expect(clientService.graphAuthenticated).toEqual(graphMock)
|
|
})
|
|
})
|
|
describe('ocs', () => {
|
|
it('initializes a fetch client with static headers', () => {
|
|
const ocsMock = mock<OCS>()
|
|
const ocsSpy = vi.mocked(ocs).mockReturnValue(ocsMock)
|
|
const clientService = getClientServiceMock()
|
|
|
|
expect(ocsSpy).toHaveBeenCalledWith(serverUrl, expect.any(FetchClient))
|
|
expect(clientService.ocs).toEqual(ocsMock)
|
|
})
|
|
})
|
|
describe('webdav', () => {
|
|
it('initializes a webdav client', () => {
|
|
const webDavMock = mock<WebDAV>()
|
|
const webDavSpy = vi.mocked(webdav).mockReturnValue(webDavMock)
|
|
const clientService = getClientServiceMock()
|
|
expect(webDavSpy).toHaveBeenCalledWith(serverUrl, expect.anything(), expect.anything())
|
|
expect(clientService.webdav).toEqual(webDavMock)
|
|
})
|
|
})
|
|
})
|