Skip to content

Commit

Permalink
fix(icon): mapping same icon does not warn (#1279)
Browse files Browse the repository at this point in the history
  • Loading branch information
liamdebeasi authored Sep 13, 2023
1 parent 0f8ade3 commit 8297018
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
33 changes: 22 additions & 11 deletions src/components/icon/test/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,27 +126,38 @@ describe('addIcons', () => {
expect(getIconMap().get('myCoolIcon')).toEqual(logoIonitron);
});

it('should not warn when mapping the same icon twice', () => {
const spy = jest.spyOn(console, 'warn');

const myIcon = 'my-icon';

expect(spy).not.toHaveBeenCalled();

addIcons({ myIcon });

expect(spy).not.toHaveBeenCalled();

addIcons({ myIcon });

expect(spy).not.toHaveBeenCalled();
});

it('should not overwrite icons', () => {
const spy = jest.spyOn(console, 'warn');

const logoA = 'logo a';
const logoB = 'logo b';

expect(spy).not.toHaveBeenCalled();

expect(getIconMap().get('logo-a')).toEqual(undefined);

addIcons({ 'logo-a': logoB, logoA });

expect(getIconMap().get('logo-a')).toEqual(logoB);
expect(getIconMap().get('logoA')).toEqual(logoA);
});

it('passing kebab case key should not generate a camel case key', () => {
const logoIonitron = 'stubbed data';

expect(getIconMap().get('kebab-key')).toEqual(undefined);
expect(getIconMap().get('kebabKey')).toEqual(undefined);

addIcons({ 'kebab-key': logoIonitron });

expect(getIconMap().get('kebab-key')).toEqual(logoIonitron);
expect(getIconMap().get('kebabKey')).toEqual(undefined);
expect(spy).toHaveBeenCalled();
});

});
11 changes: 9 additions & 2 deletions src/components/icon/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,17 @@ export const addIcons = (icons: { [name: string]: string; }) => {

const addToIconMap = (name: string, data: any) => {
const map = getIconMap();

const existingIcon = map.get(name);

if (map.get(name) === undefined) {
if (existingIcon === undefined) {
map.set(name, data);
} else {

/**
* Importing and defining the same icon reference
* multiple times should not yield a warning.
*/
} else if (existingIcon !== data) {
console.warn(`[Ionicons Warning]: Multiple icons were mapped to name "${name}". Ensure that multiple icons are not mapped to the same icon name.`)
}
}
Expand Down

0 comments on commit 8297018

Please sign in to comment.