Skip to content

Commit cc8bf09

Browse files
committed
test - Add a test for plotly subfigures
1 parent 432c04f commit cc8bf09

File tree

5 files changed

+97
-1
lines changed

5 files changed

+97
-1
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
title: "Bugged plotly figure: phantom subfigure"
3+
keep-ipynb: true
4+
keep-md: true
5+
_quarto:
6+
tests:
7+
html:
8+
ensureHtmlElements:
9+
-
10+
- 'figure.quarto-float-fig div#fig-gapminder-1 figure.quarto-subfloat-fig div.plotly-graph-div'
11+
- 'figure.quarto-float-fig div#fig-gapminder-2 figure.quarto-subfloat-fig div.plotly-graph-div'
12+
ensureHtmlElementContents:
13+
selectors:
14+
- 'div#fig-gapminder-1 figcaption.quarto-subfloat-caption'
15+
- 'div#fig-gapminder-2 figcaption.quarto-subfloat-caption'
16+
matches: ['\((a|b)\) Gapminder: (1957|2007)']
17+
ensureHtmlElementCount:
18+
selectors: ['figure.quarto-float-fig figure.quarto-subfloat-fig']
19+
counts: [2]
20+
---
21+
22+
```{python}
23+
#| label: fig-gapminder
24+
#| fig-cap: "Life Expectancy and GDP"
25+
#| fig-subcap:
26+
#| - "Gapminder: 1957"
27+
#| - "Gapminder: 2007"
28+
#| layout-ncol: 2
29+
#| column: page
30+
31+
import plotly.express as px
32+
import plotly.io as pio
33+
gapminder = px.data.gapminder()
34+
def gapminder_plot(year):
35+
gapminderYear = gapminder.query("year == " +
36+
str(year))
37+
fig = px.scatter(gapminderYear,
38+
x="gdpPercap", y="lifeExp",
39+
size="pop", size_max=60,
40+
hover_name="country")
41+
fig.show()
42+
43+
gapminder_plot(1957)
44+
gapminder_plot(2007)
45+
```

tests/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ dependencies = [
1818
"great-tables>=0.17.0",
1919
"polars>=1.29.0",
2020
"pyarrow>=20.0.0",
21+
"plotly>=6.1.1",
2122
]

tests/smoke/smoke-all.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ import {
3636
ensureLatexFileRegexMatches,
3737
printsMessage,
3838
shouldError,
39-
ensureHtmlElementContents
39+
ensureHtmlElementContents,
40+
ensureHtmlElementCount,
4041
} from "../verify.ts";
4142
import { readYamlFromMarkdown } from "../../src/core/yaml.ts";
4243
import { findProjectDir, findProjectOutputDir, outputForInput } from "../utils.ts";
@@ -130,6 +131,7 @@ function resolveTestSpecs(
130131
ensureEpubFileRegexMatches,
131132
ensureHtmlElements,
132133
ensureHtmlElementContents,
134+
ensureHtmlElementCount,
133135
ensureFileRegexMatches,
134136
ensureLatexFileRegexMatches,
135137
ensureTypstFileRegexMatches,

tests/uv.lock

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/verify.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,39 @@ export const ensureHtmlElementContents = (
402402

403403
}
404404

405+
export const ensureHtmlElementCount = (
406+
file: string,
407+
options: {
408+
selectors: string[] | string,
409+
counts: number[] | number
410+
}
411+
): Verify => {
412+
return {
413+
name: "Verify number of elements for selectors",
414+
verify: async (_output: ExecuteOutput[]) => {
415+
const htmlInput = await Deno.readTextFile(file);
416+
const doc = new DOMParser().parseFromString(htmlInput, "text/html")!;
417+
418+
// Convert single values to arrays for unified processing
419+
const selectorsArray = Array.isArray(options.selectors) ? options.selectors : [options.selectors];
420+
const countsArray = Array.isArray(options.counts) ? options.counts : [options.counts];
421+
422+
if (selectorsArray.length !== countsArray.length) {
423+
throw new Error("Selectors and counts arrays must have the same length");
424+
}
425+
426+
selectorsArray.forEach((selector, index) => {
427+
const expectedCount = countsArray[index];
428+
const elements = doc.querySelectorAll(selector);
429+
assert(
430+
elements.length === expectedCount,
431+
`Selector '${selector}' matched ${elements.length} elements, expected ${expectedCount}.`
432+
);
433+
});
434+
}
435+
};
436+
};
437+
405438
export const ensureSnapshotMatches = (
406439
file: string,
407440
): Verify => {

0 commit comments

Comments
 (0)