Giter Club home page Giter Club logo

meeting-app-unit-tests-playground's Introduction

unit-tests-playground

Requirements

$ node -v
v12.16

Where we have code - project structure

api/ - directory with our backend and 2 simple endpoints for app

done/ - directory with ready to run app and all unit tests as an examples

practices/ - directory with ready to run add but without any test cases finished

Start app locally

Each directory has own package.json file which contains informations about scripts. Hoever here is a list to start API from api/ directory and app from done/ directory:

First CLI tab:

cd api && yarn && yarn start

Second CLI tab:

cd done && yarn && yarn serve

After that API should be available on http://localhost:5679 And app should be available on http://localhost:8080

Run tests

From done/ directory

yarn test:unit

Same command is in /practices directory

To run single test file from done/ directory:

jest path/to/test/file.spec.js

Real example:

jest tests/unit/Home.spec.js

Overview of 2 routes booking app:

  1. Homepage view with all booked meetings
  2. Simple API in other directory on express
  3. Second view to book meeting:
    • input for email or select with list of people to meet with toggle checkbox
    • selecting date of meeting
    • save button
  4. Saving button adds meeting after backend response to booked meetings view

Unit tests to write:

  1. Message component
    • renders prop title when passed
    • emit event when isMessageShowed updates
    • default component matches snapshot
  2. Checkbox component
    • emit events when change checked value
    • default component matches snapshot
  3. Home view
    • meeting list contains elements from store
    • render empty list message when don't have items
  4. Add Meeting view
    • default form is rendered
    • has loading class when form is blocked
    • predefined addreses are available
    • predefined select change value to selected option
    • predefined checkbox toggle fields and clear values
    • can set custom meeting day in form
    • min attribute in input date has today date
    • error is showed when form is invalid
    • button can be disable or enabled
    • can add meeting to store
    • message is showed after button click and hide on @hideMessage event
  5. Actions
    • doReservation commits to mutation
    • exampleApiAction makes commit based on API response
  6. Mutations
    • addMeeting push item to state.meetingList
  7. Getters
    • getTodayMeetings returns meetings from today

Covered topics and good practices in this repo:

  • Basic tests
  • Mocking functions (axios)
  • Testing mocked API responses
  • Mocking vuex store
  • Timer mocks
  • Factory functions
  • Emitt events testing
  • async testing syntax
  • Testing vuex actions, mutations and getters
  • Both side component testing
  • Snapshot testing
  • Acceptance testing and snapthot-diff checking
  • Using data-testid in components and tests
  • Using husky to run Githooks for unit tests

Extra topic, not recommended in big stores:

  • Testing Vuex store instance

What about Vuex modules?

It's the same approach as importing actions from single module Vuex store. You are just importing actions from store module and i.e. testing in separate file for each store module

storeModuleA.js
storeModuleB.js
storeModuleAActions.spec.js
storeModuleAGetters.spec.js
storeModuleBActions.spec.js
...

Found a bug or repo needs update? - contribute

If you found some problem with API or meeting app please create an issue or PR with changes

Have a problem?

Open an issue or search in existing ones

License

Over MIT License

meeting-app-unit-tests-playground's People

Contributors

dependabot[bot] avatar kjugi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

meeting-app-unit-tests-playground's Issues

Can't run builds or tests

Nice playground! Definitely good inspiration on how to write tests.

But unfortunately I think you're missing a package or something, because when I run cd done && yarn && yarn serve.

I get this:

ERROR  Failed to compile with 1 errors                                                                                                                                 2:17:46 PM

 error  in ./src/main.js

Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: [BABEL] /Users/USERNAME/Documents/www/meeting-app-unit-tests-playground/done/src/main.js: Cannot find module '@babel/compat-data/corejs3-shipped-proposals'
Require stack:
- /Users/USERNAME/Documents/www/meeting-app-unit-tests-playground/done/node_modules/@babel/preset-env/lib/polyfills/corejs3/usage-plugin.js
- /Users/USERNAME/Documents/www/meeting-app-unit-tests-playground/done/node_modules/@babel/preset-env/lib/index.js
- /Users/USERNAME/Documents/www/meeting-app-unit-tests-playground/done/node_modules/@vue/babel-preset-app/index.js
- /Users/USERNAME/Documents/www/meeting-app-unit-tests-playground/done/node_modules/@vue/cli-plugin-babel/preset.js
- /Users/USERNAME/Documents/www/meeting-app-unit-tests-playground/done/node_modules/@babel/core/lib/config/files/plugins.js
- /Users/USERNAME/Documents/www/meeting-app-unit-tests-playground/done/node_modules/@babel/core/lib/config/files/index.js
- /Users/USERNAME/Documents/www/meeting-app-unit-tests-playground/done/node_modules/@babel/core/lib/index.js
- /Users/USERNAME/Documents/www/meeting-app-unit-tests-playground/done/node_modules/@vue/cli-plugin-babel/index.js
- /Users/USERNAME/Documents/www/meeting-app-unit-tests-playground/done/node_modules/@vue/cli-service/lib/Service.js
- /Users/USERNAME/Documents/www/meeting-app-unit-tests-playground/done/node_modules/@vue/cli-service/bin/vue-cli-service.js (While processing: "/Users/USERNAME/Documents/www/meeting-app-unit-tests-playground/done/node_modules/@vue/cli-plugin-babel/preset.js")
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1020:15)
    at Function.Module._load (internal/modules/cjs/loader.js:890:27)
    at Module.require (internal/modules/cjs/loader.js:1080:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (/Users/USERNAME/Documents/www/meeting-app-unit-tests-playground/done/node_modules/@babel/preset-env/lib/polyfills/corejs3/usage-plugin.js:10:55)
    at Module._compile (internal/modules/cjs/loader.js:1176:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1196:10)
    at Module.load (internal/modules/cjs/loader.js:1040:32)
    at Function.Module._load (internal/modules/cjs/loader.js:929:14)
    at Module.require (internal/modules/cjs/loader.js:1080:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (/Users/USERNAME/Documents/www/meeting-app-unit-tests-playground/done/node_modules/@babel/preset-env/lib/index.js:29:44)
    at Module._compile (internal/modules/cjs/loader.js:1176:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1196:10)
    at Module.load (internal/modules/cjs/loader.js:1040:32)
    at Function.Module._load (internal/modules/cjs/loader.js:929:14)

 @ multi (webpack)-dev-server/client?http://192.168.1.84:8081/sockjs-node (webpack)/hot/dev-server.js ./src/main.js

I tried installing @babel/compat-data to no avail.

Happy hunting!

What when vue 3.0 will be released?

After stable release, I will wait some time for vue-test-utils and other necessary libs to be compatible with a new release.

After checking what is changing in testing the vue apps I will consider 2 options:

  1. If changes will be trivial I will just update this repo with the following pattern:
project
|
+-- vue2
      |
      +-- done
      +-- practices
+-- vue3
      |
      +-- done
      +-- practices

This is probably the most possible scenario and with this option will just keep it in one place what is great.

  1. If changes will be huge, probably I will work on the new repo for vue3 best unit test practices and manage them separately

Also need to keep in mind that vue 2.7 is in WIP stage. That means an update for vue2 app version

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.