Skip to content

Commit

Permalink
test: follow mike request - replace data-testid for querySelector by …
Browse files Browse the repository at this point in the history
…class on story_search_spec.js file
  • Loading branch information
wrspada02 committed Sep 30, 2024
1 parent ccaac75 commit b6adec2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
6 changes: 1 addition & 5 deletions app/assets/javascripts/components/search/StorySearch.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ export const StorySearch = ({ projectId, search, loading }) => {
className="StorySearch"
>
{loading && (
<span
className="StorySearch__spinner"
data-id="spinner-loading"
data-testid="story-search-spinner"
>
<span className="StorySearch__spinner" data-id="spinner-loading">
<i className="fa fa-spinner fa-spin"></i>
</span>
)}
Expand Down
35 changes: 22 additions & 13 deletions spec/javascripts/components/search/story_search_spec.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
import React from 'react';
import { render } from '@testing-library/react';
import ReactDOM from 'react-dom';
import { render, screen } from '@testing-library/react';
import { StorySearch } from 'components/search/StorySearch';

describe('<StorySearch />', () => {
beforeAll(() => {
ReactDOM.createPortal = vi.fn(element => {
return element;
});
});

beforeEach(() => {
const divPortal = global.document.createElement('div');
const divPortal = document.createElement('div');
divPortal.setAttribute('data-portal', 'search');
const body = global.document.querySelector('body');
const body = document.querySelector('body');
body.appendChild(divPortal);
});

it('renders the component', () => {
const { container } = render(
<StorySearch projectId={1} search={vi.fn()} loading={false} />
);

expect(container).toBeInTheDocument();
});

describe('loading', () => {
describe('when loading is true', () => {
it('renders the spinner', () => {
const { getByTestId } = render(
const { container, debug } = render(
<StorySearch projectId={1} search={vi.fn()} loading={true} />
);

expect(getByTestId('story-search-spinner')).toBeInTheDocument();
expect(
container.querySelector('.StorySearch__spinner')
).toBeInTheDocument();
});
});
});

it('renders the component', () => {
const { container } = render(
<StorySearch projectId={1} search={vi.fn()} loading={false} />
);

expect(container).toBeInTheDocument();
});
});

0 comments on commit b6adec2

Please sign in to comment.