File size: 3,991 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { validateGoogleFontFunctionCall } from './validate-google-font-function-call'

describe('validateFontFunctionCall errors', () => {
  test('Missing function name', () => {
    expect(() =>
      validateGoogleFontFunctionCall(
        '', // default import
        undefined
      )
    ).toThrowErrorMatchingInlineSnapshot(
      `"next/font/google has no default export"`
    )
  })

  test('Unknown font', () => {
    expect(() =>
      validateGoogleFontFunctionCall('Unknown_Font', undefined)
    ).toThrowErrorMatchingInlineSnapshot(`"Unknown font \`Unknown Font\`"`)
  })

  test('Unknown weight', () => {
    expect(() =>
      validateGoogleFontFunctionCall('Inter', {
        weight: '123',
        subsets: ['latin'],
      })
    ).toThrowErrorMatchingInlineSnapshot(`
        "Unknown weight \`123\` for font \`Inter\`.
        Available weights: \`100\`, \`200\`, \`300\`, \`400\`, \`500\`, \`600\`, \`700\`, \`800\`, \`900\`, \`variable\`"
      `)
  })

  test('Missing weight for non variable font', () => {
    expect(() => validateGoogleFontFunctionCall('Abel', { subsets: ['latin'] }))
      .toThrowErrorMatchingInlineSnapshot(`
        "Missing weight for font \`Abel\`.
        Available weights: \`400\`"
      `)
  })

  test('Unknown style', () => {
    expect(() =>
      validateGoogleFontFunctionCall('Molle', {
        weight: '400',
        style: 'normal',
        subsets: ['latin'],
      })
    ).toThrowErrorMatchingInlineSnapshot(`
        "Unknown style \`normal\` for font \`Molle\`.
        Available styles: \`italic\`"
      `)
  })

  test('Invalid display value', () => {
    expect(() =>
      validateGoogleFontFunctionCall('Inter', {
        display: 'Invalid',
        subsets: ['latin'],
      })
    ).toThrowErrorMatchingInlineSnapshot(`
        "Invalid display value \`Invalid\` for font \`Inter\`.
        Available display values: \`auto\`, \`block\`, \`swap\`, \`fallback\`, \`optional\`"
      `)
  })

  test('Variable in weight array', async () => {
    expect(() =>
      validateGoogleFontFunctionCall('Inter', {
        weight: ['100', 'variable'],
        subsets: ['latin'],
      })
    ).toThrowErrorMatchingInlineSnapshot(
      `"Unexpected \`variable\` in weight array for font \`Inter\`. You only need \`variable\`, it includes all available weights."`
    )
  })

  test('Invalid subset in call', async () => {
    expect(() =>
      validateGoogleFontFunctionCall('Inter', { subsets: ['latin', 'oops'] })
    ).toThrowErrorMatchingInlineSnapshot(`
        "Unknown subset \`oops\` for font \`Inter\`.
        Available subsets: \`cyrillic\`, \`cyrillic-ext\`, \`greek\`, \`greek-ext\`, \`latin\`, \`latin-ext\`, \`vietnamese\`"
      `)
  })

  test('Missing subsets in config and call', async () => {
    expect(() => validateGoogleFontFunctionCall('Inter', {}))
      .toThrowErrorMatchingInlineSnapshot(`
      "Preload is enabled but no subsets were specified for font \`Inter\`. Please specify subsets or disable preloading if your intended subset can't be preloaded.
      Available subsets: \`cyrillic\`, \`cyrillic-ext\`, \`greek\`, \`greek-ext\`, \`latin\`, \`latin-ext\`, \`vietnamese\`

      Read more: https://nextjs.org/docs/messages/google-fonts-missing-subsets"
    `)
  })

  test('Setting axes on non variable font', async () => {
    expect(() =>
      validateGoogleFontFunctionCall('Abel', {
        weight: '400',
        axes: [],
        subsets: ['latin'],
      })
    ).toThrowErrorMatchingInlineSnapshot(
      `"Axes can only be defined for variable fonts."`
    )
  })

  test('Setting axes on variable font with incorrect weight', async () => {
    expect(() =>
      validateGoogleFontFunctionCall('Roboto_Flex', {
        weight: ['400', '700'],
        axes: ['wght'],
        subsets: ['latin'],
      })
    ).toThrowErrorMatchingInlineSnapshot(
      '"Axes can only be defined for variable fonts when the weight property is nonexistent or set to `variable`."'
    )
  })
})