-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from sinansonmez/feature/add-get-emoji
Feature: Get emoji by name or short name
- Loading branch information
Showing
7 changed files
with
196 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,100 @@ | ||
import emojis from '../emojis'; | ||
import {countryCodeToFlag, emojiToLetter, flagToCountryCode, letterToEmoji, getRandomEmoji} from '../index'; | ||
|
||
test('countryCodeToEmoji', () => { | ||
expect(countryCodeToFlag('US')).toBe('🇺🇸'); | ||
}); | ||
|
||
test('flagToCountryCode', () => { | ||
expect(flagToCountryCode('🇺🇸')).toBe('US'); | ||
}); | ||
|
||
test('letterToEmoji', () => { | ||
expect(letterToEmoji('A')).toBe('🇦'); | ||
}); | ||
|
||
test('letterToEmoji with input more than single character', () => { | ||
expect(() => { | ||
letterToEmoji('AS'); | ||
}).toThrow('letterToEmoji: letter must be a single character'); | ||
}); | ||
|
||
test('letterToEmoji with input less than single character', () => { | ||
expect(() => { | ||
letterToEmoji(''); | ||
}).toThrow('letterToEmoji: letter must be a single character'); | ||
}); | ||
|
||
test('emojiToLetter', () => { | ||
expect(emojiToLetter('🇦')).toBe('A'); | ||
}); | ||
|
||
test('should return a different emoji on subsequent calls', () => { | ||
const emoji1 = getRandomEmoji(); | ||
const emoji2 = getRandomEmoji(); | ||
expect(emoji1).not.toEqual(emoji2); | ||
}); | ||
import { countryCodeToFlag, emojiToLetter, flagToCountryCode, letterToEmoji, getRandomEmoji, getEmojiByShortName, getEmojiByName } from '../index'; | ||
|
||
describe('index', () => { | ||
beforeAll(() => { | ||
// Mock console.error and console.warn to prevent actual logging during tests | ||
console.error = jest.fn(); | ||
console.warn = jest.fn(); | ||
}); | ||
|
||
it('countryCodeToEmoji', () => { | ||
expect(countryCodeToFlag('US')).toBe('🇺🇸'); | ||
}); | ||
|
||
it('flagToCountryCode', () => { | ||
expect(flagToCountryCode('🇺🇸')).toBe('US'); | ||
}); | ||
|
||
it('letterToEmoji', () => { | ||
expect(letterToEmoji('A')).toBe('🇦'); | ||
}); | ||
|
||
it('letterToEmoji with input more than single character', () => { | ||
expect(() => { | ||
letterToEmoji('AS'); | ||
}).toThrow('letterToEmoji: letter must be a single character'); | ||
}); | ||
|
||
it('letterToEmoji with input less than single character', () => { | ||
expect(() => { | ||
letterToEmoji(''); | ||
}).toThrow('letterToEmoji: letter must be a single character'); | ||
}); | ||
|
||
it('emojiToLetter', () => { | ||
expect(emojiToLetter('🇦')).toBe('A'); | ||
}); | ||
|
||
it('should return a different emoji on subsequent calls', () => { | ||
const emoji1 = getRandomEmoji(); | ||
const emoji2 = getRandomEmoji(); | ||
expect(emoji1).not.toEqual(emoji2); | ||
}); | ||
|
||
it('should return the correct emoji for a valid short name', () => { | ||
const emojiShortName = ':apple:'; | ||
const result = getEmojiByShortName(emojiShortName); | ||
expect(result).toBe('🍎'); | ||
}); | ||
|
||
it('should return an empty string and log an error for an invalid short name', () => { | ||
const invalidShortName = 'invalid-emoji'; | ||
const result = getEmojiByShortName(invalidShortName); | ||
expect(result).toBe(''); | ||
expect(console.error).toHaveBeenCalledWith( | ||
'you didnt pass a short name. Please pass correct short name like :apple: or use getEmojiByName' | ||
); | ||
}); | ||
|
||
it('should return an empty string and log a warning for a short name that is not found', () => { | ||
const nonExistentShortName = ':grape:'; | ||
const result = getEmojiByShortName(nonExistentShortName); | ||
expect(result).toBe(''); | ||
expect(console.warn).toHaveBeenCalledWith( | ||
'emoji not found with name: ', nonExistentShortName | ||
); | ||
}); | ||
|
||
it('should return the correct emoji for a valid emoji name', () => { | ||
const emojiName = 'RED Apple'; | ||
const result = getEmojiByName(emojiName); | ||
expect(result).toBe('🍎'); | ||
}); | ||
|
||
it('should return an empty string and log a warning for an invalid emoji name', () => { | ||
const invalidEmojiName = 'Grape'; | ||
const result = getEmojiByName(invalidEmojiName); | ||
expect(result).toBe(''); | ||
expect(console.warn).toHaveBeenCalledWith( | ||
"emoji not found with name: ", invalidEmojiName | ||
); | ||
}); | ||
|
||
it('should return an empty string and log a warning for a null emoji name', () => { | ||
const nullEmojiName = null; | ||
const result = getEmojiByName(nullEmojiName as unknown as string); | ||
expect(result).toBe(''); | ||
expect(console.warn).toHaveBeenCalledWith( | ||
"emoji not found with name: ", nullEmojiName | ||
); | ||
}); | ||
|
||
it('should return an empty string and log a warning for an undefined emoji name', () => { | ||
const undefinedEmojiName = undefined; | ||
const result = getEmojiByName(undefinedEmojiName as unknown as string); | ||
expect(result).toBe(''); | ||
expect(console.warn).toHaveBeenCalledWith( | ||
"emoji not found with name: ", undefinedEmojiName | ||
); | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { isShortName } from '../utils'; | ||
|
||
describe('isShortName', () => { | ||
it('should return true if the name contains colons at the beginning and end', () => { | ||
const name = ':example:'; | ||
const result = isShortName(name); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should return false if the name does not contain colons at the beginning and end', () => { | ||
const name = 'example'; | ||
const result = isShortName(name); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('should return false if the name contains a colon at the beginning but not at the end', () => { | ||
const name = ':example'; | ||
const result = isShortName(name); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('should return false if the name contains a colon at the end but not at the beginning', () => { | ||
const name = 'example:'; | ||
const result = isShortName(name); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('should return true if the name contains multiple colons at the beginning and end', () => { | ||
const name = ':::example:::'; | ||
const result = isShortName(name); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should return false if the name is an empty string', () => { | ||
const name = ''; | ||
const result = isShortName(name); | ||
expect(result).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function isShortName(name: string) { | ||
return /:.+:/.test(name); | ||
} |