|
| 1 | +import sinon from 'sinon'; |
| 2 | +import { render, fireEvent } from '@testing-library/react'; |
| 3 | +import { expect } from 'chai'; |
| 4 | + |
| 5 | +import Breadcrumb from '../src/Breadcrumb'; |
| 6 | +import Button from '../src/Button'; |
| 7 | + |
| 8 | +describe('<Breadcrumb.Item>', () => { |
| 9 | + it('Should render `a` as inner element when is not active', () => { |
| 10 | + const { container } = render( |
| 11 | + <Breadcrumb.Item href="#">Crumb</Breadcrumb.Item>, |
| 12 | + ); |
| 13 | + |
| 14 | + container.querySelectorAll('button.active').length.should.equal(0); |
| 15 | + }); |
| 16 | + |
| 17 | + it('Should render `li` with no children as inner element when active.', () => { |
| 18 | + const { queryAllByRole, getByText } = render( |
| 19 | + <Breadcrumb.Item active>Active Crumb</Breadcrumb.Item>, |
| 20 | + ); |
| 21 | + |
| 22 | + queryAllByRole('listitem').length.should.equal(1); |
| 23 | + getByText('Active Crumb').should.exist; |
| 24 | + }); |
| 25 | + |
| 26 | + it('Should render `li` with no children as inner element when active and has href', () => { |
| 27 | + const { queryAllByRole, getByText } = render( |
| 28 | + <Breadcrumb.Item href="#" active> |
| 29 | + Active Crumb |
| 30 | + </Breadcrumb.Item>, |
| 31 | + ); |
| 32 | + |
| 33 | + queryAllByRole('listitem').length.should.equal(1); |
| 34 | + getByText('Active Crumb').should.exist; |
| 35 | + }); |
| 36 | + |
| 37 | + it('Should add custom classes onto `li` wrapper element', () => { |
| 38 | + const { getByTestId } = render( |
| 39 | + <Breadcrumb.Item className="custom-one custom-two" data-testid="test"> |
| 40 | + a |
| 41 | + </Breadcrumb.Item>, |
| 42 | + ); |
| 43 | + |
| 44 | + const item = getByTestId('test'); |
| 45 | + item.classList.contains('custom-one').should.be.true; |
| 46 | + item.classList.contains('custom-two').should.be.true; |
| 47 | + }); |
| 48 | + |
| 49 | + it('Should add aria-current to active element', () => { |
| 50 | + const { queryAllByRole } = render( |
| 51 | + <Breadcrumb.Item active>Active Crumb</Breadcrumb.Item>, |
| 52 | + ); |
| 53 | + |
| 54 | + queryAllByRole('listitem', { current: 'page' }).length.should.equal(1); |
| 55 | + }); |
| 56 | + |
| 57 | + it('Should spread additional props onto inner element', () => { |
| 58 | + const handleClick = sinon.spy(); |
| 59 | + |
| 60 | + const { getByRole } = render( |
| 61 | + <Breadcrumb.Item href="#" onClick={handleClick}> |
| 62 | + Crumb |
| 63 | + </Breadcrumb.Item>, |
| 64 | + ); |
| 65 | + |
| 66 | + fireEvent.click(getByRole('button')); |
| 67 | + |
| 68 | + handleClick.should.have.been.calledOnce; |
| 69 | + }); |
| 70 | + |
| 71 | + it('Should apply id onto the li element', () => { |
| 72 | + const { container } = render( |
| 73 | + <Breadcrumb.Item href="#" id="test-link-id"> |
| 74 | + Crumb |
| 75 | + </Breadcrumb.Item>, |
| 76 | + ); |
| 77 | + |
| 78 | + container.querySelectorAll('#test-link-id').length.should.equal(1); |
| 79 | + }); |
| 80 | + |
| 81 | + it('Should apply `href` property onto `a` inner element', () => { |
| 82 | + const { getByRole } = render( |
| 83 | + <Breadcrumb.Item href="http://getbootstrap.com/components/#breadcrumbs"> |
| 84 | + Crumb |
| 85 | + </Breadcrumb.Item>, |
| 86 | + ); |
| 87 | + |
| 88 | + const href = getByRole('link').getAttribute('href') || ''; |
| 89 | + href.should.equal('http://getbootstrap.com/components/#breadcrumbs'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('Should apply `title` property onto `a` inner element', () => { |
| 93 | + const { getByTitle } = render( |
| 94 | + <Breadcrumb.Item |
| 95 | + title="test-title" |
| 96 | + href="http://getbootstrap.com/components/#breadcrumbs" |
| 97 | + > |
| 98 | + Crumb |
| 99 | + </Breadcrumb.Item>, |
| 100 | + ); |
| 101 | + |
| 102 | + getByTitle('test-title').should.exist; |
| 103 | + }); |
| 104 | + |
| 105 | + it('Should not apply properties for inner `anchor` onto `li` wrapper element', () => { |
| 106 | + const { container } = render( |
| 107 | + <Breadcrumb.Item title="test-title" href="/hi" data-testid> |
| 108 | + Crumb |
| 109 | + </Breadcrumb.Item>, |
| 110 | + ); |
| 111 | + |
| 112 | + container.querySelectorAll('li[href="/hi"]').length.should.equal(0); |
| 113 | + container.querySelectorAll('li[title="test-title"]').length.should.equal(0); |
| 114 | + }); |
| 115 | + |
| 116 | + it('Should set `target` attribute on `anchor`', () => { |
| 117 | + const { getByRole } = render( |
| 118 | + <Breadcrumb.Item |
| 119 | + target="_blank" |
| 120 | + href="http://getbootstrap.com/components/#breadcrumbs" |
| 121 | + > |
| 122 | + Crumb |
| 123 | + </Breadcrumb.Item>, |
| 124 | + ); |
| 125 | + expect(getByRole('link').getAttribute('target')).to.be.equal('_blank'); |
| 126 | + }); |
| 127 | + |
| 128 | + it('Should have li as default component', () => { |
| 129 | + const { getByTestId } = render(<Breadcrumb.Item data-testid="test" />); |
| 130 | + |
| 131 | + getByTestId('test').tagName.toLowerCase().should.equal('li'); |
| 132 | + }); |
| 133 | + |
| 134 | + it('Should be able to customize inner link element', () => { |
| 135 | + const { container } = render(<Breadcrumb.Item linkAs={Button} />); |
| 136 | + |
| 137 | + container.querySelectorAll('a').length.should.equal(0); |
| 138 | + container.querySelectorAll('button').length.should.equal(1); |
| 139 | + }); |
| 140 | + |
| 141 | + it('Should be able to pass props to the customized inner link element', () => { |
| 142 | + const { getByRole } = render( |
| 143 | + <Breadcrumb.Item linkAs={Button} linkProps={{ type: 'submit' }} />, |
| 144 | + ); |
| 145 | + |
| 146 | + expect(getByRole('button').getAttribute('type')).to.be.equal('submit'); |
| 147 | + }); |
| 148 | + |
| 149 | + it('Should be able to pass attributes to the link element', () => { |
| 150 | + const { getByRole } = render( |
| 151 | + <Breadcrumb.Item linkProps={{ foo: 'bar' }}>Crumb</Breadcrumb.Item>, |
| 152 | + ); |
| 153 | + |
| 154 | + expect(getByRole('button').getAttribute('foo')).to.be.equal('bar'); |
| 155 | + }); |
| 156 | + |
| 157 | + it('Should be able to pass attributes to the li element', () => { |
| 158 | + const { getByRole } = render( |
| 159 | + <Breadcrumb.Item foo="bar">Crumb</Breadcrumb.Item>, |
| 160 | + ); |
| 161 | + |
| 162 | + expect(getByRole('listitem').getAttribute('foo')).to.be.equal('bar'); |
| 163 | + }); |
| 164 | +}); |
0 commit comments