Skip to content

Commit 8e97d7b

Browse files
Martynas ŽilinskasDovydasNavickas
authored andcommitted
Added clear and reset buttons.
Added clear and reset tests and fixed component errors.
1 parent d09f010 commit 8e97d7b

File tree

5 files changed

+245
-3
lines changed

5 files changed

+245
-3
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import * as React from "react";
2+
import { mount } from "enzyme";
3+
import * as Sinon from "sinon";
4+
5+
import { FormStore } from "simplr-forms-core/stores";
6+
7+
import { Form } from "../../src/components/form";
8+
import { Text } from "../../src/components/text";
9+
import { Clear } from "../../src/components/clear";
10+
11+
describe("Clear button", () => {
12+
let sandbox: Sinon.SinonSandbox;
13+
14+
beforeEach(() => {
15+
sandbox = Sinon.sandbox.create();
16+
});
17+
18+
afterEach(function () {
19+
sandbox.restore();
20+
});
21+
22+
it("clears all fields", () => {
23+
const callback = sandbox.spy(FormStore.prototype, "ClearFields");
24+
25+
const wrapper = mount(<Form>
26+
<Text name="field" />
27+
<Clear>Clear form</Clear>
28+
</Form>);
29+
30+
wrapper.find("button").simulate("click");
31+
32+
expect(callback.called).toBe(true);
33+
});
34+
35+
it("clears fields by fieldsIds", () => {
36+
const callback = sandbox.spy(FormStore.prototype, "ClearFields");
37+
let fieldsIds: string[] = [];
38+
for (let i = 0; i < 5; i++) {
39+
fieldsIds.push(`text-${i}`);
40+
}
41+
let fields: JSX.Element[] = [];
42+
fieldsIds.forEach(value => {
43+
fields.push(<Text name={value} key={value} />);
44+
});
45+
46+
const wrapper = mount(<Form>
47+
{fields}
48+
<Clear fieldIds={fieldsIds}>Clear form</Clear>
49+
</Form>);
50+
51+
wrapper.find("button").simulate("click");
52+
53+
expect(callback.called).toBe(true);
54+
// first click, first argument(fieldIds)
55+
expect(callback.args[0][0]).toBe(fieldsIds);
56+
});
57+
});

packages/simplr-forms-dom/__tests__/components/form.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from "react";
22
import { mount } from "enzyme";
3-
import * as sinon from "sinon";
3+
import * as Sinon from "sinon";
44

55
import { FormStore, FSHContainer, FormStoresHandler } from "simplr-forms-core/stores";
66

@@ -14,7 +14,7 @@ describe("Form", () => {
1414
});
1515

