summaryrefslogtreecommitdiff
path: root/spec/frontend/wikis_spec.js
blob: 1d17c8b07779101ba335d09de859311eceec7d29 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import Wikis from '~/pages/projects/wikis/wikis';
import { setHTMLFixture } from './helpers/fixtures';

describe('Wikis', () => {
  describe('setting the commit message when the title changes', () => {
    const editFormHtmlFixture = args => `<form class="wiki-form ${
      args.newPage ? 'js-new-wiki-page' : ''
    }">
        <input type="text" id="wiki_title" value="My title" />
        <input type="text" id="wiki_message" />
        <select class="form-control select-control" name="wiki[format]" id="wiki_format">
          <option value="markdown">Markdown</option>
          <option selected="selected" value="rdoc">RDoc</option>
          <option value="asciidoc">AsciiDoc</option>
          <option value="org">Org</option>
        </select>
        <code class="js-markup-link-example">{Link title}[link:page-slug]</code>
      </form>
      `;

    let wikis;
    let titleInput;
    let messageInput;
    let changeFormatSelect;
    let linkExample;

    describe('when the wiki page is being created', () => {
      const formHtmlFixture = editFormHtmlFixture({ newPage: true });

      beforeEach(() => {
        setHTMLFixture(formHtmlFixture);

        titleInput = document.getElementById('wiki_title');
        messageInput = document.getElementById('wiki_message');
        changeFormatSelect = document.querySelector('#wiki_format');
        linkExample = document.querySelector('.js-markup-link-example');
        wikis = new Wikis();
      });

      it('binds an event listener to the title input', () => {
        wikis.handleWikiTitleChange = jest.fn();

        titleInput.dispatchEvent(new Event('keyup'));

        expect(wikis.handleWikiTitleChange).toHaveBeenCalled();
      });

      it('sets the commit message when title changes', () => {
        titleInput.value = 'My title';
        messageInput.value = '';

        titleInput.dispatchEvent(new Event('keyup'));

        expect(messageInput.value).toEqual('Create My title');
      });

      it('replaces hyphens with spaces', () => {
        titleInput.value = 'my-hyphenated-title';
        titleInput.dispatchEvent(new Event('keyup'));

        expect(messageInput.value).toEqual('Create my hyphenated title');
      });
    });

    describe('when the wiki page is being updated', () => {
      const formHtmlFixture = editFormHtmlFixture({ newPage: false });

      beforeEach(() => {
        setHTMLFixture(formHtmlFixture);

        titleInput = document.getElementById('wiki_title');
        messageInput = document.getElementById('wiki_message');
        wikis = new Wikis();
      });

      it('sets the commit message when title changes, prefixing with "Update"', () => {
        titleInput.value = 'My title';
        messageInput.value = '';

        titleInput.dispatchEvent(new Event('keyup'));

        expect(messageInput.value).toEqual('Update My title');
      });

      it.each`
        value         | text
        ${'markdown'} | ${'[Link Title](page-slug)'}
        ${'rdoc'}     | ${'{Link title}[link:page-slug]'}
        ${'asciidoc'} | ${'link:page-slug[Link title]'}
        ${'org'}      | ${'[[page-slug]]'}
      `('updates a message when value=$value is selected', ({ value, text }) => {
        changeFormatSelect.value = value;
        changeFormatSelect.dispatchEvent(new Event('change'));

        expect(linkExample.innerHTML).toBe(text);
      });
    });
  });
});