Complete Vue.js 2 Web Development_ Practical guide to building end-to-end web development solutions with Vue.js 2

(singke) #1
Large Application Patterns with Vuex Chapter 18

How it works...


While the testing of the mutations is pretty straightforward, I think the testing of the actions
deserves some more explaining.


Since we didn't want to depend on external services for actions, we had to mock the axios


service. We used the inject-loader, which takes the original library and mocks the parts


we specify with arbitrary code (the @ symbol is a shorthand for src); in our case, we


mocked the axios library and, precisely, the get method. We had to use the CommonJS


syntax (with the require) because that's the only way to tell Webpack to use loaders in


imports.


What we have done in the test is that we also mocked the commit function. Normally, this


function calls a mutation that modifies the state. We just want to know if the correct


mutation is called and with the right arguments. Moreover, we had to wrap everything in a
try block; without it, the test would fail over a timeout and we'd lose the error. Instead,


now we fail immediately and we can read, from the console, what error caused the test to
fail.

Free download pdf