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

Unit tests in jest + minor cart store and hooks refactor #2414

Merged
merged 17 commits into from
Mar 6, 2019

Conversation

lukeromanowicz
Copy link
Contributor

@lukeromanowicz lukeromanowicz commented Feb 11, 2019

Related issues

#2305

Short description and why it's useful

Currently, existing tests (16 of them to be precise) written with mocha & karma are integration tests that might seem like a little bit chaotic. As VSF is growing fast, it's high time to use a more modern, comprehensive tool and start writing actual unit tests.

Upgrade Notes and Changelog

  • No upgrade steps required (100% backward compatibility)
  • I've updated the Upgrade notes and Changelog on how to port existing VS sites with this new feature

Contribution and currently important rules acceptance

Changes made in this PR:

  • removed karma tests and dependencies
  • added cart unit tests written in jest
  • moved e2e tests to from tests/ to test/ directory
  • added unit tests to Travis
  • Travis CI config revamp
  • minor cart refactoring

@lukeromanowicz lukeromanowicz changed the title WIP | Unit tests in jest WIP | Unit tests in jest + minor cart store refactor Feb 11, 2019
@lukeromanowicz lukeromanowicz changed the title WIP | Unit tests in jest + minor cart store refactor WIP | Unit tests in jest + minor cart store and hooks refactor Feb 11, 2019
@lukeromanowicz
Copy link
Contributor Author

lukeromanowicz commented Feb 12, 2019

@filrak All files in core/modules/cart except actions have full coverage, but I don't see much of a reason in testing actions. There are some areas to fix here and there in whole module including:

  • better docs for platformTotal and platformTotalSegments
  • support server_item_id in CART_UPD_ITEM mutation
  • supplement missing events (many functions have before-cart-x but don't have after-cart-x and vice versa) because, I suspect, rewriting everything to global actions is a no go because of backward compatibility(?)
  • a lot of events emitted by mutations contain current value but don't have a new one
  • almost no signs of hard typing
  • not all cart mutations modify its cartSavedAt
  • sometimes cartSavedAt is being updated, even if the primary change was not applied to state
  • definitely too much code in mutations not mentioning actions
  • alphabetical sorting would be nice considering how much code there is.
  • having parameters as an object is inconsistent, some function follow this rule, others don't
  • inconsistent parameter names in functions and emitted events
  • emitting 'application-level' events in unexpected places i.e. Vue.prototype.$bus.$emit('application-after-loaded') on load.

But I did the tests nonetheless. These issues are not super critical in opposite to what is happening to actions.ts alone:

  • dispatching actions belonging to the same namespace through rootStore
  • modifying store state directly from actions (not through mutations)
  • reading store state from other modules directly instead of through getters
  • hardcoded requests which could be moved to api
  • hardcoded notifications that could be grouped and stored separately
  • some logic could possibly be moved to helpers
  • 100+ lines actions
  • nested promises instead of async functions
  • a lot of loggers that could be placed more wisely
  • let variables that often should be const instead
  • not to mention completely unreadable code

Because of that, I suggest refactoring this area first because the code will change significantly after refactoring and will have to be rewritten anyway.

@pkarw @patzick what do you think of it?

Also please CR

@lukeromanowicz lukeromanowicz changed the title WIP | Unit tests in jest + minor cart store and hooks refactor Unit tests in jest + minor cart store and hooks refactor Feb 12, 2019
@lukeromanowicz lukeromanowicz changed the title Unit tests in jest + minor cart store and hooks refactor WIP | Unit tests in jest + minor cart store and hooks refactor Feb 12, 2019
@pkarw pkarw requested review from patzick and filrak February 15, 2019 13:14
@filrak
Copy link
Collaborator

filrak commented Feb 15, 2019

AWESOME job, thanks ;)
I guess the things that you mention are a good addition for the refactoring process we already discussed and will be discussed f2f next week (especially criticals that should be splitted into individual issues under same tag) ;). Regarding the tests I need more time to dig into it so I'll probably give my review on Monday.

Thanks again, huge step forward in quality

package.json Outdated
@@ -37,7 +37,7 @@
"build:client": "cross-env NODE_ENV=production TS_NODE_PROJECT=\"tsconfig-build.json\" webpack --config ./core/build/webpack.prod.client.config.ts --mode production --progress --hide-modules",
"build:server": "cross-env NODE_ENV=production TS_NODE_PROJECT=\"tsconfig-build.json\" webpack --config ./core/build/webpack.prod.server.config.ts --mode production --progress --hide-modules",
"build": "rimraf dist && yarn build:client && yarn build:server && yarn build:sw",
"test:unit": "karma start ./tests/unit/karma.conf.js --single-run",
"test:unit": "yarn jest -c test/unit/jest.conf.js",
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 should be without yarn, we're already calling yarn test:utils

Copy link
Collaborator

