@@ -3,8 +3,9 @@ import { act } from "preact/test-utils";
33import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
44
55/**
6- * The image compression card: the tool's own rows, driving the options that govern every image
7- * arriving in the database rather than a run the user is watching.
6+ * The images card: everything that happens to an image on its way in, compression included — the
7+ * tool's own rows, driving the options that govern every image arriving in the database rather than
8+ * a run the user is watching.
89 *
910 * What is worth holding here is the wiring, since the rows themselves are tested where they live —
1011 * that each row reads and writes the option it stands for, that the group hangs off the switch
@@ -73,13 +74,46 @@ function open(overrides: Record<string, string> = {}) {
7374
7475/** The card's rows, in the order they are drawn, named by their titles. */
7576function rowTitles ( ) : ( string | undefined ) [ ] {
76- return [ ...host . querySelectorAll ( ".media-image-compression .image-compression-section" ) ]
77- . map ( ( row ) => row . querySelector ( ".image-compression-section-title" ) ?. textContent ?? undefined ) ;
77+ return rows ( ) . map ( titleOf ) ;
78+ }
79+
80+ function rows ( ) : HTMLElement [ ] {
81+ return [ ...host . querySelectorAll < HTMLElement > ( ".media-images .tn-card-section" ) ] ;
7882}
7983
8084function row ( title : string ) : HTMLElement | undefined {
81- return [ ...host . querySelectorAll < HTMLElement > ( ".media-image-compression .image-compression-section" ) ]
82- . find ( ( candidate ) => candidate . querySelector ( ".image-compression-section-title" ) ?. textContent === title ) ;
85+ return rows ( ) . find ( ( candidate ) => titleOf ( candidate ) === title ) ;
86+ }
87+
88+ /**
89+ * What a row is called, whichever of the two kinds it is: the page's own option rows carry the
90+ * sentence inside the label, so the title is the text ahead of it, where the compression tool's
91+ * rows keep the title in an element of its own.
92+ */
93+ function titleOf ( row : Element ) : string | undefined {
94+ const label = row . querySelector ( ".tn-card-option-label" ) ;
95+
96+ return label
97+ ? label . childNodes [ 0 ] ?. textContent ?? undefined
98+ : row . querySelector ( ".image-compression-section-title" ) ?. textContent ?? undefined ;
99+ }
100+
101+ /** The sentence beneath a row's title, from whichever of the two kinds of row it is. */
102+ function describes ( title : string ) : string | undefined {
103+ const row = rowOrFail ( title ) ;
104+
105+ return row . querySelector ( ".tn-card-option-description" ) ?. textContent
106+ ?? row . querySelector ( ".image-compression-section-description" ) ?. textContent
107+ ?? undefined ;
108+ }
109+
110+ function rowOrFail ( title : string ) : HTMLElement {
111+ const found = row ( title ) ;
112+ if ( ! found ) {
113+ throw new Error ( `No row titled "${ title } ".` ) ;
114+ }
115+
116+ return found ;
83117}
84118
85119/** Presses one of a row's choice buttons by its label. */
@@ -102,15 +136,17 @@ afterEach(() => {
102136 vi . clearAllMocks ( ) ;
103137} ) ;
104138
105- describe ( "the image compression card" , ( ) => {
139+ describe ( "the images card" , ( ) => {
106140 it ( "hangs the whole group off its switch, nesting what qualifies each choice" , ( ) => {
107141 open ( ) ;
108142
109- // What an untouched install shows, in order: the switch, scaling with its bound, then one
110- // exclusive choice per format. Recompressing a JPEG brings a quality with it; optimizing a
111- // PNG does not, there being no quality to reducing it to a palette — so only one of the two
112- // choices carries a nested row here.
143+ // What an untouched install shows, in order: fetching a referenced image at all, then the
144+ // switch over what is done to the ones that arrive, scaling with its bound, and one
145+ // exclusive choice per format. Recompressing a JPEG brings a quality with it; optimizing
146+ // a PNG does not, there being no quality to reducing it to a palette — so only one of the
147+ // two choices carries a nested row here.
113148 expect ( rowTitles ( ) ) . toEqual ( [
149+ "images.download_images_automatically" ,
114150 "images.automatic_image_compression" ,
115151 "space_usage.compress_resize" ,
116152 "space_usage.compress_max_dimensions" ,
@@ -120,9 +156,13 @@ describe("the image compression card", () => {
120156 ] ) ;
121157
122158 // Switched off, the settings are not merely greyed out but gone: there is nothing for them
123- // to govern, and a bound sitting there would read as one still in force.
159+ // to govern, and a bound sitting there would read as one still in force. What the switch
160+ // does not govern stays where it was — the card is not the compression alone.
124161 open ( { compressImages : "false" } ) ;
125- expect ( rowTitles ( ) ) . toEqual ( [ "images.automatic_image_compression" ] ) ;
162+ expect ( rowTitles ( ) ) . toEqual ( [
163+ "images.download_images_automatically" ,
164+ "images.automatic_image_compression"
165+ ] ) ;
126166 } ) ;
127167
128168 it ( "drops a quality that no longer qualifies anything" , ( ) => {
@@ -131,6 +171,7 @@ describe("the image compression card", () => {
131171 // Neither format is being re-encoded, so neither quality is in force — and resizing is
132172 // still on offer, being the one step that reaches an image whatever its encoding.
133173 expect ( rowTitles ( ) ) . toEqual ( [
174+ "images.download_images_automatically" ,
134175 "images.automatic_image_compression" ,
135176 "space_usage.compress_resize" ,
136177 "space_usage.compress_max_dimensions" ,
@@ -146,9 +187,6 @@ describe("the image compression card", () => {
146187 it ( "carries the sentences the settings had before they were rows" , ( ) => {
147188 open ( ) ;
148189
149- const describes = ( title : string ) =>
150- row ( title ) ?. querySelector ( ".image-compression-section-description" ) ?. textContent ;
151-
152190 expect ( describes ( "images.automatic_image_compression" ) ) . toBe ( "images.enable_image_compression_description" ) ;
153191 expect ( describes ( "space_usage.compress_resize" ) ) . toBe ( "images.max_image_dimensions_description" ) ;
154192 // Both qualities take the same advice, being the same scale read twice.
0 commit comments