-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #100 from tjinauyeung/feat/export-all-props-in-for…
…m-component
- Loading branch information
Showing
6 changed files
with
125 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<script> | ||
import {getContext} from 'svelte'; | ||
import {key} from '../../../../lib'; | ||
export let field; | ||
const context = getContext(key); | ||
const form = context.form; | ||
</script> | ||
|
||
<label for={field}> | ||
{field} | ||
<input type="text" id={field} name={field} bind:value={$form[field]} /> | ||
</label> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import {render, fireEvent, waitFor} from '@testing-library/svelte'; | ||
import {writable, get as svelteGet} from 'svelte/store'; | ||
|
||
import Input from './fixtures/input.svelte'; | ||
|
||
import Form from 'lib/components/Form.svelte'; | ||
|
||
const defaultProps = { | ||
initialValues: {foo: ''}, | ||
onSubmit() {}, | ||
}; | ||
|
||
const get = (store) => { | ||
/** | ||
* not sure why we need to invoke `get` twice, but we do | ||
*/ | ||
return svelteGet(svelteGet(store)); | ||
}; | ||
|
||
describe('Form', () => { | ||
test('-> exposes `form` prop via slot properties', async () => { | ||
const inputId = 'foo'; | ||
const props = { | ||
...defaultProps, | ||
initialValues: {[inputId]: ''}, | ||
}; | ||
let form = writable({}); | ||
const {getByLabelText} = render( | ||
<Form {...props} let_form={form}> | ||
<Input field={inputId} /> | ||
</Form>, | ||
); | ||
const input = getByLabelText(inputId); | ||
|
||
expect(get(form)).toHaveProperty('foo', ''); | ||
|
||
const value = 'bar'; | ||
|
||
await fireEvent.input(input, {target: {value}}); | ||
|
||
expect(get(form)).toHaveProperty('foo', value); | ||
}); | ||
|
||
test('-> exposes `state` prop via slot properties', async () => { | ||
const inputId = 'foo'; | ||
const props = { | ||
...defaultProps, | ||
initialValues: {[inputId]: ''}, | ||
}; | ||
let state = writable({}); | ||
const {getByLabelText} = render( | ||
<Form {...props} let_state={state}> | ||
<Input field={inputId} /> | ||
</Form>, | ||
); | ||
const input = getByLabelText(inputId); | ||
|
||
expect(get(state)).toHaveProperty('isModified', false); | ||
|
||
const value = 'bar'; | ||
|
||
await fireEvent.input(input, {target: {value}}); | ||
|
||
expect(get(state)).toHaveProperty('isModified', true); | ||
}); | ||
|
||
test.each` | ||
methodName | ||
${'handleChange'} | ||
${'handleSubmit'} | ||
${'updateField'} | ||
${'updateInitialValues'} | ||
${'updateTouched'} | ||
${'updateValidateField'} | ||
${'validateField'} | ||
`('-> exposes $methodName via slot properties', ({methodName}) => { | ||
const inputId = 'foo'; | ||
const methodSetter = jest.fn(); | ||
const props = { | ||
...defaultProps, | ||
initialValues: {[inputId]: ''}, | ||
[`let_${methodName}`]: methodSetter, | ||
}; | ||
render( | ||
<Form {...props}> | ||
<Input field={inputId} /> | ||
</Form>, | ||
); | ||
|
||
expect(methodSetter.mock.calls[0][0]).toBeInstanceOf(Function); | ||
expect(methodSetter.mock.calls[0][0]).toHaveProperty('name', methodName); | ||
}); | ||
}); |
4 changes: 2 additions & 2 deletions
4
test/textarea.spec.js → test/specs/components/textarea.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters