From e114a84809664041d709f022924d24913ae5481e Mon Sep 17 00:00:00 2001 From: reaper Date: Fri, 4 Oct 2024 19:13:56 +0530 Subject: [PATCH] test(google): check if filteration provides correct values --- test/index.test.ts | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/test/index.test.ts b/test/index.test.ts index 314f14c..4f6ac10 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -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(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())] +} describe('unifont', () => { it('works with no providers', async () => { @@ -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) + }) })