Skip to content

Commit 29aeea0

Browse files
author
Larry Botha
authored
Merge pull request #100 from tjinauyeung/feat/export-all-props-in-form-component
2 parents f96f154 + 161ca00 commit 29aeea0

File tree

6 files changed

+125
-4
lines changed

6 files changed

+125
-4
lines changed

lib/components/Form.svelte

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
handleChange,
1717
handleSubmit,
1818
updateField,
19+
updateInitialValues,
1920
updateTouched,
21+
updateValidateField,
22+
validateField,
2023
} = createForm({
2124
initialValues,
2225
validationSchema,
@@ -32,7 +35,10 @@
3235
handleChange,
3336
handleSubmit,
3437
updateField,
38+
updateInitialValues,
3539
updateTouched,
40+
updateValidateField,
41+
validateField,
3642
});
3743
</script>
3844

@@ -45,6 +51,9 @@
4551
{handleChange}
4652
{handleSubmit}
4753
{updateField}
54+
{updateInitialValues}
4855
{updateTouched}
56+
{updateValidateField}
57+
{validateField}
4958
/>
5059
</form>

test/config/jest.test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ module.exports = {
77

88
moduleFileExtensions: ['js', 'svelte'],
99

10-
testMatch: ['<rootDir>/test/*.spec.js'],
10+
moduleNameMapper: {
11+
'^lib/(.*)': '<rootDir>/lib/$1',
12+
},
13+
14+
testMatch: ['<rootDir>/test/specs/**/*.spec.js'],
1115

1216
transform: {
1317
'^.+\\.svelte$': 'svelte-jester',
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+
});

test/textarea.spec.js renamed to test/specs/components/textarea.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {render as renderTl, fireEvent, waitFor} from '@testing-library/svelte';
22

3-
import Form from '../lib/components/Form.svelte';
4-
import Textarea from '../lib/components/Textarea.svelte';
3+
import Form from 'lib/components/Form.svelte';
4+
import Textarea from 'lib/components/Textarea.svelte';
55

66
const defaultProps = {
77
initialValues: {

test/library.spec.js renamed to test/specs/library.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/* globals beforeEach, console, describe, expect, it, require, jest */
2-
const {createForm} = require('../lib');
32
const yup = require('yup');
43
const Chance = require('chance');
54

5+
const {createForm} = require('../../lib');
6+
67
const chance = new Chance();
78

89
function nonEmpty(array) {

0 commit comments

Comments
 (0)