Skip to content

Commit 227f55b

Browse files
committed
list test
1 parent 03e101f commit 227f55b

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

tests/list.test.tsx

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import { mount } from "enzyme"
2+
import React from "react"
3+
import { Field, Form, ListForm } from "rong-form"
4+
5+
describe("RForm.List", () => {
6+
it("basic", () => {
7+
const wrapper = mount(<Form>
8+
<ListForm name="list">
9+
{
10+
(fields, { add, remove }) => {
11+
return (
12+
fields.map((field, index) => (
13+
<Field {...field}>
14+
<input />
15+
</Field>
16+
))
17+
)
18+
}
19+
}
20+
</ListForm>
21+
</Form>)
22+
expect(wrapper.find('input').length).toEqual(1)
23+
})
24+
25+
it("with initialValues", () => {
26+
const wrapper = mount(<Form >
27+
<ListForm name="a" initialValue={['1','2','3']}>
28+
{
29+
(fields, { add, remove }) => {
30+
return <>{
31+
fields.map((field, index) => {
32+
33+
return <React.Fragment key={field.key}>
34+
<Field {...field} >
35+
<input />
36+
</Field>
37+
<span onClick={() => remove(index)}>remove</span>
38+
<br />
39+
<br />
40+
</React.Fragment>
41+
})
42+
43+
}
44+
<button onClick={() => add()}> add</button>
45+
</>
46+
}
47+
}
48+
</ListForm>
49+
</Form>)
50+
wrapper.update()
51+
expect(wrapper.find('input').length).toEqual(2)
52+
})
53+
54+
it("add operation", () => {
55+
const wrapper = mount(<Form>
56+
<ListForm name="list">
57+
{
58+
(fields, { add, remove }) => (
59+
<>
60+
{
61+
fields.map((field, index) => (
62+
<Field {...field}>
63+
<input />
64+
</Field>
65+
))
66+
}
67+
<button className="add-btn" onClick={() => add()}></button>
68+
</>
69+
)
70+
}
71+
</ListForm>
72+
</Form>)
73+
wrapper.find(".add-btn").simulate('click')
74+
expect(wrapper.find('input').length).toEqual(2)
75+
})
76+
77+
it("remove operation", () => {
78+
const wrapper = mount(<Form>
79+
<ListForm name="a">
80+
{
81+
(fields, { add, remove }) => (
82+
<>
83+
{
84+
fields.map((field, index) => (
85+
<>
86+
<Field {...field}>
87+
<input />
88+
</Field>
89+
<button className="remove-btn" onClick={() => remove(index)}></button>
90+
</>
91+
))
92+
}
93+
94+
</>
95+
)
96+
}
97+
</ListForm>
98+
</Form>)
99+
100+
wrapper.find('button').simulate('click')
101+
expect(wrapper.find('input').length).toEqual(0)
102+
})
103+
})

0 commit comments

Comments
 (0)