2022-09-22 21:49:02 +02:00
|
|
|
import loader from '@next/font/local/loader'
|
|
|
|
|
|
|
|
describe('@next/font/local', () => {
|
|
|
|
describe('generated CSS', () => {
|
|
|
|
test('Default CSS', async () => {
|
2022-10-24 07:21:38 +02:00
|
|
|
// @ts-expect-error
|
2022-09-22 21:49:02 +02:00
|
|
|
const { css } = await loader({
|
|
|
|
functionName: '',
|
|
|
|
data: [{ src: './my-font.woff2' }],
|
|
|
|
config: {},
|
2022-09-26 21:59:38 +02:00
|
|
|
emitFontFile: () => '/_next/static/media/my-font.woff2',
|
2022-09-22 21:49:02 +02:00
|
|
|
resolve: jest.fn(),
|
|
|
|
fs: {
|
|
|
|
readFile: (_, cb) => cb(null, 'fontdata'),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(css).toMatchInlineSnapshot(`
|
2022-10-11 21:52:10 +02:00
|
|
|
"@font-face {
|
|
|
|
font-family: 'my-font';
|
|
|
|
src: url(/_next/static/media/my-font.woff2) format('woff2');
|
|
|
|
font-display: optional;
|
|
|
|
}"
|
|
|
|
`)
|
2022-09-22 21:49:02 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
test('Weight and style', async () => {
|
2022-10-24 07:21:38 +02:00
|
|
|
// @ts-expect-error
|
2022-09-22 21:49:02 +02:00
|
|
|
const { css } = await loader({
|
|
|
|
functionName: '',
|
|
|
|
data: [{ src: './my-font.woff2', weight: '100 900', style: 'italic' }],
|
|
|
|
config: {},
|
2022-09-26 21:59:38 +02:00
|
|
|
emitFontFile: () => '/_next/static/media/my-font.woff2',
|
2022-09-22 21:49:02 +02:00
|
|
|
resolve: jest.fn(),
|
|
|
|
fs: {
|
|
|
|
readFile: (_, cb) => cb(null, 'fontdata'),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(css).toMatchInlineSnapshot(`
|
2022-10-11 21:52:10 +02:00
|
|
|
"@font-face {
|
|
|
|
font-family: 'my-font';
|
|
|
|
src: url(/_next/static/media/my-font.woff2) format('woff2');
|
|
|
|
font-display: optional;
|
|
|
|
font-weight: 100 900;
|
|
|
|
font-style: italic;
|
|
|
|
}"
|
|
|
|
`)
|
2022-09-22 21:49:02 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
test('Other properties', async () => {
|
2022-10-24 07:21:38 +02:00
|
|
|
// @ts-expect-error
|
2022-09-22 21:49:02 +02:00
|
|
|
const { css } = await loader({
|
|
|
|
functionName: '',
|
|
|
|
data: [
|
|
|
|
{
|
|
|
|
src: './my-font.woff2',
|
2022-10-11 21:52:10 +02:00
|
|
|
declarations: [
|
|
|
|
{ prop: 'font-feature-settings', value: '"smcp" on' },
|
|
|
|
{ prop: 'ascent-override', value: '90%' },
|
|
|
|
],
|
2022-09-22 21:49:02 +02:00
|
|
|
},
|
|
|
|
],
|
|
|
|
config: {},
|
2022-09-26 21:59:38 +02:00
|
|
|
emitFontFile: () => '/_next/static/media/my-font.woff2',
|
2022-09-22 21:49:02 +02:00
|
|
|
resolve: jest.fn(),
|
|
|
|
fs: {
|
|
|
|
readFile: (_, cb) => cb(null, 'fontdata'),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(css).toMatchInlineSnapshot(`
|
2022-10-11 21:52:10 +02:00
|
|
|
"@font-face {
|
|
|
|
font-feature-settings: \\"smcp\\" on;
|
|
|
|
ascent-override: 90%;
|
|
|
|
font-family: 'my-font';
|
|
|
|
src: url(/_next/static/media/my-font.woff2) format('woff2');
|
|
|
|
font-display: optional;
|
|
|
|
}"
|
|
|
|
`)
|
2022-09-22 21:49:02 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('Errors', () => {
|
|
|
|
test('Not using default export', async () => {
|
|
|
|
await expect(
|
2022-10-24 07:21:38 +02:00
|
|
|
// @ts-expect-error
|
2022-09-22 21:49:02 +02:00
|
|
|
loader({
|
|
|
|
functionName: 'Named',
|
|
|
|
data: [],
|
|
|
|
config: {},
|
|
|
|
emitFontFile: jest.fn(),
|
|
|
|
resolve: jest.fn(),
|
|
|
|
fs: {},
|
|
|
|
})
|
|
|
|
).rejects.toThrowErrorMatchingInlineSnapshot(
|
|
|
|
`"@next/font/local has no named exports"`
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-10-11 21:52:10 +02:00
|
|
|
test('Missing src', async () => {
|
|
|
|
await expect(
|
2022-10-24 07:21:38 +02:00
|
|
|
// @ts-expect-error
|
2022-10-11 21:52:10 +02:00
|
|
|
loader({
|
|
|
|
functionName: '',
|
|
|
|
data: [],
|
|
|
|
config: {},
|
|
|
|
emitFontFile: jest.fn(),
|
|
|
|
resolve: jest.fn(),
|
|
|
|
fs: {},
|
|
|
|
})
|
|
|
|
).rejects.toThrowErrorMatchingInlineSnapshot(
|
|
|
|
`"Missing required \`src\` property"`
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-09-22 21:49:02 +02:00
|
|
|
test('Invalid file extension', async () => {
|
|
|
|
await expect(
|
2022-10-24 07:21:38 +02:00
|
|
|
// @ts-expect-error
|
2022-09-22 21:49:02 +02:00
|
|
|
loader({
|
|
|
|
functionName: '',
|
|
|
|
data: [{ src: './font/font-file.abc' }],
|
|
|
|
config: {},
|
|
|
|
emitFontFile: jest.fn(),
|
|
|
|
resolve: jest.fn().mockResolvedValue(''),
|
|
|
|
fs: {},
|
|
|
|
})
|
|
|
|
).rejects.toThrowErrorMatchingInlineSnapshot(
|
|
|
|
`"Unexpected file \`./font/font-file.abc\`"`
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('Invalid display value', async () => {
|
|
|
|
await expect(
|
2022-10-24 07:21:38 +02:00
|
|
|
// @ts-expect-error
|
2022-09-22 21:49:02 +02:00
|
|
|
loader({
|
|
|
|
functionName: '',
|
|
|
|
data: [{ src: './font-file.woff2', display: 'invalid' }],
|
|
|
|
config: {},
|
|
|
|
emitFontFile: jest.fn(),
|
|
|
|
resolve: jest.fn(),
|
|
|
|
fs: {},
|
|
|
|
})
|
|
|
|
).rejects.toThrowErrorMatchingInlineSnapshot(`
|
|
|
|
"Invalid display value \`invalid\`.
|
|
|
|
Available display values: \`auto\`, \`block\`, \`swap\`, \`fallback\`, \`optional\`"
|
|
|
|
`)
|
|
|
|
})
|
2022-10-11 21:52:10 +02:00
|
|
|
|
|
|
|
test('Invalid declaration', async () => {
|
|
|
|
await expect(
|
2022-10-24 07:21:38 +02:00
|
|
|
// @ts-expect-error
|
2022-10-11 21:52:10 +02:00
|
|
|
loader({
|
|
|
|
functionName: '',
|
|
|
|
data: [
|
|
|
|
{
|
|
|
|
src: './font-file.woff2',
|
|
|
|
declarations: [{ prop: 'src', value: '/hello.woff2' }],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
|
|
|
|
config: {},
|
|
|
|
emitFontFile: jest.fn(),
|
|
|
|
resolve: jest.fn(),
|
|
|
|
fs: {},
|
|
|
|
})
|
|
|
|
).rejects.toThrowErrorMatchingInlineSnapshot(
|
|
|
|
`"Invalid declaration prop: \`src\`"`
|
|
|
|
)
|
|
|
|
})
|
2022-09-22 21:49:02 +02:00
|
|
|
})
|
|
|
|
})
|