Commit 078dac42 authored by Fatih Acet's avatar Fatih Acet

Merge branch 'axios-common-utils-favicon' into 'master'

Use axios instead of jquery ajax for setCiStatusFavicon

See merge request gitlab-org/gitlab-ce!16784
parents a0fc0fcc 2f1a55fa
import { getLocationHash } from './url_utility'; import { getLocationHash } from './url_utility';
import axios from './axios_utils';
export const getPagePath = (index = 0) => $('body').attr('data-page').split(':')[index]; export const getPagePath = (index = 0) => $('body').attr('data-page').split(':')[index];
...@@ -382,22 +383,16 @@ export const resetFavicon = () => { ...@@ -382,22 +383,16 @@ export const resetFavicon = () => {
} }
}; };
export const setCiStatusFavicon = (pageUrl) => { export const setCiStatusFavicon = pageUrl =>
$.ajax({ axios.get(pageUrl)
url: pageUrl, .then(({ data }) => {
dataType: 'json',
success: (data) => {
if (data && data.favicon) { if (data && data.favicon) {
setFavicon(data.favicon); setFavicon(data.favicon);
} else { } else {
resetFavicon(); resetFavicon();
} }
}, })
error: () => { .catch(resetFavicon);
resetFavicon();
},
});
};
export const spriteIcon = (icon, className = '') => { export const spriteIcon = (icon, className = '') => {
const classAttribute = className.length > 0 ? `class="${className}"` : ''; const classAttribute = className.length > 0 ? `class="${className}"` : '';
......
...@@ -58,8 +58,7 @@ describe('Job', () => { ...@@ -58,8 +58,7 @@ describe('Job', () => {
it('updates the build trace on an interval', function () { it('updates the build trace on an interval', function () {
const deferred1 = $.Deferred(); const deferred1 = $.Deferred();
const deferred2 = $.Deferred(); const deferred2 = $.Deferred();
const deferred3 = $.Deferred(); spyOn($, 'ajax').and.returnValues(deferred1.promise(), deferred2.promise());
spyOn($, 'ajax').and.returnValues(deferred1.promise(), deferred2.promise(), deferred3.promise());
spyOn(urlUtils, 'visitUrl'); spyOn(urlUtils, 'visitUrl');
deferred1.resolve({ deferred1.resolve({
...@@ -70,9 +69,7 @@ describe('Job', () => { ...@@ -70,9 +69,7 @@ describe('Job', () => {
complete: false, complete: false,
}); });
deferred2.resolve(); deferred2.resolve({
deferred3.resolve({
html: '<span>More</span>', html: '<span>More</span>',
status: 'running', status: 'running',
state: 'finalstate', state: 'finalstate',
...@@ -94,9 +91,8 @@ describe('Job', () => { ...@@ -94,9 +91,8 @@ describe('Job', () => {
it('replaces the entire build trace', () => { it('replaces the entire build trace', () => {
const deferred1 = $.Deferred(); const deferred1 = $.Deferred();
const deferred2 = $.Deferred(); const deferred2 = $.Deferred();
const deferred3 = $.Deferred();
spyOn($, 'ajax').and.returnValues(deferred1.promise(), deferred2.promise(), deferred3.promise()); spyOn($, 'ajax').and.returnValues(deferred1.promise(), deferred2.promise());
spyOn(urlUtils, 'visitUrl'); spyOn(urlUtils, 'visitUrl');
...@@ -107,9 +103,7 @@ describe('Job', () => { ...@@ -107,9 +103,7 @@ describe('Job', () => {
complete: false, complete: false,
}); });
deferred2.resolve(); deferred2.resolve({
deferred3.resolve({
html: '<span>Different</span>', html: '<span>Different</span>',
status: 'running', status: 'running',
append: false, append: false,
...@@ -170,9 +164,8 @@ describe('Job', () => { ...@@ -170,9 +164,8 @@ describe('Job', () => {
it('shows incremented size', () => { it('shows incremented size', () => {
const deferred1 = $.Deferred(); const deferred1 = $.Deferred();
const deferred2 = $.Deferred(); const deferred2 = $.Deferred();
const deferred3 = $.Deferred();
spyOn($, 'ajax').and.returnValues(deferred1.promise(), deferred2.promise(), deferred3.promise()); spyOn($, 'ajax').and.returnValues(deferred1.promise(), deferred2.promise());
spyOn(urlUtils, 'visitUrl'); spyOn(urlUtils, 'visitUrl');
...@@ -184,8 +177,6 @@ describe('Job', () => { ...@@ -184,8 +177,6 @@ describe('Job', () => {
total: 100, total: 100,
}); });
deferred2.resolve();
this.job = new Job(); this.job = new Job();
expect( expect(
...@@ -194,7 +185,7 @@ describe('Job', () => { ...@@ -194,7 +185,7 @@ describe('Job', () => {
jasmine.clock().tick(4001); jasmine.clock().tick(4001);
deferred3.resolve({ deferred2.resolve({
html: '<span>Update</span>', html: '<span>Update</span>',
status: 'success', status: 'success',
append: true, append: true,
......
/* eslint-disable promise/catch-or-return */ /* eslint-disable promise/catch-or-return */
import * as commonUtils from '~/lib/utils/common_utils'; import * as commonUtils from '~/lib/utils/common_utils';
import axios from '~/lib/utils/axios_utils';
import MockAdapter from 'axios-mock-adapter';
describe('common_utils', () => { describe('common_utils', () => {
describe('parseUrl', () => { describe('parseUrl', () => {
...@@ -413,37 +415,48 @@ describe('common_utils', () => { ...@@ -413,37 +415,48 @@ describe('common_utils', () => {
describe('setCiStatusFavicon', () => { describe('setCiStatusFavicon', () => {
const BUILD_URL = `${gl.TEST_HOST}/frontend-fixtures/builds-project/-/jobs/1/status.json`; const BUILD_URL = `${gl.TEST_HOST}/frontend-fixtures/builds-project/-/jobs/1/status.json`;
let mock;
beforeEach(() => { beforeEach(() => {
const favicon = document.createElement('link'); const favicon = document.createElement('link');
favicon.setAttribute('id', 'favicon'); favicon.setAttribute('id', 'favicon');
document.body.appendChild(favicon); document.body.appendChild(favicon);
mock = new MockAdapter(axios);
}); });
afterEach(() => { afterEach(() => {
mock.restore();
document.body.removeChild(document.getElementById('favicon')); document.body.removeChild(document.getElementById('favicon'));
}); });
it('should reset favicon in case of error', () => { it('should reset favicon in case of error', (done) => {
const favicon = document.getElementById('favicon'); mock.onGet(BUILD_URL).networkError();
spyOn($, 'ajax').and.callFake(function (options) {
options.error();
expect(favicon.getAttribute('href')).toEqual('null');
});
commonUtils.setCiStatusFavicon(BUILD_URL); commonUtils.setCiStatusFavicon(BUILD_URL)
.then(() => {
const favicon = document.getElementById('favicon');
expect(favicon.getAttribute('href')).toEqual('null');
done();
})
// Error is already caught in catch() block of setCiStatusFavicon,
// It won't throw another error for us to catch
.catch(done.fail);
}); });
it('should set page favicon to CI status favicon based on provided status', () => { it('should set page favicon to CI status favicon based on provided status', (done) => {
const FAVICON_PATH = '//icon_status_success'; const FAVICON_PATH = '//icon_status_success';
const favicon = document.getElementById('favicon');
spyOn($, 'ajax').and.callFake(function (options) { mock.onGet(BUILD_URL).reply(200, {
options.success({ favicon: FAVICON_PATH }); favicon: FAVICON_PATH,
expect(favicon.getAttribute('href')).toEqual(FAVICON_PATH);
}); });
commonUtils.setCiStatusFavicon(BUILD_URL); commonUtils.setCiStatusFavicon(BUILD_URL)
.then(() => {
const favicon = document.getElementById('favicon');
expect(favicon.getAttribute('href')).toEqual(FAVICON_PATH);
done();
})
.catch(done.fail);
}); });
}); });
......
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