Skip to content

Commit 50823b8

Browse files
committed
Gate title by version
1 parent 02813b9 commit 50823b8

File tree

4 files changed

+79
-88
lines changed

4 files changed

+79
-88
lines changed

package-lock.json

Lines changed: 27 additions & 47 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@
5757
"@babel/plugin-external-helpers": "^7.18.6",
5858
"@babel/preset-env": "^7.18.6",
5959
"@babel/preset-stage-3": "^7.8.3",
60-
"@fortawesome/fontawesome-svg-core": "^1.3.0",
61-
"@fortawesome/free-solid-svg-icons": "^5.15.4",
60+
"@fortawesome/fontawesome-svg-core": "^7",
61+
"@fortawesome/free-solid-svg-icons": "^7",
6262
"@pyoner/svelte-types": "^3.4.4-2",
6363
"@rollup/plugin-babel": "^5.3.1",
6464
"@rollup/plugin-commonjs": "^22.0.1",

src/components/__fixtures__/helpers.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ import { cleanup, render, screen } from '@testing-library/svelte'
44
import { parse } from '@fortawesome/fontawesome-svg-core'
55
import semver from 'semver'
66

7-
import packageJson from '@fortawesome/free-solid-svg-icons/package.json' with { type: 'json' }
7+
import svgCorePackageJson from '@fortawesome/fontawesome-svg-core/package.json' with { type: 'json' }
8+
import svgIconsPackageJson from '@fortawesome/free-solid-svg-icons/package.json' with { type: 'json' }
89

9-
const SVG_ICONS_VERSION = semver.parse(packageJson.version)
10+
const SVG_CORE_VERSION = semver.parse(svgCorePackageJson.version)
11+
const SVG_ICONS_VERSION = semver.parse(svgIconsPackageJson.version)
1012

1113
export const REFERENCE_ICON_BY_STYLE = 0x00
1214
export const ICON_ALIASES = 0x01
1315
export const REFERENCE_ICON_USING_STRING = 0x02
16+
export const USES_A11Y_TITLE = 0x03
1417

1518
export function coreHasFeature(feature) {
1619
if (feature === ICON_ALIASES) {
@@ -19,6 +22,11 @@ export function coreHasFeature(feature) {
1922
return parse.icon && SVG_ICONS_VERSION.major >= 6
2023
}
2124

25+
if (feature === USES_A11Y_TITLE) {
26+
// Accessibility changed in version 7 and the title attribute is no longer used
27+
return SVG_CORE_VERSION.major < 7
28+
}
29+
2230
if (
2331
feature === REFERENCE_ICON_BY_STYLE ||
2432
feature === REFERENCE_ICON_USING_STRING

src/components/__tests__/FontAwesomeIcon.test.js

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
REFERENCE_ICON_USING_STRING,
99
REFERENCE_ICON_BY_STYLE,
1010
ICON_ALIASES,
11+
USES_A11Y_TITLE,
1112
mount
1213
} from '../__fixtures__/helpers'
1314

@@ -426,36 +427,6 @@ describe('symbol', () => {
426427
})
427428
})
428429

429-
describe('title', () => {
430-
test('will not add a title element', () => {
431-
const vm = mount({ icon: faCoffee })
432-
433-
expect(vm.children[0].type).not.toBe('title')
434-
})
435-
436-
test('will add a title element', () => {
437-
const vm = mount({ icon: faCoffee, title: 'Coffee' })
438-
439-
expect(vm.children[0].type).toBe('title')
440-
expect(vm.children[0].children[0]).toBe('Coffee')
441-
})
442-
443-
test('will use an explicit titleId', () => {
444-
const vm = mount({
445-
icon: faCoffee,
446-
title: 'Coffee',
447-
titleId: 'coffee-title'
448-
})
449-
450-
expect(vm.props['aria-labelledby']).toBe(
451-
'svg-inline--fa-title-coffee-title'
452-
)
453-
expect(vm.children[0].props).toEqual(
454-
expect.objectContaining({ id: 'svg-inline--fa-title-coffee-title' })
455-
)
456-
})
457-
})
458-
459430
describe('swap opacity', () => {
460431
test('setting swapOpacity prop to true adds fa-swap-opacity class', () => {
461432
const vm = mount({ icon: faCoffee, swapOpacity: true })
@@ -471,11 +442,43 @@ describe('swap opacity', () => {
471442
})
472443
})
473444

474-
describe('using titleId', () => {
475-
test('setting titleId prop reflects in the aria-labelledby attribute', () => {
476-
const titleId = 'foo'
477-
const vm = mount({ icon: faCoffee, titleId, title: 'Coffee' })
478-
const ariaLabelledby = vm.props['aria-labelledby']
479-
expect(ariaLabelledby.includes(titleId)).toBeTruthy()
445+
if (coreHasFeature(USES_A11Y_TITLE)) {
446+
describe('title', () => {
447+
test('will not add a title element', () => {
448+
const vm = mount({ icon: faCoffee })
449+
450+
expect(vm.children[0].type).not.toBe('title')
451+
})
452+
453+
test('will add a title element', () => {
454+
const vm = mount({ icon: faCoffee, title: 'Coffee' })
455+
456+
expect(vm.children[0].type).toBe('title')
457+
expect(vm.children[0].children[0]).toBe('Coffee')
458+
})
459+
460+
test('will use an explicit titleId', () => {
461+
const vm = mount({
462+
icon: faCoffee,
463+
title: 'Coffee',
464+
titleId: 'coffee-title'
465+
})
466+
467+
expect(vm.props['aria-labelledby']).toBe(
468+
'svg-inline--fa-title-coffee-title'
469+
)
470+
expect(vm.children[0].props).toEqual(
471+
expect.objectContaining({ id: 'svg-inline--fa-title-coffee-title' })
472+
)
473+
})
480474
})
481-
})
475+
476+
describe('using titleId', () => {
477+
test('setting titleId prop reflects in the aria-labelledby attribute', () => {
478+
const titleId = 'foo'
479+
const vm = mount({ icon: faCoffee, titleId, title: 'Coffee' })
480+
const ariaLabelledby = vm.props['aria-labelledby']
481+
expect(ariaLabelledby.includes(titleId)).toBeTruthy()
482+
})
483+
})
484+
}

0 commit comments

Comments
 (0)