Implement the submitForm() method

As mentioned earlier, the implementation of the submitForm() method depends on registrationService, which is not our focus right now; we will simply create a blank implementation of the service. The following is how frontend/src/services/registration/index.js looks:

export default {
register (detail) {
return new Promise((resolve, reject) => {
resolve()
})
}
}

As you can see, it is just a placeholder, which is already good enough for us to implement the submitForm() method and get those tests to pass.

The following is the change we make to RegisterPage.vue:

<template>
...
<form @submit.prevent="submit">
<div v-show="errorMessage" class="alert alert-danger failed">{{
errorMessage }}</div>

...
</form>
...
</template>

<script>
import registrationService from '@/services/registration'

export default {
name: 'RegisterPage',
data: function () {
return {
...
errorMessage: ''
}
},
methods: {
submitForm () {
// TODO: Validate the data
registrationService.register(this.form).then(() => {
this.$router.push({name: 'LoginPage'})
}).catch((error) => {
this.errorMessage = 'Failed to register user. Reason: ' +
(error.message ? error.message : 'Unknown') + '.'
})
}
}
}
</script>

As you can see, inside the <form> tag, we add a <div> tag to display the error message, which will be highlighted with Bootstrap's .alert-danger class. The visibility of the error message is controlled by vm.errorMessage, which we defined in the data model.

Inside the submitForm() method, for now, we pass the registration details, vm.form, to registrationService. When the registration succeeds, we will redirect the user. When it fails, we will show the error message. That's all we need to write to make the tests pass. After running the npm run test:unit command, you should see all of the tests have passed.

Normally, when we do TDD, there will be several rounds of trying and fixing to make the tests pass. We skip those steps in this book and show you the final result, and we are going to do the same for the rest of the tests in this book. If you're interested in learning how to practice TDD step by step, Kent Beck's book, Test-Driven Development by Example, is highly recommended.

Let's move on to the next part of implementing our registration process in the frontend, which is implementing the registrationService.register() method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.101.109