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
32 changes: 32 additions & 0 deletions packages/starlight/__tests__/basics/omit-canonical.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { expect, test, vi } from 'vitest';
import { getRouteDataTestContext } from '../test-utils';
import { generateRouteData } from '../../utils/routing/data';
import { routes } from '../../utils/routing';

vi.mock('astro:content', async () =>
(await import('../test-utils')).mockedAstroContent({
docs: [
['index.mdx', { title: 'Home Page' }],
[
'environmental-impact.md',
{
title: 'Eco-friendly docs',
description:
'Learn how Starlight can help you build greener documentation sites and reduce your carbon footprint.',
},
],
],
})
);

test('omits link canonical tag when site is not set', () => {
const route = routes[0]!;
const { head } = generateRouteData({
props: { ...route, headings: [] },
context: getRouteDataTestContext(undefined, false),
});

const canonicalExists = head.some((tag) => tag.tag === 'link' && tag.attrs?.rel === 'canonical');

expect(canonicalExists).toBe(false);
});
15 changes: 13 additions & 2 deletions packages/starlight/__tests__/head/head.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,15 @@ test('places the default favicon below any user provided icons', () => {
expect(defaultFaviconIndex).toBeGreaterThan(userFaviconIndex);
});

function getTestHead(heads: HeadConfig = [], route = routes[0]!): HeadConfig {
test('omits meta og:url tag when site is not set', () => {
const head = getTestHead(undefined, undefined, false);

const ogUrlExists = head.some((tag) => tag.tag === 'meta' && tag.attrs?.property === 'og:url');

expect(ogUrlExists).toBe(false);
});

function getTestHead(heads: HeadConfig = [], route = routes[0]!, setSite?: boolean): HeadConfig {
return generateRouteData({
props: {
...route,
Expand All @@ -201,6 +209,9 @@ function getTestHead(heads: HeadConfig = [], route = routes[0]!): HeadConfig {
},
},
},
context: getRouteDataTestContext(),
context:
setSite === undefined
? getRouteDataTestContext()
: getRouteDataTestContext(undefined, setSite),
}).head;
}
8 changes: 6 additions & 2 deletions packages/starlight/__tests__/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,15 @@ export async function mockedCollectionConfig(docsUserSchema?: Parameters<typeof
};
}

export function getRouteDataTestContext(pathname?: string): RouteDataContext {
export function getRouteDataTestContext(
pathname?: string,
setSite: boolean = true
): RouteDataContext {
const site = new URL('https://example.com');

return {
generator: 'Astro',
site,
url: pathname ? new URL(pathname, site) : site,
site: setSite ? site : undefined,
};
}