summaryrefslogtreecommitdiff
path: root/spec/frontend/snippets_spec.js
blob: 5b391606371bce5a84f1e34650c97d5336c059da (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
import snippetEmbed from '~/snippet/snippet_embed';
import { loadHTMLFixture } from './helpers/fixtures';

describe('Snippets', () => {
  let embedBtn;
  let snippetUrlArea;
  let shareBtn;
  let scriptTag;

  const snippetUrl = 'http://test.host/snippets/1';

  beforeEach(() => {
    loadHTMLFixture('snippets/show.html');

    embedBtn = document.querySelector('.js-embed-btn');
    snippetUrlArea = document.querySelector('.js-snippet-url-area');
    shareBtn = document.querySelector('.js-share-btn');
  });

  it('selects the fields content when it is clicked', () => {
    jest.spyOn(snippetUrlArea, 'select');
    snippetEmbed();

    expect(snippetUrlArea.select).not.toHaveBeenCalled();
    snippetUrlArea.dispatchEvent(new Event('click'));
    expect(snippetUrlArea.select).toHaveBeenCalled();
  });

  describe('when the snippet url does not include params', () => {
    beforeEach(() => {
      snippetEmbed();

      scriptTag = `<script src="${snippetUrl}.js"></script>`;
    });

    it('shows the script tag as default', () => {
      expect(snippetUrlArea.value).toEqual(scriptTag);
    });

    it('sets the proper url depending on the button clicked', () => {
      shareBtn.dispatchEvent(new Event('click'));
      expect(snippetUrlArea.value).toEqual(snippetUrl);

      embedBtn.dispatchEvent(new Event('click'));
      expect(snippetUrlArea.value).toEqual(scriptTag);
    });
  });

  describe('when the snippet url includes params', () => {
    beforeEach(() => {
      scriptTag = `<script src="${snippetUrl}.js?foo=bar"></script>`;
      snippetUrlArea.value = scriptTag;
      snippetUrlArea.dataset.url = `${snippetUrl}?foo=bar`;

      snippetEmbed();
    });

    it('shows the script tag as default', () => {
      expect(snippetUrlArea.value).toEqual(scriptTag);
    });

    it('sets the proper url depending on the button clicked', () => {
      shareBtn.dispatchEvent(new Event('click'));
      expect(snippetUrlArea.value).toEqual(`${snippetUrl}?foo=bar`);

      embedBtn.dispatchEvent(new Event('click'));
      expect(snippetUrlArea.value).toEqual(scriptTag);
    });
  });
});