diff options
Diffstat (limited to 'spec/frontend/emoji/emoji_spec.js')
-rw-r--r-- | spec/frontend/emoji/emoji_spec.js | 157 |
1 files changed, 126 insertions, 31 deletions
diff --git a/spec/frontend/emoji/emoji_spec.js b/spec/frontend/emoji/emoji_spec.js index 53c6d0835bc..f528313ef02 100644 --- a/spec/frontend/emoji/emoji_spec.js +++ b/spec/frontend/emoji/emoji_spec.js @@ -1,7 +1,6 @@ -import MockAdapter from 'axios-mock-adapter'; import { trimText } from 'helpers/text_helper'; -import axios from '~/lib/utils/axios_utils'; -import { initEmojiMap, glEmojiTag, EMOJI_VERSION } from '~/emoji'; +import { emojiFixtureMap, initEmojiMock, describeEmojiFields } from 'helpers/emoji'; +import { glEmojiTag, searchEmoji, getEmoji } from '~/emoji'; import isEmojiUnicodeSupported, { isFlagEmoji, isRainbowFlagEmoji, @@ -30,37 +29,11 @@ const emptySupportMap = { 1.1: false, }; -const emojiFixtureMap = { - bomb: { - name: 'bomb', - moji: '💣', - unicodeVersion: '6.0', - }, - construction_worker_tone5: { - name: 'construction_worker_tone5', - moji: '👷🏿', - unicodeVersion: '8.0', - }, - five: { - name: 'five', - moji: '5️⃣', - unicodeVersion: '3.0', - }, - grey_question: { - name: 'grey_question', - moji: '❔', - unicodeVersion: '6.0', - }, -}; - describe('gl_emoji', () => { let mock; - beforeEach(() => { - mock = new MockAdapter(axios); - mock.onGet(`/-/emojis/${EMOJI_VERSION}/emojis.json`).reply(200); - - return initEmojiMap().catch(() => {}); + beforeEach(async () => { + mock = await initEmojiMock(); }); afterEach(() => { @@ -378,4 +351,126 @@ describe('gl_emoji', () => { expect(isSupported).toBeFalsy(); }); }); + + describe('getEmoji', () => { + const { grey_question } = emojiFixtureMap; + + describe('when query is undefined', () => { + it('should return null by default', () => { + expect(getEmoji()).toBe(null); + }); + + it('should return fallback emoji when fallback is true', () => { + expect(getEmoji(undefined, true).name).toEqual(grey_question.name); + }); + }); + }); + + describe('searchEmoji', () => { + const { atom, grey_question } = emojiFixtureMap; + const search = (query, opts) => searchEmoji(query, opts).map(({ name }) => name); + const mangle = str => str.slice(0, 1) + str.slice(-1); + const partial = str => str.slice(0, 2); + + describe('with default options', () => { + const subject = query => search(query); + + describeEmojiFields('with $field', ({ accessor }) => { + it(`should match by lower case: ${accessor(atom)}`, () => { + expect(subject(accessor(atom))).toContain(atom.name); + }); + + it(`should match by upper case: ${accessor(atom).toUpperCase()}`, () => { + expect(subject(accessor(atom).toUpperCase())).toContain(atom.name); + }); + + it(`should not match by partial: ${mangle(accessor(atom))}`, () => { + expect(subject(mangle(accessor(atom)))).not.toContain(atom.name); + }); + }); + + it(`should match by unicode value: ${atom.moji}`, () => { + expect(subject(atom.moji)).toContain(atom.name); + }); + + it('should not return a fallback value', () => { + expect(subject('foo bar baz')).toHaveLength(0); + }); + + it('should not return a fallback value when query is falsey', () => { + expect(subject()).toHaveLength(0); + }); + }); + + describe('with fuzzy match', () => { + const subject = query => search(query, { match: 'fuzzy' }); + + describeEmojiFields('with $field', ({ accessor }) => { + it(`should match by lower case: ${accessor(atom)}`, () => { + expect(subject(accessor(atom))).toContain(atom.name); + }); + + it(`should match by upper case: ${accessor(atom).toUpperCase()}`, () => { + expect(subject(accessor(atom).toUpperCase())).toContain(atom.name); + }); + + it(`should match by partial: ${mangle(accessor(atom))}`, () => { + expect(subject(mangle(accessor(atom)))).toContain(atom.name); + }); + }); + }); + + describe('with contains match', () => { + const subject = query => search(query, { match: 'contains' }); + + describeEmojiFields('with $field', ({ accessor }) => { + it(`should match by lower case: ${accessor(atom)}`, () => { + expect(subject(accessor(atom))).toContain(atom.name); + }); + + it(`should match by upper case: ${accessor(atom).toUpperCase()}`, () => { + expect(subject(accessor(atom).toUpperCase())).toContain(atom.name); + }); + + it(`should match by partial: ${partial(accessor(atom))}`, () => { + expect(subject(partial(accessor(atom)))).toContain(atom.name); + }); + + it(`should not match by mangled: ${mangle(accessor(atom))}`, () => { + expect(subject(mangle(accessor(atom)))).not.toContain(atom.name); + }); + }); + }); + + describe('with fallback', () => { + const subject = query => search(query, { fallback: true }); + + it.each` + query + ${'foo bar baz'} | ${undefined} + `('should return a fallback value when given $query', ({ query }) => { + expect(subject(query)).toContain(grey_question.name); + }); + }); + + describe('with name and alias fields', () => { + const subject = query => search(query, { fields: ['name', 'alias'] }); + + it(`should match by name: ${atom.name}`, () => { + expect(subject(atom.name)).toContain(atom.name); + }); + + it(`should match by alias: ${atom.aliases[0]}`, () => { + expect(subject(atom.aliases[0])).toContain(atom.name); + }); + + it(`should not match by description: ${atom.description}`, () => { + expect(subject(atom.description)).not.toContain(atom.name); + }); + + it(`should not match by unicode value: ${atom.moji}`, () => { + expect(subject(atom.moji)).not.toContain(atom.name); + }); + }); + }); }); |