|
1 | | -import { expect, describe, it, vitest } from "vitest"; |
2 | | - |
| 1 | +import { render, screen } from "@testing-library/react"; |
| 2 | +import { expect, describe, it } from "vitest"; |
3 | 3 | import React from "react"; |
4 | | -import { render } from "@testing-library/react"; |
5 | 4 |
|
6 | 5 | import AuthorImage from "../../js/templates/AuthorImage"; |
7 | | -import defaultImage from "../../images/author_default.png"; |
8 | | - |
9 | | -describe("Author image component", () => { |
10 | | - it("renders", () => { |
11 | | - const result = render( |
12 | | - <AuthorImage url="http://pictures.org/link-to-author" /> |
13 | | - ); |
14 | 6 |
|
15 | | - expect(result.container.childNodes.length).toBe(1); |
16 | | - }); |
17 | | - |
18 | | - it("renders with correct link", () => { |
19 | | - const LINK = "https://custom-link.com/12345"; |
| 7 | +describe("AuthorImage Component", () => { |
| 8 | + it("renders with the provided URL", () => { |
| 9 | + render(<AuthorImage url="https://example.com/12345" />); |
20 | 10 |
|
21 | | - const result = render(<AuthorImage url={LINK} />); |
| 11 | + const imageLink = screen.getByRole("link", { name: /author image/i }); |
| 12 | + expect(imageLink).toHaveAttribute("href", "https://example.com/12345"); |
22 | 13 |
|
23 | | - expect( |
24 | | - result.container.querySelector("#author_image_link").getAttribute("href") |
25 | | - ).toEqual(LINK); |
26 | | - }); |
27 | | - |
28 | | - it("renders with correct image", () => { |
29 | | - const LINK = "https://custom-link.com/12345"; |
30 | | - |
31 | | - const result = render(<AuthorImage url={LINK} />); |
32 | | - |
33 | | - expect( |
34 | | - result.container.querySelector("#author_image").getAttribute("style") |
35 | | - ).toContain(`background-image: url(${LINK})`); |
| 14 | + const imageDiv = screen.getByRole("img"); |
| 15 | + expect(imageDiv).toHaveStyle( |
| 16 | + "background-image: url(https://example.com/12345)" |
| 17 | + ); |
36 | 18 | }); |
37 | 19 |
|
38 | | - it("renders with correct default image", () => { |
39 | | - const LINK = undefined; |
40 | | - const EXPECTED_LINK = defaultImage; |
| 20 | + it("renders with the default image if no URL is provided", () => { |
| 21 | + render(<AuthorImage />); // No URL passed |
41 | 22 |
|
42 | | - const result = render(<AuthorImage url={LINK} />); |
| 23 | + const imageLink = screen.getByRole("link", { name: /author image/i }); |
| 24 | + expect(imageLink).toHaveAttribute( |
| 25 | + "href", |
| 26 | + expect.stringContaining("author_default.png") |
| 27 | + ); |
43 | 28 |
|
44 | | - expect( |
45 | | - result.container.querySelector("#author_image").getAttribute("style") |
46 | | - ).toContain(`background-image: url(${EXPECTED_LINK})`); |
| 29 | + const imageDiv = screen.getByRole("img"); |
| 30 | + expect(imageDiv).toHaveStyle( |
| 31 | + 'background-image: url("/vis/images/author_default.png")' |
| 32 | + ); // Adjust the path if needed |
47 | 33 | }); |
48 | 34 | }); |
0 commit comments