Skip to content

Commit 8297018

Browse files
authored
fix(icon): mapping same icon does not warn (#1279)
1 parent 0f8ade3 commit 8297018

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

src/components/icon/test/utils.spec.ts

+22-11
Original file line numberDiff line numberDiff line change
@@ -126,27 +126,38 @@ describe('addIcons', () => {
126126
expect(getIconMap().get('myCoolIcon')).toEqual(logoIonitron);
127127
});
128128

129+
it('should not warn when mapping the same icon twice', () => {
130+
const spy = jest.spyOn(console, 'warn');
131+
132+
const myIcon = 'my-icon';
133+
134+
expect(spy).not.toHaveBeenCalled();
135+
136+
addIcons({ myIcon });
137+
138+
expect(spy).not.toHaveBeenCalled();
139+
140+
addIcons({ myIcon });
141+
142+
expect(spy).not.toHaveBeenCalled();
143+
});
144+
129145
it('should not overwrite icons', () => {
146+
const spy = jest.spyOn(console, 'warn');
147+
130148
const logoA = 'logo a';
131149
const logoB = 'logo b';
132150

151+
expect(spy).not.toHaveBeenCalled();
152+
133153
expect(getIconMap().get('logo-a')).toEqual(undefined);
134154

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

137157
expect(getIconMap().get('logo-a')).toEqual(logoB);
138158
expect(getIconMap().get('logoA')).toEqual(logoA);
139-
});
140-
141-
it('passing kebab case key should not generate a camel case key', () => {
142-
const logoIonitron = 'stubbed data';
143-
144-
expect(getIconMap().get('kebab-key')).toEqual(undefined);
145-
expect(getIconMap().get('kebabKey')).toEqual(undefined);
146-
147-
addIcons({ 'kebab-key': logoIonitron });
148159

149-
expect(getIconMap().get('kebab-key')).toEqual(logoIonitron);
150-
expect(getIconMap().get('kebabKey')).toEqual(undefined);
160+
expect(spy).toHaveBeenCalled();
151161
});
162+
152163
});

src/components/icon/utils.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,17 @@ export const addIcons = (icons: { [name: string]: string; }) => {
4040

4141
const addToIconMap = (name: string, data: any) => {
4242
const map = getIconMap();
43+
44+
const existingIcon = map.get(name);
4345

44-
if (map.get(name) === undefined) {
46+
if (existingIcon === undefined) {
4547
map.set(name, data);
46-
} else {
48+
49+
/**
50+
* Importing and defining the same icon reference
51+
* multiple times should not yield a warning.
52+
*/
53+
} else if (existingIcon !== data) {
4754
console.warn(`[Ionicons Warning]: Multiple icons were mapped to name "${name}". Ensure that multiple icons are not mapped to the same icon name.`)
4855
}
4956
}

0 commit comments

Comments
 (0)