Skip to content

Commit 161ca00

Browse files
author
Larry Botha
committed
test(form component): test exposed props
1 parent 8a7fe80 commit 161ca00

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<script>
2+
import {getContext} from 'svelte';
3+
import {key} from '../../../../lib';
4+
5+
export let field;
6+
7+
const context = getContext(key);
8+
const form = context.form;
9+
</script>
10+
11+
<label for={field}>
12+
{field}
13+
<input type="text" id={field} name={field} bind:value={$form[field]} />
14+
</label>

test/specs/components/form.spec.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import {render, fireEvent, waitFor} from '@testing-library/svelte';
2+
import {writable, get as svelteGet} from 'svelte/store';
3+
4+
import Input from './fixtures/input.svelte';
5+
6+
import Form from 'lib/components/Form.svelte';
7+
8+
const defaultProps = {
9+
initialValues: {foo: ''},
10+
onSubmit() {},
11+
};
12+
13+
const get = (store) => {
14+
/**
15+
* not sure why we need to invoke `get` twice, but we do
16+
*/
17+
return svelteGet(svelteGet(store));
18+
};
19+
20+
describe('Form', () => {
21+
test('-> exposes `form` prop via slot properties', async () => {
22+
const inputId = 'foo';
23+
const props = {
24+
...defaultProps,
25+
initialValues: {[inputId]: ''},
26+
};
27+
let form = writable({});
28+
const {getByLabelText} = render(
29+
<Form {...props} let_form={form}>
30+
<Input field={inputId} />
31+
</Form>,
32+
);
33+
const input = getByLabelText(inputId);
34+
35+
expect(get(form)).toHaveProperty('foo', '');
36+
37+
const value = 'bar';
38+
39+
await fireEvent.input(input, {target: {value}});
40+
41+
expect(get(form)).toHaveProperty('foo', value);
42+
});
43+
44+
test('-> exposes `state` prop via slot properties', async () => {
45+
const inputId = 'foo';
46+
const props = {
47+
...defaultProps,
48+
initialValues: {[inputId]: ''},
49+
};
50+
let state = writable({});
51+
const {getByLabelText} = render(
52+
<Form {...props} let_state={state}>
53+
<Input field={inputId} />
54+
</Form>,
55+
);
56+
const input = getByLabelText(inputId);
57+
58+
expect(get(state)).toHaveProperty('isModified', false);
59+
60+
const value = 'bar';
61+
62+
await fireEvent.input(input, {target: {value}});
63+
64+
expect(get(state)).toHaveProperty('isModified', true);
65+
});
66+
67+
test.each`
68+
methodName
69+
${'handleChange'}
70+
${'handleSubmit'}
71+
${'updateField'}
72+
${'updateInitialValues'}
73+
${'updateTouched'}
74+
${'updateValidateField'}
75+
${'validateField'}
76+
`('-> exposes $methodName via slot properties', ({methodName}) => {
77+
const inputId = 'foo';
78+
const methodSetter = jest.fn();
79+
const props = {
80+
...defaultProps,
81+
initialValues: {[inputId]: ''},
82+
[`let_${methodName}`]: methodSetter,
83+
};
84+
render(
85+
<Form {...props}>
86+
<Input field={inputId} />
87+
</Form>,
88+
);
89+
90+
expect(methodSetter.mock.calls[0][0]).toBeInstanceOf(Function);
91+
expect(methodSetter.mock.calls[0][0]).toHaveProperty('name', methodName);
92+
});
93+
});

0 commit comments

Comments
 (0)