File size: 3,236 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { pickFontFileForFallbackGeneration } from './pick-font-file-for-fallback-generation'

describe('pickFontFileForFallbackGeneration', () => {
  it('should pick the weight closest to 400', () => {
    expect(
      pickFontFileForFallbackGeneration([
        {
          weight: '300',
        },
        {
          weight: '600',
        },
      ])
    ).toEqual({
      weight: '300',
    })

    expect(
      pickFontFileForFallbackGeneration([
        { weight: '200' },
        {
          weight: '500',
        },
      ])
    ).toEqual({
      weight: '500',
    })

    expect(
      pickFontFileForFallbackGeneration([
        {
          weight: 'normal',
        },
        {
          weight: '700',
        },
      ])
    ).toEqual({
      weight: 'normal',
    })

    expect(
      pickFontFileForFallbackGeneration([
        {
          weight: 'bold',
        },
        {
          weight: '900',
        },
      ])
    ).toEqual({
      weight: 'bold',
    })
  })

  it('should pick the thinner weight if both have the same distance to 400', () => {
    expect(
      pickFontFileForFallbackGeneration([
        {
          weight: '300',
        },
        {
          weight: '500',
        },
      ])
    ).toEqual({
      weight: '300',
    })
  })

  it('should pick variable range closest to 400', () => {
    expect(
      pickFontFileForFallbackGeneration([
        {
          weight: '100 300',
        },
        {
          weight: '600 900',
        },
      ])
    ).toEqual({
      weight: '100 300',
    })

    expect(
      pickFontFileForFallbackGeneration([
        { weight: '100 200' },
        {
          weight: '500 800',
        },
      ])
    ).toEqual({
      weight: '500 800',
    })

    expect(
      pickFontFileForFallbackGeneration([
        { weight: '100 900' },
        {
          weight: '300 399',
        },
      ])
    ).toEqual({
      weight: '100 900',
    })
  })

  it('should prefer normal style over italic', () => {
    expect(
      pickFontFileForFallbackGeneration([
        { weight: '400', style: 'normal' },
        { weight: '400', style: 'italic' },
      ])
    ).toEqual({ weight: '400', style: 'normal' })
  })

  it('should error on invalid weight in array', async () => {
    expect(() =>
      pickFontFileForFallbackGeneration([
        { path: './font1.woff2', weight: 'normal bold' },
        { path: './font2.woff2', weight: '400 bold' },
        { path: './font3.woff2', weight: 'normal 700' },
        { path: './font4.woff2', weight: '100 abc' },
      ])
    ).toThrowErrorMatchingInlineSnapshot(`
      "Invalid weight value in src array: \`100 abc\`.
      Expected \`normal\`, \`bold\` or a number."
    `)
  })

  test('Invalid variable weight in array', async () => {
    expect(() =>
      pickFontFileForFallbackGeneration([
        { path: './font1.woff2', weight: 'normal bold' },
        { path: './font2.woff2', weight: '400 bold' },
        { path: './font3.woff2', weight: 'normal 700' },
        { path: './font4.woff2', weight: '100 abc' },
      ])
    ).toThrowErrorMatchingInlineSnapshot(`
      "Invalid weight value in src array: \`100 abc\`.
      Expected \`normal\`, \`bold\` or a number."
    `)
  })
})