11import type { ResolveFontOptions } from '../../src'
22import { describe , expect , it } from 'vitest'
33import { createUnifont , providers } from '../../src'
4+ import { splitCssIntoSubsets } from '../../src/providers/google'
45import { getOptimizerIdentityFromUrl , groupBy , pickUniqueBy } from '../utils'
56
67describe ( 'google' , ( ) => {
@@ -16,7 +17,7 @@ describe('google', () => {
1617
1718 const { fonts } = await unifont . resolveFont ( 'Poppins' )
1819
19- expect ( fonts ) . toHaveLength ( 8 )
20+ expect ( fonts ) . toHaveLength ( 6 )
2021 } )
2122
2223 it ( 'filters fonts based on provided options' , async ( ) => {
@@ -27,13 +28,12 @@ describe('google', () => {
2728 const { fonts } = await unifont . resolveFont ( 'Poppins' , {
2829 styles,
2930 weights,
30- subsets : [ ] ,
3131 } )
3232
3333 const resolvedStyles = pickUniqueBy ( fonts , fnt => fnt . style )
3434 const resolvedWeights = pickUniqueBy ( fonts , fnt => String ( fnt . weight ) )
3535
36- expect ( fonts ) . toHaveLength ( 4 )
36+ expect ( fonts ) . toHaveLength ( 3 )
3737 expect ( resolvedStyles ) . toMatchObject ( styles )
3838 expect ( resolvedWeights ) . toMatchObject ( weights )
3939 } )
@@ -107,7 +107,6 @@ describe('google', () => {
107107 const { fonts } = await unifont . resolveFont ( 'Poppins' , {
108108 styles : [ 'normal' ] ,
109109 weights : [ '400' ] ,
110- subsets : [ ] ,
111110 } )
112111
113112 // Do not use sanitizeFontSource here, as we must test the optimizer identity in url params
@@ -146,4 +145,60 @@ describe('google', () => {
146145 }
147146 ` )
148147 } )
148+
149+ it ( 'filters subsets correctly' , async ( ) => {
150+ const unifont = await createUnifont ( [ providers . google ( ) ] )
151+
152+ const { fonts } = await unifont . resolveFont ( 'Roboto' , { subsets : [ 'latin' ] } )
153+ expect ( fonts . length ) . toEqual ( 4 )
154+ } )
155+
156+ describe ( 'splitCssIntoSubsets()' , ( ) => {
157+ it ( 'associates subsets and css correctly if there are comments' , ( ) => {
158+ expect (
159+ splitCssIntoSubsets ( `
160+ /* vietnamese */
161+ @font-face {
162+ font-family: 'A';
163+ }
164+ /* latin-ext */
165+ @font-face {
166+ font-family: 'B';
167+ }
168+ @font-face {
169+ font-family: 'Still B';
170+ }
171+ /* latin */
172+ @font-face {
173+ font-family: 'C';
174+ }
175+ body {
176+ --google-font-color-bungeetint:none;
177+ }
178+ @font-face {
179+ font-family: 'Still C';
180+ }
181+ ` ) ,
182+ ) . toEqual ( [
183+ { subset : 'vietnamese' , css : '@font-face{font-family:"A"}' } ,
184+ { subset : 'latin-ext' , css : '@font-face{font-family:"B"}' } ,
185+ { subset : 'latin-ext' , css : '@font-face{font-family:"Still B"}' } ,
186+ { subset : 'latin' , css : '@font-face{font-family:"C"}' } ,
187+ { subset : 'latin' , css : '@font-face{font-family:"Still C"}' } ,
188+ ] )
189+ } )
190+ } )
191+
192+ it ( 'it does not associate subsets if there are no comments' , ( ) => {
193+ const input = `
194+ @font-face {
195+ font-family: 'A';
196+ }
197+ @font-face {
198+ font-family: 'B';
199+ }
200+ `
201+
202+ expect ( splitCssIntoSubsets ( input ) ) . toEqual ( [ { subset : null , css : input } ] )
203+ } )
149204} )
0 commit comments