-
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.
IMP-208: Add isEmojiSupported util fn (#295)
- Loading branch information
1 parent
39d97e6
commit d281114
Showing
4 changed files
with
104 additions
and
8 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
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,51 @@ | ||
import { isEmojiSupported } from './isEmojiSupported'; | ||
|
||
describe('isEmojiSupported', () => { | ||
let ctx; | ||
|
||
beforeEach(() => { | ||
// Mock the canvas context | ||
ctx = { | ||
fillStyle: '', | ||
fillRect: jest.fn(), | ||
textBaseline: '', | ||
font: '', | ||
fillText: jest.fn(), | ||
getImageData: jest.fn(), | ||
}; | ||
// Setup to return a fake image data array | ||
ctx.getImageData.mockReturnValue({ | ||
data: new Uint8ClampedArray(32 * 32 * 4).fill(0), // All pixels transparent | ||
}); | ||
// Mock canvas creation | ||
document.createElement = jest.fn().mockReturnValue({ | ||
getContext: () => ctx, | ||
width: 32, | ||
height: 32, | ||
}); | ||
}); | ||
|
||
test('should detect supported emoji by checking non-transparent pixels', () => { | ||
// Mocking the context to simulate an emoji being supported (non-transparent pixels) | ||
ctx.getImageData.mockReturnValue({ | ||
data: new Uint8ClampedArray(32 * 32 * 4).fill(255), // All pixels non-transparent | ||
}); | ||
|
||
expect(isEmojiSupported('😊')).toBe(true); | ||
}); | ||
|
||
test('should detect unsupported emoji by checking all transparent pixels', () => { | ||
// Using the initial all-transparent setup by default | ||
|
||
expect(isEmojiSupported('😊')).toBe(false); | ||
}); | ||
|
||
test('should handle null canvas context gracefully', () => { | ||
// Simulate canvas context not being available | ||
(document.createElement as any).mockReturnValue({ | ||
getContext: () => null, | ||
}); | ||
|
||
expect(isEmojiSupported('😊')).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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { isNil } from './isNil'; | ||
|
||
// Tests flag emoji | ||
export function isEmojiSupported(emoji: string): boolean { | ||
if (isNil(emoji)) { | ||
return false; | ||
} | ||
const canvas = document.createElement('canvas'); | ||
canvas.width = canvas.height = 32; // The size should be large enough to hold the emoji | ||
const ctx = canvas.getContext('2d'); | ||
if (!ctx) { | ||
return false; // In case the browser doesn't support canvas | ||
} | ||
|
||
ctx.fillStyle = 'white'; // Background color | ||
ctx.fillRect(0, 0, 32, 32); // Fill the background | ||
ctx.textBaseline = 'top'; | ||
ctx.font = '32px Arial'; // A common font that may not support the emoji | ||
|
||
ctx.fillText(emoji, 0, 0); // Draw the emoji on the canvas | ||
|
||
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height).data; | ||
|
||
let isSupported = false; | ||
|
||
// Loop through pixel data; every four items in the array represent the RGBA values of a pixel | ||
for (let i = 0; i < imageData.length; i += 4) { | ||
const alpha = imageData[i + 3]; // Get the alpha value of the pixel | ||
if (alpha > 0) { | ||
// Check if the pixel is not completely transparent | ||
isSupported = true; | ||
break; | ||
} | ||
} | ||
|
||
return isSupported; | ||
} |
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