rsnext/test/unit/eslint-plugin-next/google-font-display.test.ts
Mark Ladyshau 2b85da7735
Change condition to check for string attribute in lint rules utility function (#42625)
<!--
Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change that you're making:
-->

Fixes #42604.

## Bug

- [x] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2022-11-09 19:54:38 -08:00

172 lines
3.9 KiB
TypeScript

import rule from '@next/eslint-plugin-next/dist/rules/google-font-display'
import { RuleTester } from 'eslint'
;(RuleTester as any).setDefaultConfig({
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module',
ecmaFeatures: {
modules: true,
jsx: true,
},
},
})
const ruleTester = new RuleTester()
ruleTester.run('google-font-display', rule, {
valid: [
`import Head from "next/head";
export default Test = () => {
return (
<Head>
<link href={test} rel="test" />
<link
href={process.env.NEXT_PUBLIC_CANONICAL_URL}
rel="canonical"
/>
<link
href={new URL("../public/favicon.ico", import.meta.url).toString()}
rel="icon"
/>
<link
href="https://fonts.googleapis.com/css2?family=Krona+One&display=optional"
rel="stylesheet"
/>
</Head>
);
};
`,
`import Document, { Html, Head } from "next/document";
class MyDocument extends Document {
render() {
return (
<Html>
<Head>
<link
href="https://fonts.googleapis.com/css?family=Krona+One&display=swap"
rel="stylesheet"
/>
</Head>
</Html>
);
}
}
export default MyDocument;
`,
`import Document, { Html, Head } from "next/document";
class MyDocument extends Document {
render() {
return (
<Html>
<Head>
<link
href="https://fonts.googleapis.com/css?family=Krona+One&display=swap"
rel="stylesheet"
crossOrigin=""
/>
</Head>
</Html>
);
}
}
export default MyDocument;
`,
],
invalid: [
{
code: `import Head from "next/head";
export default Test = () => {
return (
<Head>
<link
href="https://fonts.googleapis.com/css2?family=Krona+One"
rel="stylesheet"
/>
</Head>
);
};
`,
errors: [
{
message:
'A font-display parameter is missing (adding `&display=optional` is recommended). See: https://nextjs.org/docs/messages/google-font-display',
type: 'JSXOpeningElement',
},
],
},
{
code: `import Head from "next/head";
export default Test = () => {
return (
<Head>
<link
href="https://fonts.googleapis.com/css2?family=Krona+One&display=block"
rel="stylesheet"
/>
</Head>
);
};
`,
errors: [
{
message:
'Block is not recommended. See: https://nextjs.org/docs/messages/google-font-display',
type: 'JSXOpeningElement',
},
],
},
{
code: `import Head from "next/head";
export default Test = () => {
return (
<Head>
<link
href="https://fonts.googleapis.com/css2?family=Krona+One&display=auto"
rel="stylesheet"
/>
</Head>
);
};
`,
errors: [
{
message:
'Auto is not recommended. See: https://nextjs.org/docs/messages/google-font-display',
type: 'JSXOpeningElement',
},
],
},
{
code: `import Head from "next/head";
export default Test = () => {
return (
<Head>
<link
href="https://fonts.googleapis.com/css2?display=fallback&family=Krona+One"
rel="stylesheet"
/>
</Head>
);
};
`,
errors: [
{
message:
'Fallback is not recommended. See: https://nextjs.org/docs/messages/google-font-display',
type: 'JSXOpeningElement',
},
],
},
],
})