Commit a5af922a authored by Martin Wortschack's avatar Martin Wortschack

Merge branch 'move-frequent-items-component-to-jest' into 'master'

Move frequent items list component to jest

See merge request gitlab-org/gitlab!26114
parents b18ad174 4cf5b021
...@@ -58,7 +58,7 @@ export default { ...@@ -58,7 +58,7 @@ export default {
<template> <template>
<div class="frequent-items-list-container"> <div class="frequent-items-list-container">
<ul class="list-unstyled"> <ul ref="frequentItemsList" class="list-unstyled">
<li v-if="isListEmpty" :class="{ 'section-failure': isFetchFailed }" class="section-empty"> <li v-if="isListEmpty" :class="{ 'section-failure': isFetchFailed }" class="section-empty">
{{ listEmptyMessage }} {{ listEmptyMessage }}
</li> </li>
......
import { shallowMount } from '@vue/test-utils'; import { shallowMount } from '@vue/test-utils';
import { trimText } from 'helpers/text_helper'; import { trimText } from 'helpers/text_helper';
import frequentItemsListItemComponent from '~/frequent_items/components/frequent_items_list_item.vue'; import frequentItemsListItemComponent from '~/frequent_items/components/frequent_items_list_item.vue';
import mockData from '../mock_data'; // can also use 'mockGroup', but not useful to test here import { mockProject } from '../mock_data'; // can also use 'mockGroup', but not useful to test here
const mockProject = mockData();
describe('FrequentItemsListItemComponent', () => { describe('FrequentItemsListItemComponent', () => {
let wrapper; let wrapper;
......
import Vue from 'vue'; import { mount } from '@vue/test-utils';
import mountComponent from 'spec/helpers/vue_mount_component_helper';
import frequentItemsListComponent from '~/frequent_items/components/frequent_items_list.vue'; import frequentItemsListComponent from '~/frequent_items/components/frequent_items_list.vue';
import frequentItemsListItemComponent from '~/frequent_items/components/frequent_items_list_item.vue';
import { mockFrequentProjects } from '../mock_data'; import { mockFrequentProjects } from '../mock_data';
const createComponent = (namespace = 'projects') => {
const Component = Vue.extend(frequentItemsListComponent);
return mountComponent(Component, {
namespace,
items: mockFrequentProjects,
isFetchFailed: false,
hasSearchQuery: false,
matcher: 'lab',
});
};
describe('FrequentItemsListComponent', () => { describe('FrequentItemsListComponent', () => {
let vm; let wrapper;
beforeEach(() => { const createComponent = (props = {}) => {
vm = createComponent(); wrapper = mount(frequentItemsListComponent, {
}); propsData: {
namespace: 'projects',
items: mockFrequentProjects,
isFetchFailed: false,
hasSearchQuery: false,
matcher: 'lab',
...props,
},
});
};
afterEach(() => { afterEach(() => {
vm.$destroy(); wrapper.destroy();
}); });
describe('computed', () => { describe('computed', () => {
describe('isListEmpty', () => { describe('isListEmpty', () => {
it('should return `true` or `false` representing whether if `items` is empty or not with projects', () => { it('should return `true` or `false` representing whether if `items` is empty or not with projects', () => {
vm.items = []; createComponent({
items: [],
});
expect(vm.isListEmpty).toBe(true); expect(wrapper.vm.isListEmpty).toBe(true);
vm.items = mockFrequentProjects; wrapper.setProps({
items: mockFrequentProjects,
});
expect(vm.isListEmpty).toBe(false); expect(wrapper.vm.isListEmpty).toBe(false);
}); });
}); });
describe('fetched item messages', () => { describe('fetched item messages', () => {
it('should return appropriate empty list message based on value of `localStorageFailed` prop with projects', () => { it('should return appropriate empty list message based on value of `localStorageFailed` prop with projects', () => {
vm.isFetchFailed = true; createComponent({
isFetchFailed: true,
});
expect(vm.listEmptyMessage).toBe('This feature requires browser localStorage support'); expect(wrapper.vm.listEmptyMessage).toBe(
'This feature requires browser localStorage support',
);
vm.isFetchFailed = false; wrapper.setProps({
isFetchFailed: false,
});
expect(vm.listEmptyMessage).toBe('Projects you visit often will appear here'); expect(wrapper.vm.listEmptyMessage).toBe('Projects you visit often will appear here');
}); });
}); });
describe('searched item messages', () => { describe('searched item messages', () => {
it('should return appropriate empty list message based on value of `searchFailed` prop with projects', () => { it('should return appropriate empty list message based on value of `searchFailed` prop with projects', () => {
vm.hasSearchQuery = true; createComponent({
vm.isFetchFailed = true; hasSearchQuery: true,
isFetchFailed: true,
});
expect(vm.listEmptyMessage).toBe('Something went wrong on our end.'); expect(wrapper.vm.listEmptyMessage).toBe('Something went wrong on our end.');
vm.isFetchFailed = false; wrapper.setProps({
isFetchFailed: false,
});
expect(vm.listEmptyMessage).toBe('Sorry, no projects matched your search'); expect(wrapper.vm.listEmptyMessage).toBe('Sorry, no projects matched your search');
}); });
}); });
}); });
describe('template', () => { describe('template', () => {
it('should render component element with list of projects', done => { it('should render component element with list of projects', () => {
vm.items = mockFrequentProjects; createComponent();
Vue.nextTick(() => { return wrapper.vm.$nextTick(() => {
expect(vm.$el.classList.contains('frequent-items-list-container')).toBe(true); expect(wrapper.classes('frequent-items-list-container')).toBe(true);
expect(vm.$el.querySelectorAll('ul.list-unstyled').length).toBe(1); expect(wrapper.findAll({ ref: 'frequentItemsList' })).toHaveLength(1);
expect(vm.$el.querySelectorAll('li.frequent-items-list-item-container').length).toBe(5); expect(wrapper.findAll(frequentItemsListItemComponent)).toHaveLength(5);
done();
}); });
}); });
it('should render component element with empty message', done => { it('should render component element with empty message', () => {
vm.items = []; createComponent({
items: [],
});
Vue.nextTick(() => { return wrapper.vm.$nextTick(() => {
expect(vm.$el.querySelectorAll('li.section-empty').length).toBe(1); expect(wrapper.vm.$el.querySelectorAll('li.section-empty')).toHaveLength(1);
expect(vm.$el.querySelectorAll('li.frequent-items-list-item-container').length).toBe(0); expect(wrapper.findAll(frequentItemsListItemComponent)).toHaveLength(0);
done();
}); });
}); });
}); });
......
import { TEST_HOST } from 'helpers/test_constants'; import { TEST_HOST } from 'helpers/test_constants';
export default () => ({ export const mockFrequentProjects = [
{
id: 1,
name: 'GitLab Community Edition',
namespace: 'gitlab-org / gitlab-ce',
webUrl: `${TEST_HOST}/gitlab-org/gitlab-foss`,
avatarUrl: null,
frequency: 1,
lastAccessedOn: Date.now(),
},
{
id: 2,
name: 'GitLab CI',
namespace: 'gitlab-org / gitlab-ci',
webUrl: `${TEST_HOST}/gitlab-org/gitlab-ci`,
avatarUrl: null,
frequency: 9,
lastAccessedOn: Date.now(),
},
{
id: 3,
name: 'Typeahead.Js',
namespace: 'twitter / typeahead-js',
webUrl: `${TEST_HOST}/twitter/typeahead-js`,
avatarUrl: '/uploads/-/system/project/avatar/7/TWBS.png',
frequency: 2,
lastAccessedOn: Date.now(),
},
{
id: 4,
name: 'Intel',
namespace: 'platform / hardware / bsp / intel',
webUrl: `${TEST_HOST}/platform/hardware/bsp/intel`,
avatarUrl: null,
frequency: 3,
lastAccessedOn: Date.now(),
},
{
id: 5,
name: 'v4.4',
namespace: 'platform / hardware / bsp / kernel / common / v4.4',
webUrl: `${TEST_HOST}/platform/hardware/bsp/kernel/common/v4.4`,
avatarUrl: null,
frequency: 8,
lastAccessedOn: Date.now(),
},
];
export const mockProject = {
id: 1, id: 1,
name: 'GitLab Community Edition', name: 'GitLab Community Edition',
namespace: 'gitlab-org / gitlab-ce', namespace: 'gitlab-org / gitlab-ce',
webUrl: `${TEST_HOST}/gitlab-org/gitlab-foss`, webUrl: `${TEST_HOST}/gitlab-org/gitlab-foss`,
avatarUrl: null, avatarUrl: null,
}); };
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment