-
Notifications
You must be signed in to change notification settings - Fork 53
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
Write tests with jest (Coverage 100%) #19
base: master
Are you sure you want to change the base?
Changes from all commits
095f0f5
798e729
938a3f8
69e04b8
4723878
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all options in this file are redundant with Maybe you can remove those and only keep There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have to say I have no clue anymore, from which of my older projects I copied the prettierrc ; )... |
||
endOfLine: "lf", | ||
semi: true, | ||
singleQuote: true, | ||
tabWidth: 2, | ||
trailingComma: "es5", | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
|
||
Mohammad Rajabifard <hi@morajabi.me> | ||
+Behnam Rajabifard <b.rajabifard@gmail.com> | ||
+Tim Hagn <mail@timhagn.com> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`generatedDefaultMedia() expect <Box /> to have matching media style rules 1`] = ` | ||
.c0 { | ||
background: black; | ||
} | ||
|
||
@media (max-width:768px) { | ||
.c0 { | ||
background: red; | ||
} | ||
} | ||
|
||
@media (min-width:768px) and (max-width:1170px) { | ||
.c0 { | ||
background: green; | ||
} | ||
} | ||
|
||
@media (min-width:1170px) { | ||
.c0 { | ||
background: blue; | ||
} | ||
} | ||
|
||
@media (max-width:1170px) { | ||
.c0 { | ||
background: white; | ||
} | ||
} | ||
|
||
@media (min-width:450px) { | ||
.c0 { | ||
background: yellow; | ||
} | ||
} | ||
|
||
<div | ||
className="c0" | ||
/> | ||
`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { defaultBreakpoints } from '../src/index'; | ||
import { pxToEm, pxToRem } from '../src/convertors'; | ||
|
||
export const itBreakPoints = { | ||
huge: '1440', | ||
large: '1170', | ||
medium: '768', | ||
small: '450', | ||
}; | ||
|
||
describe(`pxToEm()`, () => { | ||
it('pxToEm to equal defaultBreakpoints converted to em', () => { | ||
expect(pxToEm(defaultBreakpoints, 16)).toEqual({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: In this file, all those There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I once was a fan of snapshot testing, but wouldn't recommend it in this case - especially when thinking about CI/CD. |
||
huge: '90em', | ||
large: '73.125em', | ||
medium: '48em', | ||
small: '28.125em', | ||
}); | ||
}); | ||
|
||
it('pxToEm without ratio to equal defaultBreakpoints converted to em', () => { | ||
expect(pxToEm(defaultBreakpoints)).toEqual({ | ||
huge: '90em', | ||
large: '73.125em', | ||
medium: '48em', | ||
small: '28.125em', | ||
}); | ||
}); | ||
|
||
it('pxToEm itBreakPoints to equal itBreakPoints', () => { | ||
expect(pxToEm(itBreakPoints, 16)).toEqual(itBreakPoints); | ||
}); | ||
|
||
it('pxToEm with integer to match empty Object', () => { | ||
expect(pxToRem(16, 16)).toMatchObject({}); | ||
}); | ||
}); | ||
|
||
describe(`pxToEm()`, () => { | ||
it('pxToRem to equal defaultBreakpoints converted to rem', () => { | ||
expect(pxToRem(defaultBreakpoints, 16)).toEqual({ | ||
huge: '90rem', | ||
large: '73.125rem', | ||
medium: '48rem', | ||
small: '28.125rem', | ||
}); | ||
}); | ||
|
||
it('pxToRem without ratio to equal defaultBreakpoints converted to rem', () => { | ||
expect(pxToRem(defaultBreakpoints)).toEqual({ | ||
huge: '90rem', | ||
large: '73.125rem', | ||
medium: '48rem', | ||
small: '28.125rem', | ||
}); | ||
}); | ||
|
||
it('pxToRem itBreakPoints to equal itBreakPoints', () => { | ||
expect(pxToRem(itBreakPoints, 16)).toEqual(itBreakPoints); | ||
}); | ||
|
||
it('pxToRem {} to equal {}', () => { | ||
expect(pxToRem({}, 16)).toEqual({}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
import 'jest-styled-components'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this test suit is testing more than it should be. In a nutshell, creating react components and check if the styles were applied to them is a task for styled component itself, since we're using a method from them. The library:
In that sense, it might be a cleaner implementation:
What do you think about this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds like more work than I can invest at the moment ; ). Gonna try looking into it, soon, anyhow. |
||
import { generateMedia, defaultBreakpoints } from '../src/index'; | ||
import React from 'react'; | ||
import renderer from 'react-test-renderer'; | ||
import styled from 'styled-components'; | ||
import { css } from 'styled-components'; | ||
|
||
const generatedDefaultMedia = generateMedia(defaultBreakpoints); | ||
|
||
const Box = styled.div` | ||
background: black; | ||
|
||
${generatedDefaultMedia.lessThan('medium')` | ||
background: red; | ||
`} | ||
|
||
${generatedDefaultMedia.between('medium', 'large')` | ||
/* screen width is between 768px (medium) and 1170px (large) */ | ||
background: green; | ||
`} | ||
|
||
${generatedDefaultMedia.greaterThan('large')` | ||
/* screen width is greater than 1170px (large) */ | ||
background: blue; | ||
`} | ||
|
||
${generatedDefaultMedia.to.large` | ||
background: white; | ||
`} | ||
|
||
${generatedDefaultMedia.from.small` | ||
background: yellow; | ||
`} | ||
`; | ||
|
||
describe(`generatedDefaultMedia()`, () => { | ||
it('expect <Box /> to have matching media style rules', () => { | ||
let tree = renderer.create(<Box/>).toJSON(); | ||
expect(tree).toMatchSnapshot(); | ||
expect(tree).toHaveStyleRule('background', 'red', { | ||
media: '(max-width:768px)', | ||
}); | ||
expect(tree).toHaveStyleRule('background', 'green', { | ||
media: '(min-width:768px) and (max-width:1170px)', | ||
}); | ||
expect(tree).toHaveStyleRule('background', 'blue', { | ||
media: '(min-width:1170px)', | ||
}); | ||
expect(tree).toHaveStyleRule('background', 'white', { | ||
media: '(max-width:1170px)', | ||
}); | ||
expect(tree).toHaveStyleRule('background', 'yellow', { | ||
media: '(min-width:450px)', | ||
}); | ||
}); | ||
}); | ||
|
||
describe(`lessThan()`, () => { | ||
it('expect lessThan("medium") to create Array', () => { | ||
let tree = renderer | ||
.create( | ||
css` | ||
${generatedDefaultMedia.lessThan('medium')` | ||
background: red | ||
`} | ||
` | ||
) | ||
.toJSON(); | ||
let itArray = [ | ||
'\n @media (max-width: ', | ||
'768px', | ||
') {\n ', | ||
'\n background: red\n ', | ||
'\n }\n ', | ||
]; | ||
expect(tree).toEqual(expect.arrayContaining(itArray)); | ||
}); | ||
}); | ||
|
||
describe(`between()`, () => { | ||
it('expect between("medium", "large") to create Array', () => { | ||
let tree = renderer | ||
.create( | ||
css` | ||
${generatedDefaultMedia.between('medium', 'large')` | ||
background: green; | ||
`} | ||
` | ||
) | ||
.toJSON(); | ||
let itArray = [ | ||
'\n @media (min-width: ', | ||
'768px', | ||
') and\n (max-width: ', | ||
'1170px', | ||
') {\n ', | ||
'\n background: green;\n ', | ||
'\n }\n ', | ||
]; | ||
expect(tree).toEqual(expect.arrayContaining(itArray)); | ||
}); | ||
}); | ||
|
||
describe(`greaterThan()`, () => { | ||
it('expect greaterThan("large") to create Array', () => { | ||
let tree = renderer | ||
.create( | ||
css` | ||
${generatedDefaultMedia.greaterThan('large')` | ||
background: blue; | ||
`} | ||
` | ||
) | ||
.toJSON(); | ||
let itArray = [ | ||
'\n @media (min-width: ', | ||
'1170px', | ||
') {\n ', | ||
'\n background: blue;\n ', | ||
'\n }\n ', | ||
]; | ||
expect(tree).toEqual(expect.arrayContaining(itArray)); | ||
}); | ||
|
||
it('expect greaterThan(320) to create Array', () => { | ||
let tree = renderer | ||
.create( | ||
css` | ||
${generatedDefaultMedia.greaterThan('320')` | ||
background: blue; | ||
`} | ||
` | ||
) | ||
.toJSON(); | ||
let itArray = [ | ||
'\n @media (min-width: ', | ||
'320', | ||
') {\n ', | ||
'\n background: blue;\n ', | ||
'\n }\n ', | ||
]; | ||
expect(tree).toEqual(expect.arrayContaining(itArray)); | ||
}); | ||
|
||
it('expect greaterThan() to create Array', () => { | ||
let tree = renderer | ||
.create( | ||
css` | ||
${generatedDefaultMedia.greaterThan('')` | ||
background: blue; | ||
`} | ||
` | ||
) | ||
.toJSON(); | ||
let itArray = [ | ||
'\n @media (min-width: ', | ||
'0', | ||
') {\n ', | ||
'\n background: blue;\n ', | ||
'\n }\n ', | ||
]; | ||
expect(tree).toEqual(expect.arrayContaining(itArray)); | ||
}); | ||
}); | ||
|
||
describe(`defaultBreakpoints`, () => { | ||
it('expect defaultBreakpoints to equal sizes', () => { | ||
expect(defaultBreakpoints.small).toEqual('450px'); | ||
expect(defaultBreakpoints.medium).toEqual('768px'); | ||
expect(defaultBreakpoints.large).toEqual('1170px'); | ||
expect(defaultBreakpoints.huge).toEqual('1440px'); | ||
}); | ||
}); |
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.
Why
@babel/react
?In Jest docs they recommend to use
@babel/preset
:https://jestjs.io/docs/en/getting-started#using-babel
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.
@babel/preset-env
is for advanced ES6+ transpilation@babel/react
does the same for JSX, etc. (react-test-renderer), see: https://jestjs.io/docs/en/tutorial-react