Commit 7ff33777 authored by Mike Greiling's avatar Mike Greiling

Merge branch '221086-clean-up-awards-handler-spec' into 'master'

Resolve "Flaky test in spec/frontend/awards_handler_spec.js"

Closes #221086

See merge request gitlab-org/gitlab!36754
parents 791dda51 6fc54299
...@@ -13,11 +13,6 @@ import * as Emoji from '~/emoji'; ...@@ -13,11 +13,6 @@ import * as Emoji from '~/emoji';
const animationEndEventString = 'animationend webkitAnimationEnd MSAnimationEnd oAnimationEnd'; const animationEndEventString = 'animationend webkitAnimationEnd MSAnimationEnd oAnimationEnd';
const transitionEndEventString = 'transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd'; const transitionEndEventString = 'transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd';
const requestAnimationFrame =
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.setTimeout;
const FROM_SENTENCE_REGEX = /(?:, and | and |, )/; // For separating lists produced by ruby's Array#toSentence const FROM_SENTENCE_REGEX = /(?:, and | and |, )/; // For separating lists produced by ruby's Array#toSentence
......
...@@ -3,64 +3,51 @@ import Cookies from 'js-cookie'; ...@@ -3,64 +3,51 @@ import Cookies from 'js-cookie';
import MockAdapter from 'axios-mock-adapter'; import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import loadAwardsHandler from '~/awards_handler'; import loadAwardsHandler from '~/awards_handler';
import '~/lib/utils/common_utils'; import { setTestTimeout } from './helpers/timeout';
import waitForPromises from './helpers/wait_for_promises';
import { EMOJI_VERSION } from '~/emoji'; import { EMOJI_VERSION } from '~/emoji';
import { useFakeRequestAnimationFrame } from 'helpers/fake_request_animation_frame';
window.gl = window.gl || {}; window.gl = window.gl || {};
window.gon = window.gon || {}; window.gon = window.gon || {};
let openAndWaitForEmojiMenu;
let mock; let mock;
let awardsHandler = null; let awardsHandler = null;
const urlRoot = gon.relative_url_root; const urlRoot = gon.relative_url_root;
const lazyAssert = (done, assertFn) => {
jest.runOnlyPendingTimers();
waitForPromises()
.then(() => {
assertFn();
done();
})
.catch(e => {
throw e;
});
};
describe('AwardsHandler', () => { describe('AwardsHandler', () => {
useFakeRequestAnimationFrame();
const emojiData = getJSONFixture('emojis/emojis.json'); const emojiData = getJSONFixture('emojis/emojis.json');
preloadFixtures('snippets/show.html'); preloadFixtures('snippets/show.html');
beforeEach(done => { const openAndWaitForEmojiMenu = (sel = '.js-add-award') => {
mock = new MockAdapter(axios); $(sel)
mock.onGet(`/-/emojis/${EMOJI_VERSION}/emojis.json`).reply(200, emojiData);
loadFixtures('snippets/show.html');
loadAwardsHandler(true)
.then(obj => {
awardsHandler = obj;
jest.spyOn(awardsHandler, 'postEmoji').mockImplementation((button, url, emoji, cb) => cb());
done();
})
.catch(done.fail);
let isEmojiMenuBuilt = false;
openAndWaitForEmojiMenu = () => {
return new Promise(resolve => {
if (isEmojiMenuBuilt) {
resolve();
} else {
$('.js-add-award')
.eq(0) .eq(0)
.click(); .click();
jest.advanceTimersByTime(200);
const $menu = $('.emoji-menu'); const $menu = $('.emoji-menu');
return new Promise(resolve => {
$menu.one('build-emoji-menu-finish', () => { $menu.one('build-emoji-menu-finish', () => {
isEmojiMenuBuilt = true;
resolve(); resolve();
}); });
}
}); });
}; };
beforeEach(async () => {
// These tests have had some timeout issues
// https://gitlab.com/gitlab-org/gitlab/-/issues/221086
setTestTimeout(6000);
mock = new MockAdapter(axios);
mock.onGet(`/-/emojis/${EMOJI_VERSION}/emojis.json`).reply(200, emojiData);
loadFixtures('snippets/show.html');
awardsHandler = await loadAwardsHandler(true);
jest.spyOn(awardsHandler, 'postEmoji').mockImplementation((button, url, emoji, cb) => cb());
}); });
afterEach(() => { afterEach(() => {
...@@ -76,11 +63,9 @@ describe('AwardsHandler', () => { ...@@ -76,11 +63,9 @@ describe('AwardsHandler', () => {
}); });
describe('::showEmojiMenu', () => { describe('::showEmojiMenu', () => {
it('should show emoji menu when Add emoji button clicked', done => { it('should show emoji menu when Add emoji button clicked', async () => {
$('.js-add-award') await openAndWaitForEmojiMenu();
.eq(0)
.click();
lazyAssert(done, () => {
const $emojiMenu = $('.emoji-menu'); const $emojiMenu = $('.emoji-menu');
expect($emojiMenu.length).toBe(1); expect($emojiMenu.length).toBe(1);
...@@ -88,22 +73,18 @@ describe('AwardsHandler', () => { ...@@ -88,22 +73,18 @@ describe('AwardsHandler', () => {
expect($emojiMenu.find('.js-emoji-menu-search').length).toBe(1); expect($emojiMenu.find('.js-emoji-menu-search').length).toBe(1);
expect($('.js-awards-block.current').length).toBe(1); expect($('.js-awards-block.current').length).toBe(1);
}); });
});
it('should also show emoji menu for the smiley icon in notes', done => { it('should also show emoji menu for the smiley icon in notes', async () => {
$('.js-add-award.note-action-button').click(); await openAndWaitForEmojiMenu('.js-add-award.note-action-button');
lazyAssert(done, () => {
const $emojiMenu = $('.emoji-menu'); const $emojiMenu = $('.emoji-menu');
expect($emojiMenu.length).toBe(1); expect($emojiMenu.length).toBe(1);
}); });
});
it('should remove emoji menu when body is clicked', done => { it('should remove emoji menu when body is clicked', async () => {
$('.js-add-award') await openAndWaitForEmojiMenu();
.eq(0)
.click();
lazyAssert(done, () => {
const $emojiMenu = $('.emoji-menu'); const $emojiMenu = $('.emoji-menu');
$('body').click(); $('body').click();
...@@ -111,13 +92,10 @@ describe('AwardsHandler', () => { ...@@ -111,13 +92,10 @@ describe('AwardsHandler', () => {
expect($emojiMenu.hasClass('is-visible')).toBe(false); expect($emojiMenu.hasClass('is-visible')).toBe(false);
expect($('.js-awards-block.current').length).toBe(0); expect($('.js-awards-block.current').length).toBe(0);
}); });
});
it('should not remove emoji menu when search is clicked', done => { it('should not remove emoji menu when search is clicked', async () => {
$('.js-add-award') await openAndWaitForEmojiMenu();
.eq(0)
.click();
lazyAssert(done, () => {
const $emojiMenu = $('.emoji-menu'); const $emojiMenu = $('.emoji-menu');
$('.emoji-search').click(); $('.emoji-search').click();
...@@ -126,7 +104,6 @@ describe('AwardsHandler', () => { ...@@ -126,7 +104,6 @@ describe('AwardsHandler', () => {
expect($('.js-awards-block.current').length).toBe(1); expect($('.js-awards-block.current').length).toBe(1);
}); });
}); });
});
describe('::addAwardToEmojiBar', () => { describe('::addAwardToEmojiBar', () => {
it('should add emoji to votes block', () => { it('should add emoji to votes block', () => {
...@@ -272,9 +249,9 @@ describe('AwardsHandler', () => { ...@@ -272,9 +249,9 @@ describe('AwardsHandler', () => {
}); });
describe('::searchEmojis', () => { describe('::searchEmojis', () => {
it('should filter the emoji', done => { it('should filter the emoji', async () => {
openAndWaitForEmojiMenu() await openAndWaitForEmojiMenu();
.then(() => {
expect($('[data-name=angel]').is(':visible')).toBe(true); expect($('[data-name=angel]').is(':visible')).toBe(true);
expect($('[data-name=anger]').is(':visible')).toBe(true); expect($('[data-name=anger]').is(':visible')).toBe(true);
awardsHandler.searchEmojis('ali'); awardsHandler.searchEmojis('ali');
...@@ -283,16 +260,11 @@ describe('AwardsHandler', () => { ...@@ -283,16 +260,11 @@ describe('AwardsHandler', () => {
expect($('[data-name=anger]').is(':visible')).toBe(false); expect($('[data-name=anger]').is(':visible')).toBe(false);
expect($('[data-name=alien]').is(':visible')).toBe(true); expect($('[data-name=alien]').is(':visible')).toBe(true);
expect($('.js-emoji-menu-search').val()).toBe('ali'); expect($('.js-emoji-menu-search').val()).toBe('ali');
})
.then(done)
.catch(err => {
done.fail(`Failed to open and build emoji menu: ${err.message}`);
});
}); });
it('should clear the search when searching for nothing', done => { it('should clear the search when searching for nothing', async () => {
openAndWaitForEmojiMenu() await openAndWaitForEmojiMenu();
.then(() => {
awardsHandler.searchEmojis('ali'); awardsHandler.searchEmojis('ali');
expect($('[data-name=angel]').is(':visible')).toBe(false); expect($('[data-name=angel]').is(':visible')).toBe(false);
...@@ -304,16 +276,12 @@ describe('AwardsHandler', () => { ...@@ -304,16 +276,12 @@ describe('AwardsHandler', () => {
expect($('[data-name=anger]').is(':visible')).toBe(true); expect($('[data-name=anger]').is(':visible')).toBe(true);
expect($('[data-name=alien]').is(':visible')).toBe(true); expect($('[data-name=alien]').is(':visible')).toBe(true);
expect($('.js-emoji-menu-search').val()).toBe(''); expect($('.js-emoji-menu-search').val()).toBe('');
})
.then(done)
.catch(err => {
done.fail(`Failed to open and build emoji menu: ${err.message}`);
});
}); });
}); });
describe('emoji menu', () => { describe('emoji menu', () => {
const emojiSelector = '[data-name="sunglasses"]'; const emojiSelector = '[data-name="sunglasses"]';
const openEmojiMenuAndAddEmoji = () => { const openEmojiMenuAndAddEmoji = () => {
return openAndWaitForEmojiMenu().then(() => { return openAndWaitForEmojiMenu().then(() => {
const $menu = $('.emoji-menu'); const $menu = $('.emoji-menu');
...@@ -329,17 +297,13 @@ describe('AwardsHandler', () => { ...@@ -329,17 +297,13 @@ describe('AwardsHandler', () => {
}); });
}; };
it('should add selected emoji to awards block', done => { it('should add selected emoji to awards block', async () => {
openEmojiMenuAndAddEmoji() await openEmojiMenuAndAddEmoji();
.then(done)
.catch(err => {
done.fail(`Failed to open and build emoji menu: ${err.message}`);
});
}); });
it('should remove already selected emoji', done => { it('should remove already selected emoji', async () => {
openEmojiMenuAndAddEmoji() await openEmojiMenuAndAddEmoji();
.then(() => {
$('.js-add-award') $('.js-add-award')
.eq(0) .eq(0)
.click(); .click();
...@@ -350,11 +314,6 @@ describe('AwardsHandler', () => { ...@@ -350,11 +314,6 @@ describe('AwardsHandler', () => {
$emoji.click(); $emoji.click();
expect($block.find(emojiSelector).length).toBe(0); expect($block.find(emojiSelector).length).toBe(0);
})
.then(done)
.catch(err => {
done.fail(`Failed to open and build emoji menu: ${err.message}`);
});
}); });
}); });
...@@ -364,25 +323,20 @@ describe('AwardsHandler', () => { ...@@ -364,25 +323,20 @@ describe('AwardsHandler', () => {
Cookies.set('frequently_used_emojis', ''); Cookies.set('frequently_used_emojis', '');
}); });
it('shouldn\'t have any "Frequently used" heading if no frequently used emojis', done => { it('shouldn\'t have any "Frequently used" heading if no frequently used emojis', async () => {
return openAndWaitForEmojiMenu() await openAndWaitForEmojiMenu();
.then(() => {
const emojiMenu = document.querySelector('.emoji-menu'); const emojiMenu = document.querySelector('.emoji-menu');
Array.prototype.forEach.call(emojiMenu.querySelectorAll('.emoji-menu-title'), title => { Array.prototype.forEach.call(emojiMenu.querySelectorAll('.emoji-menu-title'), title => {
expect(title.textContent.trim().toLowerCase()).not.toBe('frequently used'); expect(title.textContent.trim().toLowerCase()).not.toBe('frequently used');
}); });
})
.then(done)
.catch(err => {
done.fail(`Failed to open and build emoji menu: ${err.message}`);
});
}); });
it('should have any frequently used section when there are frequently used emojis', done => { it('should have any frequently used section when there are frequently used emojis', async () => {
awardsHandler.addEmojiToFrequentlyUsedList('8ball'); awardsHandler.addEmojiToFrequentlyUsedList('8ball');
return openAndWaitForEmojiMenu() await openAndWaitForEmojiMenu();
.then(() => {
const emojiMenu = document.querySelector('.emoji-menu'); const emojiMenu = document.querySelector('.emoji-menu');
const hasFrequentlyUsedHeading = Array.prototype.some.call( const hasFrequentlyUsedHeading = Array.prototype.some.call(
emojiMenu.querySelectorAll('.emoji-menu-title'), emojiMenu.querySelectorAll('.emoji-menu-title'),
...@@ -390,11 +344,6 @@ describe('AwardsHandler', () => { ...@@ -390,11 +344,6 @@ describe('AwardsHandler', () => {
); );
expect(hasFrequentlyUsedHeading).toBe(true); expect(hasFrequentlyUsedHeading).toBe(true);
})
.then(done)
.catch(err => {
done.fail(`Failed to open and build emoji menu: ${err.message}`);
});
}); });
it('should disregard invalid frequently used emoji that are being attempted to be added', () => { it('should disregard invalid frequently used emoji that are being attempted to be added', () => {
......
// eslint-disable-next-line import/prefer-default-export
export const useFakeRequestAnimationFrame = () => {
let orig;
beforeEach(() => {
orig = global.requestAnimationFrame;
global.requestAnimationFrame = cb => cb();
});
afterEach(() => {
global.requestAnimationFrame = orig;
});
};
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