2019-01-24 00:06:02 +00:00
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2020-03-17 09:09:20 +00:00
|
|
|
import { clearSvgIconPathContentCache, getSvgIconPathContent } from '~/lib/utils/icon_utils';
|
2019-01-24 00:06:02 +00:00
|
|
|
|
|
|
|
describe('Icon utils', () => {
|
|
|
|
describe('getSvgIconPathContent', () => {
|
|
|
|
let spriteIcons;
|
2020-03-17 09:09:20 +00:00
|
|
|
let axiosMock;
|
|
|
|
const mockName = 'mockIconName';
|
|
|
|
const mockPath = 'mockPath';
|
|
|
|
const mockIcons = `<svg><symbol id="${mockName}"><path d="${mockPath}"/></symbol></svg>`;
|
2019-01-24 00:06:02 +00:00
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
spriteIcons = gon.sprite_icons;
|
|
|
|
gon.sprite_icons = 'mockSpriteIconsEndpoint';
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
gon.sprite_icons = spriteIcons;
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
axiosMock = new MockAdapter(axios);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
axiosMock.restore();
|
2020-03-17 09:09:20 +00:00
|
|
|
clearSvgIconPathContentCache();
|
2019-01-24 00:06:02 +00:00
|
|
|
});
|
|
|
|
|
2020-03-17 09:09:20 +00:00
|
|
|
describe('when the icons can be loaded', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
axiosMock.onGet(gon.sprite_icons).reply(200, mockIcons);
|
2019-12-03 00:06:28 +00:00
|
|
|
});
|
2019-01-24 00:06:02 +00:00
|
|
|
|
2020-03-17 09:09:20 +00:00
|
|
|
it('extracts svg icon path content from sprite icons', () => {
|
2020-12-23 21:10:24 +00:00
|
|
|
return getSvgIconPathContent(mockName).then((path) => {
|
2020-03-17 09:09:20 +00:00
|
|
|
expect(path).toBe(mockPath);
|
|
|
|
});
|
|
|
|
});
|
2019-12-03 00:06:28 +00:00
|
|
|
|
2020-03-17 09:09:20 +00:00
|
|
|
it('returns null if icon path content does not exist', () => {
|
2020-12-23 21:10:24 +00:00
|
|
|
return getSvgIconPathContent('missing-icon').then((path) => {
|
2020-03-17 09:09:20 +00:00
|
|
|
expect(path).toBe(null);
|
|
|
|
});
|
2019-12-03 00:06:28 +00:00
|
|
|
});
|
2019-01-24 00:06:02 +00:00
|
|
|
});
|
|
|
|
|
2020-03-17 09:09:20 +00:00
|
|
|
describe('when the icons cannot be loaded on the first 2 tries', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
axiosMock
|
|
|
|
.onGet(gon.sprite_icons)
|
|
|
|
.replyOnce(500)
|
|
|
|
.onGet(gon.sprite_icons)
|
|
|
|
.replyOnce(500)
|
|
|
|
.onGet(gon.sprite_icons)
|
|
|
|
.reply(200, mockIcons);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns null', () => {
|
2020-12-23 21:10:24 +00:00
|
|
|
return getSvgIconPathContent(mockName).then((path) => {
|
2020-03-17 09:09:20 +00:00
|
|
|
expect(path).toBe(null);
|
|
|
|
});
|
|
|
|
});
|
2019-12-03 00:06:28 +00:00
|
|
|
|
2020-03-17 09:09:20 +00:00
|
|
|
it('extracts svg icon path content, after 2 attempts', () => {
|
|
|
|
return getSvgIconPathContent(mockName)
|
2020-12-23 21:10:24 +00:00
|
|
|
.then((path1) => {
|
2020-03-17 09:09:20 +00:00
|
|
|
expect(path1).toBe(null);
|
|
|
|
return getSvgIconPathContent(mockName);
|
|
|
|
})
|
2020-12-23 21:10:24 +00:00
|
|
|
.then((path2) => {
|
2020-03-17 09:09:20 +00:00
|
|
|
expect(path2).toBe(null);
|
|
|
|
return getSvgIconPathContent(mockName);
|
|
|
|
})
|
2020-12-23 21:10:24 +00:00
|
|
|
.then((path3) => {
|
2020-03-17 09:09:20 +00:00
|
|
|
expect(path3).toBe(mockPath);
|
|
|
|
});
|
2019-12-03 00:06:28 +00:00
|
|
|
});
|
2019-01-24 00:06:02 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|