Skip to content

Commit

Permalink
test(google): check if filteration provides correct values
Browse files Browse the repository at this point in the history
  • Loading branch information
barelyhuman committed Oct 4, 2024
1 parent ff5345b commit e114a84
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { describe, expect, it } from 'vitest'
import { createUnifont, providers } from '../src'
import { createUnifont, providers, type ResolveFontOptions } from '../src'

// TODO: move to a test/utils folder or something else
// Quick and dirty way to pick into a new array, not needed if the test is un-necessary
function pickUniqueBy<T, K>(arr: T[], by: (arg: T) => K): K[] {
return [...arr.reduce((acc, fnt) => {
const prop = by(fnt)
if (!acc.has(prop))
acc.add(prop)
return acc
}, new Set<K>())]
}

describe('unifont', () => {
it('works with no providers', async () => {
Expand All @@ -17,12 +28,29 @@ describe('unifont', () => {
})

it('works with google provider', async () => {
const unifont = await createUnifont([
providers.google(),
])
const unifont = await createUnifont([providers.google()])

const { fonts } = await unifont.resolveFont('Poppins')

expect(fonts).toHaveLength(6)
})

it('filters the fonts with google provider', async () => {
const unifont = await createUnifont([providers.google()])

const styles = ['normal'] as ResolveFontOptions['styles']
const weights = ['600']
const { fonts } = await unifont.resolveFont('Poppins', {
styles,
weights,
subsets: [],
})

const resolvedStyles = pickUniqueBy(fonts, fnt => fnt.style)
const resolvedWeights = pickUniqueBy(fonts, fnt => String(fnt.weight))

expect(fonts).toHaveLength(3)
expect(resolvedStyles).toMatchObject(styles)
expect(resolvedWeights).toMatchObject(weights)
})
})

0 comments on commit e114a84

Please sign in to comment.