Skip to content

Commit ce0a612

Browse files
committed
tests
1 parent 4c78716 commit ce0a612

File tree

7 files changed

+39
-19
lines changed

7 files changed

+39
-19
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
- label
2-
- write tests
32
- disable style
43
- run tests & update snapshots
54
- build package

dist/index.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"@babel/core": "7.0.0",
5858
"@types/enzyme": "^3.9.1",
5959
"@types/jest": "^24.0.12",
60+
"@types/sinon": "^7.0.11",
6061
"@typescript-eslint/eslint-plugin": "^1.7.0",
6162
"@typescript-eslint/parser": "^1.7.0",
6263
"awesome-typescript-loader": "^5.2.1",

src/__tests__/index.test.tsx

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import * as React from 'react';
22
import { shallow, mount } from 'enzyme';
3-
//@ts-ignore
43
import * as sinon from 'sinon';
54

5+
import { Inputs } from '../components/interfaces';
66
import MultiFieldsInput from '../components/index';
77

88
describe('MultiFieldsInput', () => {
9-
const inputs = [
9+
const inputs: Inputs[] = [
1010
{
1111
type: 'number',
1212
maxLength: 2,
@@ -29,7 +29,6 @@ describe('MultiFieldsInput', () => {
2929
<MultiFieldsInput
3030
label="Sort Code"
3131
name="sortCode"
32-
//@ts-ignore
3332
inputs={inputs}
3433
value="202020"
3534
onBlur={() => {}}
@@ -46,25 +45,37 @@ describe('MultiFieldsInput', () => {
4645
<MultiFieldsInput
4746
label="Sort Code"
4847
name="sortCode"
49-
//@ts-ignore
5048
inputs={inputs}
5149
value="202122"
5250
onBlur={spyBlur}
5351
onChange={spyChange}
5452
/>
5553
);
54+
const instance = component.instance() as MultiFieldsInput;
5655

5756
component
5857
.find('input[name="sortCode0"]')
5958
.simulate('change', { target: { name: 'sortCode0', value: '10' } });
6059

6160
expect(spyChange.calledOnce).toBe(true);
6261
component.update();
62+
6363
expect(component.find('input[name="sortCode0"]').prop('value')).toBe('10');
64-
//call getValue() method and see if it returns the correct value as well
64+
expect(instance.getValue()).toBe('102122');
6565
});
6666

6767
it('Error class is activated when not valid', () => {
68-
// expect(component).toMatchSnapshot();
68+
const component = mount(
69+
<MultiFieldsInput
70+
label="Sort Code"
71+
name="sortCode"
72+
inputs={inputs}
73+
isValid={false}
74+
value="202020"
75+
onBlur={() => {}}
76+
onChange={() => {}}
77+
/>
78+
);
79+
expect(component.find('input.rmfi-error').length).toBe(3);
6980
});
7081
});

src/components/index.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class MultiFieldsInput extends Component<Props> {
9898
};
9999

100100
render() {
101-
const { inputs, isValid, name } = this.props;
101+
const { inputs, isValid, name, label } = this.props;
102102
const { state } = this;
103103

104104
const globalProps = {
@@ -108,6 +108,11 @@ class MultiFieldsInput extends Component<Props> {
108108

109109
return (
110110
<div className="rmfi-container">
111+
{label && (
112+
<label htmlFor={name} className="rmfi-label">
113+
{label}
114+
</label>
115+
)}
111116
{inputs.map((field, index) => {
112117
return (
113118
<input

src/components/interfaces.tsx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
interface Inputs {
1+
export interface Inputs {
22
type: 'number' | 'text';
33
maxLength: number;
44
placeholder: string;
@@ -11,17 +11,16 @@ export interface Target {
1111
value: string;
1212
}
1313

14-
export interface Props {
14+
export interface DefaultProps {
15+
autoFocus: boolean;
16+
isValid: boolean;
17+
}
18+
19+
export interface Props extends DefaultProps {
1520
inputs: Inputs[];
1621
name: string;
1722
onBlur(target: Target): void;
1823
onChange(target: Target): void;
19-
isValid?: boolean;
2024
value?: string;
21-
autoFocus?: boolean;
22-
}
23-
24-
export interface DefaultProps {
25-
autoFocus: boolean;
26-
isValid: boolean;
25+
label?: string;
2726
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,11 @@
508508
"@types/prop-types" "*"
509509
csstype "^2.2.0"
510510

511+
"@types/sinon@^7.0.11":
512+
version "7.0.11"
513+
resolved "https://registry.yarnpkg.com/@types/sinon/-/sinon-7.0.11.tgz#6f28f005a36e779b7db0f1359b9fb9eef72aae88"
514+
integrity sha512-6ee09Ugx6GyEr0opUIakmxIWFNmqYPjkqa3/BuxCBokA0klsOLPgMD5K4q40lH7/yZVuJVzOfQpd7pipwjngkQ==
515+
511516
"@types/stack-utils@^1.0.1":
512517
version "1.0.1"
513518
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-1.0.1.tgz#0a851d3bd96498fa25c33ab7278ed3bd65f06c3e"

0 commit comments

Comments
 (0)