-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Section and pagination components (#186)
* Add the new Section component from Admin * Add the new Pagination component from Admin * Add Jest support and test Pagination * Export both components * Add Github action for tests * Fix review comments * Update pagination
- Loading branch information
Showing
14 changed files
with
2,572 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,6 @@ jobs: | |
- name: Linting | ||
run: yarn lint | ||
|
||
- name: Test | ||
run: yarn test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module.exports = api => { | ||
const isTestEnv = api.env('test'); | ||
|
||
return { | ||
exclude: 'node_modules/**', | ||
plugins: [ | ||
'@babel/plugin-proposal-nullish-coalescing-operator', | ||
[ | ||
'babel-plugin-styled-components', | ||
{ | ||
pure: true, | ||
}, | ||
], | ||
], | ||
presets: [ | ||
isTestEnv | ||
? [ | ||
'@babel/preset-env', | ||
{ | ||
targets: { | ||
node: 'current', | ||
}, | ||
}, | ||
] | ||
: [ | ||
'@babel/preset-env', | ||
{ | ||
modules: false, | ||
}, | ||
], | ||
'@babel/preset-react', | ||
], | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// For a detailed explanation regarding each configuration property, visit: | ||
// https://jestjs.io/docs/en/configuration.html | ||
|
||
module.exports = { | ||
// Automatically clear mock calls and instances between every test | ||
clearMocks: true, | ||
|
||
// Automatically reset mock state between every test | ||
resetMocks: true, | ||
|
||
// We only want to test JS components and not other stuff | ||
// Like webpack's test.js | ||
roots: ['./src/components'], | ||
moduleNameMapper: { | ||
'\\.(css)$': '<rootDir>/src/__mocks__/styleMock.js', | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// eslint-disable-next-line import/no-default-export | ||
/* | ||
Mocks styles files imported in Jest | ||
See jest config > moduleNameMapper | ||
*/ | ||
module.exports = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import Pagination from '../index'; | ||
|
||
describe('Pagination', () => { | ||
it('displays the prev/next buttons', () => { | ||
const { getByTestId } = render( | ||
<Pagination goNext={() => {}} goPrev={() => {}} />, | ||
); | ||
|
||
expect(getByTestId('pagination-prev')).toBeDefined(); | ||
expect(getByTestId('pagination-next')).toBeDefined(); | ||
}); | ||
|
||
it("Doesn't render if both are disabled", () => { | ||
const { queryByTestId } = render( | ||
<Pagination | ||
goNext={() => {}} | ||
goPrev={() => {}} | ||
nextDisabled | ||
prevDisabled | ||
/>, | ||
); | ||
|
||
expect(queryByTestId('pagination-prev')).toBeNull(); | ||
expect(queryByTestId('pagination-next')).toBeNull(); | ||
}); | ||
|
||
it('triggers the goNext/goPrev props on click', () => { | ||
const goNext = jest.fn(); | ||
const goPrev = jest.fn(); | ||
|
||
const { getByTestId } = render( | ||
<Pagination goNext={goNext} goPrev={goPrev} />, | ||
); | ||
|
||
userEvent.click(getByTestId('pagination-prev')); | ||
expect(goPrev).toHaveBeenCalled(); | ||
|
||
userEvent.click(getByTestId('pagination-next')); | ||
expect(goNext).toHaveBeenCalled(); | ||
}); | ||
|
||
it('The next arrow shows as disabled correctly', () => { | ||
const { queryByTestId } = render( | ||
<Pagination goNext={() => {}} goPrev={() => {}} nextDisabled />, | ||
); | ||
|
||
expect( | ||
queryByTestId('pagination-next').hasAttribute('disabled'), | ||
).toBeTruthy(); | ||
}); | ||
|
||
it('The prev arrow shows as disabled correctly', () => { | ||
const { queryByTestId } = render( | ||
<Pagination goNext={() => {}} goPrev={() => {}} prevDisabled />, | ||
); | ||
|
||
expect( | ||
queryByTestId('pagination-prev').hasAttribute('disabled'), | ||
).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import styled from 'styled-components'; | ||
|
||
import IconButton from '../IconButton'; | ||
import { theme } from '../../theme'; | ||
|
||
const texts = { | ||
next: { | ||
id: 'flamingo.pagination.next', | ||
defaultText: 'Next', | ||
}, | ||
previous: { | ||
id: 'flamingo.pagination.previous', | ||
defaultText: 'Previous', | ||
}, | ||
disabled: { | ||
id: 'flamingo.pagination.disabled', | ||
defaultText: 'Disabled', | ||
}, | ||
}; | ||
|
||
const Pagination = ({ | ||
className, | ||
position, | ||
prevDisabled, | ||
nextDisabled, | ||
translate, | ||
goPrev, | ||
goNext, | ||
}) => { | ||
// Don't show it if both are disabled | ||
if (prevDisabled && nextDisabled) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Container className={className} position={position}> | ||
<ArrowIcon | ||
data-testid={'pagination-prev'} | ||
icon={'IconArrowLeft'} | ||
onClick={goPrev} | ||
disabled={prevDisabled} | ||
title={`${translate(texts.previous)} ${ | ||
prevDisabled ? `- ${translate(texts.disabled)}` : '' | ||
}`} | ||
/> | ||
|
||
<ArrowIcon | ||
data-testid={'pagination-next'} | ||
icon={'IconArrowRight'} | ||
onClick={goNext} | ||
disabled={nextDisabled} | ||
title={`${translate(texts.next)} ${ | ||
nextDisabled ? `- ${translate(texts.disabled)}` : '' | ||
}`} | ||
/> | ||
</Container> | ||
); | ||
}; | ||
|
||
Pagination.displayName = 'Pagination'; | ||
|
||
Pagination.propTypes = { | ||
goPrev: PropTypes.func.isRequired, | ||
prevDisabled: PropTypes.bool, | ||
goNext: PropTypes.func.isRequired, | ||
nextDisabled: PropTypes.bool, | ||
className: PropTypes.string, | ||
translate: PropTypes.func, | ||
position: PropTypes.string, | ||
}; | ||
|
||
Pagination.defaultProps = { | ||
translate: ({ defaultText }) => defaultText, | ||
}; | ||
|
||
const Container = styled('div')` | ||
display: flex; | ||
justify-content: ${({ position }) => position || 'flex-end'}; | ||
padding: 20px 0 10px; | ||
`; | ||
|
||
const ArrowIcon = styled(IconButton)` | ||
i { | ||
width: 16px; | ||
height: 16px; | ||
svg { | ||
fill: ${({ disabled }) => | ||
disabled ? theme.color.element.inactive : theme.color.text.link}; | ||
width: 16px; | ||
} | ||
} | ||
`; | ||
|
||
export default Pagination; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* eslint-disable no-return-assign */ | ||
|
||
import React, { useState } from 'react'; | ||
import { storiesOf } from '@storybook/react'; | ||
import { select } from '@storybook/addon-knobs'; | ||
|
||
import Heading from '../Heading'; | ||
import Pagination from './index'; | ||
|
||
const stories = storiesOf('Pagination', module); | ||
|
||
stories.add('Playground', () => { | ||
const [page, setPage] = useState(0); | ||
return ( | ||
<> | ||
<Heading>Pagination</Heading> | ||
<Heading level={2}>Page #{page}</Heading> | ||
|
||
<Pagination | ||
prevDisabled={page === 0} | ||
nextDisabled={page === 3} | ||
goNext={() => setPage(p => p + 1)} | ||
goPrev={() => setPage(p => p - 1)} | ||
position={select('Position', ['flex-end', 'flex-start', 'center'])} | ||
/> | ||
</> | ||
); | ||
}); |
Oops, something went wrong.
656c69d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: