Skip to content

test(viewer): unit-test the viewer-main Vue component (#53)#138

Merged
Jaggob merged 2 commits into
mainfrom
test/53-viewer-main-vitest
Jun 11, 2026
Merged

test(viewer): unit-test the viewer-main Vue component (#53)#138
Jaggob merged 2 commits into
mainfrom
test/53-viewer-main-vitest

Conversation

@Jaggob

@Jaggob Jaggob commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

Closes #53.

src/viewer-main.js (~540 LOC) was the largest untested frontend module. The issue flagged a decision: add @vue/test-utils or rely on e2e. Since we don't bundle Vue (NC provides it at runtime and the component carries both beforeDestroy/beforeUnmount to stay Vue 2/3 agnostic), adding a self-chosen Vue dev-dep would test against a runtime that isn't NC's. So this goes the no-mount route instead.

Approach

  • The component is a plain Vue options object. It's captured at import time by stubbing OCA.Viewer.registerHandler (same import-for-side-effect pattern as embed-main.test.js) — no source change, production bundle untouched.
  • A small makeInstance() helper seeds props/data, exposes computed as live getters, and binds methods to one context.
  • render is driven with a mock createElement; helpers query the produced vnode tree (by tag/class/text) so render branches are asserted without a DOM mount.
  • The six src/lib/* collaborators are mocked (oc-compat, pad-sync, api-client, sanitize-html, pad-frame-srcdoc, urls); fetch is stubbed per test.

Coverage (27 cases)

  • computedfilePath derivation (fileInfo path, filename+dir fallback, DAV source, non-pad), resolvedFileId (prop precedence, null cases).
  • resolveOpenUrl — happy path (iframeSrc + sync configured/started + update:loaded), no sync_url, external snapshot, readonly snapshot, missing-frontmatter init-then-retry, missing_binding recovery gating + the public-share exclusion, and stale-generation abort.
  • recoverFromSnapshot — posts + clears + re-resolves; no-op when unavailable; error path.
  • teardown — flush/stop/unhook on beforeUnmount; no controller spun up just to tear down.
  • render — error-card states (plain, checking-original, original-found, no-original), external + readonly snapshot views (incl. sanitized HTML), loading placeholder, iframe srcdoc shell.

Verification

Test-only — no production code touched. npm test green: 16 files, 150 tests (was 123). No js/ rebuild needed.

Jaggob added 2 commits June 11, 2026 13:37
viewer-main.js was the largest untested frontend module. Rather than
add a Vue runtime as a dev dependency (we don't bundle Vue — NC provides
it at runtime, and the component is deliberately Vue 2/3 agnostic), the
component options object is exercised directly: computed getters and
methods run against a controlled `this`, and the render function is
driven with a mock `createElement` so the produced vnode tree can be
asserted. The component is captured by stubbing
`OCA.Viewer.registerHandler` before import, so no source export is
needed and the production bundle is untouched.

27 cases cover path/fileId derivation, resolveOpenUrl (happy path,
no-sync, external + readonly snapshot, missing-frontmatter retry,
missing_binding recovery gating incl. the public-share exclusion, and
stale-generation abort), recoverFromSnapshot, sync teardown on unmount,
and every render branch (error card states, snapshot views, loading,
iframe shell).

Closes #53.
- flush microtasks before asserting the fire-and-forget original-pad
  hint, so the assertion survives a future second await in that path.
- cover the resolvedFileId Files-URL fallback (openfile=true + the
  /apps/files/files/<id> regex, and the negative case without openfile).
- cover the initialize-by-id branch (init retry with a resolved file id),
  complementing the existing by-path init-retry case.

30 cases; npm test green (153 total).
@Jaggob Jaggob merged commit cfe4b7c into main Jun 11, 2026
11 checks passed
@Jaggob Jaggob deleted the test/53-viewer-main-vitest branch June 11, 2026 11:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Vue component lifecycle tests for viewer-main.js

1 participant