This repository has been archived by the owner on Nov 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
drop-down.spec.tsx
90 lines (69 loc) · 3.18 KB
/
drop-down.spec.tsx
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
import * as React from 'react';
import {ClientRenderer, expect, simulate, sinon, waitFor} from 'test-drive-react';
import {DropDownDemo} from '../../demo/components/drop-down.demo';
import {DropDown} from '../../src';
const dropDown = 'DROP_DOWN';
const dropDownDemo = dropDown + '_DEMO';
const input = dropDown + '_INPUT';
const list = dropDown + '_LIST';
describe('<DropDown />', () => {
const clientRenderer = new ClientRenderer();
afterEach(() => clientRenderer.cleanup());
const items = [
{label: 'Muffins'},
{label: 'Pancakes'},
{label: 'Waffles'}
];
it('renders a dropdown, opens it with a click, selects an item', async () => {
const {select, waitForDom} = clientRenderer.render(<DropDownDemo />);
await waitForDom(() => expect(select(dropDownDemo, list)).to.be.absent());
simulate.click(select(dropDownDemo, input));
await waitForDom(() => expect(select(dropDownDemo, list)).to.be.present());
simulate.click(select(dropDownDemo, list)!.children![0].children[0]);
return waitForDom(() => {
expect(select(dropDownDemo, list)).to.be.absent();
expect(select(dropDownDemo)).to.have.text(items[0].label);
});
});
it('renders to the screen', () => {
const {select, waitForDom} = clientRenderer.render(<DropDown />);
return waitForDom(() => {
expect(select(dropDown)).to.be.present();
expect(select(dropDown)).to.have.text('Default Text');
});
});
it('has correct selected item text', () => {
const item = {label: 'Test'};
const {select, waitForDom} = clientRenderer.render(<DropDown selectedItem={item}/>);
return waitForDom(() => {
expect(select(input)).to.be.present();
expect(select(input)).to.have.text(item.label);
});
});
it('invokes the onClick when dropdown label is clicked', () => {
const onClick = sinon.spy();
const {select} = clientRenderer.render(<DropDown onInputClick={onClick}/>);
simulate.click(select(dropDown, input));
return waitFor(() => expect(onClick).to.have.been.calledOnce);
});
it('displays item list to choose from when open is true', () => {
const {select, waitForDom} =
clientRenderer.render(<DropDown selectedItem={undefined} open={true} items={items} />);
const dropDownList = select(dropDown, list, 'LIST');
return waitForDom(() => {
expect(dropDownList).to.be.present();
items.forEach((elem, idx) => {
expect(dropDownList!.children[idx]).to.be.present();
expect(dropDownList!.children[idx]).to.have.text(elem.label);
});
});
});
it('invokes onClick handler when an item is clicked', () => {
const onClick = sinon.spy();
const {select} =
clientRenderer.render(<DropDown selectedItem={undefined} open={true} items={items} onItemClick={onClick}/>);
const dropDownList = select(dropDown, list, 'LIST');
simulate.click(dropDownList!.children[0]);
return waitFor(() => expect(onClick).to.have.been.calledWithMatch(items[0]));
});
});