|
| 1 | +import { findFontFilesInCss } from './find-font-files-in-css' |
| 2 | + |
| 3 | +describe('findFontFilesInCss', () => { |
| 4 | + it('should find all font files and preload requested subsets', () => { |
| 5 | + const css = `/* latin */ |
| 6 | + @font-face { |
| 7 | + font-family: 'Fraunces'; |
| 8 | + font-style: normal; |
| 9 | + font-weight: 300; |
| 10 | + src: url(latin1.woff2) format('woff2'); |
| 11 | + } |
| 12 | +
|
| 13 | + /* greek */ |
| 14 | + @font-face { |
| 15 | + font-family: 'Fraunces'; |
| 16 | + font-style: normal; |
| 17 | + font-weight: 300; |
| 18 | + src: url(greek1.woff2) format('woff2'); |
| 19 | + } |
| 20 | +
|
| 21 | + /* latin */ |
| 22 | + @font-face { |
| 23 | + font-family: 'Fraunces'; |
| 24 | + font-style: normal; |
| 25 | + font-weight: 400; |
| 26 | + src: url(latin2.woff2) format('woff2'); |
| 27 | + } |
| 28 | +
|
| 29 | + /* greek */ |
| 30 | + @font-face { |
| 31 | + font-family: 'Fraunces'; |
| 32 | + font-style: normal; |
| 33 | + font-weight: 400; |
| 34 | + src: url(greek2.woff2) format('woff2'); |
| 35 | + } |
| 36 | +
|
| 37 | + /* cyrilic */ |
| 38 | + @font-face { |
| 39 | + font-family: 'Fraunces'; |
| 40 | + font-style: normal; |
| 41 | + font-weight: 400; |
| 42 | + src: url(cyrilic.woff2) format('woff2'); |
| 43 | + } |
| 44 | + ` |
| 45 | + |
| 46 | + expect(findFontFilesInCss(css, ['latin', 'cyrilic'])).toEqual([ |
| 47 | + { googleFontFileUrl: 'latin1.woff2', preloadFontFile: true }, |
| 48 | + { googleFontFileUrl: 'greek1.woff2', preloadFontFile: false }, |
| 49 | + { googleFontFileUrl: 'latin2.woff2', preloadFontFile: true }, |
| 50 | + { googleFontFileUrl: 'greek2.woff2', preloadFontFile: false }, |
| 51 | + { googleFontFileUrl: 'cyrilic.woff2', preloadFontFile: true }, |
| 52 | + ]) |
| 53 | + }) |
| 54 | + |
| 55 | + it('should not return duplicate font files when several variants use the same font file', () => { |
| 56 | + const css = `/* latin */ |
| 57 | + @font-face { |
| 58 | + font-family: 'Fraunces'; |
| 59 | + font-style: normal; |
| 60 | + font-weight: 100; |
| 61 | + font-display: swap; |
| 62 | + src: url(https://fonts.gstatic.com/s/fraunces/v24/6NUu8FyLNQOQZAnv9bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib14c7qv8oRcTn.woff2) format('woff2'); |
| 63 | + unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD; |
| 64 | + } |
| 65 | + /* latin */ |
| 66 | + @font-face { |
| 67 | + font-family: 'Fraunces'; |
| 68 | + font-style: normal; |
| 69 | + font-weight: 300; |
| 70 | + font-display: swap; |
| 71 | + src: url(https://fonts.gstatic.com/s/fraunces/v24/6NUu8FyLNQOQZAnv9bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib14c7qv8oRcTn.woff2) format('woff2'); |
| 72 | + unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD; |
| 73 | + } |
| 74 | + /* latin */ |
| 75 | + @font-face { |
| 76 | + font-family: 'Fraunces'; |
| 77 | + font-style: normal; |
| 78 | + font-weight: 900; |
| 79 | + font-display: swap; |
| 80 | + src: url(https://fonts.gstatic.com/s/fraunces/v24/6NUu8FyLNQOQZAnv9bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib14c7qv8oRcTn.woff2) format('woff2'); |
| 81 | + unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD; |
| 82 | + } |
| 83 | + ` |
| 84 | + |
| 85 | + expect(findFontFilesInCss(css)).toEqual([ |
| 86 | + { |
| 87 | + googleFontFileUrl: |
| 88 | + 'https://fonts.gstatic.com/s/fraunces/v24/6NUu8FyLNQOQZAnv9bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib14c7qv8oRcTn.woff2', |
| 89 | + preloadFontFile: false, |
| 90 | + }, |
| 91 | + ]) |
| 92 | + }) |
| 93 | +}) |
0 commit comments