Skip to content

Commit

Permalink
chore: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raisedadead committed Dec 16, 2024
1 parent fe7a77b commit 09d93f5
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions src/__tests__/components/head.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { MetaHead } from '../../components/head';

vi.mock('next/head', () => ({
__esModule: true,
default: ({ children }: { children: React.ReactNode }) => <>{children}</>
default: ({ children }: { children: React.ReactNode }) => {
return <>{children}</>;
}
}));

describe('MetaHead', () => {
Expand All @@ -20,22 +22,27 @@ describe('MetaHead', () => {
return render(<MetaHead {...{ ...defaultProps, ...props }} />);
}

beforeEach(() => {
// Clear any previous head elements
document.head.innerHTML = '';
});

it('renders default meta tags', () => {
const { container } = renderMetaHead();
renderMetaHead();

expect(container.querySelector('title')?.textContent).toBe(
expect(document.title).toBe(
'Mrugesh Mohapatra — Portfolio of a nocturnal developer.'
);
expect(
container
document
.querySelector('meta[name="description"]')
?.getAttribute('content')
).toContain('Namaste! I am a technologist based out of Bengaluru, India.');
expect(
container.querySelector('link[rel="canonical"]')?.getAttribute('href')
document.querySelector('link[rel="canonical"]')?.getAttribute('href')
).toBe('https://mrugesh.dev');
expect(
container
document
.querySelector('meta[property="og:image"]')
?.getAttribute('content')
).toBe('http://mrugesh.dev/images/og-image.webp');
Expand All @@ -48,59 +55,57 @@ describe('MetaHead', () => {
pageUrl: 'https://example.com',
pageImage: 'https://example.com/image.jpg'
};
const { container } = renderMetaHead(customProps);
renderMetaHead(customProps);

expect(container.querySelector('title')?.textContent).toBe(
'Custom Title • Mrugesh Mohapatra'
);
expect(document.title).toBe('Custom Title • Mrugesh Mohapatra');
expect(
container
document
.querySelector('meta[name="description"]')
?.getAttribute('content')
).toBe('Custom description');
expect(
container.querySelector('link[rel="canonical"]')?.getAttribute('href')
document.querySelector('link[rel="canonical"]')?.getAttribute('href')
).toBe('https://example.com');
expect(
container
document
.querySelector('meta[property="og:image"]')
?.getAttribute('content')
).toBe('https://example.com/image.jpg');
});

it('renders correct Open Graph and Twitter tags', () => {
const { container } = renderMetaHead();
renderMetaHead();

expect(
container
document
.querySelector('meta[property="og:type"]')
?.getAttribute('content')
).toBe('website');
expect(
container
document
.querySelector('meta[property="og:site_name"]')
?.getAttribute('content')
).toBe('Mrugesh Mohapatra');
expect(
container
document
.querySelector('meta[name="twitter:card"]')
?.getAttribute('content')
).toBe('summary_large_image');
expect(
container
document
.querySelector('meta[name="twitter:creator"]')
?.getAttribute('content')
).toBe('@raisedadead');
});

it('renders favicon and theme color', () => {
const { container } = renderMetaHead();
renderMetaHead();

expect(
container.querySelector('link[rel="icon"]')?.getAttribute('href')
document.querySelector('link[rel="icon"]')?.getAttribute('href')
).toBe('/favicon.ico');
expect(
container
document
.querySelector('meta[name="theme-color"]')
?.getAttribute('content')
).toBe('#32ded4');
Expand Down

0 comments on commit 09d93f5

Please sign in to comment.