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

Conversation

timhagn
Copy link
Collaborator

@timhagn timhagn commented Oct 25, 2019

As written in the now closed #15, I updated the tests in regard to the changes made to this repo in the meantime.
As the default values for the internal functions pxToEmOrRem() and getSizeFromBreakpoint were never used, I removed them.
Now the tests are at 100% and I only added packages as devDependencies : ).

Is this now mergeable ; )?

Best,

Tim.

=============================== Coverage summary ===============================
Statements   : 100%
Branches     : 100%
Functions    : 100%
Lines        : 100%
================================================================================

@timhagn
Copy link
Collaborator Author

timhagn commented Nov 22, 2019

Hola, is anybody out there % )?

Copy link
Collaborator

@raulfdm raulfdm left a comment

Choose a reason for hiding this comment

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

Hey @timhagn. I'm a new collaborator here.

First of all I'd say thank you for this contribution. It adds a really good value on it since the idea is keep adding functionality, adding support for emotion/glamour and having a good test suit is the best way to start.

I made some comments to refine a bit the PR but in general looks really good! nice job

Keep in mind my suggestion are not final, we can of course discuss them if you have a different opinion.

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

@@ -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 ; )...


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.

@@ -0,0 +1,183 @@
// For a detailed explanation regarding each configuration property, visit:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would be nice having some clean up in this file and keep only what's been setup.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, I concur, I just took the template and adapted it (after all, this should once been extended ; ).

* @param {'rem' | 'em'} unit
*/
function pxToEmOrRem(breakpoints, ratio = 16, unit) {
function pxToEmOrRem(breakpoints, ratio, unit) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

why the default ratio was removed? It might be a breaking change?

* @param {Object} breakpoints - Map labels to breakpoint sizes
* @return {string|*}
*/
function getSizeFromBreakpoint(breakpointValue, breakpoints) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Also here, why the default breakpoints was been removed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Same as above. We don't have to fear breakpoints could ever be empty, as generateMedia gets called with (breakpoints = defaultBreakpoints) when no arguments are given. Also, with them set 100% coverage would never have been possible, as their branches were never taken cause of this ; ).

@@ -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.

@timhagn
Copy link
Collaborator Author

timhagn commented Jul 7, 2020

Hi there,

welcome to the collaborator club : ). Am in the suggestions - but keep in mind that I wrote this PR three quarts of a year ago ; ).
Also, I'm quite booked at the moment, so don't know, when I will have time to change this.

Best,

Tim.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants