Skip to content

Commit 7565c0f

Browse files
committed
test: Cover theme token re-render
1 parent d9184a4 commit 7565c0f

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

docs/handoffs/css-theme-tokens.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ Implemented shape:
1414

1515
Validation notes:
1616
- `npm test` covers token injection in tab, markdown embed, and Canvas embed modes while preserving cross-origin iframe isolation.
17-
- A live `obsidian-cli` probe verified theme-toggle re-render: injected `color-scheme` changed from `light` to `dark`, then the original light theme was restored.
17+
- `npm test` also flips parent `theme-light`/`theme-dark` classes and verifies the open HTML tab re-renders with the opposite injected `color-scheme`.
18+
- A live `obsidian-cli` probe also verified the real Obsidian theme toggle path: injected `color-scheme` changed from `light` to `dark`, then the original light theme was restored.

test/test.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,53 @@ OUTER="$(oeval "
205205
})()
206206
")"
207207

208+
THEME_RERENDER="$(oeval "
209+
(async () => {
210+
const readInjectedScheme = async (iframe) => {
211+
if (!iframe) return null;
212+
const html = await fetch(iframe.src).then((response) => response.text());
213+
return html.match(/color-scheme:\\s*(light|dark);/)?.[1] || null;
214+
};
215+
let view = null;
216+
app.workspace.iterateAllLeaves((leaf) => {
217+
const file = leaf.view && leaf.view.file;
218+
if (!view && file && file.path === '$FIXTURE_REL') view = leaf.view;
219+
});
220+
221+
const body = document.body;
222+
const originalLight = body.classList.contains('theme-light');
223+
const originalDark = body.classList.contains('theme-dark');
224+
const result = {
225+
before: null,
226+
after: null,
227+
target: null,
228+
rerendered: false,
229+
restored: false,
230+
};
231+
232+
try {
233+
const iframe = view && view.contentEl && view.contentEl.querySelector('iframe.html-docs-iframe');
234+
result.before = await readInjectedScheme(iframe);
235+
result.target = result.before === 'dark' ? 'light' : 'dark';
236+
body.classList.toggle('theme-light', result.target === 'light');
237+
body.classList.toggle('theme-dark', result.target === 'dark');
238+
await new Promise(resolve => setTimeout(resolve, 300));
239+
const refreshedIframe = view && view.contentEl && view.contentEl.querySelector('iframe.html-docs-iframe');
240+
result.after = await readInjectedScheme(refreshedIframe);
241+
result.rerendered = !!iframe && !!refreshedIframe && iframe.src !== refreshedIframe.src;
242+
} finally {
243+
body.classList.toggle('theme-light', originalLight);
244+
body.classList.toggle('theme-dark', originalDark);
245+
await new Promise(resolve => setTimeout(resolve, 300));
246+
result.restored =
247+
body.classList.contains('theme-light') === originalLight &&
248+
body.classList.contains('theme-dark') === originalDark;
249+
}
250+
251+
return JSON.stringify(result);
252+
})()
253+
")"
254+
208255
INNER="$(oeval "JSON.stringify(window.__hvResults)")"
209256
REGISTRY="$(oeval "
210257
JSON.stringify({
@@ -396,6 +443,9 @@ check "sandbox is locked down" "$(echo "$OUTER" | jq -r .sandbox)"
396443
check "iframe.src is a blob URL" "$(echo "$OUTER" | jq -r .srcIsBlob)" "true"
397444
check "contentDocument is cross-origin" "$(echo "$OUTER" | jq -r .contentDocAccessible)" "false"
398445
check "theme style injected in tab" "$(echo "$OUTER" | jq -r .themeStyleInjected)" "true"
446+
check "theme class re-render flips scheme" "$(echo "$THEME_RERENDER" | jq -r '.after == .target and .before != .after')" "true"
447+
check "theme class re-render swaps blob" "$(echo "$THEME_RERENDER" | jq -r .rerendered)" "true"
448+
check "theme class test restores classes" "$(echo "$THEME_RERENDER" | jq -r .restored)" "true"
399449
check ".html routes to html-docs view" "$(echo "$REGISTRY" | jq -r .htmlViewType)" "html-docs"
400450
check ".htm is not registered" "$(echo "$REGISTRY" | jq -r .htmViewType)" "null"
401451
check ".html embed registered" "$(echo "$REGISTRY" | jq -r .htmlEmbedRegistered)" "true"

0 commit comments

Comments
 (0)