Coder Social home page Coder Social logo

Comments (1)

yurisldk avatar yurisldk commented on August 23, 2024

I rewrote the tests for the component and also added a test for "Ref". For all other components, I would use this testing way. @estevanmaito Is there any further development of the project planned? I would rewrite all the tests for RTL compatibility.

import React, { createRef } from 'react'
import { render, screen, within } from '@testing-library/react'
import Avatar from '../Avatar'

const avatar = () => screen.getByTestId('avatar')
const img = () => within(avatar()).getByRole('img')

describe('Avatar', () => {
  it('should render without crashing', () => {
    render(<Avatar src="#" />)

    expect(avatar()).toBeInTheDocument()
  })

  describe('Root', () => {
    it('should render with base styles', () => {
      const expectedClasses = 'relative rounded-full inline-block'
      render(<Avatar src="#" />)

      expect(avatar()).toHaveClass(expectedClasses)
    })

    describe('prop: ref', () => {
      it('should be able to access the <Avatar />', () => {
        const avatarRef = createRef<HTMLDivElement>()
        render(<Avatar ref={avatarRef} src="#" />)
        expect(avatar()).toEqual(avatarRef.current)
      })
    })

    describe('prop: size', () => {
      it('should render with large styles', () => {
        const expectedClasses = 'w-10 h-10'
        render(<Avatar src="#" size="large" />)

        expect(avatar()).toHaveClass(expectedClasses)
      })

      it('should render with regular styles', () => {
        const expectedClasses = 'w-8 h-8'
        render(<Avatar src="#" size="regular" />)

        expect(avatar()).toHaveClass(expectedClasses)
      })

      it('should render with small styles', () => {
        const expectedClasses = 'w-6 h-6'
        render(<Avatar src="#" size="small" />)

        expect(avatar()).toHaveClass(expectedClasses)
      })
    })
  })

  describe('Image', () => {
    it('should contain an image with the correct src', () => {
      const expectedSrc = '#'
      render(<Avatar src="#" />)

      expect(img()).toHaveAttribute('src', expectedSrc)
    })

    it('should contain an image with alt text', () => {
      const expectedAltText = 'Lorem'
      render(<Avatar src="#" alt="Lorem" />)

      expect(img()).toHaveAccessibleName(expectedAltText)
    })
  })
})

image

from windmill-react-ui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.