I have a User component defined for a Reddit app like so:
// User.js
import React from "react";
import { useSelector } from "react-redux";
import { selectUsername, selectAuthState } from "./userSlice";
import Reddit from "../../api/Reddit";
const User = () => {
const userID = useSelector(selectUsername);
const authState = useSelector(selectAuthState);
const { isLoading } = useSelector(state => state.user)
const handleAuth = (e) => {
e.preventDefault();
Reddit.getAccessToken();
}
if (isLoading) {
return (
<svg className="spinner" viewBox="0 0 50 50">
<circle class="path" cx="25" cy="25" r="20" fill="none" stroke-width="5"></circle>
</svg>
)
} else if (authState) {
return (
<a id="reddit-username" target="_blank" rel="noreferrer" href={`https://www.reddit.com/user/${userID}`} aria-label="Current user ID">{userID}</a>
)
} else {
return (
<a id="reddit-username" href="/" onClick={handleAuth} aria-label="Connect the web app with your Reddit account">Link with Reddit</a>
)
}
}
export default User;
So far, the test file for the component is laid out like so:
// User.spec.js
import React from 'react';
import { render } from '@testing-library/react';
import { Provider } from 'react-redux';
import { store } from '../../app/store';
import User from "../../features/User/User";
test("renders 'Link with Reddit' link by default", () => {
const { getByText } = render(
<Provider store={store}>
<User />
</Provider>
);
expect(getByText("Link with Reddit")).toBeInTheDocument();
});
I'm trying to write a test for rendering the SVG element while the component isLoading, but I can't figure out how as I'm still new to testing in React-Redux. How should I go about getting that done?
data-testidattribute on it and query for that usinggetByTestId