Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(web-components): add avatar tests #358

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 120 additions & 13 deletions packages/web-components/src/avatar/avatar.test.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,130 @@
import { render, screen, userEvent } from "@internals/test-helpers";
import { describe, expect, it, jest } from "@jest/globals";
import { fireEvent, render, screen } from "@internals/test-helpers";
import { describe, expect, it } from "@jest/globals";
import { createComponent } from "@lit/react";
import React from "react";
import {
itShouldMount,
itSupportsClassName,
itSupportsDataSetProps,
itSupportsStyle,
} from "../internals/tests.tsx";
import { TapsiAvatar } from "./index.ts";

import "./index.ts";
const Avatar = createComponent({
react: React,
elementClass: TapsiAvatar,
tagName: "tapsi-avatar",
});

const mockRequiredProps = {
"data-testid": "test-avatar",
};

const getTestAvatarRoot = async () => screen.findByShadowTestId("avatar-root");

const getTestAvatarImage = async () =>
screen.findByShadowTestId("avatar-image");

const getTestAvatarPlaceholder = async () =>
screen.findByShadowTestId("avatar-placeholder");

describe("🧪 avatar: UI", () => {
itShouldMount(Avatar, mockRequiredProps);
itSupportsClassName(Avatar, mockRequiredProps);
itSupportsStyle(Avatar, mockRequiredProps);
itSupportsDataSetProps(Avatar, mockRequiredProps);

it("should render placeholder if no image was set", async () => {
render(<Avatar {...mockRequiredProps} />);

const placeholder = await getTestAvatarPlaceholder();

expect(placeholder).toBeInTheDocument();
});

it("should render image if `image` property was set", async () => {
render(
<Avatar
{...mockRequiredProps}
image="image.png"
alt="image-alt"
/>,
);

expect(await screen.findByShadowTestId("avatar-image")).toBeInTheDocument();
});

it("should render placeholder if image has error", async () => {
render(
<Avatar
{...mockRequiredProps}
image="image.png"
alt="image-alt"
/>,
);

describe("tapsi-avatar", () => {
it("renders", async () => {
// @ts-expect-error Current React does not support custom elements.
render(<tapsi-avatar data-testid="test"></tapsi-avatar>);
const image = await getTestAvatarImage();

const el = screen.getByTestId("test");
fireEvent.error(image);

const handleClick = jest.fn();
const placeholder = await getTestAvatarPlaceholder();

el.addEventListener("click", handleClick);
expect(placeholder).toBeInTheDocument();
});

it("should apply `loading` attribute correctly", async () => {
render(
<Avatar
{...mockRequiredProps}
image="image.png"
loading="lazy"
/>,
);

const image = await getTestAvatarImage();

expect(image).toHaveAttribute("loading", "lazy");
});

it("should apply the correct size class", async () => {
render(
<Avatar
{...mockRequiredProps}
image="image.png"
size="lg"
/>,
);

const avatarRoot = await getTestAvatarRoot();

expect(avatarRoot).toHaveClass("lg");
});

it("should apply `alt` attribute to the `img` tag correctly", async () => {
render(
<Avatar
{...mockRequiredProps}
image="image.png"
alt="Test Alt"
/>,
);

const image = await getTestAvatarImage();

expect(image).toHaveAttribute("alt", "Test Alt");
});

await userEvent.click(el);
it("should apply `label` property as `aria-label`", async () => {
render(
<Avatar
{...mockRequiredProps}
image="image.png"
label="Avatar Label"
/>,
);

expect(handleClick).toHaveBeenCalled();
const avatarRoot = await getTestAvatarRoot();

expect(await screen.findByShadowRole("img")).toBeInTheDocument();
expect(avatarRoot).toHaveAttribute("aria-label", "Avatar Label");
});
});
3 changes: 3 additions & 0 deletions packages/web-components/src/avatar/avatar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class Avatar extends LitElement {
return html`
<img
part="image"
data-testid="avatar-image"
class="image"
alt=${this.alt}
src="${this.image}"
Expand All @@ -57,6 +58,7 @@ export class Avatar extends LitElement {

private _renderPlaceholder() {
return html` <div
data-testid="avatar-placeholder"
part="placeholder"
class="placeholder"
aria-hidden="true"
Expand All @@ -73,6 +75,7 @@ export class Avatar extends LitElement {

return html`
<div
data-testid="avatar-root"
role="img"
aria-label=${this.label}
part="root"
Expand Down
Loading