Commit cd4c086d authored by Paul Slaughter's avatar Paul Slaughter Committed by Kushal Pandya

Move create_diffs_store to Jest

- Also make Karma's version a proxy to this
parent a6e7af1b
/* global Mousetrap */
// `mousetrap` uses amd which webpack understands but Jest does not
// Thankfully it also writes to a global export so we can es6-ify it
import 'mousetrap';
export default Mousetrap;
import Vuex from 'vuex'; import Vuex from 'vuex';
import { shallowMount, createLocalVue } from '@vue/test-utils'; import { shallowMount, createLocalVue } from '@vue/test-utils';
import { GlLoadingIcon } from '@gitlab/ui'; import { GlLoadingIcon } from '@gitlab/ui';
import MockAdapter from 'axios-mock-adapter';
import { TEST_HOST } from 'spec/test_constants'; import { TEST_HOST } from 'spec/test_constants';
import Mousetrap from 'mousetrap'; import Mousetrap from 'mousetrap';
import App from '~/diffs/components/app.vue'; import App from '~/diffs/components/app.vue';
...@@ -12,14 +13,17 @@ import CommitWidget from '~/diffs/components/commit_widget.vue'; ...@@ -12,14 +13,17 @@ import CommitWidget from '~/diffs/components/commit_widget.vue';
import TreeList from '~/diffs/components/tree_list.vue'; import TreeList from '~/diffs/components/tree_list.vue';
import { INLINE_DIFF_VIEW_TYPE, PARALLEL_DIFF_VIEW_TYPE } from '~/diffs/constants'; import { INLINE_DIFF_VIEW_TYPE, PARALLEL_DIFF_VIEW_TYPE } from '~/diffs/constants';
import createDiffsStore from '../create_diffs_store'; import createDiffsStore from '../create_diffs_store';
import axios from '~/lib/utils/axios_utils';
import diffsMockData from '../mock_data/merge_request_diffs'; import diffsMockData from '../mock_data/merge_request_diffs';
const mergeRequestDiff = { version_index: 1 }; const mergeRequestDiff = { version_index: 1 };
const TEST_ENDPOINT = `${TEST_HOST}/diff/endpoint`;
describe('diffs/components/app', () => { describe('diffs/components/app', () => {
const oldMrTabs = window.mrTabs; const oldMrTabs = window.mrTabs;
let store; let store;
let wrapper; let wrapper;
let mock;
function createComponent(props = {}, extendStore = () => {}) { function createComponent(props = {}, extendStore = () => {}) {
const localVue = createLocalVue(); const localVue = createLocalVue();
...@@ -34,7 +38,7 @@ describe('diffs/components/app', () => { ...@@ -34,7 +38,7 @@ describe('diffs/components/app', () => {
wrapper = shallowMount(localVue.extend(App), { wrapper = shallowMount(localVue.extend(App), {
localVue, localVue,
propsData: { propsData: {
endpoint: `${TEST_HOST}/diff/endpoint`, endpoint: TEST_ENDPOINT,
endpointMetadata: `${TEST_HOST}/diff/endpointMetadata`, endpointMetadata: `${TEST_HOST}/diff/endpointMetadata`,
endpointBatch: `${TEST_HOST}/diff/endpointBatch`, endpointBatch: `${TEST_HOST}/diff/endpointBatch`,
projectPath: 'namespace/project', projectPath: 'namespace/project',
...@@ -61,8 +65,12 @@ describe('diffs/components/app', () => { ...@@ -61,8 +65,12 @@ describe('diffs/components/app', () => {
beforeEach(() => { beforeEach(() => {
// setup globals (needed for component to mount :/) // setup globals (needed for component to mount :/)
window.mrTabs = jasmine.createSpyObj('mrTabs', ['resetViewContainer']); window.mrTabs = {
window.mrTabs.expandViewContainer = jasmine.createSpy(); resetViewContainer: jest.fn(),
};
window.mrTabs.expandViewContainer = jest.fn();
mock = new MockAdapter(axios);
mock.onGet(TEST_ENDPOINT).reply(200, {});
}); });
afterEach(() => { afterEach(() => {
...@@ -71,6 +79,8 @@ describe('diffs/components/app', () => { ...@@ -71,6 +79,8 @@ describe('diffs/components/app', () => {
// reset component // reset component
wrapper.destroy(); wrapper.destroy();
mock.restore();
}); });
describe('fetch diff methods', () => { describe('fetch diff methods', () => {
...@@ -80,15 +90,15 @@ describe('diffs/components/app', () => { ...@@ -80,15 +90,15 @@ describe('diffs/components/app', () => {
store.state.notes.discussions = 'test'; store.state.notes.discussions = 'test';
return Promise.resolve({ real_size: 100 }); return Promise.resolve({ real_size: 100 });
}; };
spyOn(window, 'requestIdleCallback').and.callFake(fn => fn()); jest.spyOn(window, 'requestIdleCallback').mockImplementation(fn => fn());
createComponent(); createComponent();
spyOn(wrapper.vm, 'fetchDiffFiles').and.callFake(fetchResolver); jest.spyOn(wrapper.vm, 'fetchDiffFiles').mockImplementation(fetchResolver);
spyOn(wrapper.vm, 'fetchDiffFilesMeta').and.callFake(fetchResolver); jest.spyOn(wrapper.vm, 'fetchDiffFilesMeta').mockImplementation(fetchResolver);
spyOn(wrapper.vm, 'fetchDiffFilesBatch').and.callFake(fetchResolver); jest.spyOn(wrapper.vm, 'fetchDiffFilesBatch').mockImplementation(fetchResolver);
spyOn(wrapper.vm, 'setDiscussions'); jest.spyOn(wrapper.vm, 'setDiscussions').mockImplementation(() => {});
spyOn(wrapper.vm, 'startRenderDiffsQueue'); jest.spyOn(wrapper.vm, 'startRenderDiffsQueue').mockImplementation(() => {});
spyOn(wrapper.vm, 'unwatchDiscussions'); jest.spyOn(wrapper.vm, 'unwatchDiscussions').mockImplementation(() => {});
spyOn(wrapper.vm, 'unwatchRetrievingBatches'); jest.spyOn(wrapper.vm, 'unwatchRetrievingBatches').mockImplementation(() => {});
store.state.diffs.retrievingBatches = true; store.state.diffs.retrievingBatches = true;
store.state.diffs.diffFiles = []; store.state.diffs.diffFiles = [];
wrapper.vm.$nextTick(done); wrapper.vm.$nextTick(done);
...@@ -236,7 +246,7 @@ describe('diffs/components/app', () => { ...@@ -236,7 +246,7 @@ describe('diffs/components/app', () => {
wrapper.vm.fetchData(false); wrapper.vm.fetchData(false);
expect(wrapper.vm.fetchDiffFiles).toHaveBeenCalled(); expect(wrapper.vm.fetchDiffFiles).toHaveBeenCalled();
setTimeout(() => { setImmediate(() => {
expect(wrapper.vm.startRenderDiffsQueue).toHaveBeenCalled(); expect(wrapper.vm.startRenderDiffsQueue).toHaveBeenCalled();
expect(wrapper.vm.fetchDiffFilesMeta).not.toHaveBeenCalled(); expect(wrapper.vm.fetchDiffFilesMeta).not.toHaveBeenCalled();
expect(wrapper.vm.fetchDiffFilesBatch).not.toHaveBeenCalled(); expect(wrapper.vm.fetchDiffFilesBatch).not.toHaveBeenCalled();
...@@ -255,7 +265,7 @@ describe('diffs/components/app', () => { ...@@ -255,7 +265,7 @@ describe('diffs/components/app', () => {
wrapper.vm.fetchData(false); wrapper.vm.fetchData(false);
expect(wrapper.vm.fetchDiffFiles).not.toHaveBeenCalled(); expect(wrapper.vm.fetchDiffFiles).not.toHaveBeenCalled();
setTimeout(() => { setImmediate(() => {
expect(wrapper.vm.startRenderDiffsQueue).toHaveBeenCalled(); expect(wrapper.vm.startRenderDiffsQueue).toHaveBeenCalled();
expect(wrapper.vm.fetchDiffFilesMeta).toHaveBeenCalled(); expect(wrapper.vm.fetchDiffFilesMeta).toHaveBeenCalled();
expect(wrapper.vm.fetchDiffFilesBatch).toHaveBeenCalled(); expect(wrapper.vm.fetchDiffFilesBatch).toHaveBeenCalled();
...@@ -272,7 +282,7 @@ describe('diffs/components/app', () => { ...@@ -272,7 +282,7 @@ describe('diffs/components/app', () => {
wrapper.vm.fetchData(false); wrapper.vm.fetchData(false);
expect(wrapper.vm.fetchDiffFiles).not.toHaveBeenCalled(); expect(wrapper.vm.fetchDiffFiles).not.toHaveBeenCalled();
setTimeout(() => { setImmediate(() => {
expect(wrapper.vm.startRenderDiffsQueue).toHaveBeenCalled(); expect(wrapper.vm.startRenderDiffsQueue).toHaveBeenCalled();
expect(wrapper.vm.fetchDiffFilesMeta).toHaveBeenCalled(); expect(wrapper.vm.fetchDiffFilesMeta).toHaveBeenCalled();
expect(wrapper.vm.fetchDiffFilesBatch).toHaveBeenCalled(); expect(wrapper.vm.fetchDiffFilesBatch).toHaveBeenCalled();
...@@ -350,23 +360,21 @@ describe('diffs/components/app', () => { ...@@ -350,23 +360,21 @@ describe('diffs/components/app', () => {
}); });
// Component uses $nextTick so we wait until that has finished // Component uses $nextTick so we wait until that has finished
setTimeout(() => { setImmediate(() => {
expect(store.state.diffs.highlightedRow).toBe('ABC_123'); expect(store.state.diffs.highlightedRow).toBe('ABC_123');
done(); done();
}); });
}); });
it('marks current diff file based on currently highlighted row', done => { it('marks current diff file based on currently highlighted row', () => {
createComponent({ createComponent({
shouldShow: true, shouldShow: true,
}); });
// Component uses $nextTick so we wait until that has finished // Component uses $nextTick so we wait until that has finished
setTimeout(() => { return wrapper.vm.$nextTick().then(() => {
expect(store.state.diffs.currentDiffFileId).toBe('ABC'); expect(store.state.diffs.currentDiffFileId).toBe('ABC');
done();
}); });
}); });
}); });
...@@ -403,7 +411,7 @@ describe('diffs/components/app', () => { ...@@ -403,7 +411,7 @@ describe('diffs/components/app', () => {
}); });
// Component uses $nextTick so we wait until that has finished // Component uses $nextTick so we wait until that has finished
setTimeout(() => { setImmediate(() => {
expect(store.state.diffs.currentDiffFileId).toBe('ABC'); expect(store.state.diffs.currentDiffFileId).toBe('ABC');
done(); done();
...@@ -449,7 +457,7 @@ describe('diffs/components/app', () => { ...@@ -449,7 +457,7 @@ describe('diffs/components/app', () => {
describe('visible app', () => { describe('visible app', () => {
beforeEach(() => { beforeEach(() => {
spy = jasmine.createSpy('spy'); spy = jest.fn();
createComponent({ createComponent({
shouldShow: true, shouldShow: true,
...@@ -459,21 +467,18 @@ describe('diffs/components/app', () => { ...@@ -459,21 +467,18 @@ describe('diffs/components/app', () => {
}); });
}); });
it('calls `jumpToFile()` with correct parameter whenever pre-defined key is pressed', done => { it.each(Object.keys(mappings))(
wrapper.vm 'calls `jumpToFile()` with correct parameter whenever pre-defined %s is pressed',
.$nextTick() key => {
.then(() => { return wrapper.vm.$nextTick().then(() => {
Object.keys(mappings).forEach(function(key) { expect(spy).not.toHaveBeenCalled();
Mousetrap.trigger(key);
expect(spy.calls.mostRecent().args).toEqual([mappings[key]]); Mousetrap.trigger(key);
});
expect(spy.calls.count()).toEqual(Object.keys(mappings).length); expect(spy).toHaveBeenCalledWith(mappings[key]);
})
.then(done)
.catch(done.fail);
}); });
},
);
it('does not call `jumpToFile()` when unknown key is pressed', done => { it('does not call `jumpToFile()` when unknown key is pressed', done => {
wrapper.vm wrapper.vm
...@@ -490,7 +495,7 @@ describe('diffs/components/app', () => { ...@@ -490,7 +495,7 @@ describe('diffs/components/app', () => {
describe('hideen app', () => { describe('hideen app', () => {
beforeEach(() => { beforeEach(() => {
spy = jasmine.createSpy('spy'); spy = jest.fn();
createComponent({ createComponent({
shouldShow: false, shouldShow: false,
...@@ -504,7 +509,7 @@ describe('diffs/components/app', () => { ...@@ -504,7 +509,7 @@ describe('diffs/components/app', () => {
wrapper.vm wrapper.vm
.$nextTick() .$nextTick()
.then(() => { .then(() => {
Object.keys(mappings).forEach(function(key) { Object.keys(mappings).forEach(key => {
Mousetrap.trigger(key); Mousetrap.trigger(key);
expect(spy).not.toHaveBeenCalled(); expect(spy).not.toHaveBeenCalled();
...@@ -520,7 +525,7 @@ describe('diffs/components/app', () => { ...@@ -520,7 +525,7 @@ describe('diffs/components/app', () => {
let spy; let spy;
beforeEach(() => { beforeEach(() => {
spy = jasmine.createSpy(); spy = jest.fn();
createComponent({}, () => { createComponent({}, () => {
store.state.diffs.diffFiles = [ store.state.diffs.diffFiles = [
...@@ -545,15 +550,15 @@ describe('diffs/components/app', () => { ...@@ -545,15 +550,15 @@ describe('diffs/components/app', () => {
.then(() => { .then(() => {
wrapper.vm.jumpToFile(+1); wrapper.vm.jumpToFile(+1);
expect(spy.calls.mostRecent().args).toEqual(['222.js']); expect(spy.mock.calls[spy.mock.calls.length - 1]).toEqual(['222.js']);
store.state.diffs.currentDiffFileId = '222'; store.state.diffs.currentDiffFileId = '222';
wrapper.vm.jumpToFile(+1); wrapper.vm.jumpToFile(+1);
expect(spy.calls.mostRecent().args).toEqual(['333.js']); expect(spy.mock.calls[spy.mock.calls.length - 1]).toEqual(['333.js']);
store.state.diffs.currentDiffFileId = '333'; store.state.diffs.currentDiffFileId = '333';
wrapper.vm.jumpToFile(-1); wrapper.vm.jumpToFile(-1);
expect(spy.calls.mostRecent().args).toEqual(['222.js']); expect(spy.mock.calls[spy.mock.calls.length - 1]).toEqual(['222.js']);
}) })
.then(done) .then(done)
.catch(done.fail); .catch(done.fail);
...@@ -602,7 +607,7 @@ describe('diffs/components/app', () => { ...@@ -602,7 +607,7 @@ describe('diffs/components/app', () => {
expect(wrapper.contains(CompareVersions)).toBe(true); expect(wrapper.contains(CompareVersions)).toBe(true);
expect(wrapper.find(CompareVersions).props()).toEqual( expect(wrapper.find(CompareVersions).props()).toEqual(
jasmine.objectContaining({ expect.objectContaining({
targetBranch: { targetBranch: {
branchName: 'target-branch', branchName: 'target-branch',
versionIndex: -1, versionIndex: -1,
...@@ -625,7 +630,7 @@ describe('diffs/components/app', () => { ...@@ -625,7 +630,7 @@ describe('diffs/components/app', () => {
expect(wrapper.contains(HiddenFilesWarning)).toBe(true); expect(wrapper.contains(HiddenFilesWarning)).toBe(true);
expect(wrapper.find(HiddenFilesWarning).props()).toEqual( expect(wrapper.find(HiddenFilesWarning).props()).toEqual(
jasmine.objectContaining({ expect.objectContaining({
total: '5', total: '5',
plainDiffPath: 'plain diff path', plainDiffPath: 'plain diff path',
emailPatchPath: 'email patch path', emailPatchPath: 'email patch path',
...@@ -663,7 +668,7 @@ describe('diffs/components/app', () => { ...@@ -663,7 +668,7 @@ describe('diffs/components/app', () => {
let toggleShowTreeList; let toggleShowTreeList;
beforeEach(() => { beforeEach(() => {
toggleShowTreeList = jasmine.createSpy('toggleShowTreeList'); toggleShowTreeList = jest.fn();
}); });
afterEach(() => { afterEach(() => {
......
import Vue from 'vue';
import Vuex from 'vuex';
import diffsModule from '~/diffs/store/modules';
import notesModule from '~/notes/stores/modules';
Vue.use(Vuex);
export default function createDiffsStore() {
return new Vuex.Store({
modules: {
diffs: diffsModule(),
notes: notesModule(),
},
});
}
/* eslint-disable class-methods-use-this */
export default class TreeWorkerMock {
addEventListener() {}
terminate() {}
postMessage() {}
}
import Vue from 'vue'; export { default } from '../../frontend/diffs/create_diffs_store';
import Vuex from 'vuex';
import diffsModule from '~/diffs/store/modules';
import notesModule from '~/notes/stores/modules';
Vue.use(Vuex);
export default function createDiffsStore() {
return new Vuex.Store({
modules: {
diffs: diffsModule(),
notes: notesModule(),
},
});
}
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