Skip to content

Testing Resource Component #60

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^11.2.7",
"@testing-library/user-event": "^12.8.3",
<<<<<<< HEAD
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you had a merge conflict here which inserts these lines. You will need to remove them - lines 9, 11 and 13. I believe this pull request build is failing because of it.

"axios": "^0.24.0",
=======
"d3": "^7.1.1",
>>>>>>> 181ae805f4b2c4c9bf7121743c7973150befd1dd
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router-dom": "^5.3.0",
Expand Down
13 changes: 7 additions & 6 deletions src/components/Resources/Resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ return (
{
ResourcesList.map(resource =>
<article key={resource.id}>
<h1>{resource.name}</h1>
<p className="text">{resource.body}
<h1 data-testid="name">{resource.name}</h1>
<p data-testid="body" className="text">{resource.body}
</p>
<Link className="resourceLink" to={`/resources/${resource.id}`}><span>More info</span></Link>
<Link data-testid="link" className="resourceLink" to={`/resources/${resource.id}`}><span>More info</span></Link>

</article>)}
</div>
</article>)
}
</div>
<Disclaimer />
</div>
</div>
);
}
62 changes: 62 additions & 0 deletions src/components/Resources/Resources.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { render, screen, fireEvent } from "@testing-library/react";
import { MemoryRouter, Link } from 'react-router-dom';
import { createMemoryHistory } from 'history';
import SingleResource from '../SingleResource/SingleResource';
import Resources from "./Resources";
import Toggle from "../Toggle";
import axios from "axios";

// test.afterEach(cleanup)

describe("Resources Component", () => {
it('rendered toggle', () => {
const { getByTestId } = render(<Toggle />);
const toggle = getByTestId("toggle");
expect(toggle).toBeTruthy();
});
})

test("resource name", () => {
render(<div data-testid="name" />);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Little confused why you are rendering a div in this test. I would think you would need to render the Resource component itself?

const resourceName = screen.getByTestId("name");
expect(resourceName).toBeInTheDocument();
});
// screen.debug();

test("resource body", () => {
render(<div data-testid="body" />);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question here. I would think you would have to render the Resource component first and then just have the lines to get the resourceBody here and verify it is there.

const resourceBody = screen.getByTestId("body");
expect(resourceBody).toBeInTheDocument();
});
// test("link working", () => {
// const history = createMemoryHistory();
// history.push = jest.fn();


jest.mock('axios');

it('returns the first resource', async () => {
axios.get.mockResolvedValue({
data: [
{
userId: 1,
id: 1,
title: 'My First Album'
}
]
});

const resource = await SingleResource();
expect(resource).toHaveBeenCalledTimes('/resources/1');
});


// const { getByText } = render(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove all the commented out code from here?

// <MemoryRouter history={history}>
// <Link to={`/resources/1`}>More info</Link>
// </MemoryRouter>
// );

// fireEvent.click(getByText('More info'));
// expect(history.push).toHaveBeenCalledWith(`resources/1`);
// });
2 changes: 1 addition & 1 deletion src/components/Toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import './Toggle.css';
export default function Toggle(){
const history = useHistory();
return (
<div className="flex">
<div className="flex" data-testid="toggle">
<h1>Open Source Programs</h1>
<div className="flex-container">
<button onClick={() => history.push('/TableView')}><img alt ="table" src={table}></img></button>
Expand Down
Loading