|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import {render, screen} from '@testing-library/react'; |
| 4 | + |
| 5 | +import {testCustomClassName, testOnClick} from '../../../../test-utils/shared/common'; |
| 6 | +import {FileLinkProps} from '../../../models'; |
| 7 | +import {getQaAttrubutes} from '../../../utils'; |
| 8 | +import FileLink from '../FileLink'; |
| 9 | + |
| 10 | +const fileLink = { |
| 11 | + href: 'qwe.pdf', |
| 12 | + text: 'Link to file', |
| 13 | + qa: 'file-link-component', |
| 14 | +}; |
| 15 | + |
| 16 | +const TYPES: Array<FileLinkProps['type']> = ['horizontal', 'vertical']; |
| 17 | +const TEXT_SIZRS: Array<FileLinkProps['textSize']> = ['s', 'xs', 'm', 'l']; |
| 18 | +const THEMES: Array<FileLinkProps['theme']> = ['default', 'dark', 'light']; |
| 19 | + |
| 20 | +const qaAttributes = getQaAttrubutes(fileLink.qa, 'link', 'link-container'); |
| 21 | + |
| 22 | +describe('FileLink', () => { |
| 23 | + test('render FileLink by default', async () => { |
| 24 | + render(<FileLink {...fileLink} />); |
| 25 | + const component = screen.getByTestId(qaAttributes.default); |
| 26 | + |
| 27 | + expect(component).toBeInTheDocument(); |
| 28 | + expect(component).toBeVisible(); |
| 29 | + }); |
| 30 | + |
| 31 | + test('render FileLink with text', async () => { |
| 32 | + render(<FileLink {...fileLink} />); |
| 33 | + const component = screen.getByTestId(qaAttributes.link); |
| 34 | + |
| 35 | + expect(component).toHaveTextContent(fileLink.text); |
| 36 | + }); |
| 37 | + |
| 38 | + test('render FileLink with href', async () => { |
| 39 | + render(<FileLink {...fileLink} />); |
| 40 | + const component = screen.getByTestId(qaAttributes.link); |
| 41 | + |
| 42 | + expect(component).toHaveAttribute('href', fileLink.href); |
| 43 | + }); |
| 44 | + |
| 45 | + test.each(new Array<FileLinkProps['type']>(...TYPES))('render with given "%s" type', (type) => { |
| 46 | + render(<FileLink {...fileLink} type={type} />); |
| 47 | + const cardBase = screen.getByTestId(qaAttributes.default); |
| 48 | + |
| 49 | + expect(cardBase).toHaveClass(`pc-file-link_type_${type}`); |
| 50 | + }); |
| 51 | + |
| 52 | + test.each(new Array<FileLinkProps['textSize']>(...TEXT_SIZRS))( |
| 53 | + 'render with given "%s" textSize', |
| 54 | + (textSize) => { |
| 55 | + render(<FileLink {...fileLink} textSize={textSize} />); |
| 56 | + const cardBase = screen.getByTestId(qaAttributes.default); |
| 57 | + |
| 58 | + expect(cardBase).toHaveClass(`pc-file-link_size_${textSize}`); |
| 59 | + }, |
| 60 | + ); |
| 61 | + |
| 62 | + test('add className', () => { |
| 63 | + testCustomClassName<FileLinkProps>({ |
| 64 | + component: FileLink, |
| 65 | + props: fileLink, |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + test.each(new Array<FileLinkProps['theme']>(...THEMES))( |
| 70 | + 'render with given "%s" theme', |
| 71 | + (theme) => { |
| 72 | + render(<FileLink {...fileLink} theme={theme} />); |
| 73 | + const cardBase = screen.getByTestId(qaAttributes.default); |
| 74 | + |
| 75 | + expect(cardBase).toHaveClass(`pc-file-link_theme_${theme}`); |
| 76 | + }, |
| 77 | + ); |
| 78 | + |
| 79 | + test('call onClick', async () => { |
| 80 | + testOnClick<FileLinkProps>({ |
| 81 | + component: FileLink, |
| 82 | + props: fileLink, |
| 83 | + options: {qaId: qaAttributes.link}, |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + test.each(new Array<number>(1, 2, 3))('render with given "%s" tabIndex', (tabIndex) => { |
| 88 | + render(<FileLink {...fileLink} tabIndex={tabIndex} />); |
| 89 | + const cardBase = screen.getByTestId(qaAttributes.link); |
| 90 | + |
| 91 | + expect(cardBase).toHaveAttribute('tabIndex', tabIndex.toString()); |
| 92 | + }); |
| 93 | + |
| 94 | + test('render FileLink with urlTitle', async () => { |
| 95 | + const urlTitle = 'Url Title'; |
| 96 | + render(<FileLink {...fileLink} urlTitle={urlTitle} />); |
| 97 | + const component = screen.getByTestId(qaAttributes.link); |
| 98 | + |
| 99 | + expect(component).toHaveAttribute('title', urlTitle); |
| 100 | + }); |
| 101 | +}); |
0 commit comments