Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/useFavicon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useEffect } from 'react';
const useFavicon = (href: string) => {
useEffect(() => {
const link: HTMLLinkElement =
document.querySelector("link[rel*='icon']") || document.createElement('link');
document.querySelector("link[rel~='icon']") || document.createElement('link');
link.type = 'image/x-icon';
link.rel = 'shortcut icon';
link.href = href;
Expand Down
44 changes: 40 additions & 4 deletions tests/useFavicon.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import { renderHook } from '@testing-library/react-hooks';
import useFavicon from '../src/useFavicon';

afterEach(() => {
const favicon = document.querySelector("link[rel*='icon']");
if (favicon) {
favicon.remove();
}
document.querySelectorAll("link[rel*='icon']").forEach((link) => link.remove());
});

describe('useFavicon', () => {
Expand Down Expand Up @@ -54,4 +51,43 @@ describe('useFavicon', () => {
hook.rerender('https://en.wikipedia.org/wiki/Favicon');
expect(favicon.href).toBe('https://en.wikipedia.org/wiki/Favicon');
});

it.each(['apple-touch-icon', 'mask-icon'])(
'should leave %s links unchanged when creating a favicon',
(rel) => {
const link = document.createElement('link');
link.rel = rel;
link.href = 'https://example.com/special-icon.png';
document.head.appendChild(link);

renderHook(() => useFavicon('https://example.com/favicon.ico'));

expect(link.rel).toBe(rel);
expect(link.href).toBe('https://example.com/special-icon.png');
expect(document.querySelector<HTMLLinkElement>('link[rel~="icon"]')!.href).toBe(
'https://example.com/favicon.ico'
);
}
);

it.each(['icon', 'shortcut icon'])(
'should find an existing %s link after an Apple touch icon',
(rel) => {
const touchIcon = document.createElement('link');
touchIcon.rel = 'apple-touch-icon';
touchIcon.href = 'https://example.com/touch.png';
const favicon = document.createElement('link');
favicon.rel = rel;
favicon.href = 'https://example.com/old.ico';
document.head.appendChild(touchIcon);
document.head.appendChild(favicon);

renderHook(() => useFavicon('https://example.com/new.ico'));

expect(favicon.href).toBe('https://example.com/new.ico');
expect(touchIcon.href).toBe('https://example.com/touch.png');
expect(touchIcon.rel).toBe('apple-touch-icon');
expect(document.querySelectorAll('link[rel~="icon"]')).toHaveLength(1);
}
);
});