Skip to content

Commit

Permalink
Merge pull request #48 from oscard0m/improve-test-coverage
Browse files Browse the repository at this point in the history
test(search): improve test coverage
  • Loading branch information
jsumners authored Jan 9, 2021
2 parents d054a71 + 76b2270 commit ed5eb2a
Show file tree
Hide file tree
Showing 5 changed files with 336 additions and 4 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"build": "./build.sh",
"load": "npm run build && open alfred-emoji.alfredworkflow",
"test": "tap 'test/**/*.test.js'",
"test:watch": "npm test -- --watch",
"test-ci": "tap --cov 'test/**/*.test.js'",
"lint": "standard | snazzy",
"lint-ci": "standard"
Expand All @@ -19,10 +20,11 @@
"fontkit": "^1.7.7"
},
"devDependencies": {
"mock-require": "^3.0.3",
"pre-commit": "^1.2.2",
"snazzy": "^8.0.0",
"standard": "^12.0.1",
"tap": "^12.1.0",
"tap": "^14.1.0",
"webpack": "^4.25.1",
"webpack-cli": "^3.1.2"
}
Expand Down
16 changes: 14 additions & 2 deletions src/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ const setSkinToneModifier = (tone) => {

const addModifier = (emoji, modifier) => {
if (!modifier || !emoji.fitzpatrick_scale) return emoji.char

/*
* There are some emojis categorized as a sequence of emojis
* Emoji ZWJ Sequence is a combination of multiple emojis which display as a single emoji
* on supported platforms. These sequences are joined with a Zero Width Joiner character.
*
* https://emojipedia.org/emoji-zwj-sequence/
*/

const zwj = new RegExp('‍', 'g')
return emoji.char.match(zwj) ? emoji.char.replace(zwj, modifier + '‍') : emoji.char + modifier
}
Expand Down Expand Up @@ -70,11 +79,14 @@ const alfredItems = (names) => {

const all = () => alfredItems(emojiNames)

const libHasEmoji = (name, term) => {
return emojilib.lib[name] &&
emojilib.lib[name].keywords.some((keyword) => keyword.includes(term))
}
const matches = (terms) => {
return emojiNames.filter((name) => {
return terms.every((term) => {
return name.includes(term) ||
emojilib.lib[name].keywords.some((keyword) => keyword.includes(term))
return name.includes(term) || libHasEmoji(name, term)
})
})
}
Expand Down
230 changes: 230 additions & 0 deletions test/emojisMock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
{
"thinking": {
"keywords": ["face", "hmmm", "think", "consider"],
"char": "🤔",
"fitzpatrick_scale": false,
"category": "people"
},
"thought_balloon": {
"keywords": ["bubble", "cloud", "speech", "thinking", "dream"],
"char": "💭",
"fitzpatrick_scale": false,
"category": "symbols"
},
"right_anger_bubble": {
"keywords": ["caption", "speech", "thinking", "mad"],
"char": "🗯",
"fitzpatrick_scale": false,
"category": "symbols"
},
"grimacing": {
"keywords": ["face", "grimace", "teeth"],
"char": "😬",
"fitzpatrick_scale": false,
"category": "people"
},
"vomiting": {
"keywords": ["face", "sick"],
"char": "🤮",
"fitzpatrick_scale": false,
"category": "people"
},
"+1": {
"keywords": ["thumbsup", "yes", "awesome", "good", "agree", "accept", "cool", "hand", "like"],
"char": "👍",
"fitzpatrick_scale": true,
"category": "people"
},
"-1": {
"keywords": ["thumbsdown", "no", "dislike", "hand"],
"char": "👎",
"fitzpatrick_scale": true,
"category": "people"
},
"hear_no_evil": {
"keywords": ["animal", "monkey", "nature"],
"char": "🙉",
"fitzpatrick_scale": false,
"category": "animals_and_nature"
},
"unicorn": {
"keywords": ["animal", "nature", "mystical"],
"char": "🦄",
"fitzpatrick_scale": false,
"category": "animals_and_nature"
},
"rainbow": {
"keywords": ["nature", "happy", "unicorn_face", "photo", "sky", "spring"],
"char": "🌈",
"fitzpatrick_scale": false,
"category": "travel_and_places"
},
"cloud_with_lightning": {
"keywords": ["weather", "thunder"],
"char": "🌩",
"fitzpatrick_scale": false,
"category": "animals_and_nature"
},
"zap": {
"keywords": ["thunder", "weather", "lightning bolt", "fast"],
"char": "",
"fitzpatrick_scale": false,
"category": "animals_and_nature"
},
"green_apple": {
"keywords": ["fruit", "nature"],
"char": "🍏",
"fitzpatrick_scale": false,
"category": "food_and_drink"
},
"apple": {
"keywords": ["fruit", "mac", "school"],
"char": "🍎",
"fitzpatrick_scale": false,
"category": "food_and_drink"
},
"pear": {
"keywords": ["fruit", "nature", "food"],
"char": "🍐",
"fitzpatrick_scale": false,
"category": "food_and_drink"
},
"tangerine": {
"keywords": ["food", "fruit", "nature", "orange"],
"char": "🍊",
"fitzpatrick_scale": false,
"category": "food_and_drink"
},
"lemon": {
"keywords": ["fruit", "nature"],
"char": "🍋",
"fitzpatrick_scale": false,
"category": "food_and_drink"
},
"banana": {
"keywords": ["fruit", "food", "monkey"],
"char": "🍌",
"fitzpatrick_scale": false,
"category": "food_and_drink"
},
"watermelon": {
"keywords": ["fruit", "food", "picnic", "summer"],
"char": "🍉",
"fitzpatrick_scale": false,
"category": "food_and_drink"
},
"grapes": {
"keywords": ["fruit", "food", "wine"],
"char": "🍇",
"fitzpatrick_scale": false,
"category": "food_and_drink"
},
"strawberry": {
"keywords": ["fruit", "food", "nature"],
"char": "🍓",
"fitzpatrick_scale": false,
"category": "food_and_drink"
},
"melon": {
"keywords": ["fruit", "nature", "food"],
"char": "🍈",
"fitzpatrick_scale": false,
"category": "food_and_drink"
},
"cherries": {
"keywords": ["food", "fruit"],
"char": "🍒",
"fitzpatrick_scale": false,
"category": "food_and_drink"
},
"peach": {
"keywords": ["fruit", "nature", "food"],
"char": "🍑",
"fitzpatrick_scale": false,
"category": "food_and_drink"
},
"pineapple": {
"keywords": ["fruit", "nature", "food"],
"char": "🍍",
"fitzpatrick_scale": false,
"category": "food_and_drink"
},
"coconut": {
"keywords": ["fruit", "nature", "food", "palm"],
"char": "🥥",
"fitzpatrick_scale": false,
"category": "food_and_drink"
},
"kiwi_fruit": {
"keywords": ["fruit", "food"],
"char": "🥝",
"fitzpatrick_scale": false,
"category": "food_and_drink"
},
"man_singer": {
"keywords": ["rockstar", "entertainer", "man", "human"],
"char": "👨‍🎤",
"fitzpatrick_scale": true,
"category": "people"
},
"mango": {
"keywords": ["fruit", "food", "tropical"],
"char": "🥭",
"fitzpatrick_scale": false,
"category": "food_and_drink"
},
"avocado": {
"keywords": ["fruit", "food"],
"char": "🥑",
"fitzpatrick_scale": false,
"category": "food_and_drink"
},
"broccoli": {
"keywords": ["fruit", "food", "vegetable"],
"char": "🥦",
"fitzpatrick_scale": false,
"category": "food_and_drink"
},
"tomato": {
"keywords": ["fruit", "vegetable", "nature", "food"],
"char": "🍅",
"fitzpatrick_scale": false,
"category": "food_and_drink"
},
"eggplant": {
"keywords": ["vegetable", "nature", "food", "aubergine"],
"char": "🍆",
"fitzpatrick_scale": false,
"category": "food_and_drink"
},
"cucumber": {
"keywords": ["fruit", "food", "pickle"],
"char": "🥒",
"fitzpatrick_scale": false,
"category": "food_and_drink"
},
"slot_machine": {
"keywords": ["bet", "gamble", "vegas", "fruit machine", "luck", "casino"],
"char": "🎰",
"fitzpatrick_scale": false,
"category": "activity"
},
"teddy_bear": {
"keywords": ["plush", "stuffed"],
"char": "🧸",
"fitzpatrick_scale": false,
"category": "objects"
},
"open_book": {
"keywords": ["book", "read", "library", "knowledge", "literature", "learn", "study"],
"char": "📖",
"fitzpatrick_scale": false,
"category": "objects"
},
"lithuania": {
"keywords": ["lt", "flag", "nation", "country", "banner"],
"char": "🇱🇹",
"fitzpatrick_scale": false,
"category": "flags"
}
}
43 changes: 43 additions & 0 deletions test/orderedMock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[
"thinking",
"thought_balloon",
"right_anger_bubble",
"grimacing",
"vomiting",
"+1",
"-1",
"hear_no_evil",
"unicorn",
"rainbow",
"cloud_with_lightning",
"zap",
"green_apple",
"apple",
"pear",
"tangerine",
"lemon",
"banana",
"watermelon",
"grapes",
"strawberry",
"melon",
"cherries",
"peach",
"mango",
"pineapple",
"coconut",
"kiwi_fruit",
"tomato",
"eggplant",
"avocado",
"broccoli",
"tomato",
"eggplant",
"cucumber",
"slot_machine",
"teddy_bear",
"open_book",
"lithuania",
"man_singer",
"baz"
]
47 changes: 46 additions & 1 deletion test/search.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
'use strict'

const test = require('tap').test
const { test } = require('tap')
const mock = require('mock-require')
mock('emojilib', {
lib: require('./emojisMock.json'),
ordered: require('./orderedMock.json'),
fitzpatrick_scale_modifiers: ['🏻', '🏼', '🏽', '🏾', '🏿']
})

const search = require('../src/search')

test('finds "thumbs up"', (t) => {
Expand All @@ -9,6 +16,38 @@ test('finds "thumbs up"', (t) => {
t.ok(Object.keys(found.items).length > 0)
})

/*
* There are some emojis categorized as a sequence of emojis
* Emoji ZWJ Sequence is a combination of multiple emojis which display as a single emoji
* on supported platforms. These sequences are joined with a Zero Width Joiner character.
*
* https://emojipedia.org/emoji-zwj-sequence/
*/

test('finds emoji with zwj ("rockstar")', (t) => {
t.plan(1)
const found = search('rockstar', 2)
t.ok(Object.keys(found.items).length > 0)
})

test('does not throw when emoji is in orderedList but not in emojiLib', (t) => {
t.plan(1)
const found = search('baz')
t.ok(Object.keys(found.items).length === 0)
})

test('finds "thumbs up" when "pasteByDefault" is enabled', (t) => {
t.plan(1)
const found = search('thumbs up', 1, true)
t.ok(Object.keys(found.items).length > 0)
})

test('finds ALL results when no query is provided', (t) => {
t.plan(1)
const found = search()
t.ok(Object.keys(found.items).length > 0)
})

test('finds "thu" partial', (t) => {
t.plan(1)
const found = search('thu')
Expand All @@ -27,6 +66,12 @@ test('omits "rage1"', (t) => {
t.ok(Object.keys(found.items).length === 0)
})

test('applies modifier if possible', (t) => {
t.plan(1)
const found = search('a', 1)
t.ok(Object.keys(found.items).length > 0)
})

test('enables uid', (t) => {
t.plan(1)
const found = search('grimacing')
Expand Down

0 comments on commit ed5eb2a

Please sign in to comment.