Skip to content

Commit 688dc3b

Browse files
committed
refactor: frontend CI pipeline and tests for AuthorImage component
1 parent 14ae0d1 commit 688dc3b

3 files changed

Lines changed: 51 additions & 59 deletions

File tree

.github/workflows/frontend.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ jobs:
2323
node-version: 18
2424
cache: "npm"
2525

26-
- name: Install dependencies
27-
run: npm ci
26+
- name: Clean node_modules and lockfile
27+
run: rm -rf node_modules package-lock.json
28+
29+
- name: Install project dependencies
30+
run: npm install
2831

2932
- name: Run tests
3033
run: npm run test
Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,33 @@
1-
import { expect, describe, it, vitest } from "vitest";
2-
1+
import { render, screen } from "@testing-library/react";
2+
import { expect, describe, it } from "vitest";
33
import React from "react";
4-
import { render } from "@testing-library/react";
5-
64
import AuthorImage from "../../js/templates/AuthorImage";
7-
import defaultImage from "../../images/author_default.png";
85

9-
describe("Author image component", () => {
10-
it("renders", () => {
11-
const result = render(
12-
<AuthorImage url="http://pictures.org/link-to-author" />
13-
);
6+
describe("AuthorImage Component", () => {
7+
it("renders with the provided URL", () => {
8+
render(<AuthorImage url="https://example.com/12345" />);
149

15-
expect(result.container.childNodes.length).toBe(1);
16-
});
10+
const imageLink = screen.getByRole("link", { name: /author image/i });
11+
expect(imageLink).toHaveAttribute("href", "https://example.com/12345");
1712

18-
it("renders with correct link", () => {
19-
const LINK = "https://custom-link.com/12345";
20-
21-
const result = render(<AuthorImage url={LINK} />);
22-
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})`);
13+
const imageDiv = screen.getByRole("img");
14+
expect(imageDiv).toHaveStyle(
15+
"background-image: url(https://example.com/12345)"
16+
);
3617
});
3718

38-
it("renders with correct default image", () => {
39-
const LINK = undefined;
40-
const EXPECTED_LINK = defaultImage;
19+
it("renders with the default image if no URL is provided", () => {
20+
render(<AuthorImage />);
4121

42-
const result = render(<AuthorImage url={LINK} />);
22+
const imageLink = screen.getByRole("link", { name: /author image/i });
23+
expect(imageLink).toHaveAttribute(
24+
"href",
25+
expect.stringContaining("author_default.png")
26+
);
4327

44-
expect(
45-
result.container.querySelector("#author_image").getAttribute("style")
46-
).toContain(`background-image: url(${EXPECTED_LINK})`);
28+
const imageDiv = screen.getByRole("img");
29+
expect(imageDiv).toHaveStyle(
30+
'background-image: url("/vis/images/author_default.png")'
31+
);
4732
});
4833
});
Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
1-
import { render, screen } from '@testing-library/react';
2-
import { expect, describe, it } from 'vitest';
3-
import React from 'react';
1+
import { render, screen } from "@testing-library/react";
2+
import { expect, describe, it } from "vitest";
3+
import React from "react";
4+
import AuthorImage from "../../js/templates/AuthorImage";
45

5-
import AuthorImage from '../../js/templates/AuthorImage';
6-
7-
describe('AuthorImage Component', () => {
8-
it('renders with the provided URL', () => {
6+
describe("AuthorImage Component", () => {
7+
it("renders with the provided URL", () => {
98
render(<AuthorImage url="https://example.com/12345" />);
10-
11-
const imageLink = screen.getByRole('link', { name: /author image/i });
12-
expect(imageLink).toHaveAttribute('href', 'https://example.com/12345');
13-
14-
const imageDiv = screen.getByRole('img');
15-
expect(imageDiv).toHaveStyle('background-image: url(https://example.com/12345)');
9+
10+
const imageLink = screen.getByRole("link", { name: /author image/i });
11+
expect(imageLink).toHaveAttribute("href", "https://example.com/12345");
12+
13+
const imageDiv = screen.getByRole("img");
14+
expect(imageDiv).toHaveStyle(
15+
'background-image: url("https://example.com/12345")'
16+
);
1617
});
1718

18-
it('renders with the default image if no URL is provided', () => {
19-
render(<AuthorImage />); // No URL passed
19+
it("renders with the default image if no URL is provided", () => {
20+
render(<AuthorImage />);
2021

21-
const imageLink = screen.getByRole('link', { name: /author image/i });
22-
expect(imageLink).toHaveAttribute('href', expect.stringContaining('author_default.png'));
22+
const imageLink = screen.getByRole("link", { name: /author image/i });
23+
expect(imageLink).toHaveAttribute("href");
24+
expect(imageLink.getAttribute("href")).toContain("author_default.png");
2325

24-
const imageDiv = screen.getByRole('img');
25-
expect(imageDiv).toHaveStyle('background-image: url(/vis/images/author_default.png)'); // Adjust the path if needed
26+
const imageDiv = screen.getByRole("img");
27+
expect(imageDiv).toHaveStyle(
28+
'background-image: url("/vis/images/author_default.png")'
29+
);
2630
});
27-
});
31+
});

0 commit comments

Comments
 (0)