Commit 25186c7a authored by Tim Zallmann's avatar Tim Zallmann

Fixed Repo Sidebar Tests

parent 1429e5b5
...@@ -18,45 +18,46 @@ export default { ...@@ -18,45 +18,46 @@ export default {
}, },
created() { created() {
this.addPopEventListener(); window.addEventListener('popstate', this.checkHistory);
},
destroyed() {
window.removeEventListener('popstate', this.checkHistory);
}, },
data: () => Store, data: () => Store,
methods: { methods: {
addPopEventListener() { checkHistory() {
window.addEventListener('popstate', () => { let selectedFile = this.files.find(file => location.pathname.indexOf(file.url) > -1);
let selectedFile = this.files.find(file => location.pathname.indexOf(file.url) > -1); if (!selectedFile) {
if (!selectedFile) { // Maybe it is not in the current tree but in the opened tabs
// Maybe it is not in the current tree but in the opened tabs selectedFile = Store.openedFiles.find(file => location.pathname.indexOf(file.url) > -1);
selectedFile = Store.openedFiles.find(file => location.pathname.indexOf(file.url) > -1); }
if (selectedFile) {
if (selectedFile.url !== this.activeFile.url) {
this.fileClicked(selectedFile);
} }
if (selectedFile) {
if (selectedFile.url !== this.activeFile.url) {
this.fileClicked(selectedFile);
}
if (location.hash.indexOf('#L') > -1) { if (location.hash.indexOf('#L') > -1) {
const lineNumber = Number(location.hash.substr(2)); const lineNumber = Number(location.hash.substr(2));
if (!isNaN(lineNumber)) { if (!isNaN(lineNumber)) {
Store.setActiveLine(lineNumber); Store.setActiveLine(lineNumber);
if (Store.isPreviewView()) { if (Store.isPreviewView()) {
document.getElementById(`L${lineNumber}`).scrollIntoView(); document.getElementById(`L${lineNumber}`).scrollIntoView();
} else { } else {
Helper.monacoInstance.setPosition({ Helper.monacoInstance.setPosition({
lineNumber: this.activeLine, lineNumber: this.activeLine,
column: 1, column: 1,
}); });
}
} }
} }
} else {
// Not opened at all lets open new tab
this.fileClicked({
url: location.href,
});
} }
}); } else {
// Not opened at all lets open new tab
this.fileClicked({
url: location.href,
});
}
}, },
fileClicked(clickedFile) { fileClicked(clickedFile) {
......
...@@ -5,18 +5,26 @@ import RepoStore from '~/repo/stores/repo_store'; ...@@ -5,18 +5,26 @@ import RepoStore from '~/repo/stores/repo_store';
import repoSidebar from '~/repo/components/repo_sidebar.vue'; import repoSidebar from '~/repo/components/repo_sidebar.vue';
describe('RepoSidebar', () => { describe('RepoSidebar', () => {
let vm;
function createComponent() { function createComponent() {
const RepoSidebar = Vue.extend(repoSidebar); const RepoSidebar = Vue.extend(repoSidebar);
return new RepoSidebar().$mount(); return new RepoSidebar().$mount();
} }
afterEach(() => {
vm.$destroy();
});
it('renders a sidebar', () => { it('renders a sidebar', () => {
RepoStore.files = [{ RepoStore.files = [{
id: 0, id: 0,
}]; }];
RepoStore.openedFiles = []; RepoStore.openedFiles = [];
const vm = createComponent(); RepoStore.isRoot = false;
vm = createComponent();
const thead = vm.$el.querySelector('thead'); const thead = vm.$el.querySelector('thead');
const tbody = vm.$el.querySelector('tbody'); const tbody = vm.$el.querySelector('tbody');
...@@ -35,7 +43,7 @@ describe('RepoSidebar', () => { ...@@ -35,7 +43,7 @@ describe('RepoSidebar', () => {
RepoStore.openedFiles = [{ RepoStore.openedFiles = [{
id: 0, id: 0,
}]; }];
const vm = createComponent(); vm = createComponent();
expect(vm.$el.classList.contains('sidebar-mini')).toBeTruthy(); expect(vm.$el.classList.contains('sidebar-mini')).toBeTruthy();
expect(vm.$el.querySelector('thead')).toBeFalsy(); expect(vm.$el.querySelector('thead')).toBeFalsy();
...@@ -47,7 +55,7 @@ describe('RepoSidebar', () => { ...@@ -47,7 +55,7 @@ describe('RepoSidebar', () => {
tree: true, tree: true,
}; };
RepoStore.files = []; RepoStore.files = [];
const vm = createComponent(); vm = createComponent();
expect(vm.$el.querySelectorAll('tbody .loading-file').length).toEqual(5); expect(vm.$el.querySelectorAll('tbody .loading-file').length).toEqual(5);
}); });
...@@ -57,7 +65,7 @@ describe('RepoSidebar', () => { ...@@ -57,7 +65,7 @@ describe('RepoSidebar', () => {
id: 0, id: 0,
}]; }];
RepoStore.isRoot = true; RepoStore.isRoot = true;
const vm = createComponent(); vm = createComponent();
expect(vm.$el.querySelector('tbody .prev-directory')).toBeTruthy(); expect(vm.$el.querySelector('tbody .prev-directory')).toBeTruthy();
}); });
...@@ -103,7 +111,7 @@ describe('RepoSidebar', () => { ...@@ -103,7 +111,7 @@ describe('RepoSidebar', () => {
}; };
RepoStore.files = [file1]; RepoStore.files = [file1];
RepoStore.isRoot = true; RepoStore.isRoot = true;
const vm = createComponent(); vm = createComponent();
vm.fileClicked(file1); vm.fileClicked(file1);
...@@ -114,7 +122,7 @@ describe('RepoSidebar', () => { ...@@ -114,7 +122,7 @@ describe('RepoSidebar', () => {
describe('goToPreviousDirectoryClicked', () => { describe('goToPreviousDirectoryClicked', () => {
it('should hide files in directory if already open', () => { it('should hide files in directory if already open', () => {
const prevUrl = 'foo/bar'; const prevUrl = 'foo/bar';
const vm = createComponent(); vm = createComponent();
vm.goToPreviousDirectoryClicked(prevUrl); vm.goToPreviousDirectoryClicked(prevUrl);
...@@ -135,13 +143,15 @@ describe('RepoSidebar', () => { ...@@ -135,13 +143,15 @@ describe('RepoSidebar', () => {
RepoStore.openedFiles = [file1, file2]; RepoStore.openedFiles = [file1, file2];
RepoStore.isRoot = true; RepoStore.isRoot = true;
const vm = createComponent(); vm = createComponent();
vm.fileClicked(file1); vm.fileClicked(file1);
it('render previous file when using back button', () => { it('render previous file when using back button', () => {
spyOn(Helper, 'getContent').and.callThrough(); spyOn(Helper, 'getContent').and.callThrough();
vm.fileClicked(file2); vm.fileClicked(file2);
expect(Helper.getContent).toHaveBeenCalledWith(file2); expect(Helper.getContent).toHaveBeenCalledWith(file2);
Helper.getContent.calls.reset();
history.pushState({ history.pushState({
key: Math.random(), key: Math.random(),
...@@ -150,7 +160,7 @@ describe('RepoSidebar', () => { ...@@ -150,7 +160,7 @@ describe('RepoSidebar', () => {
popEvent.initEvent('popstate', true, true); popEvent.initEvent('popstate', true, true);
window.dispatchEvent(popEvent); window.dispatchEvent(popEvent);
expect(Helper.getContent).toHaveBeenCalledWith(file1); expect(Helper.getContent.calls.mostRecent().args[0].url).toContain(file1.url);
}); });
}); });
}); });
......
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