2018-07-06 09:40:11 -04:00
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
|
|
|
import Vue from 'vue';
|
2020-05-19 05:08:12 -04:00
|
|
|
import { mountComponentWithStore } from 'helpers/vue_mount_component_helper';
|
2019-12-10 04:07:51 -05:00
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2018-07-06 09:40:11 -04:00
|
|
|
import appComponent from '~/frequent_items/components/app.vue';
|
|
|
|
import eventHub from '~/frequent_items/event_hub';
|
|
|
|
import store from '~/frequent_items/store';
|
|
|
|
import { FREQUENT_ITEMS, HOUR_IN_MS } from '~/frequent_items/constants';
|
|
|
|
import { getTopFrequentItems } from '~/frequent_items/utils';
|
|
|
|
import { currentSession, mockFrequentProjects, mockSearchedProjects } from '../mock_data';
|
2020-05-19 05:08:12 -04:00
|
|
|
import { useLocalStorageSpy } from 'helpers/local_storage_helper';
|
|
|
|
import waitForPromises from 'helpers/wait_for_promises';
|
|
|
|
|
|
|
|
useLocalStorageSpy();
|
2018-07-06 09:40:11 -04:00
|
|
|
|
|
|
|
let session;
|
|
|
|
const createComponentWithStore = (namespace = 'projects') => {
|
|
|
|
session = currentSession[namespace];
|
|
|
|
gon.api_version = session.apiVersion;
|
|
|
|
const Component = Vue.extend(appComponent);
|
|
|
|
|
|
|
|
return mountComponentWithStore(Component, {
|
|
|
|
store,
|
|
|
|
props: {
|
|
|
|
namespace,
|
|
|
|
currentUserName: session.username,
|
|
|
|
currentItem: session.project || session.group,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
describe('Frequent Items App Component', () => {
|
|
|
|
let vm;
|
|
|
|
let mock;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
vm = createComponentWithStore();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
|
|
|
vm.$destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('methods', () => {
|
|
|
|
describe('dropdownOpenHandler', () => {
|
|
|
|
it('should fetch frequent items when no search has been previously made on desktop', () => {
|
2020-05-19 05:08:12 -04:00
|
|
|
jest.spyOn(vm, 'fetchFrequentItems').mockImplementation(() => {});
|
2018-07-06 09:40:11 -04:00
|
|
|
|
|
|
|
vm.dropdownOpenHandler();
|
|
|
|
|
|
|
|
expect(vm.fetchFrequentItems).toHaveBeenCalledWith();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('logItemAccess', () => {
|
|
|
|
let storage;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
storage = {};
|
|
|
|
|
2020-05-19 05:08:12 -04:00
|
|
|
localStorage.setItem.mockImplementation((storageKey, value) => {
|
2018-07-06 09:40:11 -04:00
|
|
|
storage[storageKey] = value;
|
|
|
|
});
|
|
|
|
|
2020-05-19 05:08:12 -04:00
|
|
|
localStorage.getItem.mockImplementation(storageKey => {
|
2018-07-06 09:40:11 -04:00
|
|
|
if (storage[storageKey]) {
|
|
|
|
return storage[storageKey];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create a project store if it does not exist and adds a project', () => {
|
|
|
|
vm.logItemAccess(session.storageKey, session.project);
|
|
|
|
|
|
|
|
const projects = JSON.parse(storage[session.storageKey]);
|
|
|
|
|
|
|
|
expect(projects.length).toBe(1);
|
|
|
|
expect(projects[0].frequency).toBe(1);
|
|
|
|
expect(projects[0].lastAccessedOn).toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should prevent inserting same report multiple times into store', () => {
|
|
|
|
vm.logItemAccess(session.storageKey, session.project);
|
|
|
|
vm.logItemAccess(session.storageKey, session.project);
|
|
|
|
|
|
|
|
const projects = JSON.parse(storage[session.storageKey]);
|
|
|
|
|
|
|
|
expect(projects.length).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should increase frequency of report if it was logged multiple times over the course of an hour', () => {
|
|
|
|
let projects;
|
|
|
|
const newTimestamp = Date.now() + HOUR_IN_MS + 1;
|
|
|
|
|
|
|
|
vm.logItemAccess(session.storageKey, session.project);
|
|
|
|
projects = JSON.parse(storage[session.storageKey]);
|
|
|
|
|
|
|
|
expect(projects[0].frequency).toBe(1);
|
|
|
|
|
|
|
|
vm.logItemAccess(session.storageKey, {
|
|
|
|
...session.project,
|
|
|
|
lastAccessedOn: newTimestamp,
|
|
|
|
});
|
|
|
|
projects = JSON.parse(storage[session.storageKey]);
|
|
|
|
|
|
|
|
expect(projects[0].frequency).toBe(2);
|
|
|
|
expect(projects[0].lastAccessedOn).not.toBe(session.project.lastAccessedOn);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should always update project metadata', () => {
|
|
|
|
let projects;
|
|
|
|
const oldProject = {
|
|
|
|
...session.project,
|
|
|
|
};
|
|
|
|
|
|
|
|
const newProject = {
|
|
|
|
...session.project,
|
|
|
|
name: 'New Name',
|
|
|
|
avatarUrl: 'new/avatar.png',
|
|
|
|
namespace: 'New / Namespace',
|
|
|
|
webUrl: 'http://localhost/new/web/url',
|
|
|
|
};
|
|
|
|
|
|
|
|
vm.logItemAccess(session.storageKey, oldProject);
|
|
|
|
projects = JSON.parse(storage[session.storageKey]);
|
|
|
|
|
|
|
|
expect(projects[0].name).toBe(oldProject.name);
|
|
|
|
expect(projects[0].avatarUrl).toBe(oldProject.avatarUrl);
|
|
|
|
expect(projects[0].namespace).toBe(oldProject.namespace);
|
|
|
|
expect(projects[0].webUrl).toBe(oldProject.webUrl);
|
|
|
|
|
|
|
|
vm.logItemAccess(session.storageKey, newProject);
|
|
|
|
projects = JSON.parse(storage[session.storageKey]);
|
|
|
|
|
|
|
|
expect(projects[0].name).toBe(newProject.name);
|
|
|
|
expect(projects[0].avatarUrl).toBe(newProject.avatarUrl);
|
|
|
|
expect(projects[0].namespace).toBe(newProject.namespace);
|
|
|
|
expect(projects[0].webUrl).toBe(newProject.webUrl);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not add more than 20 projects in store', () => {
|
|
|
|
for (let id = 0; id < FREQUENT_ITEMS.MAX_COUNT; id += 1) {
|
|
|
|
const project = {
|
|
|
|
...session.project,
|
|
|
|
id,
|
|
|
|
};
|
|
|
|
vm.logItemAccess(session.storageKey, project);
|
|
|
|
}
|
|
|
|
|
|
|
|
const projects = JSON.parse(storage[session.storageKey]);
|
|
|
|
|
|
|
|
expect(projects.length).toBe(FREQUENT_ITEMS.MAX_COUNT);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('created', () => {
|
|
|
|
it('should bind event listeners on eventHub', done => {
|
2020-05-19 05:08:12 -04:00
|
|
|
jest.spyOn(eventHub, '$on').mockImplementation(() => {});
|
2018-07-06 09:40:11 -04:00
|
|
|
|
|
|
|
createComponentWithStore().$mount();
|
|
|
|
|
|
|
|
Vue.nextTick(() => {
|
2020-05-19 05:08:12 -04:00
|
|
|
expect(eventHub.$on).toHaveBeenCalledWith('projects-dropdownOpen', expect.any(Function));
|
2018-07-06 09:40:11 -04:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('beforeDestroy', () => {
|
|
|
|
it('should unbind event listeners on eventHub', done => {
|
2020-05-19 05:08:12 -04:00
|
|
|
jest.spyOn(eventHub, '$off').mockImplementation(() => {});
|
2018-07-06 09:40:11 -04:00
|
|
|
|
|
|
|
vm.$mount();
|
|
|
|
vm.$destroy();
|
|
|
|
|
|
|
|
Vue.nextTick(() => {
|
2020-05-19 05:08:12 -04:00
|
|
|
expect(eventHub.$off).toHaveBeenCalledWith('projects-dropdownOpen', expect.any(Function));
|
2018-07-06 09:40:11 -04:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('template', () => {
|
|
|
|
it('should render search input', () => {
|
|
|
|
expect(vm.$el.querySelector('.search-input-container')).toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should render loading animation', done => {
|
|
|
|
vm.$store.dispatch('fetchSearchedItems');
|
|
|
|
|
|
|
|
Vue.nextTick(() => {
|
|
|
|
const loadingEl = vm.$el.querySelector('.loading-animation');
|
|
|
|
|
|
|
|
expect(loadingEl).toBeDefined();
|
|
|
|
expect(loadingEl.classList.contains('prepend-top-20')).toBe(true);
|
2019-03-18 08:09:07 -04:00
|
|
|
expect(loadingEl.querySelector('span').getAttribute('aria-label')).toBe('Loading projects');
|
2018-07-06 09:40:11 -04:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should render frequent projects list header', done => {
|
|
|
|
Vue.nextTick(() => {
|
|
|
|
const sectionHeaderEl = vm.$el.querySelector('.section-header');
|
|
|
|
|
|
|
|
expect(sectionHeaderEl).toBeDefined();
|
|
|
|
expect(sectionHeaderEl.innerText.trim()).toBe('Frequently visited');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should render frequent projects list', done => {
|
|
|
|
const expectedResult = getTopFrequentItems(mockFrequentProjects);
|
2020-05-19 05:08:12 -04:00
|
|
|
localStorage.getItem.mockImplementation(() => JSON.stringify(mockFrequentProjects));
|
2018-07-06 09:40:11 -04:00
|
|
|
|
|
|
|
expect(vm.$el.querySelectorAll('.frequent-items-list-container li').length).toBe(1);
|
|
|
|
|
|
|
|
vm.fetchFrequentItems();
|
|
|
|
Vue.nextTick(() => {
|
|
|
|
expect(vm.$el.querySelectorAll('.frequent-items-list-container li').length).toBe(
|
|
|
|
expectedResult.length,
|
|
|
|
);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should render searched projects list', done => {
|
|
|
|
mock.onGet(/\/api\/v4\/projects.json(.*)$/).replyOnce(200, mockSearchedProjects);
|
|
|
|
|
|
|
|
expect(vm.$el.querySelectorAll('.frequent-items-list-container li').length).toBe(1);
|
|
|
|
|
|
|
|
vm.$store.dispatch('setSearchQuery', 'gitlab');
|
2018-10-17 03:13:26 -04:00
|
|
|
vm.$nextTick()
|
2018-07-06 09:40:11 -04:00
|
|
|
.then(() => {
|
|
|
|
expect(vm.$el.querySelector('.loading-animation')).toBeDefined();
|
|
|
|
})
|
2020-05-19 05:08:12 -04:00
|
|
|
.then(waitForPromises)
|
2018-07-06 09:40:11 -04:00
|
|
|
.then(() => {
|
|
|
|
expect(vm.$el.querySelectorAll('.frequent-items-list-container li').length).toBe(
|
2019-11-12 13:06:57 -05:00
|
|
|
mockSearchedProjects.data.length,
|
2018-07-06 09:40:11 -04:00
|
|
|
);
|
|
|
|
})
|
|
|
|
.then(done)
|
|
|
|
.catch(done.fail);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|