1616
it("calls submit callback when submit button is clicked", () => {
17-
const submitCallback = sinon.stub();
17+
const submitCallback = Sinon.stub();
1818

1919
const wrapper = mount(<Form onSubmit={submitCallback}>
2020
<button type="submit">Submit</button>
@@ -27,7 +27,7 @@ describe("Form", () => {
2727

2828
it("calls submit callback when submit called from FormStore", () => {
2929
const formId = "form-id";
30-
const submitCallback = sinon.stub();
30+
const submitCallback = Sinon.stub();
3131

3232
const wrapper = mount(<Form formId={formId} onSubmit={submitCallback}></Form>);
3333

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import * as React from "react";
2+
import { mount } from "enzyme";
3+
import * as Sinon from "sinon";
4+
5+
import { FormStore } from "simplr-forms-core/stores";
6+
7+
import { Form } from "../../src/components/form";
8+
import { Text } from "../../src/components/text";
9+
import { Reset } from "../../src/components/reset";
10+
11+
describe("Reset button", () => {
12+
let sandbox: Sinon.SinonSandbox;
13+
14+
beforeEach(() => {
15+
sandbox = Sinon.sandbox.create();
16+
});
17+
18+
afterEach(function () {
19+
sandbox.restore();
20+
});
21+
22+
it("reset all fields", () => {
23+
const callback = sandbox.spy(FormStore.prototype, "ResetFields");
24+
25+
const wrapper = mount(<Form>
26+
<Text name="field" />
27+
<Reset>Clear form</Reset>
28+
</Form>);
29+
30+
wrapper.find("button").simulate("click");
31+
32+
expect(callback.called).toBe(true);
33+
});
34+
35+
it("reset fields by fieldsIds", () => {
36+
const callback = sandbox.spy(FormStore.prototype, "ResetFields");
37+
let fieldsIds: string[] = [];
38+
for (let i = 0; i < 5; i++) {
39+
fieldsIds.push(`text-${i}`);
40+
}
41+
let fields: JSX.Element[] = [];
42+
fieldsIds.forEach(value => {
43+
fields.push(<Text name={value} key={value} />);
44+
});
45+
46+
const wrapper = mount(<Form>
47+
{fields}
48+
<Reset fieldIds={fieldsIds}>Clear form</Reset>
49+
</Form>);
50+
51+
wrapper.find("button").simulate("click");
52+
53+
expect(callback.called).toBe(true);
54+
// first click, first argument(fieldIds)
55+
expect(callback.args[0][0]).toBe(fieldsIds);
56+
});
57+
});
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import * as React from "react";
2+
import { recordify, TypedRecord } from "typed-immutable-record";
3+
4+
import { BaseContainer, BaseContainerProps } from "simplr-forms-core";
5+
import { FormError } from "simplr-forms-core/contracts";
6+
7+
export interface ClearProps extends BaseContainerProps, React.HTMLProps<HTMLButtonElement> {
8+
fieldIds?: string[];
9+
}
10+
11+
export interface ClearState {
12+
Submitting: boolean;
13+
}
14+
15+
export interface ClearStateRecord extends TypedRecord<ClearStateRecord>, ClearState { }
16+
17+
export class Clear extends BaseContainer<ClearProps, ClearStateRecord> {
18+
constructor(props: ClearProps) {
19+
super(props);
20+
21+
this.state = recordify<ClearState, ClearStateRecord>({
22+
Submitting: false
23+
});
24+
}
25+
26+
protected OnStoreUpdated(): void {
27+
const formStore = this.FormStore.GetState();
28+
const newState = recordify({
29+
Submitting: formStore.Form.Submitting,
30+
});
31+
32+
if (!newState.equals(this.state)) {
33+
this.setState(() => newState);
34+
}
35+
}
36+
37+
protected get Disabled() {
38+
if (this.props.disabled != null) {
39+
return this.props.disabled;
40+
}
41+
42+
return this.state.Submitting;
43+
}
44+
45+
protected OnButtonClick: React.MouseEventHandler<HTMLButtonElement> = (event) => {
46+
event.persist();
47+
this.FormStore.ClearFields(this.props.fieldIds);
48+
49+
if (this.props.onClick != null) {
50+
this.props.onClick(event);
51+
}
52+
}
53+
54+
render() {
55+
// TODO: Pass all other props.
56+
return <button
57+
type="button"
58+
disabled={this.Disabled}
59+
onClick={this.OnButtonClick}
60+
>
61+
{this.props.children}
62+
</button>;
63+
}
64+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import * as React from "react";
2+
import { recordify, TypedRecord } from "typed-immutable-record";
3+
4+
import { BaseContainer, BaseContainerProps } from "simplr-forms-core";
5+
import { FormError } from "simplr-forms-core/contracts";
6+
7+
export interface ResetProps extends BaseContainerProps, React.HTMLProps<HTMLButtonElement> {
8+
fieldIds?: string[];
9+
}
10+
11+
export interface ResetState {
12+
Submitting: boolean;
13+
}
14+
15+
export interface ResetStateRecord extends TypedRecord<ResetStateRecord>, ResetState { }
16+
17+
export class Reset extends BaseContainer<ResetProps, ResetStateRecord> {
18+
constructor(props: ResetProps) {
19+
super(props);
20+
21+
this.state = recordify<ResetState, ResetStateRecord>({
22+
Submitting: false
23+
});
24+
}
25+
26+
protected OnStoreUpdated(): void {
27+
const formStore = this.FormStore.GetState();
28+
const newState = recordify({
29+
Submitting: formStore.Form.Submitting,
30+
});
31+
32+
if (!newState.equals(this.state)) {
33+
this.setState(() => newState);
34+
}
35+
}
36+
37+
protected get Disabled() {
38+
if (this.props.disabled != null) {
39+
return this.props.disabled;
40+
}
41+
42+
return this.state.Submitting;
43+
}
44+
45+
protected OnButtonClick: React.MouseEventHandler<HTMLButtonElement> = (event) => {
46+
event.persist();
47+
this.FormStore.ResetFields(this.props.fieldIds);
48+
49+
if (this.props.onClick != null) {
50+
this.props.onClick(event);
51+
}
52+
}
53+
54+
render() {
55+
// TODO: Pass all other props.
56+
return <button
57+
type="button"
58+
disabled={this.Disabled}
59+
onClick={this.OnButtonClick}
60+
>
61+
{this.props.children}
62+
</button>;
63+
}
64+
}

0 commit comments

Comments
 (0)