Commit c44b9979 authored by Kushal Pandya's avatar Kushal Pandya

Merge branch 'ps-use-gitlab-favicon-overlay' into 'master'

Use @gitlab/favicon-overlay instead of common_utils

See merge request gitlab-org/gitlab!50530
parents 5740a53b 7ea0b73c
...@@ -2,7 +2,7 @@ import Visibility from 'visibilityjs'; ...@@ -2,7 +2,7 @@ import Visibility from 'visibilityjs';
import * as types from './mutation_types'; import * as types from './mutation_types';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import Poll from '~/lib/utils/poll'; import Poll from '~/lib/utils/poll';
import { setFaviconOverlay, resetFavicon } from '~/lib/utils/common_utils'; import { setFaviconOverlay, resetFavicon } from '~/lib/utils/favicon';
import { deprecatedCreateFlash as flash } from '~/flash'; import { deprecatedCreateFlash as flash } from '~/flash';
import { __ } from '~/locale'; import { __ } from '~/locale';
import { import {
......
...@@ -6,7 +6,6 @@ import { GlBreakpointInstance as breakpointInstance } from '@gitlab/ui/dist/util ...@@ -6,7 +6,6 @@ import { GlBreakpointInstance as breakpointInstance } from '@gitlab/ui/dist/util
import $ from 'jquery'; import $ from 'jquery';
import { isFunction, defer } from 'lodash'; import { isFunction, defer } from 'lodash';
import Cookies from 'js-cookie'; import Cookies from 'js-cookie';
import axios from './axios_utils';
import { getLocationHash } from './url_utility'; import { getLocationHash } from './url_utility';
import { convertToCamelCase, convertToSnakeCase } from './text_utility'; import { convertToCamelCase, convertToSnakeCase } from './text_utility';
import { isObject } from './type_utility'; import { isObject } from './type_utility';
...@@ -548,92 +547,6 @@ export const backOff = (fn, timeout = 60000) => { ...@@ -548,92 +547,6 @@ export const backOff = (fn, timeout = 60000) => {
}); });
}; };
export const createOverlayIcon = (iconPath, overlayPath) => {
const faviconImage = document.createElement('img');
return new Promise((resolve) => {
faviconImage.onload = () => {
const size = 32;
const canvas = document.createElement('canvas');
canvas.width = size;
canvas.height = size;
const context = canvas.getContext('2d');
context.clearRect(0, 0, size, size);
context.drawImage(
faviconImage,
0,
0,
faviconImage.width,
faviconImage.height,
0,
0,
size,
size,
);
const overlayImage = document.createElement('img');
overlayImage.onload = () => {
context.drawImage(
overlayImage,
0,
0,
overlayImage.width,
overlayImage.height,
0,
0,
size,
size,
);
const faviconWithOverlayUrl = canvas.toDataURL();
resolve(faviconWithOverlayUrl);
};
overlayImage.src = overlayPath;
};
faviconImage.src = iconPath;
});
};
export const setFaviconOverlay = (overlayPath) => {
const faviconEl = document.getElementById('favicon');
if (!faviconEl) {
return null;
}
const iconPath = faviconEl.getAttribute('data-original-href');
return createOverlayIcon(iconPath, overlayPath).then((faviconWithOverlayUrl) =>
faviconEl.setAttribute('href', faviconWithOverlayUrl),
);
};
export const resetFavicon = () => {
const faviconEl = document.getElementById('favicon');
if (faviconEl) {
const originalFavicon = faviconEl.getAttribute('data-original-href');
faviconEl.setAttribute('href', originalFavicon);
}
};
export const setCiStatusFavicon = (pageUrl) =>
axios
.get(pageUrl)
.then(({ data }) => {
if (data && data.favicon) {
return setFaviconOverlay(data.favicon);
}
return resetFavicon();
})
.catch((error) => {
resetFavicon();
throw error;
});
export const spriteIcon = (icon, className = '') => { export const spriteIcon = (icon, className = '') => {
const classAttribute = className.length > 0 ? `class="${className}"` : ''; const classAttribute = className.length > 0 ? `class="${className}"` : '';
......
import { FaviconOverlayManager } from '@gitlab/favicon-overlay';
import { memoize } from 'lodash';
// FaviconOverlayManager is a glorious singleton/static class. Let's start to encapsulate that with this helper.
const getDefaultFaviconManager = memoize(async () => {
await FaviconOverlayManager.initialize({ faviconSelector: '#favicon' });
return FaviconOverlayManager;
});
export const setFaviconOverlay = async (path) => {
const manager = await getDefaultFaviconManager();
manager.setFaviconOverlay(path);
};
export const resetFavicon = async () => {
const manager = await getDefaultFaviconManager();
manager.resetFaviconOverlay();
};
/**
* Clears the cached memoization of the default manager.
*
* This is needed for determinism in tests.
*/
export const clearMemoizeCache = () => {
getDefaultFaviconManager.cache.clear();
};
import axios from './axios_utils';
import { setFaviconOverlay, resetFavicon } from './favicon';
export const setCiStatusFavicon = (pageUrl) =>
axios
.get(pageUrl)
.then(({ data }) => {
if (data && data.favicon) {
return setFaviconOverlay(data.favicon);
}
return resetFavicon();
})
.catch((error) => {
resetFavicon();
throw error;
});
import LinkedTabs from './lib/utils/bootstrap_linked_tabs'; import LinkedTabs from './lib/utils/bootstrap_linked_tabs';
import { setCiStatusFavicon } from './lib/utils/common_utils'; import { setCiStatusFavicon } from './lib/utils/favicon_ci';
export default class Pipelines { export default class Pipelines {
constructor(options = {}) { constructor(options = {}) {
......
...@@ -43,7 +43,7 @@ import SourceBranchRemovalStatus from './components/source_branch_removal_status ...@@ -43,7 +43,7 @@ import SourceBranchRemovalStatus from './components/source_branch_removal_status
import TerraformPlan from './components/terraform/mr_widget_terraform_container.vue'; import TerraformPlan from './components/terraform/mr_widget_terraform_container.vue';
import GroupedCodequalityReportsApp from '../reports/codequality_report/grouped_codequality_reports_app.vue'; import GroupedCodequalityReportsApp from '../reports/codequality_report/grouped_codequality_reports_app.vue';
import GroupedTestReportsApp from '../reports/components/grouped_test_reports_app.vue'; import GroupedTestReportsApp from '../reports/components/grouped_test_reports_app.vue';
import { setFaviconOverlay } from '../lib/utils/common_utils'; import { setFaviconOverlay } from '../lib/utils/favicon';
import GroupedAccessibilityReportsApp from '../reports/accessibility_report/grouped_accessibility_reports_app.vue'; import GroupedAccessibilityReportsApp from '../reports/accessibility_report/grouped_accessibility_reports_app.vue';
import getStateQuery from './queries/get_state.query.graphql'; import getStateQuery from './queries/get_state.query.graphql';
......
...@@ -91,7 +91,7 @@ module.exports = (path) => { ...@@ -91,7 +91,7 @@ module.exports = (path) => {
'^.+\\.(md|zip|png)$': 'jest-raw-loader', '^.+\\.(md|zip|png)$': 'jest-raw-loader',
}, },
transformIgnorePatterns: [ transformIgnorePatterns: [
'node_modules/(?!(@gitlab/ui|bootstrap-vue|three|monaco-editor|monaco-yaml|fast-mersenne-twister)/)', 'node_modules/(?!(@gitlab/ui|@gitlab/favicon-overlay|bootstrap-vue|three|monaco-editor|monaco-yaml|fast-mersenne-twister)/)',
], ],
timers: 'fake', timers: 'fake',
testEnvironment: '<rootDir>/spec/frontend/environment.js', testEnvironment: '<rootDir>/spec/frontend/environment.js',
......
...@@ -514,27 +514,6 @@ describe('common_utils', () => { ...@@ -514,27 +514,6 @@ describe('common_utils', () => {
}); });
}); });
describe('resetFavicon', () => {
beforeEach(() => {
const favicon = document.createElement('link');
favicon.setAttribute('id', 'favicon');
favicon.setAttribute('data-original-href', 'default/favicon');
document.body.appendChild(favicon);
});
afterEach(() => {
document.body.removeChild(document.getElementById('favicon'));
});
it('should reset page favicon to the default icon', () => {
const favicon = document.getElementById('favicon');
favicon.setAttribute('href', 'new/favicon');
commonUtils.resetFavicon();
expect(document.getElementById('favicon').getAttribute('href')).toEqual('default/favicon');
});
});
describe('spriteIcon', () => { describe('spriteIcon', () => {
let beforeGon; let beforeGon;
......
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
import { setFaviconOverlay, resetFavicon } from '~/lib/utils/favicon';
import { setCiStatusFavicon } from '~/lib/utils/favicon_ci';
jest.mock('~/lib/utils/favicon');
const TEST_URL = '/test/pipelinable/1';
const TEST_FAVICON = '/favicon.test.ico';
describe('~/lib/utils/favicon_ci', () => {
let mock;
beforeEach(() => {
mock = new MockAdapter(axios);
});
afterEach(() => {
mock.restore();
mock = null;
});
describe('setCiStatusFavicon', () => {
it.each`
response | setFaviconOverlayCalls | resetFaviconCalls
${{}} | ${[]} | ${[[]]}
${{ favicon: TEST_FAVICON }} | ${[[TEST_FAVICON]]} | ${[]}
`(
'with response=$response',
async ({ response, setFaviconOverlayCalls, resetFaviconCalls }) => {
mock.onGet(TEST_URL).replyOnce(200, response);
expect(setFaviconOverlay).not.toHaveBeenCalled();
expect(resetFavicon).not.toHaveBeenCalled();
await setCiStatusFavicon(TEST_URL);
expect(setFaviconOverlay.mock.calls).toEqual(setFaviconOverlayCalls);
expect(resetFavicon.mock.calls).toEqual(resetFaviconCalls);
},
);
it('with error', async () => {
mock.onGet(TEST_URL).replyOnce(500);
await expect(setCiStatusFavicon(TEST_URL)).rejects.toEqual(expect.any(Error));
expect(resetFavicon).toHaveBeenCalled();
});
});
});
import { FaviconOverlayManager } from '@gitlab/favicon-overlay';
import * as faviconUtils from '~/lib/utils/favicon';
jest.mock('@gitlab/favicon-overlay');
describe('~/lib/utils/favicon', () => {
afterEach(() => {
faviconUtils.clearMemoizeCache();
});
describe.each`
fnName | managerFn | args
${'setFaviconOverlay'} | ${FaviconOverlayManager.setFaviconOverlay} | ${['test']}
${'resetFavicon'} | ${FaviconOverlayManager.resetFaviconOverlay} | ${[]}
`('$fnName', ({ fnName, managerFn, args }) => {
const call = () => faviconUtils[fnName](...args);
it('initializes only once when called', async () => {
expect(FaviconOverlayManager.initialize).not.toHaveBeenCalled();
// Call twice so we can make sure initialize is only called once
await call();
await call();
expect(FaviconOverlayManager.initialize).toHaveBeenCalledWith({
faviconSelector: '#favicon',
});
expect(FaviconOverlayManager.initialize).toHaveBeenCalledTimes(1);
});
it('passes call to manager', async () => {
expect(managerFn).not.toHaveBeenCalled();
await call();
expect(managerFn).toHaveBeenCalledWith(...args);
});
});
});
...@@ -3,6 +3,3 @@ export const faviconDataUrl = ...@@ -3,6 +3,3 @@ export const faviconDataUrl =
export const overlayDataUrl = export const overlayDataUrl =
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAA85JREFUWAntVllIVGEUPv/9b46O41KplYN7PeRkti8TjQlhCUGh3MmeQugpIsGKAi2soIcIooiohxYKK2daqDAlIpIiWwxtQaJcaHE0d5tMrbn37z9XRqfR0TvVW56Hudf//uec72zfEWBCJjIwkYGJDPzvGSD/KgExN3Oi2Q+2DJgSDYQEMwItVGH1iZGmJw/Si1y+/PwVAMYYib22MYc/8hVQFgKDEfYoId0KYzagAQebsos/ewMZoeB9wdffcTYpQSaCTWHKoqSQaDk7zkIt0+aCUR8BelEHrf3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E/gfREpUiaQHqAizXbO0UN98BMTSo39Cw7UW7E2Rc728qJGHP68ASbQyNYCQTkAUzCSwQ+CwvSjnsQPGLOnI/C0YO3Lwxq5yhhtqb1KNpGqT1TXvigJU0jh33xpAf7NymoGNDJ9sJtPkYuNkqTh7KnY8vGaoeZPy93+GA1joe4kzzv/SVLqvYngA/dFgVfnlb8tjtm6Ux+I39y/Gqone24IQM+GxL15UO3q7WrhsnhJatCs8PAC9md3OrPK0goaDyEj7uXsuXi0qg4HkIUGE52XHNqmXIl0RGOiHoUV7xb+v5K14SC39At79Ximdhc8ekjImuiyjsXryUszLnY40yThIhSi4bbUHsbfBJ6ZKE5dpQdz4HQOgf2a8tLvklY+M6cuvSnJummxSZ46+X+7biMzaRnSu84IauNYsE5HCOX+HDCPWi7DrKW8/BTcVZ2UN8Me57kc5448TaCYR5XJwC0BtHMwPjs/SgAP1pfuCqSL8Pxhr/wunLWAOAAAAAElFTkSuQmCC';
...@@ -13,6 +13,9 @@ import './helpers/dom_shims'; ...@@ -13,6 +13,9 @@ import './helpers/dom_shims';
import './helpers/jquery'; import './helpers/jquery';
import '~/commons/bootstrap'; import '~/commons/bootstrap';
// This module has some fairly decent visual test coverage in it's own repository.
jest.mock('@gitlab/favicon-overlay');
process.on('unhandledRejection', global.promiseRejectionHandler); process.on('unhandledRejection', global.promiseRejectionHandler);
setupManualMocks(); setupManualMocks();
......
...@@ -7,6 +7,7 @@ import mrWidgetOptions from '~/vue_merge_request_widget/mr_widget_options.vue'; ...@@ -7,6 +7,7 @@ import mrWidgetOptions from '~/vue_merge_request_widget/mr_widget_options.vue';
import eventHub from '~/vue_merge_request_widget/event_hub'; import eventHub from '~/vue_merge_request_widget/event_hub';
import notify from '~/lib/utils/notify'; import notify from '~/lib/utils/notify';
import SmartInterval from '~/smart_interval'; import SmartInterval from '~/smart_interval';
import { setFaviconOverlay } from '~/lib/utils/favicon';
import { stateKey } from '~/vue_merge_request_widget/stores/state_maps'; import { stateKey } from '~/vue_merge_request_widget/stores/state_maps';
import mockData from './mock_data'; import mockData from './mock_data';
import { faviconDataUrl, overlayDataUrl } from '../lib/utils/mock_data'; import { faviconDataUrl, overlayDataUrl } from '../lib/utils/mock_data';
...@@ -14,6 +15,8 @@ import { SUCCESS } from '~/vue_merge_request_widget/components/deployment/consta ...@@ -14,6 +15,8 @@ import { SUCCESS } from '~/vue_merge_request_widget/components/deployment/consta
jest.mock('~/smart_interval'); jest.mock('~/smart_interval');
jest.mock('~/lib/utils/favicon');
const returnPromise = (data) => const returnPromise = (data) =>
new Promise((resolve) => { new Promise((resolve) => {
resolve({ resolve({
...@@ -421,21 +424,12 @@ describe('mrWidgetOptions', () => { ...@@ -421,21 +424,12 @@ describe('mrWidgetOptions', () => {
document.body.removeChild(document.getElementById('favicon')); document.body.removeChild(document.getElementById('favicon'));
}); });
it('should call setFavicon method', (done) => { it('should call setFavicon method', async () => {
vm.mr.ciStatusFaviconPath = overlayDataUrl; vm.mr.ciStatusFaviconPath = overlayDataUrl;
vm.setFaviconHelper()
.then(() => { await vm.setFaviconHelper();
/*
It would be better if we'd could mock commonUtils.setFaviconURL expect(setFaviconOverlay).toHaveBeenCalledWith(overlayDataUrl);
with a spy and test that it was called. We are doing the following
tests as a proxy to show that the function has been called
*/
expect(faviconElement.getAttribute('href')).not.toEqual(null);
expect(faviconElement.getAttribute('href')).not.toEqual(overlayDataUrl);
expect(faviconElement.getAttribute('href')).not.toEqual(faviconDataUrl);
})
.then(done)
.catch(done.fail);
}); });
it('should not call setFavicon when there is no ciStatusFaviconPath', (done) => { it('should not call setFavicon when there is no ciStatusFaviconPath', (done) => {
......
...@@ -5,30 +5,8 @@ ...@@ -5,30 +5,8 @@
* https://gitlab.com/groups/gitlab-org/-/epics/895#what-if-theres-a-karma-spec-which-is-simply-unmovable-to-jest-ie-it-is-dependent-on-a-running-browser-environment * https://gitlab.com/groups/gitlab-org/-/epics/895#what-if-theres-a-karma-spec-which-is-simply-unmovable-to-jest-ie-it-is-dependent-on-a-running-browser-environment
*/ */
import MockAdapter from 'axios-mock-adapter';
import { GlBreakpointInstance as breakpointInstance } from '@gitlab/ui/dist/utils'; import { GlBreakpointInstance as breakpointInstance } from '@gitlab/ui/dist/utils';
import axios from '~/lib/utils/axios_utils';
import * as commonUtils from '~/lib/utils/common_utils'; import * as commonUtils from '~/lib/utils/common_utils';
import { faviconDataUrl, overlayDataUrl, faviconWithOverlayDataUrl } from './mock_data';
const PIXEL_TOLERANCE = 0.2;
/**
* Loads a data URL as the src of an
* {@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/Image|Image}
* and resolves to that Image once loaded.
*
* @param url
* @returns {Promise}
*/
const urlToImage = (url) =>
new Promise((resolve) => {
const img = new Image();
img.onload = function () {
resolve(img);
};
img.src = url;
});
describe('common_utils browser specific specs', () => { describe('common_utils browser specific specs', () => {
describe('contentTop', () => { describe('contentTop', () => {
...@@ -63,90 +41,6 @@ describe('common_utils browser specific specs', () => { ...@@ -63,90 +41,6 @@ describe('common_utils browser specific specs', () => {
}); });
}); });
describe('createOverlayIcon', () => {
it('should return the favicon with the overlay', (done) => {
commonUtils
.createOverlayIcon(faviconDataUrl, overlayDataUrl)
.then((url) => Promise.all([urlToImage(url), urlToImage(faviconWithOverlayDataUrl)]))
.then(([actual, expected]) => {
expect(actual).toImageDiffEqual(expected, PIXEL_TOLERANCE);
done();
})
.catch(done.fail);
});
});
describe('setFaviconOverlay', () => {
beforeEach(() => {
const favicon = document.createElement('link');
favicon.setAttribute('id', 'favicon');
favicon.setAttribute('data-original-href', faviconDataUrl);
document.body.appendChild(favicon);
});
afterEach(() => {
document.body.removeChild(document.getElementById('favicon'));
});
it('should set page favicon to provided favicon overlay', (done) => {
commonUtils
.setFaviconOverlay(overlayDataUrl)
.then(() => document.getElementById('favicon').getAttribute('href'))
.then((url) => Promise.all([urlToImage(url), urlToImage(faviconWithOverlayDataUrl)]))
.then(([actual, expected]) => {
expect(actual).toImageDiffEqual(expected, PIXEL_TOLERANCE);
done();
})
.catch(done.fail);
});
});
describe('setCiStatusFavicon', () => {
const BUILD_URL = `${gl.TEST_HOST}/frontend-fixtures/builds-project/-/jobs/1/status.json`;
let mock;
beforeEach(() => {
const favicon = document.createElement('link');
favicon.setAttribute('id', 'favicon');
favicon.setAttribute('href', 'null');
favicon.setAttribute('data-original-href', faviconDataUrl);
document.body.appendChild(favicon);
mock = new MockAdapter(axios);
});
afterEach(() => {
mock.restore();
document.body.removeChild(document.getElementById('favicon'));
});
it('should reset favicon in case of error', (done) => {
mock.onGet(BUILD_URL).replyOnce(500);
commonUtils.setCiStatusFavicon(BUILD_URL).catch(() => {
const favicon = document.getElementById('favicon');
expect(favicon.getAttribute('href')).toEqual(faviconDataUrl);
done();
});
});
it('should set page favicon to CI status favicon based on provided status', (done) => {
mock.onGet(BUILD_URL).reply(200, {
favicon: overlayDataUrl,
});
commonUtils
.setCiStatusFavicon(BUILD_URL)
.then(() => document.getElementById('favicon').getAttribute('href'))
.then((url) => Promise.all([urlToImage(url), urlToImage(faviconWithOverlayDataUrl)]))
.then(([actual, expected]) => {
expect(actual).toImageDiffEqual(expected, PIXEL_TOLERANCE);
done();
})
.catch(done.fail);
});
});
describe('isInViewport', () => { describe('isInViewport', () => {
let el; let el;
......
...@@ -861,6 +861,11 @@ ...@@ -861,6 +861,11 @@
eslint-plugin-vue "^6.2.1" eslint-plugin-vue "^6.2.1"
vue-eslint-parser "^7.0.0" vue-eslint-parser "^7.0.0"
"@gitlab/favicon-overlay@2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@gitlab/favicon-overlay/-/favicon-overlay-2.0.0.tgz#2f32d0b6a4d5b8ac44e2927083d9ab478a78c984"
integrity sha512-GNcORxXJ98LVGzOT9dDYKfbheqH6lNgPDD72lyXRnQIH7CjgGyos8i17aSBPq1f4s3zF3PyedFiAR4YEZbva2Q==
"@gitlab/svgs@1.178.0": "@gitlab/svgs@1.178.0":
version "1.178.0" version "1.178.0"
resolved "https://registry.yarnpkg.com/@gitlab/svgs/-/svgs-1.178.0.tgz#069edb8abb4c7137d48f527592476655f066538b" resolved "https://registry.yarnpkg.com/@gitlab/svgs/-/svgs-1.178.0.tgz#069edb8abb4c7137d48f527592476655f066538b"
......
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