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

Write tests with jest (Coverage 100%) #19

Open
wants to merge 5 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
15 changes: 14 additions & 1 deletion .babelrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,17 @@ const presets = [
],
];

module.exports = { presets };
const testPresets = [
[
"@babel/react",
Copy link
Collaborator

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

Copy link
Collaborator Author

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

],
];

module.exports = {
presets,
env: {
test: {
presets: testPresets,
},
},
};
7 changes: 7 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

all options in this file are redundant with prettier defaults but singleQuote.

https://prettier.io/docs/en/options.html

Maybe you can remove those and only keep singleQuote

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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",
}
42 changes: 0 additions & 42 deletions .yarnclean

This file was deleted.

1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@

Mohammad Rajabifard <hi@morajabi.me>
+Behnam Rajabifard <b.rajabifard@gmail.com>
+Tim Hagn <mail@timhagn.com>
41 changes: 41 additions & 0 deletions __tests__/__snapshots__/index.test.js.snap
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"
/>
`;
65 changes: 65 additions & 0 deletions __tests__/convertors.test.js
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({
Copy link
Collaborator

Choose a reason for hiding this comment

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

suggestion: In this file, all those .toEqual could be replaced to .toMatchInlineSnapshot. This would bring the same value and capability to change/refactor as doing manually.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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. .toMatch[Inline]Snapshot() gets overwritten with each deployment, that's why I rather equality tests than them.

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({});
});
});
173 changes: 173 additions & 0 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import 'jest-styled-components';
Copy link
Collaborator

Choose a reason for hiding this comment

The 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:

  1. returns an object with some methods already configured;
  2. each function of this is calling styled-components/css tag template literals with a string

In that sense, it might be a cleaner implementation:

  1. remove all React stuffs;
  2. mock css from styled-component
  3. Check if it has been called with the case template.

What do you think about this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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');
});
});
Loading