2017-08-17 22:00:37 +05:30
# Vue
2019-12-21 20:55:43 +05:30
To get started with Vue, read through [their documentation ](https://vuejs.org/v2/guide/ ).
2017-08-17 22:00:37 +05:30
2018-11-18 11:00:15 +05:30
## Examples
2017-08-17 22:00:37 +05:30
2018-11-18 11:00:15 +05:30
What is described in the following sections can be found in these examples:
2018-03-17 18:26:18 +05:30
2019-12-04 20:38:33 +05:30
- web ide: < https: // gitlab . com / gitlab-org / gitlab-foss / tree / master / app / assets / javascripts / ide / stores >
- security products: < https: // gitlab . com / gitlab-org / gitlab / tree / master / ee / app / assets / javascripts / vue_shared / security_reports >
- registry: < https: // gitlab . com / gitlab-org / gitlab-foss / tree / master / app / assets / javascripts / registry / stores >
2017-08-17 22:00:37 +05:30
2018-11-18 11:00:15 +05:30
## Vue architecture
2017-08-17 22:00:37 +05:30
2018-11-18 11:00:15 +05:30
All new features built with Vue.js must follow a [Flux architecture][flux].
The main goal we are trying to achieve is to have only one data flow and only one data entry.
In order to achieve this goal we use [vuex ](#vuex ).
2017-08-17 22:00:37 +05:30
You can also read about this architecture in vue docs about [state management][state-management]
and about [one way data flow][one-way-data-flow].
2018-11-18 11:00:15 +05:30
### Components and Store
2017-08-17 22:00:37 +05:30
In some features implemented with Vue.js, like the [issue board][issue-boards]
or [environments table][environments-table]
you can find a clear separation of concerns:
2020-04-22 19:07:51 +05:30
```plaintext
2017-08-17 22:00:37 +05:30
new_feature
├── components
2018-05-09 12:01:36 +05:30
│ └── component.vue
2017-08-17 22:00:37 +05:30
│ └── ...
2018-11-18 11:00:15 +05:30
├── store
2018-05-09 12:01:36 +05:30
│ └── new_feature_store.js
2018-10-15 14:42:47 +05:30
├── index.js
2017-08-17 22:00:37 +05:30
```
2019-09-30 21:07:59 +05:30
2017-08-17 22:00:37 +05:30
_For consistency purposes, we recommend you to follow the same structure._
Let's look into each of them:
2019-07-31 22:56:46 +05:30
### An `index.js` file
2017-08-17 22:00:37 +05:30
This is the index file of your new feature. This is where the root Vue instance
of the new feature should be.
The Store and the Service should be imported and initialized in this file and
provided as a prop to the main component.
2019-07-31 22:56:46 +05:30
Be sure to read about [page-specific JavaScript][page_specific_javascript].
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
### Bootstrapping Gotchas
2019-09-30 21:07:59 +05:30
2018-11-08 19:23:39 +05:30
#### Providing data from HAML to JavaScript
2019-09-30 21:07:59 +05:30
2018-03-17 18:26:18 +05:30
While mounting a Vue application may be a need to provide data from Rails to JavaScript.
To do that, provide the data through `data` attributes in the HTML element and query them while mounting the application.
2018-11-08 19:23:39 +05:30
_Note:_ You should only do this while initializing the application, because the mounted element will be replaced with Vue-generated DOM.
2018-03-17 18:26:18 +05:30
The advantage of providing data from the DOM to the Vue instance through `props` in the `render` function
instead of querying the DOM inside the main vue component is that makes tests easier by avoiding the need to
create a fixture or an HTML element in the unit test. See the following example:
```javascript
// haml
.js-vue-app{ data: { endpoint: 'foo' }}
2018-11-08 19:23:39 +05:30
// index.js
2018-03-17 18:26:18 +05:30
document.addEventListener('DOMContentLoaded', () => new Vue({
el: '.js-vue-app',
data() {
const dataset = this.$options.el.dataset;
return {
endpoint: dataset.endpoint,
};
},
render(createElement) {
return createElement('my-component', {
props: {
2020-01-01 13:55:28 +05:30
endpoint: this.endpoint,
2018-03-17 18:26:18 +05:30
},
});
},
}));
```
#### Accessing the `gl` object
2019-09-30 21:07:59 +05:30
When we need to query the `gl` object for data that won't change during the application's life cycle, we should do it in the same place where we query the DOM.
2018-03-17 18:26:18 +05:30
By following this practice, we can avoid the need to mock the `gl` object, which will make tests easier.
It should be done while initializing our Vue instance, and the data should be provided as `props` to the main component:
```javascript
document.addEventListener('DOMContentLoaded', () => new Vue({
el: '.js-vue-app',
render(createElement) {
return createElement('my-component', {
props: {
username: gon.current_username,
},
});
},
}));
```
2019-12-21 20:55:43 +05:30
#### Accessing feature flags
Use Vue's [provide/inject ](https://vuejs.org/v2/api/#provide-inject ) mechanism
to make feature flags available to any descendant components in a Vue
application. The `glFeatures` object is already provided in `commons/vue.js` , so
only the mixin is required to utilize the flags:
```javascript
// An arbitrary descendant component
import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
export default {
// ...
mixins: [glFeatureFlagsMixin()],
// ...
created() {
if (this.glFeatures.myFlag) {
// ...
}
},
}
```
This approach has a few benefits:
- Arbitrarily deeply nested components can opt-in and access the flag without
intermediate components being aware of it (c.f. passing the flag down via
props).
- Good testability, since the flag can be provided to `mount` /`shallowMount`
from `vue-test-utils` as easily as a prop.
```javascript
import { shallowMount } from '@vue/test-utils';
shallowMount(component, {
provide: {
glFeatures: { myFlag: true },
},
});
```
- No need to access a global variable, except in the application's
[entry point ](#accessing-the-gl-object ).
2017-08-17 22:00:37 +05:30
### A folder for Components
This folder holds all components that are specific of this new feature.
If you need to use or create a component that will probably be used somewhere
else, please refer to `vue_shared/components` .
A good thumb rule to know when you should create a component is to think if
it will be reusable elsewhere.
For example, tables are used in a quite amount of places across GitLab, a table
would be a good fit for a component. On the other hand, a table cell used only
in one table would not be a good use of this pattern.
You can read more about components in Vue.js site, [Component System][component-system]
### A folder for the Store
2018-10-15 14:42:47 +05:30
#### Vuex
2019-09-30 21:07:59 +05:30
2018-10-15 14:42:47 +05:30
Check this [page ](vuex.md ) for more details.
2019-07-07 11:18:12 +05:30
### Mixing Vue and jQuery
- Mixing Vue and jQuery is not recommended.
2019-09-30 21:07:59 +05:30
- If you need to use a specific jQuery plugin in Vue, [create a wrapper around it ](https://vuejs.org/v2/examples/select2.html ).
2019-07-07 11:18:12 +05:30
- It is acceptable for Vue to listen to existing jQuery events using jQuery event listeners.
- It is not recommended to add new jQuery events for Vue to interact with jQuery.
2017-08-17 22:00:37 +05:30
## Style guide
2020-01-01 13:55:28 +05:30
Please refer to the Vue section of our [style guide ](style/vue.md )
2017-08-17 22:00:37 +05:30
for best practices while writing your Vue components and templates.
## Testing Vue Components
Each Vue component has a unique output. This output is always present in the render function.
Although we can test each method of a Vue component individually, our goal must be to test the output
of the render/template function, which represents the state at all times.
2019-07-07 11:18:12 +05:30
Make use of the [axios mock adapter ](axios.md#mock-axios-response-in-tests ) to mock data returned.
2017-08-17 22:00:37 +05:30
Here's how we would test the Todo App above:
```javascript
2018-11-08 19:23:39 +05:30
import Vue from 'vue';
import axios from '~/lib/utils/axios_utils';
import MockAdapter from 'axios-mock-adapter';
2017-08-17 22:00:37 +05:30
describe('Todos App', () => {
2018-11-08 19:23:39 +05:30
let vm;
let mock;
beforeEach(() => {
// Create a mock adapter for stubbing axios API requests
mock = new MockAdapter(axios);
2017-08-17 22:00:37 +05:30
const Component = Vue.extend(component);
2018-11-08 19:23:39 +05:30
// Mount the Component
vm = new Component().$mount();
});
afterEach(() => {
// Reset the mock adapter
mock.restore();
// Destroy the mounted component
vm.$destroy();
});
2017-08-17 22:00:37 +05:30
2018-11-08 19:23:39 +05:30
it('should render the loading state while the request is being made', () => {
2017-08-17 22:00:37 +05:30
expect(vm.$el.querySelector('i.fa-spin')).toBeDefined();
});
2018-11-08 19:23:39 +05:30
it('should render todos returned by the endpoint', done => {
// Mock the get request on the API endpoint to return data
mock.onGet('/todos').replyOnce(200, [
{
2017-08-17 22:00:37 +05:30
title: 'This is a todo',
2018-11-08 19:23:39 +05:30
text: 'This is the text',
},
]);
2017-08-17 22:00:37 +05:30
2018-11-08 19:23:39 +05:30
Vue.nextTick(() => {
const items = vm.$el.querySelectorAll('.js-todo-list div')
expect(items.length).toBe(1);
expect(items[0].textContent).toContain('This is the text');
done();
2017-08-17 22:00:37 +05:30
});
2018-11-08 19:23:39 +05:30
});
2017-08-17 22:00:37 +05:30
2018-11-08 19:23:39 +05:30
it('should add a todos on button click', (done) => {
2017-08-17 22:00:37 +05:30
2018-11-08 19:23:39 +05:30
// Mock the put request and check that the sent data object is correct
mock.onPut('/todos').replyOnce((req) => {
expect(req.data).toContain('text');
expect(req.data).toContain('title');
2017-08-17 22:00:37 +05:30
2018-11-08 19:23:39 +05:30
return [201, {}];
2017-08-17 22:00:37 +05:30
});
2018-11-08 19:23:39 +05:30
vm.$el.querySelector('.js-add-todo').click();
2017-08-17 22:00:37 +05:30
2018-11-08 19:23:39 +05:30
// Add a new interceptor to mock the add Todo request
Vue.nextTick(() => {
expect(vm.$el.querySelectorAll('.js-todo-list div').length).toBe(2);
done();
2017-08-17 22:00:37 +05:30
});
});
});
```
2018-11-08 19:23:39 +05:30
### `mountComponent` helper
2019-09-30 21:07:59 +05:30
2018-03-17 18:26:18 +05:30
There is a helper in `spec/javascripts/helpers/vue_mount_component_helper.js` that allows you to mount a component with the given props:
```javascript
import Vue from 'vue';
2018-11-18 11:00:15 +05:30
import mountComponent from 'spec/helpers/vue_mount_component_helper'
2018-03-17 18:26:18 +05:30
import component from 'component.vue'
const Component = Vue.extend(component);
const data = {prop: 'foo'};
const vm = mountComponent(Component, data);
```
2018-11-08 19:23:39 +05:30
### Test the component's output
2019-09-30 21:07:59 +05:30
2017-08-17 22:00:37 +05:30
The main return value of a Vue component is the rendered output. In order to test the component we
need to test the rendered output. [Vue][vue-test] guide's to unit test show us exactly that:
2019-02-15 15:39:39 +05:30
## Vue.js Expert Role
2019-09-30 21:07:59 +05:30
2019-02-15 15:39:39 +05:30
One should apply to be a Vue.js expert by opening an MR when the Merge Request's they create and review show:
2019-07-07 11:18:12 +05:30
2020-01-01 13:55:28 +05:30
- Deep understanding of Vue and Vuex reactivity
2019-02-15 15:39:39 +05:30
- Vue and Vuex code are structured according to both official and our guidelines
- Full understanding of testing a Vue and Vuex application
2019-09-04 21:01:54 +05:30
- Vuex code follows the [documented pattern ](vuex.md#actions-pattern-request-and-receive-namespaces )
2019-02-15 15:39:39 +05:30
- Knowledge about the existing Vue and Vuex applications and existing reusable components
2019-12-04 20:38:33 +05:30
[issue-boards]: https://gitlab.com/gitlab-org/gitlab-foss/tree/master/app/assets/javascripts/boards
[environments-table]: https://gitlab.com/gitlab-org/gitlab-foss/tree/master/app/assets/javascripts/environments
2019-07-31 22:56:46 +05:30
[page_specific_javascript]: ./performance.md#page-specific-javascript
2017-08-17 22:00:37 +05:30
[component-system]: https://vuejs.org/v2/guide/#Composing-with-Components
[state-management]: https://vuejs.org/v2/guide/state-management.html#Simple-State-Management-from-Scratch
[one-way-data-flow]: https://vuejs.org/v2/guide/components.html#One-Way-Data-Flow
[vue-test]: https://vuejs.org/v2/guide/unit-testing.html
2019-12-21 20:55:43 +05:30
[flux]: https://facebook.github.io/flux/
2018-03-17 18:26:18 +05:30
[axios]: https://github.com/axios/axios