Skip to content

Commit d2bd7a8

Browse files
authored
Merge pull request #17 from coderan-io/feature/inputs
Feature/inputs
2 parents 14bc509 + 2e7383c commit d2bd7a8

32 files changed

+2979
-1432
lines changed

__tests__/FieldBase.test.tsx

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import React from 'react';
2+
import { mount, render } from 'enzyme';
3+
import FieldBase from '@/components/Field/FieldBase';
4+
import { Variant } from '@/components';
5+
import { FieldContext } from '@/components/Field/FieldContext';
6+
7+
describe('FieldBase test', () => {
8+
it('should render field base', () => {
9+
const container = render(
10+
<div>
11+
<FieldBase />
12+
</div>
13+
);
14+
15+
expect(container.find('.form-field-base').length).toBe(1);
16+
});
17+
18+
it('should set variant', () => {
19+
const container = render(
20+
<div>
21+
<FieldBase
22+
variant={Variant.PRIMARY}
23+
/>
24+
</div>
25+
);
26+
27+
expect(container.find('.form-field-base.form-field-primary').length).toBe(1);
28+
});
29+
30+
it('should set variant', () => {
31+
const container = render(
32+
<div>
33+
<FieldBase
34+
variant={Variant.PRIMARY}
35+
/>
36+
</div>
37+
);
38+
39+
expect(container.find('.form-field-base.form-field-primary').length).toBe(1);
40+
});
41+
42+
it('should make floating label component', () => {
43+
const container = render(
44+
<div>
45+
<FieldBase
46+
label={<strong>test</strong>}
47+
/>
48+
</div>
49+
);
50+
51+
expect(container.find('.form-field-base').hasClass('floating-label')).toBeTruthy();
52+
expect(container.find('.form-field-base .form-field-label-floating strong').text())
53+
.toBe('test');
54+
});
55+
56+
it('should make invalid state icon', () => {
57+
const container = render(
58+
<div>
59+
<FieldBase
60+
label={<strong>test</strong>}
61+
valid={false}
62+
>
63+
<FieldContext.Consumer>
64+
{({ stateIcon }) => stateIcon}
65+
</FieldContext.Consumer>
66+
</FieldBase>
67+
</div>
68+
);
69+
70+
expect(container.find('.field-state-icon .icon-close-circle').length).toBe(1);
71+
})
72+
73+
it('should make valid state icon', () => {
74+
const container = render(
75+
<div>
76+
<FieldBase
77+
label={<strong>test</strong>}
78+
valid={true}
79+
>
80+
<FieldContext.Consumer>
81+
{({ stateIcon }) => stateIcon}
82+
</FieldContext.Consumer>
83+
</FieldBase>
84+
</div>
85+
);
86+
87+
expect(container.find('.field-state-icon .icon-checkmark-circle-2').length).toBe(1);
88+
})
89+
90+
it('should change focus and value', () => {
91+
const container = mount(
92+
<div>
93+
<FieldBase>
94+
<FieldContext.Consumer>
95+
{({ changeFocus, changeValue }) => {
96+
changeFocus(true);
97+
changeValue(true);
98+
return undefined
99+
}}
100+
</FieldContext.Consumer>
101+
</FieldBase>
102+
</div>
103+
);
104+
105+
expect(container.find('.form-field-base.focused.has-value').length).toBe(1);
106+
})
107+
108+
it('should render actions', () => {
109+
const container = mount(
110+
<div>
111+
<FieldBase actions={<span>foo</span>} />
112+
</div>
113+
);
114+
115+
expect(container.find('.form-field-base .form-field-actions span').text()).toBe('foo');
116+
});
117+
});

__tests__/FieldContainer.test.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { render } from 'enzyme';
2+
import React from 'react';
3+
import FieldContainer from '@/components/Field/FieldContainer';
4+
5+
describe('FieldBase test', () => {
6+
it('should render field container', () => {
7+
const container = render(
8+
<div>
9+
<FieldContainer/>
10+
</div>
11+
);
12+
13+
expect(container.find('.form-field-container').length).toBe(1);
14+
});
15+
16+
it('should render field container toggle', () => {
17+
const container = render(
18+
<div>
19+
<FieldContainer toggles />
20+
</div>
21+
);
22+
23+
expect(container.find('.form-field-container.toggles').length).toBe(1);
24+
});
25+
});

__tests__/FieldContext.test.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React from 'react';
2+
import { render } from 'enzyme';
3+
import { FieldContext } from '@/components/Field/FieldContext';
4+
5+
describe('FieldContext test', () => {
6+
it('should use default values when no consumer used', () => {
7+
const mockChangeValue = jest.fn();
8+
const mockChangeFocus = jest.fn();
9+
const container = render(
10+
<div>
11+
<FieldContext.Consumer>
12+
{({ changeValue, changeFocus }) => {
13+
const a = changeValue(true);
14+
const b = changeFocus(true);
15+
16+
return <span>{a === undefined && b === undefined ? 'undefined' : 'defined'}</span>;
17+
}}
18+
</FieldContext.Consumer>
19+
</div>,
20+
{ context: {
21+
changeValue: mockChangeValue,
22+
changeFocus: mockChangeFocus
23+
} }
24+
)
25+
26+
expect(container.find('span').text()).toBe('undefined');
27+
});
28+
})

