Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use css style on component testing #2701

Merged
merged 3 commits into from
Jun 21, 2024
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
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
Loading