Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix getEmojiByShortcode when shortcodes are optional #98

Merged
merged 2 commits into from Dec 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/database/idbInterface.js
Expand Up @@ -172,7 +172,7 @@ export async function getEmojiByShortcode (db, shortcode) {
}

return emojis.filter(_ => {
const lowerShortcodes = _.shortcodes.map(_ => _.toLowerCase())
const lowerShortcodes = (_.shortcodes || []).map(_ => _.toLowerCase())
return lowerShortcodes.includes(shortcode.toLowerCase())
})[0] || null
}
Expand Down
14 changes: 14 additions & 0 deletions test/spec/database/custom.test.js
Expand Up @@ -228,4 +228,18 @@ describe('custom emoji', () => {
)
expect(await db.getEmojiByUnicodeOrName('unknown')).toBeNull()
})

test('shortcodes are optional in custom emoji', async () => {
const customs = JSON.parse(JSON.stringify(customEmojis))
for (const custom of customs) {
if (!custom.shortcodes.includes('monkey')) {
delete custom.shortcodes
}
}
db.customEmoji = customs

expect((await db.getEmojiByShortcode('monkey')).name).toEqual('monkey')
expect(await db.getEmojiByShortcode('a')).toEqual(null)
expect(await db.getEmojiByShortcode('doesnotexist')).toEqual(null)
})
})
26 changes: 26 additions & 0 deletions test/spec/database/getEmojiByShortcode.test.js
Expand Up @@ -40,6 +40,8 @@ describe('getEmojiByShortcode', () => {
expect((await db.getEmojiByShortcode(shortcode.toUpperCase())).unicode).toEqual(emoji.emoji)
}
}

await db.delete()
})

test('short nonexistent shortcodes', async () => {
Expand All @@ -48,5 +50,29 @@ describe('getEmojiByShortcode', () => {
expect(await db.getEmojiByShortcode('z')).toEqual(null)
expect(await db.getEmojiByShortcode('1')).toEqual(null)
expect(await db.getEmojiByShortcode(' ')).toEqual(null)

await db.delete()
})

test('works although shortcodes are optional', async () => {
const dataSource = 'http://localhost/shortcodes-optional.json'
const emojis = JSON.parse(JSON.stringify(truncatedEmoji))
for (const emoji of emojis) {
if (!emoji.shortcodes.includes('dog_face')) {
delete emoji.shortcodes
}
}

fetch
.get(dataSource, () => new Response(JSON.stringify(emojis), { headers: { ETag: 'W/optional' } }))
.head(dataSource, () => new Response(null, { headers: { ETag: 'W/optional' } }))

const db = new Database({ dataSource })

expect((await db.getEmojiByShortcode('dog_face')).unicode).toEqual('🐶')
expect(await db.getEmojiByShortcode('monkey_face')).toEqual(null)
expect(await db.getEmojiByShortcode('doesnotexist')).toEqual(null)

await db.delete()
})
})