|
1 | 1 | import user from '@testing-library/user-event' |
2 | 2 | import { render, screen } from '@testing-library/vue' |
| 3 | +import { createCommentVNode, defineComponent, h, nextTick, ref } from 'vue' |
3 | 4 | import { ark } from './factory.ts' |
4 | 5 |
|
5 | 6 | const ComponentUnderTest = ( |
@@ -33,6 +34,75 @@ describe('Factory', () => { |
33 | 34 | expect(screen.getByText('Ark UI')).toBeVisible() |
34 | 35 | }) |
35 | 36 |
|
| 37 | + it('should not duplicate the class of a plain child element', () => { |
| 38 | + render( |
| 39 | + <ark.div class="parent" asChild> |
| 40 | + <span data-testid="child" class="child"> |
| 41 | + Ark UI |
| 42 | + </span> |
| 43 | + </ark.div>, |
| 44 | + ) |
| 45 | + const child = screen.getByTestId('child') |
| 46 | + expect(child.className.split(/\s+/).filter(Boolean).sort()).toEqual(['child', 'parent']) |
| 47 | + }) |
| 48 | + |
| 49 | + it('should call each handler of a plain child element once', async () => { |
| 50 | + const onClickParent = vi.fn() |
| 51 | + const onClickChild = vi.fn() |
| 52 | + render( |
| 53 | + <ark.div onClick={onClickParent} asChild> |
| 54 | + <button type="button" data-testid="child" onClick={onClickChild} /> |
| 55 | + </ark.div>, |
| 56 | + ) |
| 57 | + await user.click(screen.getByTestId('child')) |
| 58 | + expect(onClickParent).toHaveBeenCalledTimes(1) |
| 59 | + expect(onClickChild).toHaveBeenCalledTimes(1) |
| 60 | + }) |
| 61 | + |
| 62 | + it('should apply props to the first non-comment child', () => { |
| 63 | + render( |
| 64 | + defineComponent({ |
| 65 | + setup: () => () => |
| 66 | + h(ark.div, { class: 'parent', asChild: true }, () => [ |
| 67 | + createCommentVNode('placeholder'), |
| 68 | + h('span', { 'data-testid': 'child', class: 'child' }, 'Ark UI'), |
| 69 | + ]), |
| 70 | + }), |
| 71 | + ) |
| 72 | + const child = screen.getByTestId('child') |
| 73 | + expect(child.className.split(/\s+/).filter(Boolean).sort()).toEqual(['child', 'parent']) |
| 74 | + }) |
| 75 | + |
| 76 | + it('should patch reactive props onto the same child element', async () => { |
| 77 | + const parentClass = ref('a') |
| 78 | + const { container } = render( |
| 79 | + defineComponent({ |
| 80 | + setup: () => () => |
| 81 | + h(ark.div, { class: parentClass.value, asChild: true }, () => [ |
| 82 | + h('span', { 'data-testid': 'child', class: 'child' }, 'Ark UI'), |
| 83 | + ]), |
| 84 | + }), |
| 85 | + ) |
| 86 | + const before = screen.getByTestId('child') |
| 87 | + expect(before.className.split(/\s+/).filter(Boolean).sort()).toEqual(['a', 'child']) |
| 88 | + |
| 89 | + parentClass.value = 'b' |
| 90 | + await nextTick() |
| 91 | + |
| 92 | + const after = container.querySelector('[data-testid="child"]') |
| 93 | + expect(after).toBe(before) |
| 94 | + expect(after?.className.split(/\s+/).filter(Boolean).sort()).toEqual(['b', 'child']) |
| 95 | + }) |
| 96 | + |
| 97 | + it('should render comment-only children untouched', () => { |
| 98 | + const { container } = render( |
| 99 | + defineComponent({ |
| 100 | + setup: () => () => h(ark.div, { class: 'parent', asChild: true }, () => [createCommentVNode('v-if')]), |
| 101 | + }), |
| 102 | + ) |
| 103 | + expect(container.innerHTML).toBe('<!--v-if-->') |
| 104 | + }) |
| 105 | + |
36 | 106 | it('should merge events', async () => { |
37 | 107 | const onClickParent = vi.fn() |
38 | 108 | const onClickChild = vi.fn() |
|
0 commit comments