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

Add Tests & 100% Code Coverage #34

Merged
merged 12 commits into from
Aug 4, 2019
Merged

Add Tests & 100% Code Coverage #34

merged 12 commits into from
Aug 4, 2019

Conversation

agilgur5
Copy link
Owner

@agilgur5 agilgur5 commented Jul 29, 2019

  • (test): add initial test harness
  • (test): ensure wrapper methods have equivalent output
  • (test): ensure getCanvas and getTrimmedCanvas work properly
  • (test): add code coverage configuration
  • (ci/test): add CodeCov test coverage reporting
  • (test): ensure props are set and updated correctly
  • (refactor/test): export fixtures as objects named <prefix>F
  • (test): ensure resizing works correctly
  • (refactor/test): rewrite resize test output as more BDD
  • (test): ensure on & off methods work properly
  • (fix/test): reset clearOnResize to true for size tests
  • (test/refactor): rewrite more to be closer to BDD-style

Resolves #33. Also wrote through my progress, bugs/gotchas encountered, and links everywhere in #33 as well.

This might need a bit of iteration on code style.

It's also missing coverage for exactly 1 branch, which is the DPR check, the || 1 specifically (though DPR is 1 already in jsdom). I'm not sure if I want to add a test for it though as the code around DPR has always been iffy/problematic in upstream signature_pad and occasionally here too.
DPR being 1 already points to another potential review point, which is that canvas.width and canvas.height are 0 if they're not set as there's no CSS to make it a percentage of some wrapper, etc, so this made the change size tests accidentally succeed even when clearOnResize was false (i.e. not necessarily that reliable a test).

Anyway, this took a lot longer than expected for a few reasons, so I might not review / merge / iterate on this for a bit, we'll see.

- add 1 simple test for existence of canvas and instance methods
  - (pkg): test code shouldn't be in the package, just source
    - currently preferring to keep source and tests co-located

- change test script to use jest, and move prev test to test:pub
  - (ci): change CI to run test and test:pub
- configure jest and enzyme for testing usage
  - make jest importable too
  - add .babelrc to configure babel-jest
    - can't use .babelrc.js as babel-jest@23 doesn't support it
      - jestjs/jest#5324
    - can't use .babelrc for babel-loader because babel-loader@6
      has some bugs with it and @7 doesn't support webpack@1
      - babel/babel-loader#552

- use jest instead of ava mainly because there's less to
  configure / install (directly at least, still a lot indirectly),
  it's popular / well-supported in React community, and supports
  configuration outside of package.json
  - see #33 for some more details

(deps): add jest, enzyme, and supporting deps to devDeps
- add babel-jest@23 for Babel 6 support
  - and configure it for .js files due to a jest bug
- add canvas-prebuilt@1 to support jest's jsdom v11
  - canvas is used by jsdom for, well, canvas interactions
- add enzyme-adapter-react-16 to configure Enzyme with React 16
  - upgrade to React 16 in devDeps bc adapter-react-15 requires
    react-test-renderer and no drawbacks in upgrading
- add describe as this is several tests in one
- add a fixture of just a dot
- add tests for toData, fromData, toDataURL, fromDataURL, isEmpty,
  and clear
- needed to add width & height to element as otherwise trimming would
  fail due to non-existent width & height
  - add initial and trimmed width & height as fixtures

- tried using some dataURL fixtures but they would end up slightly
  different, so I suppose a bit of that is implementation-specific
- ignore test-utils/ directory in coverage
- update gitignore with coverage/ directory
  - and fix missing newline
- change CI to output and upload coverage reports

- (docs): add coverage badge to README
- that defaults, mounted initial props and options, and updated props
  and options match
  - took me a bit to figure out I should use `toMatchObject` and then
    how to use `toMatchObject` properly

- add fixtures for props and split them into sigPad vs. rSC for
  different uses
- better organization to combine related fixture data as one object
- naming as <prefix>F makes it more clear that we're referencing a
  fixture in the code, similar to e.g. TypeScript I<suffix>
@codecov
Copy link

codecov bot commented Jul 29, 2019

Codecov Report

❗ No coverage uploaded for pull request base (master@8f0ca6a). Click here to learn what that means.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##             master    #34   +/-   ##
=======================================
  Coverage          ?   100%           
=======================================
  Files             ?      1           
  Lines             ?     59           
  Branches          ?      6           
=======================================
  Hits              ?     59           
  Misses            ?      0           
  Partials          ?      0

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8f0ca6a...aba5e33. Read the comment docs.

- clearOnResize and fixed/unfixed width & height should affect
  resizing as expected
  - also yea the tests run in parallel, but readability-wise it makes
    more sense I think(?)

- add a window.resizeTo polyfill as jsdom doesn't implement it o.o
  - and there doesn't seem to be an NPM package for this(?)
- noun / verb of describe and it for better readability
  - export it from Jest ES Module as well
- 100% statement coverage now woooooot!
- when false, canvas won't resize on window resize necessarily
  - was set false in previous test

- this was found when branch coverage was lacking a bit on resizing,
  now we're at ~93% and missing just one branch!
  - the tests didn't error out though because without input size the
    canvas size is 0 in these tests regardless of resize
    - not a perfect test in that sense :/ :/
Copy link
Owner Author

@agilgur5 agilgur5 left a comment

Choose a reason for hiding this comment

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

I think there are still some improvements to be made, but this is small enough that those might be premature without seeing how the codebase evolves. For now, I think this is good, and future iterations can improve. Several reviews, amendments, and refactors already made it in before I officially made the PR as well.

Some future things to think about:

  • DPR test as per above comments
  • make resize tests more reliable as per above comments
  • splitting fixture data into multiple files with import * as <prefix>F from '../test-utils/fixtures/<prefix>.js
    • map test-utils or fixtures etc to local scope so that one can `import ... from 'fixtures/....js' etc
  • splitting tests into multiple files? for this and a bit for fixtures, kind of depends on how strictly we're following 1-to-1 test files per source code
  • convert more of the tests to BDD-style, might require nested describes for it to make sense?
  • use beforeEach so that nested describes don't cause jest ordering problems (would also make each test cleaner).

@agilgur5 agilgur5 merged commit 5dc5b2b into master Aug 4, 2019
@agilgur5 agilgur5 deleted the tests branch August 4, 2019 20:51
@agilgur5 agilgur5 added the kind: internal Changes only affect the internals and not the API or usage label Mar 13, 2021
Repository owner locked as resolved and limited conversation to collaborators Mar 13, 2021
@agilgur5 agilgur5 added the scope: tests Tests could be improved. Or changes that only affect tests label Jun 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind: internal Changes only affect the internals and not the API or usage scope: tests Tests could be improved. Or changes that only affect tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add tests
1 participant