Skip to content

Commit 7e7c92c

Browse files
Merge pull request #204 from istex/fix/166-image-caption
Fix(latex): Display formula if latex fails to be parsed
2 parents 5ac6454 + 2c8d58a commit 7e7c92c

14 files changed

Lines changed: 78 additions & 333 deletions

packages/react-tei/src/SidePanel/authors/PersNamePart.spec.tsx

Lines changed: 4 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, it, vi } from "vitest";
1+
import { describe, expect, it } from "vitest";
22
import { render } from "vitest-browser-react";
33
import type { DocumentJson } from "../../parser/document";
44
import { TagCatalogProvider } from "../../tags/TagCatalogProvider";
@@ -14,7 +14,6 @@ describe("PersNamePart", () => {
1414
value: [],
1515
},
1616
"forename",
17-
null,
1817
],
1918
[
2019
{
@@ -23,7 +22,6 @@ describe("PersNamePart", () => {
2322
value: [],
2423
},
2524
"honorific",
26-
null,
2725
],
2826
[
2927
{
@@ -32,7 +30,6 @@ describe("PersNamePart", () => {
3230
value: [],
3331
},
3432
"degree",
35-
null,
3633
],
3734
[
3835
{
@@ -41,7 +38,6 @@ describe("PersNamePart", () => {
4138
value: [],
4239
},
4340
null,
44-
"PersNamePart roleName missing or invalid @type:",
4541
],
4642
[
4743
{
@@ -50,73 +46,55 @@ describe("PersNamePart", () => {
5046
value: [],
5147
},
5248
null,
53-
"PersNamePart roleName missing or invalid @type:",
5449
],
5550
[
5651
{
5752
tag: "surname",
5853
value: [],
5954
},
6055
"surname",
61-
null,
6256
],
6357
[
6458
{
6559
tag: "genName",
6660
value: [],
6761
},
6862
"genName",
69-
null,
7063
],
7164
[
7265
{
7366
tag: "nameLink",
7467
value: [],
7568
},
7669
"nameLink",
77-
null,
7870
],
7971
[
8072
{
8173
tag: "addName",
8274
value: [],
8375
},
8476
"addName",
85-
null,
8677
],
8778
[
8879
{
8980
tag: "orgName",
9081
value: [],
9182
},
9283
"orgName",
93-
null,
9484
],
9585
[
9686
{
9787
tag: "unknownTag",
9888
value: [],
9989
},
10090
null,
101-
"PersNamePart unknown tag:",
10291
],
10392
])("should return description type", (input: DocumentJson, expected:
10493
| string
105-
| null, warning: string | null) => {
106-
const consoleWarnSpy = vi
107-
.spyOn(console, "warn")
108-
.mockImplementation(() => {});
94+
| null) => {
10995
const descriptionKey = getDescriptionKey(input);
11096

11197
expect(descriptionKey).toBe(expected);
112-
113-
if (warning) {
114-
expect(consoleWarnSpy).toHaveBeenCalledWith(warning, input);
115-
} else {
116-
expect(consoleWarnSpy).not.toHaveBeenCalled();
117-
}
118-
119-
consoleWarnSpy.mockRestore();
12098
});
12199
});
122100

@@ -146,9 +124,6 @@ describe("PersNamePart", () => {
146124
});
147125

148126
it("should render PersNamePart value without aria-description for unknown description type", async () => {
149-
const consoleWarnSpy = vi
150-
.spyOn(console, "warn")
151-
.mockImplementation(() => {});
152127
const { getByText } = await render(
153128
<PersNamePart
154129
data={{
@@ -169,24 +144,9 @@ describe("PersNamePart", () => {
169144
const spanElement = getByText("Unknown Role");
170145
expect(spanElement).toBeInTheDocument();
171146
expect(spanElement).not.toHaveAttribute("aria-description");
172-
173-
expect(consoleWarnSpy).toHaveBeenCalledWith(
174-
"PersNamePart roleName missing or invalid @type:",
175-
{
176-
tag: "roleName",
177-
attributes: { "@type": "unknown" },
178-
value: [{ tag: "#text", value: "Unknown Role" }],
179-
},
180-
);
181-
182-
consoleWarnSpy.mockRestore();
183147
});
184148

185-
it("should render nothing and log a warning if data.value is not an array", async () => {
186-
const consoleWarnSpy = vi
187-
.spyOn(console, "warn")
188-
.mockImplementation(() => {});
189-
149+
it("should render nothing if data.value is not an array", async () => {
190150
const { container } = await render(
191151
<PersNamePart
192152
data={{
@@ -204,20 +164,9 @@ describe("PersNamePart", () => {
204164
);
205165

206166
expect(container).toBeEmptyDOMElement();
207-
208-
expect(consoleWarnSpy).toHaveBeenCalledWith(
209-
"PersName data.value is not an array:",
210-
"Not an array",
211-
);
212-
213-
consoleWarnSpy.mockRestore();
214167
});
215168

216-
it("should render nothing and log a warning if data.value is an empty array", async () => {
217-
const consoleWarnSpy = vi
218-
.spyOn(console, "warn")
219-
.mockImplementation(() => {});
220-
169+
it("should render nothing if data.value is an empty array", async () => {
221170
const { container } = await render(
222171
<PersNamePart
223172
data={{
@@ -235,15 +184,5 @@ describe("PersNamePart", () => {
235184
);
236185

237186
expect(container).toBeEmptyDOMElement();
238-
239-
expect(consoleWarnSpy).toHaveBeenCalledWith(
240-
"PersNamePart data.value is empty:",
241-
{
242-
tag: "forename",
243-
value: [],
244-
},
245-
);
246-
247-
consoleWarnSpy.mockRestore();
248187
});
249188
});

packages/react-tei/src/SidePanel/authors/PersNamePart.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useMemo } from "react";
22
import { useTranslation } from "react-i18next";
3+
import { IS_DEBUG } from "../../debug/debug.const";
34
import type { DocumentJson } from "../../parser/document";
45
import { Value } from "../../tags/Value";
56

@@ -11,7 +12,8 @@ export const getDescriptionKey = (data: DocumentJson) => {
1112
!attributes?.["@type"] ||
1213
!["honorific", "degree"].includes(attributes["@type"])
1314
) {
14-
console.warn("PersNamePart roleName missing or invalid @type:", data);
15+
IS_DEBUG &&
16+
console.warn("PersNamePart roleName missing or invalid @type:", data);
1517
return null;
1618
}
1719
return attributes["@type"];
@@ -31,7 +33,7 @@ export const getDescriptionKey = (data: DocumentJson) => {
3133
return tag;
3234
}
3335

34-
console.warn("PersNamePart unknown tag:", data);
36+
IS_DEBUG && console.warn("PersNamePart unknown tag:", data);
3537
return null;
3638
};
3739

@@ -42,12 +44,13 @@ export type PersNamePartProps = {
4244
export function PersNamePart({ data }: PersNamePartProps) {
4345
const { t } = useTranslation();
4446
if (!Array.isArray(data.value)) {
45-
console.warn("PersName data.value is not an array:", data.value);
47+
IS_DEBUG &&
48+
console.warn("PersName data.value is not an array:", data.value);
4649
return null;
4750
}
4851

4952
if (data.value.length === 0) {
50-
console.warn("PersNamePart data.value is empty:", data);
53+
IS_DEBUG && console.warn("PersNamePart data.value is empty:", data);
5154
return null;
5255
}
5356

packages/react-tei/src/SidePanel/authors/useAuthors.spec.tsx

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, it, vi } from "vitest";
1+
import { describe, expect, it } from "vitest";
22
import { renderHook } from "vitest-browser-react";
33
import { DocumentContextProvider } from "../../DocumentContextProvider";
44
import type { DocumentJson } from "../../parser/document";
@@ -128,11 +128,7 @@ describe("useAuthors", () => {
128128
expect(result.result.current).toEqual([]);
129129
});
130130

131-
it("should log a warning if an author tag has no value or value is not an array and remove it form result", async () => {
132-
const consoleWarnSpy = vi
133-
.spyOn(console, "warn")
134-
.mockImplementation(() => {});
135-
131+
it("should remove author tags with no value or value is not an array from the result", async () => {
136132
const invalidJsonDocument = [
137133
{
138134
tag: "TEI",
@@ -194,27 +190,6 @@ describe("useAuthors", () => {
194190
value: [{ tag: "persName", value: "Valid Author" }],
195191
},
196192
]);
197-
expect(consoleWarnSpy).toHaveBeenCalledTimes(2);
198-
expect(consoleWarnSpy).toHaveBeenCalledWith(
199-
"Author tag has no value or value is not an array",
200-
{
201-
author: {
202-
tag: "author",
203-
value: null,
204-
},
205-
},
206-
);
207-
expect(consoleWarnSpy).toHaveBeenCalledWith(
208-
"Author tag has no value or value is not an array",
209-
{
210-
author: {
211-
tag: "author",
212-
value: "Just a string value",
213-
},
214-
},
215-
);
216-
217-
consoleWarnSpy.mockRestore();
218193
});
219194

220195
it("should only return 10 authors at most", async () => {

packages/react-tei/src/SidePanel/authors/useAuthors.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useMemo } from "react";
22
import { useDocumentContext } from "../../DocumentContextProvider";
3+
import { IS_DEBUG } from "../../debug/debug.const";
34
import type { DocumentJson } from "../../parser/document";
45
import { getDocumentJsonAtPath } from "../../parser/getDocumentJsonAtPath";
56

@@ -21,9 +22,10 @@ export const useAuthors = (): DocumentJson[] => {
2122
return authors
2223
.filter((author) => {
2324
if (!author.value || !Array.isArray(author.value)) {
24-
console.warn("Author tag has no value or value is not an array", {
25-
author,
26-
});
25+
IS_DEBUG &&
26+
console.warn("Author tag has no value or value is not an array", {
27+
author,
28+
});
2729
return false;
2830
}
2931
return true;

0 commit comments

Comments
 (0)