Skip to content

Commit

Permalink
Skip parse.icon if everything is there (#444)
Browse files Browse the repository at this point in the history
  • Loading branch information
robmadole committed Aug 3, 2021
1 parent 8e50c34 commit 01138dc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .npmrc.proregistry
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
@fortawesome:registry=https://npm.fontawesome.com/
//npm.fontawesome.com/:_authToken=${FONTAWESOME_NPM_AUTH_TOKEN}
//npm.fontawesome.com/:_authToken=${FONTAWESOME_NPM_AUTH_TOKEN}
1 change: 1 addition & 0 deletions src/components/FontAwesomeIcon.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default function FontAwesomeIcon({ forwardedRef, ...props }) {
} = props

const iconLookup = normalizeIconArgs(iconArgs)

const classes = objectWithKey('classes', [
...classList(props),
...className.split(' ')
Expand Down
22 changes: 16 additions & 6 deletions src/components/__tests__/FontAwesomeIcon.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fontawesome from '@fortawesome/fontawesome-svg-core'
import log from '../../logger'
import { faClose } from '@fortawesome/free-solid-svg-icons'
import { faTimes } from '@fortawesome/free-solid-svg-icons'
import { faCoffee, faCircle, faSpartan } from '../__fixtures__/icons'
import { coreHasFeature, REFERENCE_ICON_USING_STRING, REFERENCE_ICON_BY_STYLE, ICON_ALIASES, mount } from '../__fixtures__/helpers'

Expand All @@ -10,6 +10,10 @@ beforeEach(() => {
fontawesome.library.add(faCoffee, faCircle, faSpartan)
})

afterEach(() => {
fontawesome.library.reset()
})

test('using a FAT icon using array format', () => {
const vm = mount({
icon: ['fat', 'spartan']
Expand All @@ -21,21 +25,19 @@ test('using a FAT icon using array format', () => {

if (coreHasFeature(ICON_ALIASES)) {
test('find a free-solid-svg-icon with array format', () => {
fontawesome.library.reset()
fontawesome.library.add(faClose)
fontawesome.library.add(faTimes)
const vm = mount({ icon: ['fas', 'xmark'] })

expect(vm.type).toBe('svg')
expect(vm.props.className.includes('fa-xmark')).toBeTruthy()
})

test('find a free-solid-svg-icon that is an alias ', () => {
fontawesome.library.reset()
fontawesome.library.add(faClose)
fontawesome.library.add(faTimes)
const vm = mount({ icon: ['fas', 'close'] })

expect(vm.type).toBe('svg')
expect(vm.props.className.includes('fa-close')).toBeTruthy()
expect(vm.props.className.includes('fa-xmark')).toBeTruthy()
})
}

Expand Down Expand Up @@ -71,6 +73,14 @@ if (coreHasFeature(REFERENCE_ICON_BY_STYLE)) {
})
}

test('using imported object from svg icons package', () => {
const vm = mount({
icon: faTimes
})

expect(vm.type).toBe('svg')
})

test('using pack and name', () => {
const vm = mount({
icon: ['fas', 'coffee'],
Expand Down
8 changes: 7 additions & 1 deletion src/utils/normalize-icon-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import { parse as faParse } from '@fortawesome/fontawesome-svg-core'

// Normalize icon arguments
export default function normalizeIconArgs(icon) {
// this has everything that it needs to be rendered which means it was probably imported
// directly from an icon svg package
if (icon && typeof icon === 'object' && icon.prefix && icon.iconName && icon.icon) {
return icon
}

if (faParse.icon) {
return faParse.icon(icon)
}
Expand All @@ -12,7 +18,7 @@ export default function normalizeIconArgs(icon) {
}

// if the icon is an object and has a prefix and an icon name, return it
if (typeof icon === 'object' && icon.prefix && icon.iconName) {
if (icon && typeof icon === 'object' && icon.prefix && icon.iconName) {
return icon
}

Expand Down

0 comments on commit 01138dc

Please sign in to comment.