Skip to content

Instantly share code, notes, and snippets.

@arshmakker
Last active May 13, 2020 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arshmakker/4d59ebb1044384448d2f839fcfe5b14f to your computer and use it in GitHub Desktop.
Save arshmakker/4d59ebb1044384448d2f839fcfe5b14f to your computer and use it in GitHub Desktop.
Header.test.js
import React from 'react'
import Header from '../components/Header'
import renderer from 'react-test-renderer'
import { render, fireEvent, screen } from '@testing-library/react'
const classHeader = "sample class"
const SampleData = new Set(["1st Sample",
"2nd Sample",
"3rd Sample"])
// describe()/
it("Renders the Header correctly with no props ", () => {
const tree = renderer
.create(<Header />)
.toJSON();
expect(tree).toMatchSnapshot();
})
it("Renders the Header correctly with test props", () => {
const tree = renderer
.create(<Header InitialData={SampleData}
classHeader={classHeader}>
</Header>)
.toJSON();
expect(tree).toMatchSnapshot();
})
//functional testing
it('Renders the correct results after the search keyword is entered', () => {
render(<Header InitialData={SampleData}/>)
fireEvent.change(screen.getByTestId("searchText"),
{ target: { value: "2nd" } })
expect(screen.getByTestId("searchText").value).toBe("2nd")
fireEvent.click(screen.getByRole('button'))
// testing a truthy condition
expect(screen.getAllByText(/2nd/i).length).toBe(2)
// testing a falsy condition
expect(screen.getAllByText(/1st/i).length).toBe(1)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment