UNPKG

1.85 kBJavaScriptView Raw
1import React from 'react';
2import { render, screen } from '@testing-library/react';
3import user from '@testing-library/user-event';
4import { ButtonToggle } from '..';
5import { testForChildrenInComponent } from '../testUtils';
6
7describe('ButtonToggle', () => {
8 it('should render children', () => {
9 testForChildrenInComponent(ButtonToggle);
10 });
11
12 it('should have button already toggled for defaultValue true', () => {
13 render(<ButtonToggle defaultValue>Ello world</ButtonToggle>);
14
15 expect(screen.getByText(/world/i)).toHaveClass('active');
16 });
17
18 describe('onClick', () => {
19 it('calls props.onClick if it exists', () => {
20 const onClick = jest.fn();
21 render(<ButtonToggle onClick={onClick}>Testing Click</ButtonToggle>);
22
23 user.click(screen.getByText(/testing click/i));
24 expect(onClick).toHaveBeenCalled();
25 });
26
27 it('should not call props.onClick if it exists and button is disabled', () => {
28 const onClick = jest.fn();
29 render(
30 <ButtonToggle onClick={onClick} disabled>
31 Testing Click
32 </ButtonToggle>,
33 );
34
35 user.click(screen.getByText(/testing click/i));
36 expect(onClick).not.toHaveBeenCalled();
37 });
38 });
39
40 describe('onFocus', () => {
41 it('calls props.onFocus if it exists', () => {
42 const onFocus = jest.fn();
43 render(<ButtonToggle onFocus={onFocus}>Testing Click</ButtonToggle>);
44
45 screen.getByText(/testing click/i).focus();
46 expect(onFocus).toHaveBeenCalled();
47 });
48 });
49
50 describe('onBlur', () => {
51 it('calls props.onBlur if it exists', () => {
52 const onBlur = jest.fn();
53 render(<ButtonToggle onBlur={onBlur}>Testing Click</ButtonToggle>);
54 screen.getByText(/testing click/i).focus();
55 screen.getByText(/testing click/i).blur();
56 expect(onBlur).toHaveBeenCalled();
57 });
58 });
59});