Skip to content

Commit

Permalink
feat(textarea): add component and tests
Browse files Browse the repository at this point in the history
closes #86
  • Loading branch information
larrybotha committed Feb 3, 2021
1 parent 782cc34 commit d520f1d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/components/Textarea.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
import {getContext} from 'svelte';
import {key} from './key';
export let name;
const {form, handleChange} = getContext(key);
</script>

<textarea {name} on:change={handleChange} on:blur={handleChange} {...$$props}>
{$form[name]}
</textarea>
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export {createForm} from './create-form';
export {default as Form} from './components/Form.svelte';
export {default as Textarea} from './components/Textarea.svelte';
export {default as Field} from './components/Field.svelte';
export {default as Select} from './components/Select.svelte';
export {default as ErrorMessage} from './components/ErrorMessage.svelte';
38 changes: 38 additions & 0 deletions test/textarea.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {render as renderTl, fireEvent, waitFor} from '@testing-library/svelte';

import Form from '../lib/components/Form.svelte';
import Textarea from '../lib/components/Textarea.svelte';

const defaultProps = {
initialValues: {
description: '',
},
['onSubmit']: () => {},
};

const render = (ui, props = defaultProps) => {
return renderTl(<Form {...props}>{ui}</Form>, {props});
};

describe('Textarea', () => {
test('-> updates value on change', async () => {
const label = 'description';
const props = {...defaultProps};
const {getByLabelText} = render(
<>
<label for={label}>{label}</label>
<Textarea id={label} name={label} />
</>,
props,
);

const textarea = getByLabelText(label);
expect(textarea.value.trim()).toBe(props.initialValues.description);

const value = 'foo';

await fireEvent.change(textarea, {target: {value}});

expect(textarea.value).toContain(value);
});
});

0 comments on commit d520f1d

Please sign in to comment.