diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/javascripts/ide/ide_router_spec.js | 150 | ||||
-rw-r--r-- | spec/javascripts/ide/mock_data.js | 11 | ||||
-rw-r--r-- | spec/javascripts/test_bundle.js | 20 |
3 files changed, 172 insertions, 9 deletions
diff --git a/spec/javascripts/ide/ide_router_spec.js b/spec/javascripts/ide/ide_router_spec.js new file mode 100644 index 00000000000..d496048bcb1 --- /dev/null +++ b/spec/javascripts/ide/ide_router_spec.js @@ -0,0 +1,150 @@ +import MockAdapter from 'axios-mock-adapter'; +import axios from '~/lib/utils/axios_utils'; +import router from '~/ide/ide_router'; +import store from '~/ide/stores'; +import { resetStore } from './helpers'; +import { project, branch, files } from './mock_data'; + +describe('IDE router', () => { + const originaPathName = location.pathname; + let mock; + + beforeEach(() => { + spyOn(history, 'pushState'); + spyOn(store, 'dispatch').and.callThrough(); + + mock = new MockAdapter(axios); + + mock.onGet('/api/v4/projects/namespace-123%2Fproject-123').reply(200, project); + mock + .onGet('/api/v4/projects/namespace-123%2Fproject-123/repository/branches/master') + .reply(200, branch); + mock.onGet('/namespace-123/project-123/files/master').reply(200, files); + + history.replaceState({}, '', router.options.base); + }); + + afterEach(done => { + mock.restore(); + + resetStore(store); + + router.push('/project', done); + }); + + afterAll(() => { + history.replaceState({}, '', originaPathName); + }); + + describe('project path', () => { + it('loads project data', done => { + router.push('/project/namespace-123/project-123/', () => { + expect(store.dispatch).toHaveBeenCalledWith('getProjectData', { + namespace: 'namespace-123', + projectId: 'project-123', + }); + + done(); + }); + }); + + it('loads project data without trailing slash', done => { + router.push('/project/namespace-123/project-123', () => { + expect(store.dispatch).toHaveBeenCalledWith('getProjectData', { + namespace: 'namespace-123', + projectId: 'project-123', + }); + + done(); + }); + }); + }); + + describe('branch data', () => { + it('loads branch data', done => { + router.push('/project/namespace-123/project-123/edit/master/', () => { + expect(store.dispatch.calls.count()).toBe(3); + expect(store.dispatch.calls.argsFor(1)).toEqual([ + 'getBranchData', + { + projectId: 'namespace-123/project-123', + branchId: 'master', + }, + ]); + + done(); + }); + }); + + it('loads branch data without trailing slash', done => { + router.push('/project/namespace-123/project-123/edit/master', () => { + expect(store.dispatch.calls.count()).toBe(3); + expect(store.dispatch.calls.argsFor(1)).toEqual([ + 'getBranchData', + { + projectId: 'namespace-123/project-123', + branchId: 'master', + }, + ]); + + done(); + }); + }); + + it('loads files for branch', done => { + router.push('/project/namespace-123/project-123/edit/master/', () => { + expect(store.dispatch.calls.argsFor(2)).toEqual([ + 'getFiles', + { + projectId: 'namespace-123/project-123', + branchId: 'master', + }, + ]); + + done(); + }); + }); + }); + + describe('setting folder open', () => { + it('calls handleTreeEntryAction with folder', done => { + router.push('/project/namespace-123/project-123/edit/master/folder', () => { + expect(store.dispatch.calls.argsFor(3)).toEqual([ + 'handleTreeEntryAction', + jasmine.anything(), + ]); + expect(store.dispatch.calls.argsFor(3)[1].path).toBe('folder'); + + done(); + }); + }); + + it('calls handleTreeEntryAction with folder with trailing slash', done => { + router.push('/project/namespace-123/project-123/edit/master/folder/', () => { + expect(store.dispatch.calls.argsFor(3)).toEqual([ + 'handleTreeEntryAction', + jasmine.anything(), + ]); + expect(store.dispatch.calls.argsFor(3)[1].path).toBe('folder'); + + done(); + }); + }); + + it('does not call handleTreeEntryAction when file is pending', done => { + router.push('/project/namespace-123/project-123/edit/master/folder', () => { + store.dispatch.calls.reset(); + store.state.entries['folder/index.js'].pending = true; + + router.push('/project/namespace-123/project-123/edit/master/folder/index.js', () => { + expect(store.dispatch.calls.argsFor(3)).not.toEqual([ + 'handleTreeEntryAction', + jasmine.anything(), + ]); + + done(); + }); + }); + }); + }); +}); diff --git a/spec/javascripts/ide/mock_data.js b/spec/javascripts/ide/mock_data.js new file mode 100644 index 00000000000..9303bec6757 --- /dev/null +++ b/spec/javascripts/ide/mock_data.js @@ -0,0 +1,11 @@ +export const project = { + web_url: '/namespace-123/project-123', +}; + +export const branch = { + commit: { + id: '123', + }, +}; + +export const files = ['folder/index.js']; diff --git a/spec/javascripts/test_bundle.js b/spec/javascripts/test_bundle.js index 1bcfdfe72b6..26892b5cec0 100644 --- a/spec/javascripts/test_bundle.js +++ b/spec/javascripts/test_bundle.js @@ -19,7 +19,7 @@ Vue.config.warnHandler = (msg, vm, trace) => { }; let hasVueErrors = false; -Vue.config.errorHandler = function (err) { +Vue.config.errorHandler = function(err) { hasVueErrors = true; fail(err); }; @@ -38,10 +38,12 @@ window.gl = window.gl || {}; window.gl.TEST_HOST = 'http://test.host'; window.gon = window.gon || {}; window.gon.test_env = true; +window.gon.relative_url_root = ''; +gon.api_version = 'v4'; let hasUnhandledPromiseRejections = false; -window.addEventListener('unhandledrejection', (event) => { +window.addEventListener('unhandledrejection', event => { hasUnhandledPromiseRejections = true; console.error('Unhandled promise rejection:'); console.error(event.reason.stack || event.reason); @@ -66,13 +68,13 @@ const axiosDefaultAdapter = getDefaultAdapter(); // render all of our tests const testsContext = require.context('.', true, /_spec$/); -testsContext.keys().forEach(function (path) { +testsContext.keys().forEach(function(path) { try { testsContext(path); } catch (err) { console.error('[ERROR] Unable to load spec: ', path); - describe('Test bundle', function () { - it(`includes '${path}'`, function () { + describe('Test bundle', function() { + it(`includes '${path}'`, function() { expect(err).toBeNull(); }); }); @@ -80,7 +82,7 @@ testsContext.keys().forEach(function (path) { }); describe('test errors', () => { - beforeAll((done) => { + beforeAll(done => { if (hasUnhandledPromiseRejections || hasVueWarnings || hasVueErrors) { setTimeout(done, 1000); } else { @@ -144,18 +146,18 @@ if (process.env.BABEL_ENV === 'coverage') { './issue_show/index.js', ]; - describe('Uncovered files', function () { + describe('Uncovered files', function() { const sourceFiles = require.context('~', true, /\.js$/); $.holdReady(true); - sourceFiles.keys().forEach(function (path) { + sourceFiles.keys().forEach(function(path) { // ignore if there is a matching spec file if (testsContext.keys().indexOf(`${path.replace(/\.js$/, '')}_spec`) > -1) { return; } - it(`includes '${path}'`, function () { + it(`includes '${path}'`, function() { try { sourceFiles(path); } catch (err) { |