Skip to content

Commit

Permalink
Use css style on component testing (#2701)
Browse files Browse the repository at this point in the history
  • Loading branch information
balanza authored Jun 21, 2024
1 parent 342b5ca commit 566b856
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
24 changes: 24 additions & 0 deletions assets/js/common/Tooltip/Tooltip.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import userEvent from '@testing-library/user-event';
import '@testing-library/jest-dom';

import Tooltip from '.';
import { WithStyle } from '../../lib/test-utils/ui';

describe('Tooltip', () => {
it('should show a text when mouse is hovering', async () => {
Expand Down Expand Up @@ -84,4 +85,27 @@ describe('Tooltip', () => {
expect(screen.queryByText('This is my tooltip text')).toBeNull()
);
});

it('should hide the text when the mouse go away', async () => {
const user = userEvent.setup();

render(
<WithStyle>
<div>
<Tooltip content="This is my tooltip text">This is my anchor</Tooltip>
</div>
</WithStyle>
);

expect(screen.queryByText('This is my tooltip text')).toBeNull();

await act(async () => user.hover(screen.queryByText('This is my anchor')));
await act(async () =>
user.unhover(screen.queryByText('This is my anchor'))
);

await waitFor(() =>
expect(screen.queryByText('This is my tooltip text')).not.toBeVisible()
);
});
});
27 changes: 27 additions & 0 deletions assets/js/lib/test-utils/ui/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react';

const css = require('fs').readFileSync(
`${__dirname}/../../../../../priv/static/assets/app.css`,
'utf8'
);

/**
* Prepend the application global css in a <style> element.
* It's meant to be used in component testing when css classes must be resolved.
*
* @example
*
* render(
* <WithStyle>
* <Elem />
* </WithStyle>
* );
*/
export function WithStyle({ children }) {
return (
<>
<style>{css}</style>
{children}
</>
);
}
1 change: 1 addition & 0 deletions assets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"lint": "eslint . --ext js,jsx",
"format:check": "prettier -c .",
"format": "prettier --write .",
"pretest": "ls ../priv/static/assets/app.css || npm run tailwind:build",
"test": "jest",
"chromatic": "npx chromatic --exit-zero-on-changes"
}
Expand Down

0 comments on commit 566b856

Please sign in to comment.