summaryrefslogtreecommitdiff
path: root/spec/frontend/lib/utils/icon_utils_spec.js
blob: db1f174703bfeb08a50b1923f930f11581c8c09a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
import { clearSvgIconPathContentCache, getSvgIconPathContent } from '~/lib/utils/icon_utils';

describe('Icon utils', () => {
  describe('getSvgIconPathContent', () => {
    let spriteIcons;
    let axiosMock;
    const mockName = 'mockIconName';
    const mockPath = 'mockPath';
    const mockIcons = `<svg><symbol id="${mockName}"><path d="${mockPath}"/></symbol></svg>`;

    beforeAll(() => {
      spriteIcons = gon.sprite_icons;
      gon.sprite_icons = 'mockSpriteIconsEndpoint';
    });

    afterAll(() => {
      gon.sprite_icons = spriteIcons;
    });

    beforeEach(() => {
      axiosMock = new MockAdapter(axios);
    });

    afterEach(() => {
      axiosMock.restore();
      clearSvgIconPathContentCache();
    });

    describe('when the icons can be loaded', () => {
      beforeEach(() => {
        axiosMock.onGet(gon.sprite_icons).reply(200, mockIcons);
      });

      it('extracts svg icon path content from sprite icons', () => {
        return getSvgIconPathContent(mockName).then((path) => {
          expect(path).toBe(mockPath);
        });
      });

      it('returns null if icon path content does not exist', () => {
        return getSvgIconPathContent('missing-icon').then((path) => {
          expect(path).toBe(null);
        });
      });
    });

    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);
        });
      });

      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);
          });
      });
    });
  });
});