2019-03-02 22:35:43 +05:30
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2023-04-23 21:23:45 +05:30
|
|
|
import { HTTP_STATUS_INTERNAL_SERVER_ERROR, HTTP_STATUS_OK } from '~/lib/utils/http_status';
|
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(() => {
|
2023-04-23 21:23:45 +05:30
|
|
|
axiosMock.onGet(gon.sprite_icons).reply(HTTP_STATUS_OK, 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', () => {
|
2021-03-08 18:12:59 +05:30
|
|
|
return getSvgIconPathContent(mockName).then((path) => {
|
2020-04-08 14:13:33 +05:30
|
|
|
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', () => {
|
2021-03-08 18:12:59 +05:30
|
|
|
return getSvgIconPathContent('missing-icon').then((path) => {
|
2020-04-08 14:13:33 +05:30
|
|
|
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)
|
2023-04-23 21:23:45 +05:30
|
|
|
.replyOnce(HTTP_STATUS_INTERNAL_SERVER_ERROR)
|
2020-04-08 14:13:33 +05:30
|
|
|
.onGet(gon.sprite_icons)
|
2023-04-23 21:23:45 +05:30
|
|
|
.replyOnce(HTTP_STATUS_INTERNAL_SERVER_ERROR)
|
2020-04-08 14:13:33 +05:30
|
|
|
.onGet(gon.sprite_icons)
|
2023-04-23 21:23:45 +05:30
|
|
|
.reply(HTTP_STATUS_OK, mockIcons);
|
2020-04-08 14:13:33 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('returns null', () => {
|
2021-03-08 18:12:59 +05:30
|
|
|
return getSvgIconPathContent(mockName).then((path) => {
|
2020-04-08 14:13:33 +05:30
|
|
|
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)
|
2021-03-08 18:12:59 +05:30
|
|
|
.then((path1) => {
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(path1).toBe(null);
|
|
|
|
return getSvgIconPathContent(mockName);
|
|
|
|
})
|
2021-03-08 18:12:59 +05:30
|
|
|
.then((path2) => {
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(path2).toBe(null);
|
|
|
|
return getSvgIconPathContent(mockName);
|
|
|
|
})
|
2021-03-08 18:12:59 +05:30
|
|
|
.then((path3) => {
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(path3).toBe(mockPath);
|
|
|
|
});
|
2020-01-01 13:55:28 +05:30
|
|
|
});
|
2019-03-02 22:35:43 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|