Commit 572ffacd authored by Roman Kuba's avatar Roman Kuba Committed by Paul Slaughter

Ported existing spec from karma to jest

https://gitlab.com/gitlab-org/gitlab/-/merge_requests/25934
parent 80f584a7
import $ from 'jquery'; import $ from 'jquery';
import '~/behaviors/quick_submit'; import '~/behaviors/quick_submit';
describe('Quick Submit behavior', function() { describe('Quick Submit behavior', () => {
let testContext;
const keydownEvent = (options = { keyCode: 13, metaKey: true }) => $.Event('keydown', options); const keydownEvent = (options = { keyCode: 13, metaKey: true }) => $.Event('keydown', options);
preloadFixtures('snippets/show.html'); preloadFixtures('snippets/show.html');
beforeEach(() => { beforeEach(() => {
loadFixtures('snippets/show.html'); loadFixtures('snippets/show.html');
testContext = {};
testContext.spies = {
submit: jest.fn(),
};
$('form').submit(e => { $('form').submit(e => {
// Prevent a form submit from moving us off the testing page // Prevent a form submit from moving us off the testing page
e.preventDefault(); e.preventDefault();
// Explicitly call the spie to know this function get's not called
testContext.spies.submit();
}); });
this.spies = { testContext.textarea = $('.js-quick-submit textarea').first();
submit: spyOnEvent('form', 'submit'),
};
this.textarea = $('.js-quick-submit textarea').first();
});
afterEach(() => {
// Undo what we did to the shared <body>
$('body').removeAttr('data-page');
}); });
it('does not respond to other keyCodes', () => { it('does not respond to other keyCodes', () => {
this.textarea.trigger( testContext.textarea.trigger(
keydownEvent({ keydownEvent({
keyCode: 32, keyCode: 32,
}), }),
); );
expect(this.spies.submit).not.toHaveBeenTriggered(); expect(testContext.spies.submit).not.toHaveBeenCalled();
}); });
it('does not respond to Enter alone', () => { it('does not respond to Enter alone', () => {
this.textarea.trigger( testContext.textarea.trigger(
keydownEvent({ keydownEvent({
ctrlKey: false, ctrlKey: false,
metaKey: false, metaKey: false,
}), }),
); );
expect(this.spies.submit).not.toHaveBeenTriggered(); expect(testContext.spies.submit).not.toHaveBeenCalled();
}); });
it('does not respond to repeated events', () => { it('does not respond to repeated events', () => {
this.textarea.trigger( testContext.textarea.trigger(
keydownEvent({ keydownEvent({
repeat: true, repeat: true,
}), }),
); );
expect(this.spies.submit).not.toHaveBeenTriggered(); expect(testContext.spies.submit).not.toHaveBeenCalled();
}); });
it('disables input of type submit', () => { it('disables input of type submit', () => {
const submitButton = $('.js-quick-submit input[type=submit]'); const submitButton = $('.js-quick-submit input[type=submit]');
this.textarea.trigger(keydownEvent()); testContext.textarea.trigger(keydownEvent());
expect(submitButton).toBeDisabled(); expect(submitButton).toBeDisabled();
}); });
it('disables button of type submit', () => { it('disables button of type submit', () => {
const submitButton = $('.js-quick-submit input[type=submit]'); const submitButton = $('.js-quick-submit input[type=submit]');
this.textarea.trigger(keydownEvent()); testContext.textarea.trigger(keydownEvent());
expect(submitButton).toBeDisabled(); expect(submitButton).toBeDisabled();
}); });
...@@ -73,71 +75,79 @@ describe('Quick Submit behavior', function() { ...@@ -73,71 +75,79 @@ describe('Quick Submit behavior', function() {
const existingSubmit = $('.js-quick-submit input[type=submit]'); const existingSubmit = $('.js-quick-submit input[type=submit]');
// Add an extra submit button // Add an extra submit button
const newSubmit = $('<button type="submit">Submit it</button>'); const newSubmit = $('<button type="submit">Submit it</button>');
newSubmit.insertAfter(this.textarea); newSubmit.insertAfter(testContext.textarea);
const oldClick = spyOnEvent(existingSubmit, 'click'); const spies = {
const newClick = spyOnEvent(newSubmit, 'click'); oldClickSpy: jest.fn(),
newClickSpy: jest.fn(),
};
existingSubmit.on('click', () => {
spies.oldClickSpy();
});
newSubmit.on('click', () => {
spies.newClickSpy();
});
this.textarea.trigger(keydownEvent()); testContext.textarea.trigger(keydownEvent());
expect(oldClick).not.toHaveBeenTriggered(); expect(spies.oldClickSpy).not.toHaveBeenCalled();
expect(newClick).toHaveBeenTriggered(); expect(spies.newClickSpy).toHaveBeenCalled();
}); });
// We cannot stub `navigator.userAgent` for CI's `rake karma` task, so we'll // We cannot stub `navigator.userAgent` for CI's `rake karma` task, so we'll
// only run the tests that apply to the current platform // only run the tests that apply to the current platform
if (navigator.userAgent.match(/Macintosh/)) { if (navigator.userAgent.match(/Macintosh/)) {
describe('In Macintosh', () => { describe('In Macintosh', () => {
it('responds to Meta+Enter', () => { it('responds to Meta+Enter', () => {
this.textarea.trigger(keydownEvent()); testContext.textarea.trigger(keydownEvent());
expect(this.spies.submit).toHaveBeenTriggered(); expect(testContext.spies.submit).toHaveBeenCalled();
}); });
it('excludes other modifier keys', () => { it('excludes other modifier keys', () => {
this.textarea.trigger( testContext.textarea.trigger(
keydownEvent({ keydownEvent({
altKey: true, altKey: true,
}), }),
); );
this.textarea.trigger( testContext.textarea.trigger(
keydownEvent({ keydownEvent({
ctrlKey: true, ctrlKey: true,
}), }),
); );
this.textarea.trigger( testContext.textarea.trigger(
keydownEvent({ keydownEvent({
shiftKey: true, shiftKey: true,
}), }),
); );
expect(this.spies.submit).not.toHaveBeenTriggered(); expect(testContext.spies.submit).not.toHaveBeenCalled();
}); });
}); });
} else { } else {
it('responds to Ctrl+Enter', () => { it('responds to Ctrl+Enter', () => {
this.textarea.trigger(keydownEvent()); testContext.textarea.trigger(keydownEvent());
expect(this.spies.submit).toHaveBeenTriggered(); expect(testContext.spies.submit).toHaveBeenCalled();
}); });
it('excludes other modifier keys', () => { it('excludes other modifier keys', () => {
this.textarea.trigger( testContext.textarea.trigger(
keydownEvent({ keydownEvent({
altKey: true, altKey: true,
}), }),
); );
this.textarea.trigger( testContext.textarea.trigger(
keydownEvent({ keydownEvent({
metaKey: true, metaKey: true,
}), }),
); );
this.textarea.trigger( testContext.textarea.trigger(
keydownEvent({ keydownEvent({
shiftKey: true, shiftKey: true,
}), }),
); );
expect(this.spies.submit).not.toHaveBeenTriggered(); expect(testContext.spies.submit).not.toHaveBeenCalled();
}); });
} }
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment