|
| 1 | +import React from 'react'; |
| 2 | +import sinon from 'sinon'; |
| 3 | +import { shallow } from 'enzyme'; |
| 4 | + |
| 5 | +import PageButton from '../src/page-button'; |
| 6 | + |
| 7 | +describe('PageButton', () => { |
| 8 | + let wrapper; |
| 9 | + const onPageChangeCallback = sinon.stub(); |
| 10 | + const props = { |
| 11 | + onPageChange: onPageChangeCallback, |
| 12 | + page: 2 |
| 13 | + }; |
| 14 | + |
| 15 | + describe('default PageButton', () => { |
| 16 | + beforeEach(() => { |
| 17 | + wrapper = shallow( |
| 18 | + <PageButton { ...props } active disabled={ false } /> |
| 19 | + ); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should rendering PageButton correctly', () => { |
| 23 | + expect(wrapper.find('a.page-link').length).toBe(1); |
| 24 | + expect(wrapper.text()).toEqual(`${props.page}`); |
| 25 | + }); |
| 26 | + |
| 27 | + describe('when clicking', () => { |
| 28 | + let preventDefault; |
| 29 | + beforeEach(() => { |
| 30 | + preventDefault = sinon.stub(); |
| 31 | + wrapper.find('a.page-link').simulate('click', { preventDefault }); |
| 32 | + }); |
| 33 | + |
| 34 | + afterEach(() => { |
| 35 | + onPageChangeCallback.reset(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should calling e.preventDefault', () => { |
| 39 | + expect(preventDefault.calledOnce).toBeTruthy(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should calling onPageChange prop', () => { |
| 43 | + expect(onPageChangeCallback.calledOnce).toBeTruthy(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should calling onPageChange prop with correct argument', () => { |
| 47 | + expect(onPageChangeCallback.calledWith(props.page)).toBeTruthy(); |
| 48 | + }); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('when active prop is true', () => { |
| 53 | + beforeEach(() => { |
| 54 | + wrapper = shallow( |
| 55 | + <PageButton { ...props } active disabled={ false } /> |
| 56 | + ); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should render PageButton correctly', () => { |
| 60 | + expect(wrapper.length).toBe(1); |
| 61 | + expect(wrapper.hasClass('active')).toBeTruthy(); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('when active prop is false', () => { |
| 66 | + beforeEach(() => { |
| 67 | + wrapper = shallow( |
| 68 | + <PageButton { ...props } active={ false } disabled={ false } /> |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should render PageButton correctly', () => { |
| 73 | + expect(wrapper.length).toBe(1); |
| 74 | + expect(wrapper.hasClass('active')).toBeFalsy(); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('when disabled prop is true', () => { |
| 79 | + beforeEach(() => { |
| 80 | + wrapper = shallow( |
| 81 | + <PageButton { ...props } active disabled /> |
| 82 | + ); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should render PageButton correctly', () => { |
| 86 | + expect(wrapper.length).toBe(1); |
| 87 | + expect(wrapper.hasClass('disabled')).toBeTruthy(); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('when disabled prop is false', () => { |
| 92 | + beforeEach(() => { |
| 93 | + wrapper = shallow( |
| 94 | + <PageButton { ...props } active disabled={ false } /> |
| 95 | + ); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should render PageButton correctly', () => { |
| 99 | + expect(wrapper.length).toBe(1); |
| 100 | + expect(wrapper.hasClass('disabled')).toBeFalsy(); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + describe('when title prop is defined', () => { |
| 105 | + const title = 'aTitle'; |
| 106 | + beforeEach(() => { |
| 107 | + wrapper = shallow( |
| 108 | + <PageButton { ...props } active disabled={ false } title={ title } /> |
| 109 | + ); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should render PageButton correctly', () => { |
| 113 | + expect(wrapper.length).toBe(1); |
| 114 | + expect(wrapper.prop('title')).toEqual(title); |
| 115 | + }); |
| 116 | + }); |
| 117 | +}); |
0 commit comments