|
1 | 1 | import { render, screen } from '@testing-library/vue';
|
2 | 2 | import { useLabel } from './useLabel';
|
3 |
| -import { shallowRef } from 'vue'; |
| 3 | +import { defineComponent, shallowRef } from 'vue'; |
4 | 4 |
|
5 | 5 | describe('label element', () => {
|
6 | 6 | test('should render label with `for` attribute', async () => {
|
@@ -95,3 +95,38 @@ describe('label target (labelledBy)', () => {
|
95 | 95 | expect(screen.getByTestId('input')?.getAttribute('aria-labelledby')).toBe(`${labelFor}-l`);
|
96 | 96 | });
|
97 | 97 | });
|
| 98 | + |
| 99 | +describe('label component', () => { |
| 100 | + test('should render label component with `for` attribute', async () => { |
| 101 | + const label = 'label'; |
| 102 | + const labelFor = 'input'; |
| 103 | + const inputRef = shallowRef<HTMLElement>(); |
| 104 | + |
| 105 | + const LabelComp = defineComponent({ |
| 106 | + props: ['for'], |
| 107 | + template: ` |
| 108 | + <label data-testid="label" :for="$props.for"> |
| 109 | + <slot /> |
| 110 | + </label> |
| 111 | + `, |
| 112 | + }); |
| 113 | + |
| 114 | + await render({ |
| 115 | + components: { LabelComp }, |
| 116 | + setup: () => { |
| 117 | + return { |
| 118 | + ...useLabel({ label: label, for: labelFor, targetRef: inputRef }), |
| 119 | + label, |
| 120 | + inputRef, |
| 121 | + }; |
| 122 | + }, |
| 123 | + template: ` |
| 124 | + <LabelComp v-bind="labelProps">{{ label }}</LabelComp> |
| 125 | + <input data-testid="input" ref="inputRef" /> |
| 126 | + `, |
| 127 | + }); |
| 128 | + |
| 129 | + expect(screen.getByTestId('label')).toHaveTextContent(label); |
| 130 | + expect(screen.getByTestId('label')?.getAttribute('for')).toBe(labelFor); |
| 131 | + }); |
| 132 | +}); |
0 commit comments