aboutsummaryrefslogtreecommitdiffstats
path: root/test/react/Tabs.spec.js
blob: 9906708d2a486fbd9e5585b6517c2b70189401b8 (plain)
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
import React from 'react';
import Tab from '../../src/react/Tab.js';
import Tabs from '../../src/react/Tabs.js';

import renderer from 'react-test-renderer';
import {mount} from 'enzyme';

class TabsForm extends React.Component {
	constructor(props) {
		super(props);
		this.state = {tabId: '1'};

		this.handleChange = this.handleChange.bind(this);
	}

	handleChange(val) {
		this.setState({tabId: val});
	}

	render() {
		return (
			<form >
				<fieldset disabled={this.props.disabled}>
					<Tabs ref={(tabs) => this.tabsInst = tabs} activeTab={this.state.tabId} onTabClick={this.handleChange} >
						<Tab title='tab 1' tabId='1' data-test-id='1'>Tab #1</Tab>
						<Tab title='tab 2' tabId='2' data-test-id='2'>Tab #2</Tab>
						<Tab title='tab 3'  tabId='3' data-test-id='3'>Tab #3</Tab>
					</Tabs>
				</fieldset>

			</form>
		);
	}
}

describe('Tabs', () => {

	test('Tabs - basic rendering', () => {
		const tabs = renderer.create(<TabsForm disabled={false} />).toJSON();
		expect(tabs).toMatchSnapshot();
	});

	test('Tabs - when active tab id is changed, the respective tab is shown', () => {
		const tabs = mount(<TabsForm disabled={false} />);
		expect(tabs.instance().tabsInst.props.activeTab).toEqual('1');
		expect(tabs.find('.sdc-tab-content').text()).toEqual('Tab #1');
		expect(tabs.find('li[data-test-id="1"]').hasClass('sdc-tab-active')).toBeTruthy();
		tabs.find('li[data-test-id="2"]').simulate('click');
		expect(tabs.instance().tabsInst.props.activeTab).toEqual('2');
		expect(tabs.find('li[data-test-id="2"]').hasClass('sdc-tab-active')).toBeTruthy();
		expect(tabs.find('li[data-test-id="1"]').hasClass('sdc-tab-active')).not.toBeTruthy();
		expect(tabs.find('.sdc-tab-content').text()).toEqual('Tab #2');
	});

});