Skip to content

Commit 8eb6d31

Browse files
committed
Re-enables test and configuration
1 parent 9bfeb29 commit 8eb6d31

5 files changed

Lines changed: 92 additions & 53 deletions

File tree

__tests__/configuration_options.js

Lines changed: 0 additions & 50 deletions
This file was deleted.

__tests__/configuration_tests.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
const { BASE_URL } = require("../scripts/testBaseUrl");
2+
3+
const FIRST_THUMB_SRC =
4+
"https://iiif.wellcomecollection.org/image/b18035723_0001.JP2/full/90,/0/default.jpg";
5+
6+
// Applies custom config through the examples page's Configuration tab:
7+
// the #customConfig JSON is merged into the viewer config by a
8+
// uv.on("configure") handler when the Apply Configurations button
9+
// re-initialises the viewer.
10+
const applyCustomConfig = async (config) => {
11+
await page.evaluate((cfg) => {
12+
document.getElementById("customConfig").value = cfg;
13+
document.getElementById("clearStorageCheckbox").checked = true;
14+
document.getElementById("setConfigButton").click();
15+
}, JSON.stringify(config));
16+
};
17+
18+
describe("Configuration options", () => {
19+
describe("thumb cache invalidation", () => {
20+
beforeEach(async () => {
21+
await page.goto(BASE_URL);
22+
await page.waitForSelector("#thumb-0 img");
23+
});
24+
25+
it("when set to false does not provide timestamp", async () => {
26+
await applyCustomConfig({
27+
modules: {
28+
contentLeftPanel: {
29+
options: { thumbsCacheInvalidation: { enabled: false } },
30+
},
31+
},
32+
});
33+
34+
// wait for the viewer to re-render thumbs without the timestamp
35+
await page.waitForFunction(
36+
(src) => {
37+
const img = document.querySelector("#thumb-0 img");
38+
return img && img.src === src;
39+
},
40+
{},
41+
FIRST_THUMB_SRC
42+
);
43+
44+
const imageSrc = await page.$eval("#thumb-0 img", (e) => e.src);
45+
expect(imageSrc).toEqual(FIRST_THUMB_SRC);
46+
});
47+
48+
it("has a configurable parameter type", async () => {
49+
await applyCustomConfig({
50+
modules: {
51+
contentLeftPanel: {
52+
options: { thumbsCacheInvalidation: { paramType: "#" } },
53+
},
54+
},
55+
});
56+
57+
// wait for the viewer to re-render thumbs with a #t= timestamp
58+
await page.waitForFunction(
59+
(src) => {
60+
const img = document.querySelector("#thumb-0 img");
61+
return img && img.src.startsWith(`${src}#t=`);
62+
},
63+
{},
64+
FIRST_THUMB_SRC
65+
);
66+
67+
const imageSrc = await page.$eval("#thumb-0 img", (e) => e.src);
68+
expect(imageSrc).toEqual(
69+
expect.stringContaining(`${FIRST_THUMB_SRC}#t=`)
70+
);
71+
});
72+
});
73+
});

src/content-handlers/iiif/extensions/config/ContentLeftPanel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ModuleConfig } from "../../BaseConfig";
22
import { ExpandPanelContent, ExpandPanelOptions } from "./ExpandPanel";
33

4-
type ThumbsCacheInvalidation = {
4+
export type ThumbsCacheInvalidation = {
55
/** Determines if cache invalidation is enabled */
66
enabled: boolean;
77
/** Type of the parameter for cache invalidation */

src/content-handlers/iiif/modules/uv-contentleftpanel-module/ContentLeftPanel.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ export class ContentLeftPanel extends LeftPanel<ContentLeftPanelConfig> {
520520

521521
this.thumbsRoot.render(
522522
createElement(ThumbsView, {
523+
cacheInvalidation: this.config.options.thumbsCacheInvalidation,
523524
thumbs,
524525
paged,
525526
viewingDirection: viewingDirection || ViewingDirection.LEFT_TO_RIGHT,

src/content-handlers/iiif/modules/uv-contentleftpanel-module/ThumbsView.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { ViewingDirection, ViewingHint } from "@iiif/vocabulary";
22
import cx from "classnames";
33
import { Thumb } from "manifesto.js";
4-
import React, { useEffect, useRef } from "react";
4+
import React, { useEffect, useMemo, useRef } from "react";
55
import { useInView } from "react-intersection-observer";
6+
import { ThumbsCacheInvalidation } from "../../extensions/config/ContentLeftPanel";
7+
import { Dates } from "../../Utils";
68

79
const ThumbImage = ({
10+
cacheInvalidation,
811
first,
912
onClick,
1013
onKeyDown,
@@ -14,6 +17,7 @@ const ThumbImage = ({
1417
truncateThumbnailLabels,
1518
viewingDirection,
1619
}: {
20+
cacheInvalidation?: ThumbsCacheInvalidation;
1721
first: boolean;
1822
onClick: (thumb: Thumb) => void;
1923
onKeyDown: (thumb: Thumb) => void;
@@ -29,6 +33,14 @@ const ThumbImage = ({
2933
triggerOnce: true,
3034
});
3135

36+
// memoised so re-renders don't generate a new timestamp and re-request the image
37+
const src = useMemo(() => {
38+
if (thumb.uri && cacheInvalidation && cacheInvalidation.enabled) {
39+
return `${thumb.uri}${cacheInvalidation.paramType}t=${Dates.getTimeStamp()}`;
40+
}
41+
return thumb.uri;
42+
}, [thumb.uri, cacheInvalidation]);
43+
3244
var keydownHandler = (e) => {
3345
if (e.key === "Enter" || e.key === " ") {
3446
e.preventDefault();
@@ -62,7 +74,7 @@ const ThumbImage = ({
6274
height: thumb.height + 8 + "px",
6375
}}
6476
>
65-
{inView && <img src={thumb.uri} alt={thumb.label} />}
77+
{inView && <img src={src} alt={thumb.label} />}
6678
</div>
6779
<div className="info">
6880
<span className="label" title={thumb.label}>
@@ -77,6 +89,7 @@ const ThumbImage = ({
7789
};
7890

7991
const Thumbnails = ({
92+
cacheInvalidation,
8093
onClick,
8194
onKeyDown,
8295
paged,
@@ -86,6 +99,7 @@ const Thumbnails = ({
8699
viewingDirection,
87100
truncateThumbnailLabels,
88101
}: {
102+
cacheInvalidation?: ThumbsCacheInvalidation;
89103
onClick: (thumb: Thumb) => void;
90104
onKeyDown: (thumb: Thumb) => void;
91105
paged: boolean;
@@ -149,6 +163,7 @@ const Thumbnails = ({
149163
className="thumb-container"
150164
>
151165
<ThumbImage
166+
cacheInvalidation={cacheInvalidation}
152167
first={index === firstNonPagedIndex}
153168
onClick={onClick}
154169
onKeyDown={onKeyDown}

0 commit comments

Comments
 (0)