Skip to content

Commit

Permalink
test: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
nolanlawson committed Aug 12, 2024
1 parent b920814 commit 6c2d25d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/picker/components/Picker/Picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,6 @@ export function createRoot (shadowRoot, props) {

function checkZwjSupportAndUpdate (zwjEmojisToCheck) {
const allSupported = checkZwjSupport(zwjEmojisToCheck, refs.baselineEmoji, emojiToDomNode)
/* istanbul ignore if */
if (!allSupported) {
console.log('Not all ZWJ emoji are supported, forcing re-render')
// Force update. We only do this if there are any unsupported ZWJ characters since otherwise,
Expand Down
8 changes: 7 additions & 1 deletion src/picker/utils/checkZwjSupport.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { supportedZwjEmojis } from './emojiSupport'

let baselineEmojiWidth

// only used in tests
let simulateBrowserNotSupportingZWJEmoji = false
export function setSimulateBrowserNotSupportingZWJEmoji (value) {
simulateBrowserNotSupportingZWJEmoji = value
}

/**
* Check if the given emojis containing ZWJ characters are supported by the current browser (don't render
* as double characters) and return true if all are supported.
Expand All @@ -23,7 +29,7 @@ export function checkZwjSupport (zwjEmojisToCheck, baselineEmoji, emojiToDomNode
// against are the ones that are 2x the size, because those are truly broken (person with red hair = person with
// floating red wig, black cat = cat with black square, polar bear = bear with snowflake, etc.)
// So here we set the threshold at 1.8 times the size of the baseline emoji.
const supported = emojiWidth / 1.8 < baselineEmojiWidth
const supported = !simulateBrowserNotSupportingZWJEmoji && emojiWidth / 1.8 < baselineEmojiWidth
supportedZwjEmojis.set(emoji.unicode, supported)
/* istanbul ignore next */
if (!supported) {
Expand Down
44 changes: 44 additions & 0 deletions test/spec/picker/zwjEmoji.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import Picker from '../../../src/picker/PickerElement'
import { ALL_EMOJI, basicAfterEach, basicBeforeEach, truncatedEmoji } from '../shared'
import { setSimulateBrowserNotSupportingZWJEmoji } from '../../../src/picker/utils/checkZwjSupport.js'
import { supportedZwjEmojis } from '../../../src/picker/utils/emojiSupport.js'
import { getAllByRole, getByRole, queryAllByRole, waitFor } from '@testing-library/dom'
import { groups } from '../../../src/picker/groups.js'

describe('ZWJ emoji', () => {
const simulations = [false, true]

simulations.forEach(simulateBrowserNotSupportingZWJ => {
describe(`simulateBrowserNotSupportingZWJ=${simulateBrowserNotSupportingZWJ}`, () => {
beforeEach(async () => {
await basicBeforeEach()
setSimulateBrowserNotSupportingZWJEmoji(simulateBrowserNotSupportingZWJ)
})
afterEach(async () => {
await basicAfterEach()
setSimulateBrowserNotSupportingZWJEmoji(false)
supportedZwjEmojis.clear() // reset global cache after each test
})

test('shows or hides ZWJ emoji appropriately', async () => {
console.log(truncatedEmoji)
const picker = new Picker({
dataSource: ALL_EMOJI
})

document.body.appendChild(picker)

const container = picker.shadowRoot.querySelector('.picker')

await waitFor(() => expect(getAllByRole(container, 'tab')).toHaveLength(groups.length))

getByRole(container, 'tab', { name: 'Flags' }).click()

// checkered flag is shown because it's not ZWJ, should always be shown
await waitFor(() => expect(getByRole(container, 'menuitem', { name: /🏁/ })).toBeVisible())
// pirate flag is a ZWJ combining black flag plus skull and crossbones, should be hidden if no browser support
await waitFor(() => expect(queryAllByRole(container, 'menuitem', { name: /🏴/ })).toHaveLength(simulateBrowserNotSupportingZWJ ? 0 : 1))
})
})
})
})

0 comments on commit 6c2d25d

Please sign in to comment.