2019-03-02 22:35:43 +05:30
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2020-04-08 14:13:33 +05:30
|
|
|
import { clearSvgIconPathContentCache, getSvgIconPathContent } from '~/lib/utils/icon_utils';
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
describe('Icon utils', () => {
|
|
|
|
describe('getSvgIconPathContent', () => {
|
|
|
|
let spriteIcons;
|
2020-04-08 14:13:33 +05:30
|
|
|
let axiosMock;
|
|
|
|
const mockName = 'mockIconName';
|
|
|
|
const mockPath = 'mockPath';
|
|
|
|
const mockIcons = `<svg><symbol id="${mockName}"><path d="${mockPath}"/></symbol></svg>`;
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
spriteIcons = gon.sprite_icons;
|
|
|
|
gon.sprite_icons = 'mockSpriteIconsEndpoint';
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
gon.sprite_icons = spriteIcons;
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
axiosMock = new MockAdapter(axios);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
axiosMock.restore();
|
2020-04-08 14:13:33 +05:30
|
|
|
clearSvgIconPathContentCache();
|
2019-03-02 22:35:43 +05:30
|
|
|
});
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
describe('when the icons can be loaded', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
axiosMock.onGet(gon.sprite_icons).reply(200, mockIcons);
|
2020-01-01 13:55:28 +05:30
|
|
|
});
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
it('extracts svg icon path content from sprite icons', () => {
|
|
|
|
return getSvgIconPathContent(mockName).then(path => {
|
|
|
|
expect(path).toBe(mockPath);
|
|
|
|
});
|
|
|
|
});
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
it('returns null if icon path content does not exist', () => {
|
|
|
|
return getSvgIconPathContent('missing-icon').then(path => {
|
|
|
|
expect(path).toBe(null);
|
|
|
|
});
|
2020-01-01 13:55:28 +05:30
|
|
|
});
|
2019-03-02 22:35:43 +05:30
|
|
|
});
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
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', () => {
|
|
|
|
return getSvgIconPathContent(mockName).then(path => {
|
|
|
|
expect(path).toBe(null);
|
|
|
|
});
|
|
|
|
});
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
it('extracts svg icon path content, after 2 attempts', () => {
|
|
|
|
return getSvgIconPathContent(mockName)
|
|
|
|
.then(path1 => {
|
|
|
|
expect(path1).toBe(null);
|
|
|
|
return getSvgIconPathContent(mockName);
|
|
|
|
})
|
|
|
|
.then(path2 => {
|
|
|
|
expect(path2).toBe(null);
|
|
|
|
return getSvgIconPathContent(mockName);
|
|
|
|
})
|
|
|
|
.then(path3 => {
|
|
|
|
expect(path3).toBe(mockPath);
|
|
|
|
});
|
2020-01-01 13:55:28 +05:30
|
|
|
});
|
2019-03-02 22:35:43 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|