__tests__/Form.test.tsx

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { render } from 'enzyme';
2+
import React from 'react';
3+
import FormGroup from '@/components/Form/Group';
4+
import FormLabel from '@/components/Form/Label';
5+
import FormMessage from '@/components/Form/Message';
6+
7+
describe('Form test', () => {
8+
it('should render form-group', () => {
9+
const container = render(
10+
<div>
11+
<FormGroup/>
12+
</div>
13+
);
14+
15+
expect(container.find('.form-group').length).toBe(1);
16+
});
17+
18+
it('should render form label', () => {
19+
const container = render(
20+
<div>
21+
<FormLabel />
22+
</div>
23+
);
24+
25+
expect(container.find('.form-label').length).toBe(1);
26+
});
27+
28+
it('should render form message', () => {
29+
const container = render(
30+
<div>
31+
<FormMessage>Message</FormMessage>
32+
</div>
33+
);
34+
35+
expect(container.find('.form-message').length).toBe(1);
36+
expect((container.find('.form-message').text())).toBe('Message');
37+
});
38+
39+
it('should render valid form message', () => {
40+
const container = render(
41+
<div>
42+
<FormMessage valid>Message</FormMessage>
43+
</div>
44+
);
45+
46+
expect(container.find('.form-message.form-message-valid').length).toBe(1);
47+
expect((container.find('.form-message').text())).toBe('Message');
48+
});
49+
50+
it('should render invalid form message', () => {
51+
const container = render(
52+
<div>
53+
<FormMessage valid={false}>Message</FormMessage>
54+
</div>
55+
);
56+
57+
expect(container.find('.form-message.form-message-invalid').length).toBe(1);
58+
expect((container.find('.form-message').text())).toBe('Message');
59+
});
60+
});

__tests__/Selectfield.test.tsx

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import { mount, render } from 'enzyme';
2+
import React from 'react';
3+
import { SelectField } from '@/components';
4+
5+
describe('SelectField test', () => {
6+
it('should render text input', () => {
7+
const container = render(
8+
<div>
9+
<SelectField />
10+
</div>
11+
);
12+
13+
expect(container.find('.form-field-base select').length).toBe(1);
14+
});
15+
16+
it('Should set initial value with value prop', () => {
17+
const fn = jest.fn();
18+
19+
const container = mount(
20+
<SelectField value="foo" onChange={() => fn()}>
21+
<option value="" />
22+
<option value="foo">bar</option>
23+
</SelectField>
24+
);
25+
26+
expect(container.find('select').props().value).toBe('foo');
27+
});
28+
29+
it('Should set initial value with defaultValue prop', () => {
30+
const fn = jest.fn();
31+
32+
const container = mount(
33+
<SelectField defaultValue="foo" onChange={() => fn()}>
34+
<option value="" />
35+
<option value="foo">bar</option>
36+
</SelectField>
37+
);
38+
39+
expect(container.find('select').props().value).toBe('foo');
40+
});
41+
42+
it('Should set initial value based on selected option', () => {
43+
const fn = jest.fn();
44+
45+
const container = mount(
46+
<SelectField onChange={() => fn()}>
47+
<option value="" />
48+
<option value="foo" selected>bar</option>
49+
</SelectField>
50+
);
51+
52+
expect(container.find('select').props().value).toBe('foo');
53+
});
54+
55+
it('should give focus class when focused', () => {
56+
const container = mount(
57+
<SelectField/>
58+
);
59+
60+
container.find('select').simulate('focus');
61+
62+
expect(container.find('.form-field-base').hasClass('focused')).toBeTruthy();
63+
});
64+
65+
it('should not have focus class when blurred after focus', () => {
66+
const container = mount(
67+
<SelectField/>
68+
);
69+
70+
container.find('select').simulate('focus').simulate('blur');
71+
72+
expect(container.find('.form-field-base').hasClass('focused')).toBeFalsy();
73+
});
74+
75+
it('should call onChange method when changing', () => {
76+
const fn = jest.fn();
77+
78+
const container = mount(
79+
<SelectField onChange={() => fn()}/>
80+
);
81+
82+
container.find('select').simulate('change');
83+
84+
expect(fn).toHaveBeenCalled();
85+
});
86+
87+
it('should not call onChange method when ommited', () => {
88+
const fn = jest.fn();
89+
90+
const container = mount(
91+
<SelectField/>
92+
);
93+
94+
container.find('select').simulate('change');
95+
96+
expect(fn).not.toHaveBeenCalled();
97+
});
98+
99+
it('should ignore invalid react elements and select last selected option', () => {
100+
101+
const container = mount(
102+
<SelectField>
103+
{undefined}
104+
<div />
105+
<option selected value="foo"></option>
106+
<option selected value="bar"></option>
107+
</SelectField>
108+
);
109+
110+
expect(container.find('select').props().value).toBe('bar');
111+
});
112+
});

0 commit comments

Comments
 (0)