Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(google): check if filteration provides correct values #31

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})