diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2019-12-03 00:06:28 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2019-12-03 00:06:28 +0000 |
commit | 10d0e5693c0eed9fd9c40f4fadeda187237db6b5 (patch) | |
tree | 82a723f14e4a44146c7c5e2259b9c7d6d9b834bb /spec | |
parent | a19a376bf35b2009566e86b8190662c21ed7e2ba (diff) | |
download | gitlab-ce-10d0e5693c0eed9fd9c40f4fadeda187237db6b5.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec')
-rw-r--r-- | spec/frontend/lib/utils/accessor_spec.js (renamed from spec/javascripts/lib/utils/accessor_spec.js) | 15 | ||||
-rw-r--r-- | spec/frontend/lib/utils/dom_utils_spec.js (renamed from spec/javascripts/lib/utils/dom_utils_spec.js) | 38 | ||||
-rw-r--r-- | spec/frontend/lib/utils/file_upload_spec.js (renamed from spec/javascripts/lib/utils/file_upload_spec.js) | 6 | ||||
-rw-r--r-- | spec/frontend/lib/utils/highlight_spec.js (renamed from spec/javascripts/lib/utils/higlight_spec.js) | 0 | ||||
-rw-r--r-- | spec/frontend/lib/utils/icon_utils_spec.js (renamed from spec/javascripts/lib/utils/icon_utils_spec.js) | 39 | ||||
-rw-r--r-- | spec/frontend/lib/utils/text_markdown_spec.js (renamed from spec/javascripts/lib/utils/text_markdown_spec.js) | 8 | ||||
-rw-r--r-- | spec/frontend/lib/utils/users_cache_spec.js (renamed from spec/javascripts/lib/utils/users_cache_spec.js) | 49 | ||||
-rw-r--r-- | spec/helpers/application_helper_spec.rb | 84 | ||||
-rw-r--r-- | spec/views/layouts/application.html.haml_spec.rb | 47 |
9 files changed, 227 insertions, 59 deletions
diff --git a/spec/javascripts/lib/utils/accessor_spec.js b/spec/frontend/lib/utils/accessor_spec.js index 0045330e470..752a88296e6 100644 --- a/spec/javascripts/lib/utils/accessor_spec.js +++ b/spec/frontend/lib/utils/accessor_spec.js @@ -1,14 +1,18 @@ +import { useLocalStorageSpy } from 'helpers/local_storage_helper'; import AccessorUtilities from '~/lib/utils/accessor'; describe('AccessorUtilities', () => { + useLocalStorageSpy(); + const testError = new Error('test error'); describe('isPropertyAccessSafe', () => { let base; it('should return `true` if access is safe', () => { - base = { testProp: 'testProp' }; - + base = { + testProp: 'testProp', + }; expect(AccessorUtilities.isPropertyAccessSafe(base, 'testProp')).toBe(true); }); @@ -54,17 +58,12 @@ describe('AccessorUtilities', () => { }); describe('isLocalStorageAccessSafe', () => { - beforeEach(() => { - spyOn(window.localStorage, 'setItem'); - spyOn(window.localStorage, 'removeItem'); - }); - it('should return `true` if access is safe', () => { expect(AccessorUtilities.isLocalStorageAccessSafe()).toBe(true); }); it('should return `false` if access to .setItem isnt safe', () => { - window.localStorage.setItem.and.callFake(() => { + window.localStorage.setItem.mockImplementation(() => { throw testError; }); diff --git a/spec/javascripts/lib/utils/dom_utils_spec.js b/spec/frontend/lib/utils/dom_utils_spec.js index 2bcf37f35c7..10b4a10a8ff 100644 --- a/spec/javascripts/lib/utils/dom_utils_spec.js +++ b/spec/frontend/lib/utils/dom_utils_spec.js @@ -25,7 +25,7 @@ describe('DOM Utils', () => { addClassIfElementExists(childElement, className); - expect(childElement.classList).toContain(className); + expect(childElement.classList).toContainEqual(className); }); it('does not throw if element does not exist', () => { @@ -40,22 +40,44 @@ describe('DOM Utils', () => { describe('canScrollUp', () => { [1, 100].forEach(scrollTop => { it(`is true if scrollTop is > 0 (${scrollTop})`, () => { - expect(canScrollUp({ scrollTop })).toBe(true); + expect( + canScrollUp({ + scrollTop, + }), + ).toBe(true); }); }); [0, -10].forEach(scrollTop => { it(`is false if scrollTop is <= 0 (${scrollTop})`, () => { - expect(canScrollUp({ scrollTop })).toBe(false); + expect( + canScrollUp({ + scrollTop, + }), + ).toBe(false); }); }); it('is true if scrollTop is > margin', () => { - expect(canScrollUp({ scrollTop: TEST_MARGIN + 1 }, TEST_MARGIN)).toBe(true); + expect( + canScrollUp( + { + scrollTop: TEST_MARGIN + 1, + }, + TEST_MARGIN, + ), + ).toBe(true); }); it('is false if scrollTop is <= margin', () => { - expect(canScrollUp({ scrollTop: TEST_MARGIN }, TEST_MARGIN)).toBe(false); + expect( + canScrollUp( + { + scrollTop: TEST_MARGIN, + }, + TEST_MARGIN, + ), + ).toBe(false); }); }); @@ -63,7 +85,11 @@ describe('DOM Utils', () => { let element; beforeEach(() => { - element = { scrollTop: 7, offsetHeight: 22, scrollHeight: 30 }; + element = { + scrollTop: 7, + offsetHeight: 22, + scrollHeight: 30, + }; }); it('is true if element can be scrolled down', () => { diff --git a/spec/javascripts/lib/utils/file_upload_spec.js b/spec/frontend/lib/utils/file_upload_spec.js index 8f7092f63de..1255d6fc14f 100644 --- a/spec/javascripts/lib/utils/file_upload_spec.js +++ b/spec/frontend/lib/utils/file_upload_spec.js @@ -20,7 +20,7 @@ describe('File upload', () => { const btn = document.querySelector('.js-button'); const input = document.querySelector('.js-input'); - spyOn(input, 'click'); + jest.spyOn(input, 'click').mockReturnValue(); btn.click(); @@ -43,7 +43,7 @@ describe('File upload', () => { const btn = document.querySelector('.js-button'); fileUpload('.js-not-button', '.js-input'); - spyOn(input, 'click'); + jest.spyOn(input, 'click').mockReturnValue(); btn.click(); @@ -55,7 +55,7 @@ describe('File upload', () => { const btn = document.querySelector('.js-button'); fileUpload('.js-button', '.js-not-input'); - spyOn(input, 'click'); + jest.spyOn(input, 'click').mockReturnValue(); btn.click(); diff --git a/spec/javascripts/lib/utils/higlight_spec.js b/spec/frontend/lib/utils/highlight_spec.js index 638bbf65ae9..638bbf65ae9 100644 --- a/spec/javascripts/lib/utils/higlight_spec.js +++ b/spec/frontend/lib/utils/highlight_spec.js diff --git a/spec/javascripts/lib/utils/icon_utils_spec.js b/spec/frontend/lib/utils/icon_utils_spec.js index 3fd3940efe8..816d634ad15 100644 --- a/spec/javascripts/lib/utils/icon_utils_spec.js +++ b/spec/frontend/lib/utils/icon_utils_spec.js @@ -17,51 +17,44 @@ describe('Icon utils', () => { let axiosMock; let mockEndpoint; - let getIcon; const mockName = 'mockIconName'; const mockPath = 'mockPath'; + const getIcon = () => iconUtils.getSvgIconPathContent(mockName); beforeEach(() => { axiosMock = new MockAdapter(axios); mockEndpoint = axiosMock.onGet(gon.sprite_icons); - getIcon = iconUtils.getSvgIconPathContent(mockName); }); afterEach(() => { axiosMock.restore(); }); - it('extracts svg icon path content from sprite icons', done => { + it('extracts svg icon path content from sprite icons', () => { mockEndpoint.replyOnce( 200, `<svg><symbol id="${mockName}"><path d="${mockPath}"/></symbol></svg>`, ); - getIcon - .then(path => { - expect(path).toBe(mockPath); - done(); - }) - .catch(done.fail); + + return getIcon().then(path => { + expect(path).toBe(mockPath); + }); }); - it('returns null if icon path content does not exist', done => { + it('returns null if icon path content does not exist', () => { mockEndpoint.replyOnce(200, ``); - getIcon - .then(path => { - expect(path).toBe(null); - done(); - }) - .catch(done.fail); + + return getIcon().then(path => { + expect(path).toBe(null); + }); }); - it('returns null if an http error occurs', done => { + it('returns null if an http error occurs', () => { mockEndpoint.replyOnce(500); - getIcon - .then(path => { - expect(path).toBe(null); - done(); - }) - .catch(done.fail); + + return getIcon().then(path => { + expect(path).toBe(null); + }); }); }); }); diff --git a/spec/javascripts/lib/utils/text_markdown_spec.js b/spec/frontend/lib/utils/text_markdown_spec.js index df4029555bb..ba3e4020e66 100644 --- a/spec/javascripts/lib/utils/text_markdown_spec.js +++ b/spec/frontend/lib/utils/text_markdown_spec.js @@ -243,7 +243,7 @@ describe('init markdown', () => { }); it('uses ace editor insert text when editor is passed in', () => { - spyOn(editor, 'insert'); + jest.spyOn(editor, 'insert').mockReturnValue(); insertMarkdownText({ text: editor.getValue, @@ -258,7 +258,7 @@ describe('init markdown', () => { }); it('adds block tags on line above and below selection', () => { - spyOn(editor, 'insert'); + jest.spyOn(editor, 'insert').mockReturnValue(); const selected = 'this text \n is multiple \n lines'; const text = `before \n ${selected} \n after`; @@ -276,7 +276,7 @@ describe('init markdown', () => { }); it('uses ace editor to navigate back tag length when nothing is selected', () => { - spyOn(editor, 'navigateLeft'); + jest.spyOn(editor, 'navigateLeft').mockReturnValue(); insertMarkdownText({ text: editor.getValue, @@ -291,7 +291,7 @@ describe('init markdown', () => { }); it('ace editor does not navigate back when there is selected text', () => { - spyOn(editor, 'navigateLeft'); + jest.spyOn(editor, 'navigateLeft').mockReturnValue(); insertMarkdownText({ text: editor.getValue, diff --git a/spec/javascripts/lib/utils/users_cache_spec.js b/spec/frontend/lib/utils/users_cache_spec.js index acb5e024acd..7ed87123482 100644 --- a/spec/javascripts/lib/utils/users_cache_spec.js +++ b/spec/frontend/lib/utils/users_cache_spec.js @@ -4,7 +4,10 @@ import UsersCache from '~/lib/utils/users_cache'; describe('UsersCache', () => { const dummyUsername = 'win'; const dummyUserId = 123; - const dummyUser = { name: 'has a farm', username: 'farmer' }; + const dummyUser = { + name: 'has a farm', + username: 'farmer', + }; const dummyUserStatus = 'my status'; beforeEach(() => { @@ -68,7 +71,6 @@ describe('UsersCache', () => { it('does nothing if cache contains no matching data', () => { UsersCache.internalStorage['no body'] = 'no data'; - UsersCache.remove(dummyUsername); expect(UsersCache.internalStorage['no body']).toBe('no data'); @@ -76,7 +78,6 @@ describe('UsersCache', () => { it('removes matching data', () => { UsersCache.internalStorage[dummyUsername] = dummyUser; - UsersCache.remove(dummyUsername); expect(UsersCache.internalStorage).toEqual({}); @@ -87,13 +88,16 @@ describe('UsersCache', () => { let apiSpy; beforeEach(() => { - spyOn(Api, 'users').and.callFake((query, options) => apiSpy(query, options)); + jest.spyOn(Api, 'users').mockImplementation((query, options) => apiSpy(query, options)); }); it('stores and returns data from API call if cache is empty', done => { apiSpy = (query, options) => { expect(query).toBe(''); - expect(options).toEqual({ username: dummyUsername }); + expect(options).toEqual({ + username: dummyUsername, + }); + return Promise.resolve({ data: [dummyUser], }); @@ -110,14 +114,18 @@ describe('UsersCache', () => { it('returns undefined if Ajax call fails and cache is empty', done => { const dummyError = new Error('server exploded'); + apiSpy = (query, options) => { expect(query).toBe(''); - expect(options).toEqual({ username: dummyUsername }); + expect(options).toEqual({ + username: dummyUsername, + }); + return Promise.reject(dummyError); }; UsersCache.retrieve(dummyUsername) - .then(user => fail(`Received unexpected user: ${JSON.stringify(user)}`)) + .then(user => done.fail(`Received unexpected user: ${JSON.stringify(user)}`)) .catch(error => { expect(error).toBe(dummyError); }) @@ -127,7 +135,8 @@ describe('UsersCache', () => { it('makes no Ajax call if matching data exists', done => { UsersCache.internalStorage[dummyUsername] = dummyUser; - apiSpy = () => fail(new Error('expected no Ajax call!')); + + apiSpy = () => done.fail(new Error('expected no Ajax call!')); UsersCache.retrieve(dummyUsername) .then(user => { @@ -142,12 +151,13 @@ describe('UsersCache', () => { let apiSpy; beforeEach(() => { - spyOn(Api, 'user').and.callFake(id => apiSpy(id)); + jest.spyOn(Api, 'user').mockImplementation(id => apiSpy(id)); }); it('stores and returns data from API call if cache is empty', done => { apiSpy = id => { expect(id).toBe(dummyUserId); + return Promise.resolve({ data: dummyUser, }); @@ -164,13 +174,15 @@ describe('UsersCache', () => { it('returns undefined if Ajax call fails and cache is empty', done => { const dummyError = new Error('server exploded'); + apiSpy = id => { expect(id).toBe(dummyUserId); + return Promise.reject(dummyError); }; UsersCache.retrieveById(dummyUserId) - .then(user => fail(`Received unexpected user: ${JSON.stringify(user)}`)) + .then(user => done.fail(`Received unexpected user: ${JSON.stringify(user)}`)) .catch(error => { expect(error).toBe(dummyError); }) @@ -180,7 +192,8 @@ describe('UsersCache', () => { it('makes no Ajax call if matching data exists', done => { UsersCache.internalStorage[dummyUserId] = dummyUser; - apiSpy = () => fail(new Error('expected no Ajax call!')); + + apiSpy = () => done.fail(new Error('expected no Ajax call!')); UsersCache.retrieveById(dummyUserId) .then(user => { @@ -195,12 +208,13 @@ describe('UsersCache', () => { let apiSpy; beforeEach(() => { - spyOn(Api, 'userStatus').and.callFake(id => apiSpy(id)); + jest.spyOn(Api, 'userStatus').mockImplementation(id => apiSpy(id)); }); it('stores and returns data from API call if cache is empty', done => { apiSpy = id => { expect(id).toBe(dummyUserId); + return Promise.resolve({ data: dummyUserStatus, }); @@ -217,13 +231,15 @@ describe('UsersCache', () => { it('returns undefined if Ajax call fails and cache is empty', done => { const dummyError = new Error('server exploded'); + apiSpy = id => { expect(id).toBe(dummyUserId); + return Promise.reject(dummyError); }; UsersCache.retrieveStatusById(dummyUserId) - .then(userStatus => fail(`Received unexpected user: ${JSON.stringify(userStatus)}`)) + .then(userStatus => done.fail(`Received unexpected user: ${JSON.stringify(userStatus)}`)) .catch(error => { expect(error).toBe(dummyError); }) @@ -232,8 +248,11 @@ describe('UsersCache', () => { }); it('makes no Ajax call if matching data exists', done => { - UsersCache.internalStorage[dummyUserId] = { status: dummyUserStatus }; - apiSpy = () => fail(new Error('expected no Ajax call!')); + UsersCache.internalStorage[dummyUserId] = { + status: dummyUserStatus, + }; + + apiSpy = () => done.fail(new Error('expected no Ajax call!')); UsersCache.retrieveStatusById(dummyUserId) .then(userStatus => { diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index d3d25d3cb74..a0c85863150 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -235,4 +235,88 @@ describe ApplicationHelper do end end end + + describe '#body_data' do + context 'when @project is not set' do + it 'does not include project data in the body data elements' do + expect(helper.body_data).to eq( + { + page: 'application', + page_type_id: nil, + find_file: nil, + group: '' + } + ) + end + + context 'when @group is set' do + it 'sets group in the body data elements' do + group = create(:group) + + assign(:group, group) + + expect(helper.body_data).to eq( + { + page: 'application', + page_type_id: nil, + find_file: nil, + group: group.path + } + ) + end + end + end + + context 'when @project is set' do + it 'includes all possible body data elements and associates the project elements with project' do + project = create(:project) + + assign(:project, project) + + expect(helper.body_data).to eq( + { + page: 'application', + page_type_id: nil, + find_file: nil, + group: '', + project_id: project.id, + project: project.name, + namespace_id: project.namespace.id + } + ) + end + + context 'when controller is issues' do + before do + stub_controller_method(:controller_path, 'projects:issues') + end + + context 'when params[:id] is present and the issue exsits and action_name is show' do + it 'sets all project and id elements correctly related to the issue' do + issue = create(:issue) + stub_controller_method(:action_name, 'show') + stub_controller_method(:params, { id: issue.id }) + + assign(:project, issue.project) + + expect(helper.body_data).to eq( + { + page: 'projects:issues:show', + page_type_id: issue.id, + find_file: nil, + group: '', + project_id: issue.project.id, + project: issue.project.name, + namespace_id: issue.project.namespace.id + } + ) + end + end + end + end + + def stub_controller_method(method_name, value) + allow(helper.controller).to receive(method_name).and_return(value) + end + end end diff --git a/spec/views/layouts/application.html.haml_spec.rb b/spec/views/layouts/application.html.haml_spec.rb new file mode 100644 index 00000000000..bdd4a97a1f5 --- /dev/null +++ b/spec/views/layouts/application.html.haml_spec.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe 'layouts/application' do + let(:user) { create(:user) } + + before do + allow(view).to receive(:current_application_settings).and_return(Gitlab::CurrentSettings.current_application_settings) + allow(view).to receive(:experiment_enabled?).and_return(false) + allow(view).to receive(:session).and_return({}) + allow(view).to receive(:user_signed_in?).and_return(true) + allow(view).to receive(:current_user).and_return(user) + end + + context 'body data elements for pageview context' do + let(:body_data) do + { + body_data_page: 'projects:issues:show', + body_data_page_type_id: '1', + body_data_project_id: '2', + body_data_namespace_id: '3' + } + end + + before do + allow(view).to receive(:body_data).and_return(body_data) + render + end + + it 'includes the body element page' do + expect(rendered).to include('data-page="projects:issues:show"') + end + + it 'includes the body element page_type_id' do + expect(rendered).to include('data-page-type-id="1"') + end + + it 'includes the body element project_id' do + expect(rendered).to include('data-project-id="2"') + end + + it 'includes the body element namespace_id' do + expect(rendered).to include('data-namespace-id="3"') + end + end +end |