|
| 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 { |
| 8 | + PopoverForRefTest, |
| 9 | + PopoverForTest, |
| 10 | + PopoverWithTargetIdForTest, |
| 11 | +} from './Popover.story'; |
| 12 | +import { placementPropTest } from './_propTests/placementPropTest'; |
| 13 | +import { popoverTargetIdPropTest } from './_propTests/popoverTargetIdPropTest'; |
| 14 | + |
| 15 | +test.describe('Popover', () => { |
| 16 | + test.describe('visual', () => { |
| 17 | + [ |
| 18 | + ...propTests.defaultComponentPropTest, |
| 19 | + ...placementPropTest, |
| 20 | + ...popoverTargetIdPropTest, |
| 21 | + ].forEach(({ |
| 22 | + name, |
| 23 | + onBeforeTest, |
| 24 | + onBeforeSnapshot, |
| 25 | + props, |
| 26 | + }) => { |
| 27 | + test(name, async ({ |
| 28 | + mount, |
| 29 | + page, |
| 30 | + }) => { |
| 31 | + if (onBeforeTest) { |
| 32 | + await onBeforeTest(page); |
| 33 | + } |
| 34 | + |
| 35 | + const hasTargetId = props?.popoverTargetId !== undefined; |
| 36 | + |
| 37 | + const component = hasTargetId |
| 38 | + ? await mount( |
| 39 | + <PopoverWithTargetIdForTest |
| 40 | + {...props} |
| 41 | + />, |
| 42 | + ) |
| 43 | + : await mount( |
| 44 | + <PopoverForTest |
| 45 | + {...props} |
| 46 | + />, |
| 47 | + ); |
| 48 | + |
| 49 | + if (onBeforeSnapshot) { |
| 50 | + await onBeforeSnapshot(page, component); |
| 51 | + } |
| 52 | + |
| 53 | + const screenshot = await component.screenshot(); |
| 54 | + expect(screenshot).toMatchSnapshot(); |
| 55 | + }); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + test.describe('non-visual', () => { |
| 60 | + test('id', async ({ mount }) => { |
| 61 | + const id = 'custom-id'; |
| 62 | + const childrenText = 'Some text'; |
| 63 | + |
| 64 | + const component = await mount( |
| 65 | + <PopoverForTest id={id}> |
| 66 | + {childrenText} |
| 67 | + </PopoverForTest>, |
| 68 | + ); |
| 69 | + |
| 70 | + await expect(component.getByText(childrenText)).toHaveAttribute('id', id); |
| 71 | + }); |
| 72 | + |
| 73 | + test('ref', async ({ mount }) => { |
| 74 | + const childrenText = 'Some text'; |
| 75 | + |
| 76 | + const component = await mount( |
| 77 | + <PopoverForRefTest |
| 78 | + testRefAttrName="test-ref" |
| 79 | + testRefAttrValue="test-ref-value" |
| 80 | + > |
| 81 | + {childrenText} |
| 82 | + </PopoverForRefTest>, |
| 83 | + ); |
| 84 | + |
| 85 | + await expect(component.getByText(childrenText)).toHaveAttribute('test-ref', 'test-ref-value'); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + test.describe('functionality', () => { |
| 90 | + test('render in portal when portalId defined', async ({ |
| 91 | + mount, |
| 92 | + page, |
| 93 | + }) => { |
| 94 | + const portalId = 'portal-id'; |
| 95 | + const portalContent = 'portal content'; |
| 96 | + |
| 97 | + await page.evaluate(() => { |
| 98 | + document.body.innerHTML += '<div id="portal-id" />'; |
| 99 | + }); |
| 100 | + |
| 101 | + await mount( |
| 102 | + <PopoverForTest portalId={portalId}> |
| 103 | + {portalContent} |
| 104 | + </PopoverForTest>, |
| 105 | + ); |
| 106 | + |
| 107 | + const portalHTMLContent = await page |
| 108 | + .evaluate((id) => document.getElementById(id).innerHTML, portalId); |
| 109 | + |
| 110 | + expect(portalHTMLContent).toContain(portalContent); |
| 111 | + }); |
| 112 | + |
| 113 | + test('pass placementStyle into style property', async ({ mount }) => { |
| 114 | + const content = 'content'; |
| 115 | + const insetPlacementStyle = { |
| 116 | + inset: '10px', |
| 117 | + 'inset-block-end': 'auto', |
| 118 | + 'inset-block-start': 'auto', |
| 119 | + 'inset-inline-end': 'auto', |
| 120 | + 'inset-inline-start': 'auto', |
| 121 | + position: 'absolute', |
| 122 | + 'transform-origin': 'center', |
| 123 | + translate: '10px', |
| 124 | + }; |
| 125 | + |
| 126 | + const positionStyleObject1 = { |
| 127 | + left: '10px', |
| 128 | + top: '10px', |
| 129 | + }; |
| 130 | + |
| 131 | + const positionStyleObject2 = { |
| 132 | + bottom: '10px', |
| 133 | + right: '10px', |
| 134 | + }; |
| 135 | + |
| 136 | + const insetStyleComponent = await mount( |
| 137 | + <PopoverForTest placementStyle={insetPlacementStyle}> |
| 138 | + {content} |
| 139 | + </PopoverForTest>, |
| 140 | + ); |
| 141 | + |
| 142 | + const insetPopover = insetStyleComponent.getByText(content); |
| 143 | + const insetStyle = await insetPopover.getAttribute('style'); |
| 144 | + |
| 145 | + expect(insetStyle).toContain('position: absolute;'); |
| 146 | + expect(insetStyle).toContain('inset: 10px;'); |
| 147 | + expect(insetStyle).toContain('inset-inline: auto;'); |
| 148 | + expect(insetStyle).toContain('translate: 10px;'); |
| 149 | + expect(insetStyle).toContain('transform-origin: center center;'); |
| 150 | + |
| 151 | + await insetStyleComponent.unmount(); |
| 152 | + |
| 153 | + const positionComponent1 = await mount( |
| 154 | + <PopoverForTest placementStyle={positionStyleObject1}> |
| 155 | + {content} |
| 156 | + </PopoverForTest>, |
| 157 | + ); |
| 158 | + |
| 159 | + const positionPopover1 = positionComponent1.getByText(content); |
| 160 | + const positionStyle1 = await positionPopover1.getAttribute('style'); |
| 161 | + |
| 162 | + expect(positionStyle1).toContain('top: 10px;'); |
| 163 | + expect(positionStyle1).toContain('left: 10px;'); |
| 164 | + |
| 165 | + await positionComponent1.unmount(); |
| 166 | + |
| 167 | + const positionComponent2 = await mount( |
| 168 | + <PopoverForTest placementStyle={positionStyleObject2}> |
| 169 | + {content} |
| 170 | + </PopoverForTest>, |
| 171 | + ); |
| 172 | + |
| 173 | + const positionPopover2 = positionComponent2.getByText(content); |
| 174 | + const positionStyle2 = await positionPopover2.getAttribute('style'); |
| 175 | + |
| 176 | + expect(positionStyle2).toContain('right: 10px;'); |
| 177 | + expect(positionStyle2).toContain('bottom: 10px;'); |
| 178 | + }); |
| 179 | + }); |
| 180 | +}); |
0 commit comments