|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +import React from 'react'; |
| 4 | +import { render } from '@testing-library/react'; |
| 5 | + |
| 6 | +import customCssProps from '../../../lib/components/internal/generated/custom-css-properties'; |
| 7 | +import createWrapper from '../../../lib/components/test-utils/dom'; |
| 8 | +import Textarea, { TextareaProps } from '../../../lib/components/textarea'; |
| 9 | + |
| 10 | +function renderTextarea( |
| 11 | + props: Omit<TextareaProps, 'value'> & { value?: string; styleClassNames?: Record<string, string> } = {} |
| 12 | +) { |
| 13 | + const { container } = render(<Textarea value="" onChange={() => {}} ariaLabel="Textarea" {...props} />); |
| 14 | + return { container, wrapper: createWrapper(container).findTextarea()! }; |
| 15 | +} |
| 16 | + |
| 17 | +describe('Textarea Style API v2', () => { |
| 18 | + test('applies the textarea class to the native textarea and ignores unknown keys', () => { |
| 19 | + const { container, wrapper } = renderTextarea({ styleClassNames: { textarea: 'a', unknown: 'b' } }); |
| 20 | + const nativeTextarea = wrapper.findNativeTextarea().getElement(); |
| 21 | + expect(nativeTextarea).toHaveClass('a'); |
| 22 | + expect(wrapper.getElement()).not.toHaveClass('a'); |
| 23 | + expect(container.querySelector('.b')).toBeNull(); |
| 24 | + }); |
| 25 | + |
| 26 | + test('applies the textarea class in every state', () => { |
| 27 | + for (const state of [{}, { invalid: true }, { warning: true }, { disabled: true }, { readOnly: true }]) { |
| 28 | + const { wrapper } = renderTextarea({ ...state, styleClassNames: { textarea: 'a' } }); |
| 29 | + expect(wrapper.findNativeTextarea().getElement()).toHaveClass('a'); |
| 30 | + } |
| 31 | + }); |
| 32 | + |
| 33 | + test('does not leak the styleClassNames prop to the DOM', () => { |
| 34 | + const { container } = renderTextarea({ styleClassNames: { textarea: 'a' } }); |
| 35 | + expect(container.querySelector('[styleClassNames]')).toBeNull(); |
| 36 | + }); |
| 37 | + |
| 38 | + test('keeps the v1 inline styles on the native textarea alongside the class', () => { |
| 39 | + const { wrapper } = renderTextarea({ |
| 40 | + styleClassNames: { textarea: 'a' }, |
| 41 | + style: { |
| 42 | + root: { borderRadius: '6px', color: { default: '#222222', hover: '#333333' } }, |
| 43 | + placeholder: { color: '#999999' }, |
| 44 | + }, |
| 45 | + }); |
| 46 | + const nativeTextarea = wrapper.findNativeTextarea().getElement(); |
| 47 | + expect(nativeTextarea).toHaveClass('a'); |
| 48 | + expect(nativeTextarea.style.borderRadius).toBe('6px'); |
| 49 | + expect(nativeTextarea.style.getPropertyValue(customCssProps.styleColorDefault)).toBe('#222222'); |
| 50 | + expect(nativeTextarea.style.getPropertyValue(customCssProps.styleColorHover)).toBe('#333333'); |
| 51 | + expect(nativeTextarea.style.getPropertyValue(customCssProps.stylePlaceholderColor)).toBe('#999999'); |
| 52 | + }); |
| 53 | +}); |
0 commit comments