2020-07-28 23:09:34 +05:30
|
|
|
import { escape } from 'lodash';
|
2021-03-08 18:12:59 +05:30
|
|
|
import { setHTMLFixture } from 'helpers/fixtures';
|
2020-06-23 00:09:42 +05:30
|
|
|
import Wikis from '~/pages/shared/wikis/wikis';
|
2020-07-28 23:09:34 +05:30
|
|
|
import Tracking from '~/tracking';
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
describe('Wikis', () => {
|
2021-03-08 18:12:59 +05:30
|
|
|
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>
|
|
|
|
<textarea id="wiki_content"></textarea>
|
|
|
|
<code class="js-markup-link-example">{Link title}[link:page-slug]</code>
|
|
|
|
<input type="submit" class="js-wiki-btn-submit">
|
|
|
|
</input>
|
|
|
|
</form>
|
|
|
|
`;
|
|
|
|
|
|
|
|
let wikis;
|
|
|
|
let titleInput;
|
|
|
|
let contentInput;
|
|
|
|
let messageInput;
|
|
|
|
let changeFormatSelect;
|
|
|
|
let linkExample;
|
|
|
|
|
|
|
|
const findBeforeUnloadWarning = () => window.onbeforeunload?.();
|
|
|
|
const findForm = () => document.querySelector('.wiki-form');
|
|
|
|
const findSubmitButton = () => document.querySelector('.js-wiki-btn-submit');
|
|
|
|
|
|
|
|
describe('when the wiki page is being created', () => {
|
|
|
|
const formHtmlFixture = editFormHtmlFixture({ newPage: true });
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
beforeEach(() => {
|
|
|
|
setHTMLFixture(formHtmlFixture);
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
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();
|
|
|
|
});
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
it('binds an event listener to the title input', () => {
|
|
|
|
wikis.handleWikiTitleChange = jest.fn();
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
titleInput.dispatchEvent(new Event('keyup'));
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
expect(wikis.handleWikiTitleChange).toHaveBeenCalled();
|
|
|
|
});
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
it('sets the commit message when title changes', () => {
|
|
|
|
titleInput.value = 'My title';
|
|
|
|
messageInput.value = '';
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
titleInput.dispatchEvent(new Event('keyup'));
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
expect(messageInput.value).toEqual('Create My title');
|
|
|
|
});
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
it('replaces hyphens with spaces', () => {
|
|
|
|
titleInput.value = 'my-hyphenated-title';
|
|
|
|
titleInput.dispatchEvent(new Event('keyup'));
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
expect(messageInput.value).toEqual('Create my hyphenated title');
|
2019-12-04 20:38:33 +05:30
|
|
|
});
|
2021-03-08 18:12:59 +05:30
|
|
|
});
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
describe('when the wiki page is being updated', () => {
|
|
|
|
const formHtmlFixture = editFormHtmlFixture({ newPage: false });
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
beforeEach(() => {
|
|
|
|
setHTMLFixture(formHtmlFixture);
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
titleInput = document.getElementById('wiki_title');
|
|
|
|
messageInput = document.getElementById('wiki_message');
|
|
|
|
wikis = new Wikis();
|
|
|
|
});
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
it('sets the commit message when title changes, prefixing with "Update"', () => {
|
|
|
|
titleInput.value = 'My title';
|
|
|
|
messageInput.value = '';
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
titleInput.dispatchEvent(new Event('keyup'));
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('starts with no unload warning', () => {
|
|
|
|
expect(findBeforeUnloadWarning()).toBeUndefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when wiki content is updated', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
contentInput = document.getElementById('wiki_content');
|
|
|
|
contentInput.value = 'Lorem ipsum dolar sit!';
|
|
|
|
contentInput.dispatchEvent(new Event('input'));
|
2019-12-04 20:38:33 +05:30
|
|
|
});
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
it('sets before unload warning', () => {
|
|
|
|
expect(findBeforeUnloadWarning()).toBe('');
|
2020-04-08 14:13:33 +05:30
|
|
|
});
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
it('when form submitted, unsets before unload warning', () => {
|
|
|
|
findForm().dispatchEvent(new Event('submit'));
|
2020-05-24 23:13:21 +05:30
|
|
|
expect(findBeforeUnloadWarning()).toBeUndefined();
|
|
|
|
});
|
2021-03-08 18:12:59 +05:30
|
|
|
});
|
|
|
|
});
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
describe('submit button state', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
setHTMLFixture(editFormHtmlFixture({ newPage: true }));
|
|
|
|
|
|
|
|
titleInput = document.getElementById('wiki_title');
|
|
|
|
contentInput = document.getElementById('wiki_content');
|
|
|
|
|
|
|
|
wikis = new Wikis();
|
2019-12-04 20:38:33 +05:30
|
|
|
});
|
2021-03-08 18:12:59 +05:30
|
|
|
|
|
|
|
it.each`
|
|
|
|
title | text | buttonState | disabledAttr
|
|
|
|
${'something'} | ${'something'} | ${'enabled'} | ${null}
|
|
|
|
${''} | ${'something'} | ${'disabled'} | ${'true'}
|
|
|
|
${'something'} | ${''} | ${'disabled'} | ${'true'}
|
|
|
|
${''} | ${''} | ${'disabled'} | ${'true'}
|
|
|
|
${' '} | ${' '} | ${'disabled'} | ${'true'}
|
|
|
|
`(
|
|
|
|
"when title='$title', content='$content', then, buttonState='$buttonState'",
|
|
|
|
({ title, text, disabledAttr }) => {
|
|
|
|
titleInput.value = title;
|
|
|
|
titleInput.dispatchEvent(new Event('keyup'));
|
|
|
|
|
|
|
|
contentInput.value = text;
|
|
|
|
contentInput.dispatchEvent(new Event('input'));
|
|
|
|
|
|
|
|
expect(findSubmitButton().getAttribute('disabled')).toBe(disabledAttr);
|
|
|
|
},
|
|
|
|
);
|
2019-12-04 20:38:33 +05:30
|
|
|
});
|
2020-07-28 23:09:34 +05:30
|
|
|
|
|
|
|
describe('trackPageView', () => {
|
|
|
|
const trackingPage = 'projects:wikis:show';
|
|
|
|
const trackingContext = { foo: 'bar' };
|
|
|
|
const showPageHtmlFixture = `
|
|
|
|
<div class="js-wiki-page-content" data-tracking-context="${escape(
|
|
|
|
JSON.stringify(trackingContext),
|
|
|
|
)}"></div>
|
|
|
|
`;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
setHTMLFixture(showPageHtmlFixture);
|
|
|
|
document.body.dataset.page = trackingPage;
|
|
|
|
jest.spyOn(Tracking, 'event').mockImplementation();
|
|
|
|
|
|
|
|
Wikis.trackPageView();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sends the tracking event and context', () => {
|
|
|
|
expect(Tracking.event).toHaveBeenCalledWith(trackingPage, 'view_wiki_page', {
|
|
|
|
label: 'view_wiki_page',
|
|
|
|
context: {
|
2021-01-03 14:25:43 +05:30
|
|
|
schema: 'iglu:com.gitlab/wiki_page_context/jsonschema/1-0-1',
|
2020-07-28 23:09:34 +05:30
|
|
|
data: trackingContext,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-12-04 20:38:33 +05:30
|
|
|
});
|