Skip to content

Commit

Permalink
test(portrait): address missing jest test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Parsium committed Dec 16, 2024
1 parent 18a4012 commit 6685d87
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions src/components/portrait/portrait.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ test("renders with a gravatar image, if a valid email is passed via the `gravata
expect(img).toHaveAttribute("src", src);
});

test("renders a decorative image, when gravatar prop is provided but alt is not", () => {
const email = "chris.barber@sage.com";
render(<Portrait gravatar={email} />);

const decorativeImg = screen.getByAltText("");
expect(decorativeImg).toBeVisible();
});

test("logs a deprecation warning once when the `gravatar` prop is passed, and a gravatar loads", () => {
const loggerSpy = jest
.spyOn(Logger, "deprecate")
Expand Down Expand Up @@ -89,24 +97,31 @@ test("if a valid gravatar email is not found and an onError event is triggered,
);
});

test("renders with a custom image, if a valid src is passed via the `src` prop", () => {
test("renders a custom image with the correct src and alt attributes", () => {
const src = "https://upload.wikimedia.org/wikipedia/en/6/6c/Heatposter.jpg";
render(<Portrait src={src} alt="foo" />);
render(<Portrait src={src} alt="Movie poster of Heat" />);

const img = screen.getByRole("img");
expect(img).toBeVisible();
expect(img).toHaveAttribute("src", src);
const image = screen.getByAltText("Movie poster of Heat");
expect(image).toHaveAttribute("src", src);
});

test("renders a decorative image, when src prop is provided but alt is not", () => {
const src = "https://upload.wikimedia.org/wikipedia/en/6/6c/Heatposter.jpg";
render(<Portrait src={src} />);

const decorativeImg = screen.getByAltText("");
expect(decorativeImg).toBeVisible();
});

test("if a valid src is not found and an onError event is triggered, the default individual icon is rendered", async () => {
const src = "not-a-url";
render(<Portrait src={src} alt="foo" />);
render(<Portrait src={src} alt="foobar" />);

const img = screen.getByRole("img");
expect(img).toBeVisible();
expect(img).toHaveAttribute("src", src);
const image = screen.getByAltText("foobar");
expect(image).toBeVisible();
expect(image).toHaveAttribute("src", src);

fireEvent.error(img);
fireEvent.error(image);

await waitFor(() => expect(screen.getByTestId("icon")).toBeVisible());
await waitFor(() =>
Expand All @@ -127,14 +142,6 @@ test("when both the `gravatar` and `src` props are passed simultaneously, an inv
consoleSpy.mockRestore();
});

test("allows the alt attribute to be set, via the `alt` prop", () => {
const src = "https://upload.wikimedia.org/wikipedia/en/6/6c/Heatposter.jpg";
render(<Portrait src={src} alt="custom-alt" />);

const alt = screen.getByAltText("custom-alt");
expect(alt).toBeVisible();
});

test("renders with a square shape, if the `shape` prop is value is `square`", () => {
render(<Portrait data-role="portrait" shape="square" />);

Expand Down

0 comments on commit 6685d87

Please sign in to comment.