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

Tests with Jest #16

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
5 changes: 2 additions & 3 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"presets": [
"react",
"env",
"stage-0"
"@babel/preset-env",
"@babel/preset-react"
]
}
1 change: 1 addition & 0 deletions __mocks__/styleMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {}
35 changes: 23 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
},
"repository": "mohitkyadav/react-pagination-nav",
"scripts": {
"build": "webpack"
"build": "webpack",
"test": "jest"
},
"publishConfig": {
"registry": "https://registry.npmjs.org/"
Expand All @@ -24,25 +25,35 @@
"react-dom": "^16.0.0"
},
"devDependencies": {
"babel-core": "^6.21.0",
"babel-loader": "^7.1.4",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.16.0",
"babel-preset-stage-0": "^6.24.1",
"@babel/core": "^7.11.6",
"@babel/preset-env": "^7.11.5",
"@babel/preset-react": "^7.10.4",
"babel-jest": "^26.3.0",
"babel-loader": "^8.1.0",
"classnames": "^2.2.6",
"css-loader": "^3.6.0",
"jest": "^26.4.2",
"path": "^0.12.7",
"react": "^16.0.0",
"react-dom": "^16.0.0",
"webpack": "^4.5.0",
"webpack-cli": "^3.2.1",
"classnames": "^2.2.6",
"css-loader": "^3.6.0",
"react-test-renderer": "^16.13.1",
"sass": "^1.26.10",
"sass-loader": "^9.0.2",
"style-loader": "^1.2.1"
"style-loader": "^1.2.1",
"webpack": "^4.5.0",
"webpack-cli": "^3.2.1"
},
"keywords": [
"pagination",
"react pagination",
"pagination nav"
]
],
"jest": {
"transform": {
"^.+\\.jsx?$": "babel-jest"
},
"moduleNameMapper": {
"\\.(css|scss)$": "<rootDir>/__mocks__/styleMock.js"
}
}
}
48 changes: 48 additions & 0 deletions src/ReactPaginationNav.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React from 'react'
import { create, act } from 'react-test-renderer'
import ReactPaginationNav from './ReactPaginationNav'

const App = ({ page = 1 }) => {
const [currentPage, setCurrentPage] = React.useState(page)
return (
<ReactPaginationNav
pageCount={6}
visiblePages={3}
currentPage={currentPage}
goToNextPage={() => setCurrentPage(currentPage + 1)}
goToPreviousPage={() => setCurrentPage(currentPage - 1)}
goToPage={(newPage) => setCurrentPage(newPage)}
/>
)
}

const snapMatch = (component) => {
const tree = component.toJSON()
expect(tree).toMatchSnapshot()
}

test('Nav renders and switches between pages', () => {
let component
act(() => {
component = create(<App />)
})
const nextBtn = component.root.findByProps({ className: 'react-pagination-nav__next-page react-pagination-nav__button' })
const prevBtn = component.root.findByProps({ className: 'react-pagination-nav__prev-page react-pagination-nav__button' })
const otherBtn = component.root.findByProps({ children: 3 })
snapMatch(component)

act(() => {
nextBtn.props.onClick()
})
snapMatch(component)

act(() => {
prevBtn.props.onClick()
})
snapMatch(component)

act(() => {
otherBtn.props.onClick()
})
snapMatch(component)
})
181 changes: 181 additions & 0 deletions src/__snapshots__/ReactPaginationNav.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Nav renders and switches between pages 1`] = `
<div
className="react-pagination-nav react-pagination-nav--dark"
>
<button
aria-label="Go to previous page"
className="react-pagination-nav__prev-page react-pagination-nav__button"
onClick={[Function]}
title="Go to previous page"
>
&lt;
</button>
<div
className="react-pagination-nav__page-list"
>
<button
className="react-pagination-nav__page-number react-pagination-nav__button react-pagination-nav__button__active"
onClick={[Function]}
>
1
</button>
<button
className="react-pagination-nav__page-number react-pagination-nav__button "
onClick={[Function]}
>
2
</button>
<button
className="react-pagination-nav__page-number react-pagination-nav__button "
onClick={[Function]}
>
3
</button>
</div>
<button
aria-label="Go to next page"
className="react-pagination-nav__next-page react-pagination-nav__button"
onClick={[Function]}
title="Go to next page"
>
&gt;
</button>
</div>
`;

exports[`Nav renders and switches between pages 2`] = `
<div
className="react-pagination-nav react-pagination-nav--dark"
>
<button
aria-label="Go to previous page"
className="react-pagination-nav__prev-page react-pagination-nav__button"
onClick={[Function]}
title="Go to previous page"
>
&lt;
</button>
<div
className="react-pagination-nav__page-list"
>
<button
className="react-pagination-nav__page-number react-pagination-nav__button "
onClick={[Function]}
>
1
</button>
<button
className="react-pagination-nav__page-number react-pagination-nav__button react-pagination-nav__button__active"
onClick={[Function]}
>
2
</button>
<button
className="react-pagination-nav__page-number react-pagination-nav__button "
onClick={[Function]}
>
3
</button>
</div>
<button
aria-label="Go to next page"
className="react-pagination-nav__next-page react-pagination-nav__button"
onClick={[Function]}
title="Go to next page"
>
&gt;
</button>
</div>
`;

exports[`Nav renders and switches between pages 3`] = `
<div
className="react-pagination-nav react-pagination-nav--dark"
>
<button
aria-label="Go to previous page"
className="react-pagination-nav__prev-page react-pagination-nav__button"
onClick={[Function]}
title="Go to previous page"
>
&lt;
</button>
<div
className="react-pagination-nav__page-list"
>
<button
className="react-pagination-nav__page-number react-pagination-nav__button react-pagination-nav__button__active"
onClick={[Function]}
>
1
</button>
<button
className="react-pagination-nav__page-number react-pagination-nav__button "
onClick={[Function]}
>
2
</button>
<button
className="react-pagination-nav__page-number react-pagination-nav__button "
onClick={[Function]}
>
3
</button>
</div>
<button
aria-label="Go to next page"
className="react-pagination-nav__next-page react-pagination-nav__button"
onClick={[Function]}
title="Go to next page"
>
&gt;
</button>
</div>
`;

exports[`Nav renders and switches between pages 4`] = `
<div
className="react-pagination-nav react-pagination-nav--dark"
>
<button
aria-label="Go to previous page"
className="react-pagination-nav__prev-page react-pagination-nav__button"
onClick={[Function]}
title="Go to previous page"
>
&lt;
</button>
<div
className="react-pagination-nav__page-list"
>
<button
className="react-pagination-nav__page-number react-pagination-nav__button "
onClick={[Function]}
>
2
</button>
<button
className="react-pagination-nav__page-number react-pagination-nav__button react-pagination-nav__button__active"
onClick={[Function]}
>
3
</button>
<button
className="react-pagination-nav__page-number react-pagination-nav__button "
onClick={[Function]}
>
4
</button>
</div>
<button
aria-label="Go to next page"
className="react-pagination-nav__next-page react-pagination-nav__button"
onClick={[Function]}
title="Go to next page"
>
&gt;
</button>
</div>
`;