Skip to content

Commit

Permalink
test: added hidden field tests
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Jan 17, 2025
1 parent 9f44365 commit e80b737
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions packages/core/src/useHiddenField/HiddeField.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { render, screen } from '@testing-library/vue';
import { HiddenField } from '.';
import { useForm } from '../useForm';
import { flush } from '@test-utils/flush';
import { describe, expect, test, vi } from 'vitest';
import { ref } from 'vue';

describe('HiddenField component', () => {
test('should not render anything', async () => {
await render({
components: { HiddenField },
setup() {
const { formProps } = useForm();

return { formProps };
},
template: `
<HiddenField name="hidden-field" value="test-value" />
`,
});

await flush();
});

test('should set the value on the form', async () => {
let getValues!: () => ReturnType<typeof useForm>['values'];
await render({
components: { HiddenField },
setup() {
const { formProps, values } = useForm();
getValues = () => values;

return { formProps };
},
template: `
<HiddenField name="hidden-field" value="test-value" />
`,
});

await flush();
expect(getValues()).toEqual({ 'hidden-field': 'test-value' });
});

test('should update the value on the form when it changes', async () => {
const val = ref('test-value');
let getValues!: () => ReturnType<typeof useForm>['values'];
await render({
components: { HiddenField },
setup() {
const { formProps, values } = useForm();
getValues = () => values;

return { formProps, val };
},
template: `
<HiddenField name="hidden-field" :value="val" />
`,
});

await flush();
expect(getValues()).toEqual({ 'hidden-field': 'test-value' });

val.value = 'updated-value';
await flush();
expect(getValues()).toEqual({ 'hidden-field': 'updated-value' });
});

test('should not submit value when disabled', async () => {
const onSubmit = vi.fn();

await render({
components: { HiddenField },
setup() {
const { formProps } = useForm();
return { formProps, onSubmit };
},
template: `
<form v-bind="formProps" data-testid="form" @submit.prevent="onSubmit">
<HiddenField name="hidden-field" value="test-value" disabled />
<button type="submit">Submit</button>
</form>
`,
});

const form = screen.getByTestId('form');
const formData = new FormData(form as HTMLFormElement);
await flush();
expect(formData.get('hidden-field')).toBeNull();
});
});

0 comments on commit e80b737

Please sign in to comment.