Skip to content

Commit

Permalink
feat(plasma-*): add target as ref test for Popover
Browse files Browse the repository at this point in the history
  • Loading branch information
TitanKuzmich committed Dec 23, 2024
1 parent 6ebf13d commit b7f21bc
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { RefObject, useRef } from 'react';
import { mount, CypressTestDecorator, getComponent } from '@salutejs/plasma-cy-utils';

import type { PopoverTrigger } from '.';
Expand Down Expand Up @@ -107,4 +107,43 @@ describe('plasma-b2c: Popover', () => {
cy.get('body').type('{esc}');
cy.get('p').contains(text).should('not.be.visible');
});

const TargetAsRef = () => {
const [isOpen, setIsOpen] = React.useState(false);
const targetRef = useRef<HTMLButtonElement>(null);

return (
<>
<Button ref={targetRef}>Target</Button>
<Popover
opened={isOpen}
onToggle={(is) => setIsOpen(is)}
role="presentation"
id="popover"
target={targetRef}
trigger="click"
closeOnEsc
>
<div>
<P1>{text}</P1>
<Button onClick={() => setIsOpen(!isOpen)}>close</Button>
</div>
</Popover>
</>
);
};

it('target as ref', () => {
mount(
<CypressTestDecorator>
<TargetAsRef />
</CypressTestDecorator>,
);

cy.get('button').first().click();
cy.get('p').contains(text).should('be.visible');

cy.get('body').type('{esc}');
cy.get('p').contains(text).should('not.be.visible');
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useRef } from 'react';
import { mount, CypressTestDecorator, getComponent } from '@salutejs/plasma-cy-utils';

import type { PopoverTrigger } from '.';
Expand Down Expand Up @@ -107,4 +107,43 @@ describe('plasma-web: Popover', () => {
cy.get('body').type('{esc}');
cy.get('p').contains(text).should('not.be.visible');
});

const TargetAsRef = () => {
const [isOpen, setIsOpen] = React.useState(false);
const targetRef = useRef<HTMLButtonElement>(null);

return (
<>
<Button ref={targetRef}>Target</Button>
<Popover
opened={isOpen}
onToggle={(is) => setIsOpen(is)}
role="presentation"
id="popover"
target={targetRef}
trigger="click"
closeOnEsc
>
<div>
<P1>{text}</P1>
<Button onClick={() => setIsOpen(!isOpen)}>close</Button>
</div>
</Popover>
</>
);
};

it('target as ref', () => {
mount(
<CypressTestDecorator>
<TargetAsRef />
</CypressTestDecorator>,
);

cy.get('button').first().click();
cy.get('p').contains(text).should('be.visible');

cy.get('body').type('{esc}');
cy.get('p').contains(text).should('not.be.visible');
});
});

0 comments on commit b7f21bc

Please sign in to comment.