test(viewer): unit-test the viewer-main Vue component (#53)#138
Merged
Conversation
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #53.
src/viewer-main.js(~540 LOC) was the largest untested frontend module. The issue flagged a decision: add@vue/test-utilsor rely on e2e. Since we don't bundle Vue (NC provides it at runtime and the component carries bothbeforeDestroy/beforeUnmountto 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
OCA.Viewer.registerHandler(same import-for-side-effect pattern asembed-main.test.js) — no source change, production bundle untouched.makeInstance()helper seeds props/data, exposescomputedas live getters, and bindsmethodsto one context.renderis driven with a mockcreateElement; helpers query the produced vnode tree (by tag/class/text) so render branches are asserted without a DOM mount.src/lib/*collaborators are mocked (oc-compat,pad-sync,api-client,sanitize-html,pad-frame-srcdoc,urls);fetchis stubbed per test.Coverage (27 cases)
filePathderivation (fileInfo path, filename+dir fallback, DAV source, non-pad),resolvedFileId(prop precedence, null cases).update:loaded), nosync_url, external snapshot, readonly snapshot, missing-frontmatter init-then-retry,missing_bindingrecovery gating + the public-share exclusion, and stale-generation abort.beforeUnmount; no controller spun up just to tear down.Verification
Test-only — no production code touched.
npm testgreen: 16 files, 150 tests (was 123). Nojs/rebuild needed.