|
| 1 | +import { render } from '@solidjs/testing-library'; |
| 2 | +import { createSignal, flush } from 'solid-js'; |
| 3 | +import { describe, expect, it, vi } from 'vitest'; |
| 4 | +import type { ActionProps } from '../types/props.js'; |
| 5 | +import { ActionElement } from './ActionElement.jsx'; |
| 6 | + |
| 7 | +const baseProps = (overrides: Partial<ActionProps> = {}): ActionProps => |
| 8 | + ({ |
| 9 | + label: 'Add rule', |
| 10 | + title: 'Add rule title', |
| 11 | + className: 'custom', |
| 12 | + testID: 'add-rule', |
| 13 | + path: [], |
| 14 | + level: 0, |
| 15 | + handleOnClick: () => {}, |
| 16 | + ruleOrGroup: { combinator: 'and', rules: [] }, |
| 17 | + schema: {}, |
| 18 | + ...overrides, |
| 19 | + }) as ActionProps; |
| 20 | + |
| 21 | +describe('ActionElement', () => { |
| 22 | + it('renders a button carrying the label, title, class, and test ID', () => { |
| 23 | + const { getByTestId } = render(() => <ActionElement {...baseProps()} />); |
| 24 | + const button = getByTestId('add-rule'); |
| 25 | + expect(button.tagName).toBe('BUTTON'); |
| 26 | + expect(button).toHaveAttribute('type', 'button'); |
| 27 | + expect(button).toHaveAttribute('title', 'Add rule title'); |
| 28 | + expect(button).toHaveClass('custom'); |
| 29 | + expect(button.textContent).toBe('Add rule'); |
| 30 | + expect(button).not.toBeDisabled(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('calls handleOnClick with the event', () => { |
| 34 | + const handleOnClick = vi.fn(); |
| 35 | + const { getByTestId } = render(() => <ActionElement {...baseProps({ handleOnClick })} />); |
| 36 | + getByTestId('add-rule').click(); |
| 37 | + expect(handleOnClick).toHaveBeenCalledTimes(1); |
| 38 | + expect(handleOnClick.mock.calls[0][0]).toBeInstanceOf(MouseEvent); |
| 39 | + }); |
| 40 | + |
| 41 | + it('is disabled when `disabled` is set and there is no disabled translation', () => { |
| 42 | + const { getByTestId } = render(() => <ActionElement {...baseProps({ disabled: true })} />); |
| 43 | + expect(getByTestId('add-rule')).toBeDisabled(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('stays enabled and swaps label/title when a disabled translation is supplied', () => { |
| 47 | + const { getByTestId } = render(() => ( |
| 48 | + <ActionElement |
| 49 | + {...baseProps({ |
| 50 | + disabled: true, |
| 51 | + disabledTranslation: { label: 'Unlock', title: 'Unlock title' }, |
| 52 | + })} |
| 53 | + /> |
| 54 | + )); |
| 55 | + const button = getByTestId('add-rule'); |
| 56 | + expect(button).not.toBeDisabled(); |
| 57 | + expect(button.textContent).toBe('Unlock'); |
| 58 | + expect(button).toHaveAttribute('title', 'Unlock title'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('ignores the disabled translation while enabled', () => { |
| 62 | + const { getByTestId } = render(() => ( |
| 63 | + <ActionElement |
| 64 | + {...baseProps({ disabledTranslation: { label: 'Unlock', title: 'Unlock title' } })} |
| 65 | + /> |
| 66 | + )); |
| 67 | + expect(getByTestId('add-rule').textContent).toBe('Add rule'); |
| 68 | + }); |
| 69 | + |
| 70 | + /** |
| 71 | + * The props-reactivity gate for this component. A destructure at the top of `ActionElement` |
| 72 | + * severs every one of these and fails no type check. |
| 73 | + */ |
| 74 | + it('updates when its props change', () => { |
| 75 | + const [label, setLabel] = createSignal('one'); |
| 76 | + const [disabled, setDisabled] = createSignal(false); |
| 77 | + const { getByTestId } = render(() => ( |
| 78 | + <ActionElement |
| 79 | + {...baseProps({ |
| 80 | + get label() { |
| 81 | + return label(); |
| 82 | + }, |
| 83 | + get disabled() { |
| 84 | + return disabled(); |
| 85 | + }, |
| 86 | + })} |
| 87 | + /> |
| 88 | + )); |
| 89 | + const button = getByTestId('add-rule'); |
| 90 | + expect(button.textContent).toBe('one'); |
| 91 | + expect(button).not.toBeDisabled(); |
| 92 | + |
| 93 | + setLabel('two'); |
| 94 | + setDisabled(true); |
| 95 | + flush(); |
| 96 | + |
| 97 | + expect(button.textContent).toBe('two'); |
| 98 | + expect(button).toBeDisabled(); |
| 99 | + }); |
| 100 | +}); |
0 commit comments