import React from 'react' import { mount, shallow } from 'enzyme' import { CrossIcon } from '../icon/crossIcon' import { Button, ButtonStatus, eventHandler } from './Button' jest.useFakeTimers() describe('Button', () => { class TestLink extends React.PureComponent<{ readonly href?: string }> { render() { return {this.props.children} } } it('Should have the proper text.', () => { const button = shallow() expect(button.prop('type')).toBe('button') expect(button.text()).toContain('blabla') }) it('Should have a title attribute.', () => { const button = shallow() expect(button.prop('title')).toBe('blabla') }) it('Should be able to change the type.', () => { const button = shallow() expect(button.prop('type')).toBe('submit') }) it('Should render the modifiers.', () => { const buttonClassName = shallow() expect(buttonClassName.hasClass('addClass')).toBe(true) const button = shallow() expect(button.hasClass('kirk-button-warning')).toBe(true) }) it('Should allow for an icon.', () => { const button = shallow( , ) expect(button.hasClass('kirk-button-bubble')).toBe(true) expect(button.contains()).toBe(true) }) it('Should have proper attribute on loading state', () => { const button = shallow(, ) expect(button.contains()).toBe(false) expect(button.text().includes('blabla')).toBe(false) }) it('Simulates a click action.', () => { const onButtonClick = jest.fn() const button = shallow() button.find('button').simulate('click') expect(onButtonClick).toHaveBeenCalledTimes(1) }) it('Simulates a blur event.', () => { const onButtonBlur = jest.fn() const button = shallow() button.find('button').simulate('blur') expect(onButtonBlur).toHaveBeenCalledTimes(1) }) it('Simulates a focus event.', () => { const onButtonFocus = jest.fn() const wrapper = shallow() wrapper.find('button').simulate('focus') expect(onButtonFocus).toHaveBeenCalledTimes(1) }) it('Does not fire events in eventhandler', () => { const firstEvent = jest.fn() const secondEvent = jest.fn() eventHandler(firstEvent, secondEvent) expect(firstEvent).not.toHaveBeenCalled() expect(secondEvent).not.toHaveBeenCalled() }) it('Fires both events in eventhandler', () => { const firstEvent = jest.fn() const secondEvent = jest.fn() eventHandler(firstEvent, secondEvent)() expect(firstEvent).toHaveBeenCalledTimes(1) expect(secondEvent).toHaveBeenCalledTimes(1) }) it('fires the callback event when valid', () => { const event = jest.fn() mount( , ) expect(event).not.toBeCalled() jest.advanceTimersByTime(1500) expect(event).toBeCalled() }) it('Should have no children for a checked state', () => { const button = mount( , ) expect(button.contains()).toBe(false) expect(button.text().includes('blabla')).toBe(false) }) describe('href', () => { it('Should be a link.', () => { const button = shallow() expect(button.prop('href')).toBe('/test-page') expect(button.text()).toContain('link') }) it('Should be a link if we pass it as a tag.', () => { const button = shallow() expect(button.prop('href')).toBe('/test-page') expect(button.text()).toContain('link') }) it('Should be a link if we pass it as a React element.', () => { const wrapper = shallow( , ) expect(wrapper.prop('href')).toBe('/test-page') expect(wrapper.html()).toContain('link') }) }) it('should be disabled while status is checked', () => { const wrapper = shallow() expect(wrapper.prop('disabled')).toBe(true) }) it('should be disabled while status is loading', () => { const wrapper = shallow() expect(wrapper.prop('disabled')).toBe(true) }) })