|
| 1 | +import React from 'react'; |
| 2 | +import { |
| 3 | + expect, |
| 4 | + test, |
| 5 | +} from '@playwright/experimental-ct-react'; |
| 6 | +import { propTests } from '../../../../tests/playwright'; |
| 7 | +import { TextLinkForTest } from './TextLink.story'; |
| 8 | + |
| 9 | +test.describe('TextLink', () => { |
| 10 | + const testHref = '/test/custom/uri'; |
| 11 | + |
| 12 | + test.describe('visual', () => { |
| 13 | + [ |
| 14 | + ...propTests.labelPropTest, |
| 15 | + ].forEach(({ |
| 16 | + name, |
| 17 | + onBeforeTest, |
| 18 | + props, |
| 19 | + }) => { |
| 20 | + test(name, async ({ |
| 21 | + mount, |
| 22 | + page, |
| 23 | + }) => { |
| 24 | + if (onBeforeTest) { |
| 25 | + await onBeforeTest(page); |
| 26 | + } |
| 27 | + |
| 28 | + const component = await mount( |
| 29 | + <TextLinkForTest |
| 30 | + {...props} |
| 31 | + />, |
| 32 | + ); |
| 33 | + |
| 34 | + const screenshot = await component.screenshot(); |
| 35 | + expect(screenshot).toMatchSnapshot(); |
| 36 | + }); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + test.describe('non-visual', () => { |
| 41 | + test('href', async ({ mount }) => { |
| 42 | + const component = await mount( |
| 43 | + <TextLinkForTest href={testHref} />, |
| 44 | + ); |
| 45 | + |
| 46 | + await expect(component).toHaveAttribute('href', testHref); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + test.describe('functionality', () => { |
| 51 | + test('calls native redirect when clicked on', async ({ mount }) => { |
| 52 | + const component = await mount(<TextLinkForTest href={testHref} />); |
| 53 | + const page = component.page(); |
| 54 | + |
| 55 | + await Promise.all([ |
| 56 | + page.waitForURL(testHref), |
| 57 | + component.click(), |
| 58 | + ]); |
| 59 | + |
| 60 | + expect(page.url()).toContain(testHref); |
| 61 | + }); |
| 62 | + |
| 63 | + test('calls native redirect when Enter pressed', async ({ mount }) => { |
| 64 | + const component = await mount(<TextLinkForTest href={testHref} />); |
| 65 | + const page = component.page(); |
| 66 | + |
| 67 | + await Promise.all([ |
| 68 | + page.waitForURL(testHref), |
| 69 | + component.press('Enter'), |
| 70 | + ]); |
| 71 | + |
| 72 | + expect(page.url()).toContain(testHref); |
| 73 | + }); |
| 74 | + |
| 75 | + test('calls onClick when clicked', async ({ mount }) => { |
| 76 | + let clicked = false; |
| 77 | + const component = await mount( |
| 78 | + <TextLinkForTest |
| 79 | + onClick={() => { |
| 80 | + clicked = true; |
| 81 | + }} |
| 82 | + />, |
| 83 | + ); |
| 84 | + await component.click(); |
| 85 | + |
| 86 | + expect(clicked).toBeTruthy(); |
| 87 | + }); |
| 88 | + |
| 89 | + test('calls onClick when Enter pressed', async ({ mount }) => { |
| 90 | + let clicked = false; |
| 91 | + const component = await mount( |
| 92 | + <TextLinkForTest |
| 93 | + onClick={() => { |
| 94 | + clicked = true; |
| 95 | + }} |
| 96 | + />, |
| 97 | + ); |
| 98 | + await component.press('Enter'); |
| 99 | + |
| 100 | + expect(clicked).toBeTruthy(); |
| 101 | + }); |
| 102 | + }); |
| 103 | +}); |
0 commit comments