import React from 'react'; import { fireEvent, render, screen } from '@testing-library/react'; import { axe } from 'jest-axe'; import userEvent from '@testing-library/user-event'; import 'jest-styled-components'; import 'jest-axe/extend-expect'; import 'regenerator-runtime/runtime'; import '@testing-library/jest-dom'; import { Add, Next } from 'grommet-icons'; import { Grommet, Button, Text } from '../..'; describe('Button', () => { test('should have no accessibility violations', async () => { const { container } = render( , ); expect(screen.getByTestId('children function')).toBeInTheDocument(); expect(container.firstChild).toMatchSnapshot(); }); test('children function with disabled prop', () => { const { container } = render( , ); expect( screen.getByRole('button', { name: 'Button#1 Disabled' }), ).toBeInTheDocument(); expect( screen.getByRole('button', { name: 'Button#2 not Disabled' }), ).toBeInTheDocument(); expect(container.firstChild).toMatchSnapshot(); }); test('warns about invalid label', () => { const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); const { container } = render( , ); expect(container.firstChild).toMatchSnapshot(); expect(warnSpy).toHaveBeenCalledWith( 'Button should not have children if icon or label is provided', ); warnSpy.mockReset(); warnSpy.mockRestore(); }); test('warns about invalid icon', () => { const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); const { container } = render( , ); expect(container.firstChild).toMatchSnapshot(); expect(warnSpy).toHaveBeenCalledWith( 'Button should not have children if icon or label is provided', ); warnSpy.mockReset(); warnSpy.mockRestore(); }); test('primary', () => { const { container } = render( , ); expect( screen.getByRole('button', { name: 'hoverIndicator' }), ).toHaveStyleRule('background-color', 'rgba(221,221,221,0.4)', { modifier: ':hover', }); expect(container.firstChild).toMatchSnapshot(); }); test('hoverIndicator as object with color', () => { const { container } = render( , ); expect( screen.getByRole('button', { name: 'hoverIndicator' }), ).toHaveStyleRule('background-color', 'rgba(125,76,219,1)', { modifier: ':hover', }); expect(container.firstChild).toMatchSnapshot(); }); test('hoverIndicator as object with invalid color', () => { const { container } = render( , ); expect(container.firstChild).toMatchSnapshot(); }); test('hoverIndicator color', () => { const { container } = render( , ); expect(container.firstChild).toMatchSnapshot(); }); test('onClick', () => { const onClick = jest.fn(); render( , ); const stringPadButton = screen.getByTestId('string-pad'); const objectPadButton = screen.getByTestId('object-pad'); const childPadButton = screen.getByTestId('child-pad'); let style; style = window.getComputedStyle(stringPadButton); expect(style.padding).toBe('96px'); style = window.getComputedStyle(objectPadButton); expect(style.paddingTop).toBe('6px'); expect(style.paddingBottom).toBe('6px'); expect(style.paddingLeft).toBe('18px'); expect(style.paddingRight).toBe('18px'); style = window.getComputedStyle(childPadButton); expect(style.padding).toBe('0px'); expect(asFragment()).toMatchSnapshot(); }); });