Skip to content

Commit

Permalink
Test(web-react): Add test for hook useFloatingUI
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelklibani committed Jan 22, 2024
1 parent c017828 commit 93236de
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,36 +88,3 @@ describe('TooltipModern', () => {
expect(onToggle).toHaveBeenCalled();
});
});

describe('TooltipTrigger', () => {
stylePropsTest((props) => <TooltipTrigger {...props} data-testid="TooltipTrigger-test" />, 'TooltipTrigger-test');

restPropsTest((props) => <TooltipTrigger elementType={Button} {...props} />, 'button');

it('should render tooltip trigger', () => {
const id = 'TooltipTriggerTest';
const triggerText = 'TooltipTrigger';

const dom = render(<TooltipTrigger data-spirit-testid={id}>{triggerText}</TooltipTrigger>);
const element = dom.container.querySelector(`[data-spirit-testid="${id}"]`) as HTMLElement;

expect(element.textContent).toBe(triggerText);
});
});

describe('TooltipPopover', () => {
classNamePrefixProviderTest(TooltipPopover, 'Tooltip');

stylePropsTest((props) => <TooltipPopover {...props} data-testid="TooltipPopover-test" />, 'TooltipPopover-test');

restPropsTest((props) => <TooltipPopover {...props} />, 'div');

it('should render tooltip popover', () => {
const popoverText = 'TooltipPopover';

const dom = render(<TooltipPopover>{popoverText}</TooltipPopover>);
const element = dom.container.querySelector('.Tooltip') as HTMLElement;

expect(element.textContent).toBe(popoverText);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import '@testing-library/jest-dom';
import React from 'react';
import { render } from '@testing-library/react';
import { classNamePrefixProviderTest } from '../../../../tests/providerTests/classNamePrefixProviderTest';
import { stylePropsTest } from '../../../../tests/providerTests/stylePropsTest';
import { restPropsTest } from '../../../../tests/providerTests/restPropsTest';
import { TooltipPopover } from '..';

describe('TooltipPopover', () => {
classNamePrefixProviderTest(TooltipPopover, 'Tooltip');

stylePropsTest((props) => <TooltipPopover {...props} data-testid="TooltipPopover-test" />, 'TooltipPopover-test');

restPropsTest((props) => <TooltipPopover {...props} />, 'div');

it('should render tooltip popover', () => {
const popoverText = 'TooltipPopover';

const dom = render(<TooltipPopover>{popoverText}</TooltipPopover>);
const element = dom.container.querySelector('.Tooltip') as HTMLElement;

expect(element.textContent).toBe(popoverText);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import '@testing-library/jest-dom';
import React from 'react';
import { render } from '@testing-library/react';
import { stylePropsTest } from '../../../../tests/providerTests/stylePropsTest';
import { restPropsTest } from '../../../../tests/providerTests/restPropsTest';
import { Button } from '../../Button';
import { TooltipTrigger } from '..';

describe('TooltipTrigger', () => {
stylePropsTest((props) => <TooltipTrigger {...props} data-testid="TooltipTrigger-test" />, 'TooltipTrigger-test');

restPropsTest((props) => <TooltipTrigger elementType={Button} {...props} />, 'button');

it('should render tooltip trigger', () => {
const id = 'TooltipTriggerTest';
const triggerText = 'TooltipTrigger';

const dom = render(<TooltipTrigger data-spirit-testid={id}>{triggerText}</TooltipTrigger>);
const element = dom.container.querySelector(`[data-spirit-testid="${id}"]`) as HTMLElement;

expect(element.textContent).toBe(triggerText);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { renderHook } from '@testing-library/react-hooks';
import { useFloatingUI } from '../useFloatingUI';

describe('useFloatingUI', () => {
it('should initialize with default values', () => {
const { result } = renderHook(() =>
useFloatingUI({
arrowRef: { current: null },
flipCrossAxis: false,
flipFallbackAxisSideDirection: 'none',
flipFallbackPlacements: 'bottom',
flipProp: false,
isOpen: false,
onToggle: jest.fn(),
shiftProp: false,
sizeProp: false,
tooltipPlacement: undefined,
}),
);

expect(result.current.context).toBeDefined();
expect(result.current.getFloatingProps).toBeDefined();
expect(result.current.getReferenceProps).toBeDefined();
expect(result.current.middlewareData).toBeDefined();
expect(result.current.placement).toBeDefined();
expect(result.current.refs).toBeDefined();
expect(result.current.x).toBeDefined();
expect(result.current.y).toBeDefined();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { renderHook } from '@testing-library/react-hooks';
import { PlacementDictionaryType } from '../../../types';
import { useTooltipModernStyleProps, UseTooltipModernStyleProps } from '../useTooltipModernStyleProps';

describe('useTooltipModernStyleProps', () => {
it('should return defaults', () => {
const { result } = renderHook(() => useTooltipModernStyleProps({}));

expect(result.current.classProps.rootClassName).toBe('Tooltip Tooltip--bottom');
expect(result.current.classProps.wrapperClassName).toBe('TooltipWrapper');
expect(result.current.classProps.arrowClassName).toBe('Tooltip__arrow');
expect(result.current.classProps.closeButtonClassName).toBe('Tooltip__close');
});

it('should change placement', () => {
const props = {
placement: 'top-right' as PlacementDictionaryType,
} as UseTooltipModernStyleProps;
const { result } = renderHook(() => useTooltipModernStyleProps(props));

expect(result.current.classProps.rootClassName).toBe('Tooltip Tooltip--topRight');
});

it('should return dismissible class', () => {
const { result } = renderHook(() => useTooltipModernStyleProps({ isDismissible: true }));

expect(result.current.classProps.rootClassName).toBe('Tooltip Tooltip--bottom Tooltip--dismissible');
});
});

0 comments on commit 93236de

Please sign in to comment.