/
UncontrolledCollapse.spec.js
108 lines (76 loc) · 3.25 KB
/
UncontrolledCollapse.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import React from 'react';
import { mount, shallow } from 'enzyme';
import { Collapse, UncontrolledCollapse } from '../';
describe('UncontrolledCollapse', () => {
let toggler;
let togglers;
beforeEach(() => {
document.body.innerHTML = `
<div>
<button id="toggler">Click Me</button>
<button class="toggler">Toggler 1</button>
<button class="toggler">Toggler 2</button>
</div>`;
toggler = document.getElementById('toggler');
togglers = document.getElementsByClassName('toggler');
});
afterEach(() => {
if (jest.isMockFunction(UncontrolledCollapse.prototype.toggle)) {
UncontrolledCollapse.prototype.toggle.mockRestore();
}
document.body.innerHTML = '';
toggler = null;
togglers = null;
});
it('should be a Collapse', () => {
const collapse = shallow(<UncontrolledCollapse toggler="#toggler">Yo!</UncontrolledCollapse>);
expect(collapse.type()).toBe(Collapse);
});
it('should have isOpen default to false', () => {
const collapse = shallow(<UncontrolledCollapse toggler="#toggler">Yo!</UncontrolledCollapse>);
expect(collapse.prop('isOpen')).toBe(false);
});
it('should toggle isOpen when toggle is called', () => {
const collapse = shallow(<UncontrolledCollapse toggler="#toggler">Yo!</UncontrolledCollapse>);
toggler.click();
collapse.update();
expect(collapse.prop('isOpen')).toBe(true);
});
it('should call toggle when toggler is clicked', () => {
jest.spyOn(UncontrolledCollapse.prototype, 'toggle');
mount(<UncontrolledCollapse toggler="#toggler">Yo!</UncontrolledCollapse>);
expect(UncontrolledCollapse.prototype.toggle.mock.calls.length).toBe(0);
toggler.click();
expect(UncontrolledCollapse.prototype.toggle.mock.calls.length).toBe(1);
});
it('should toggle for multiple togglers', () => {
const collapse = shallow(<UncontrolledCollapse toggler=".toggler">Yo!</UncontrolledCollapse>);
expect(collapse.prop('isOpen')).toBe(false);
togglers[0].click();
collapse.update();
expect(collapse.prop('isOpen')).toBe(true);
togglers[1].click();
collapse.update();
expect(collapse.prop('isOpen')).toBe(false);
});
it('should remove eventListeners when unmounted', () => {
jest.spyOn(UncontrolledCollapse.prototype, 'componentWillUnmount');
jest.spyOn(UncontrolledCollapse.prototype, 'toggle');
const wrapper = mount(<UncontrolledCollapse toggler="#toggler">Yo!</UncontrolledCollapse>);
expect(UncontrolledCollapse.prototype.toggle.mock.calls.length).toBe(0);
expect(UncontrolledCollapse.prototype.componentWillUnmount.mock.calls.length).toBe(0);
toggler.click();
expect(UncontrolledCollapse.prototype.toggle.mock.calls.length).toBe(1);
wrapper.unmount();
expect(UncontrolledCollapse.prototype.componentWillUnmount.mock.calls.length).toBe(1);
toggler.click();
expect(UncontrolledCollapse.prototype.toggle.mock.calls.length).toBe(1);
});
it('should set aria-expanded', () => {
const collapse = mount(<UncontrolledCollapse toggler="#toggler">Yo!</UncontrolledCollapse>);
expect(collapse.find('div').prop('aria-expanded')).toBe('false');
toggler.click();
collapse.update();
expect(collapse.find('div').prop('aria-expanded')).toBe('true');
});
});