Choose a reason for hiding this comment

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

any response to this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was certain that I fixed it last time. Will update in a minute

package.json Show resolved Hide resolved
Copy link
Collaborator

@pkarw pkarw left a comment

Choose a reason for hiding this comment

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

Great job.

@patzick patzick changed the base branch from master to develop February 21, 2019 06:30
@patzick patzick added this to the 1.9 milestone Feb 21, 2019
@patzick
Copy link
Collaborator

patzick commented Feb 21, 2019

@lukeromanowicz is it still in WIP status?

@lukeromanowicz
Copy link
Contributor Author

lukeromanowicz commented Feb 21, 2019

@patzick Yes, it is. I've been told to continue actions tests and clearing up dependencies is still todo.

@lukeromanowicz lukeromanowicz force-pushed the feature/jest-tests branch 4 times, most recently from 9e3a86d to 5536a16 Compare March 1, 2019 09:48
@lukeromanowicz lukeromanowicz force-pushed the feature/jest-tests branch 6 times, most recently from c4fbce7 to 83b18a4 Compare March 1, 2019 10:11
@lukeromanowicz lukeromanowicz changed the title WIP | Unit tests in jest + minor cart store and hooks refactor Unit tests in jest + minor cart store and hooks refactor Mar 1, 2019
@lukeromanowicz
Copy link
Contributor Author

@filrak @patzick @pkarw it is done for now. Please review once again

@pkarw
Copy link
Collaborator

pkarw commented Mar 1, 2019

I think we can merge this in to 1.9rc - the final decision is up to @patzick and @filrak

Copy link
Collaborator

@patzick patzick left a comment

Choose a reason for hiding this comment

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

Mostly looks good to me. It's nice that you updated travis workflow!
Just some minor improvements and conflict to resolve.

And yes - i think this should go to 1.9.0-rc.1 after review. @filrak WDYT?

if ((Date.now() - context.state.cartServerTotalsAt) >= CART_TOTALS_INTERVAL_MS) {
context.state.cartServerPullAt = Date.now()
context.state.cartServerTotalsAt = Date.now()
Copy link
Collaborator

Choose a reason for hiding this comment

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

this should be mutation

@@ -91,9 +92,9 @@ const actions: ActionTree<CartState, RootState> = {
}
},
serverTotals (context, { forceClientState = false }) { // pull current cart FROM the server
if (rootStore.state.config.cart.synchronize_totals && !Vue.prototype.$isServer) {
if (rootStore.state.config.cart.synchronize_totals && !isServer) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

in future we'd like to get rid of config inside state because it is ~16kB of data which is barely needed on initial load. Please import config and apply suggestion

Suggested change
if (rootStore.state.config.cart.synchronize_totals && !isServer) {
if (config.cart.synchronize_totals && !isServer) {

@@ -110,7 +111,7 @@ const actions: ActionTree<CartState, RootState> = {
}
},
serverCreate (context, { guestCart = false }) {
if (rootStore.state.config.cart.synchronize && !Vue.prototype.$isServer) {
if (rootStore.state.config.cart.synchronize && !isServer) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if (rootStore.state.config.cart.synchronize && !isServer) {
if (config.cart.synchronize && !isServer) {

core/modules/cart/store/mutations.ts Show resolved Hide resolved
package.json Outdated
@@ -37,7 +37,7 @@
"build:client": "cross-env NODE_ENV=production TS_NODE_PROJECT=\"tsconfig-build.json\" webpack --config ./core/build/webpack.prod.client.config.ts --mode production --progress --hide-modules",
"build:server": "cross-env NODE_ENV=production TS_NODE_PROJECT=\"tsconfig-build.json\" webpack --config ./core/build/webpack.prod.server.config.ts --mode production --progress --hide-modules",
"build": "rimraf dist && yarn build:client && yarn build:server && yarn build:sw",
"test:unit": "karma start ./tests/unit/karma.conf.js --single-run",
"test:unit": "yarn jest -c test/unit/jest.conf.js",
Copy link
Collaborator

Choose a reason for hiding this comment

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

any response to this?

{
"name": "@vue-storefront/unit-tests",
"private": true,
"version": "0.1.0"
Copy link
Collaborator

Choose a reason for hiding this comment

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

this shouldn't be current storefront version?

@patzick
Copy link
Collaborator

patzick commented Mar 5, 2019

@lukeromanowicz could you make this improvements today, so we could add this to upcoming RC version?

@lukeromanowicz
Copy link
Contributor Author

lukeromanowicz commented Mar 6, 2019

@patzick thank you for your input. I didn't really plan to do any refactoring except necessary changes to make the code possible to test but in the end, I did the changes in accordance with your suggestions. Please re-review.

@patzick patzick merged commit df3e2fa into vuestorefront:develop Mar 6, 2019
@patzick patzick mentioned this pull request Mar 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants