9ec041efbf
Updates the output path so it's the same as when font files are imported in CSS: `url(./font.woff2)` Also adds missing font types to next package. ## Bug - [ ] 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 lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
315 lines
8.2 KiB
TypeScript
315 lines
8.2 KiB
TypeScript
import loader from '@next/font/local/loader'
|
|
|
|
describe('@next/font/local', () => {
|
|
describe('generated CSS', () => {
|
|
test('Default CSS', async () => {
|
|
const { css } = await loader({
|
|
functionName: '',
|
|
data: [{ src: './my-font.woff2' }],
|
|
config: {},
|
|
emitFontFile: () => '/_next/static/media/my-font.woff2',
|
|
resolve: jest.fn(),
|
|
fs: {
|
|
readFile: (_, cb) => cb(null, 'fontdata'),
|
|
},
|
|
})
|
|
|
|
expect(css).toMatchInlineSnapshot(`
|
|
"@font-face {
|
|
font-family: 'my-font';
|
|
src: url(/_next/static/media/my-font.woff2) format('woff2');
|
|
font-display: optional;
|
|
}"
|
|
`)
|
|
})
|
|
|
|
test('Default CSS - src array with unicodeRange', async () => {
|
|
const { css } = await loader({
|
|
functionName: '',
|
|
data: [
|
|
{ src: [{ file: './my-font.woff2', unicodeRange: 'unicode-range' }] },
|
|
],
|
|
config: {},
|
|
emitFontFile: () => '/_next/static/media/my-font.woff2',
|
|
resolve: jest.fn(),
|
|
fs: {
|
|
readFile: (_, cb) => cb(null, 'fontdata'),
|
|
},
|
|
})
|
|
|
|
expect(css).toMatchInlineSnapshot(`
|
|
"@font-face {
|
|
font-family: 'my-font';
|
|
src: url(/_next/static/media/my-font.woff2) format('woff2');
|
|
font-display: optional;
|
|
unicode-range: unicode-range;
|
|
}"
|
|
`)
|
|
})
|
|
|
|
test('Default CSS - src array without unicodeRange', async () => {
|
|
const { css } = await loader({
|
|
functionName: '',
|
|
data: [{ src: [{ file: './my-font.woff2' }] }],
|
|
config: {},
|
|
emitFontFile: () => '/_next/static/media/my-font.woff2',
|
|
resolve: jest.fn(),
|
|
fs: {
|
|
readFile: (_, cb) => cb(null, 'fontdata'),
|
|
},
|
|
})
|
|
|
|
expect(css).toMatchInlineSnapshot(`
|
|
"@font-face {
|
|
font-family: 'my-font';
|
|
src: url(/_next/static/media/my-font.woff2) format('woff2');
|
|
font-display: optional;
|
|
}"
|
|
`)
|
|
})
|
|
|
|
test('Weight and style', async () => {
|
|
const { css } = await loader({
|
|
functionName: '',
|
|
data: [{ src: './my-font.woff2', weight: '100 900', style: 'italic' }],
|
|
config: {},
|
|
emitFontFile: () => '/_next/static/media/my-font.woff2',
|
|
resolve: jest.fn(),
|
|
fs: {
|
|
readFile: (_, cb) => cb(null, 'fontdata'),
|
|
},
|
|
})
|
|
|
|
expect(css).toMatchInlineSnapshot(`
|
|
"@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;
|
|
}"
|
|
`)
|
|
})
|
|
|
|
test('Other properties', async () => {
|
|
const { css } = await loader({
|
|
functionName: '',
|
|
data: [
|
|
{
|
|
src: './my-font.woff2',
|
|
weight: '100 900',
|
|
style: 'italic',
|
|
ascentOverride: 'ascentOverride',
|
|
descentOverride: 'descentOverride',
|
|
lineGapOverride: 'lineGapOverride',
|
|
fontStretch: 'fontStretch',
|
|
fontFeatureSettings: 'fontFeatureSettings',
|
|
sizeAdjust: 'sizeAdjust',
|
|
},
|
|
],
|
|
config: {},
|
|
emitFontFile: () => '/_next/static/media/my-font.woff2',
|
|
resolve: jest.fn(),
|
|
fs: {
|
|
readFile: (_, cb) => cb(null, 'fontdata'),
|
|
},
|
|
})
|
|
|
|
expect(css).toMatchInlineSnapshot(`
|
|
"@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;
|
|
ascent-override: ascentOverride;
|
|
descent-override: descentOverride;
|
|
line-gap-override: lineGapOverride;
|
|
font-stretch: fontStretch;
|
|
font-feature-settings: fontFeatureSettings;
|
|
size-adjust: sizeAdjust;
|
|
}"
|
|
`)
|
|
})
|
|
|
|
test('Multiple files', async () => {
|
|
const { css } = await loader({
|
|
functionName: '',
|
|
data: [
|
|
{
|
|
src: [
|
|
{ file: './my-font1.woff', unicodeRange: '1' },
|
|
{ file: './my-font2.woff2', unicodeRange: '2' },
|
|
{ file: './my-font3.eot', unicodeRange: '3' },
|
|
{ file: './my-font4.ttf', unicodeRange: '4' },
|
|
{ file: './my-font5.otf', unicodeRange: '5' },
|
|
],
|
|
},
|
|
],
|
|
config: {},
|
|
emitFontFile: () => `/_next/static/media/font-file`,
|
|
resolve: jest.fn(),
|
|
fs: {
|
|
readFile: (_, cb) => cb(null, 'fontdata'),
|
|
},
|
|
})
|
|
|
|
expect(css).toMatchInlineSnapshot(`
|
|
"@font-face {
|
|
font-family: 'my-font1';
|
|
src: url(/_next/static/media/font-file) format('woff');
|
|
font-display: optional;
|
|
unicode-range: 1;
|
|
}
|
|
@font-face {
|
|
font-family: 'my-font1';
|
|
src: url(/_next/static/media/font-file) format('woff2');
|
|
font-display: optional;
|
|
unicode-range: 2;
|
|
}
|
|
@font-face {
|
|
font-family: 'my-font1';
|
|
src: url(/_next/static/media/font-file) format('embedded-opentype');
|
|
font-display: optional;
|
|
unicode-range: 3;
|
|
}
|
|
@font-face {
|
|
font-family: 'my-font1';
|
|
src: url(/_next/static/media/font-file) format('truetype');
|
|
font-display: optional;
|
|
unicode-range: 4;
|
|
}
|
|
@font-face {
|
|
font-family: 'my-font1';
|
|
src: url(/_next/static/media/font-file) format('opentype');
|
|
font-display: optional;
|
|
unicode-range: 5;
|
|
}"
|
|
`)
|
|
})
|
|
})
|
|
|
|
describe('Errors', () => {
|
|
test('Not using default export', async () => {
|
|
await expect(
|
|
loader({
|
|
functionName: 'Named',
|
|
data: [],
|
|
config: {},
|
|
emitFontFile: jest.fn(),
|
|
resolve: jest.fn(),
|
|
fs: {},
|
|
})
|
|
).rejects.toThrowErrorMatchingInlineSnapshot(
|
|
`"@next/font/local has no named exports"`
|
|
)
|
|
})
|
|
|
|
test('Invalid file extension', async () => {
|
|
await expect(
|
|
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(
|
|
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\`"
|
|
`)
|
|
})
|
|
|
|
test('Empty src array', async () => {
|
|
await expect(
|
|
loader({
|
|
functionName: '',
|
|
data: [{ src: [] }],
|
|
config: {},
|
|
emitFontFile: jest.fn(),
|
|
resolve: jest.fn(),
|
|
fs: {},
|
|
})
|
|
).rejects.toThrowErrorMatchingInlineSnapshot(
|
|
`"Src must contain one or more files"`
|
|
)
|
|
})
|
|
|
|
test('Src array must have one or more elements', async () => {
|
|
await expect(
|
|
loader({
|
|
functionName: '',
|
|
data: [{ src: [] }],
|
|
config: {},
|
|
emitFontFile: jest.fn(),
|
|
resolve: jest.fn(),
|
|
fs: {},
|
|
})
|
|
).rejects.toThrowErrorMatchingInlineSnapshot(
|
|
`"Src must contain one or more files"`
|
|
)
|
|
})
|
|
|
|
test('Src array elements must have file property', async () => {
|
|
await expect(
|
|
loader({
|
|
functionName: '',
|
|
data: [
|
|
{
|
|
src: [
|
|
{ file: './my-font1.woff2', unicodeRange: '1' },
|
|
{ unicodeRange: '2' },
|
|
],
|
|
},
|
|
],
|
|
|
|
config: {},
|
|
emitFontFile: jest.fn(),
|
|
resolve: jest.fn(),
|
|
fs: {},
|
|
})
|
|
).rejects.toThrowErrorMatchingInlineSnapshot(
|
|
`"Src array objects must have a \`file\` property"`
|
|
)
|
|
})
|
|
|
|
test("Src array files must have unicodeRange if there's many files", async () => {
|
|
await expect(
|
|
loader({
|
|
functionName: '',
|
|
data: [
|
|
{
|
|
src: [
|
|
{ file: './my-font1.woff2', unicodeRange: '1' },
|
|
{ file: './my-font2.woff2' },
|
|
],
|
|
},
|
|
],
|
|
|
|
config: {},
|
|
emitFontFile: jest.fn(),
|
|
resolve: jest.fn(),
|
|
fs: {},
|
|
})
|
|
).rejects.toThrowErrorMatchingInlineSnapshot(
|
|
`"Files must have a unicode-range if there's more than one"`
|
|
)
|
|
})
|
|
})
|
|
})
|