Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions packages/tooltips/src/elements/Tooltip.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@

import React from 'react';
import userEvent from '@testing-library/user-event';
import { render, act, renderRtl, renderDark } from 'garden-test-utils';
import { render, act, renderRtl, getRenderFn } from 'garden-test-utils';
import { PALETTE } from '@zendeskgarden/react-theming';
import { Tooltip } from './Tooltip';
import { ITooltipProps } from '../types';

function getRenderFn(mode: 'dark' | 'light') {
return mode === 'dark' ? renderDark : render;
}

jest.useFakeTimers();

describe('Tooltip', () => {
Expand Down
39 changes: 39 additions & 0 deletions utils/test/garden-test-utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ const DarkThemeWrapper: React.FC<React.PropsWithChildren> = ({ children }) => (
</ThemeProvider>
);

const RtlDarkThemeWrapper: React.FC<React.PropsWithChildren> = ({ children }) => (
<ThemeProvider
theme={{ ...DEFAULT_THEME, rtl: true, colors: { ...DEFAULT_THEME.colors, base: 'dark' } }}
>
{children}
</ThemeProvider>
);

const customRender = (ui: React.ReactElement, options?: any) =>
render(ui, { wrapper: ThemeWrapper, ...options });

Expand All @@ -34,6 +42,37 @@ const customRtlRender = (ui: React.ReactElement, options?: any) =>
const customDarkRender = (ui: React.ReactElement, options?: any) =>
render(ui, { wrapper: DarkThemeWrapper, ...options });

const customRtlDarkRender = (ui: React.ReactElement, options?: any) =>
render(ui, { wrapper: RtlDarkThemeWrapper, ...options });

const MODE_TO_RENDER_FN_MAP = {
dark: customDarkRender,
light: customRender,
darkRtl: customRtlDarkRender,
lightRtl: customRtlRender
};
/**
* A utility fn that returns the correct render function
* for the mode. Useful when using `it.each()`.
*
* @throws Throws an error if mode is not recognized.
*
* @param {string} [mode='light'] The color mode.
* @param {boolean} [isRtl] If true, sets the direction to RTL
* @returns {Function} Render function associated with the mode.
*/
export function getRenderFn(mode = 'light', isRtl?: boolean) {
const _mode = isRtl ? `${mode}Rtl` : mode;

const renderFn = (MODE_TO_RENDER_FN_MAP as any)[_mode!];

if (!renderFn) {
throw new Error(`There is no render function for mode: ${_mode}`);
}

return renderFn;
}

export * from '@testing-library/react';
export { customRender as render };
export { customRtlRender as renderRtl };
Expand Down