Skip to content

Commit

Permalink
test: extract test helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Sep 11, 2024
1 parent 815f290 commit 572f3ca
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
14 changes: 2 additions & 12 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { fileURLToPath } from 'node:url'
import { describe, it, expect } from 'vitest'
import { setup, $fetch } from '@nuxt/test-utils'

import { extractFontFaces, extractPreloadLinks } from './utils'

await setup({
rootDir: fileURLToPath(new URL('../playground', import.meta.url)),
})
Expand Down Expand Up @@ -238,15 +240,3 @@ describe('features', () => {
`)
})
})

function extractFontFaces(fontFamily: string, html: string) {
const matches = html.matchAll(new RegExp(`@font-face\\s*{[^}]*font-family:\\s*(?<quote>['"])?${fontFamily}\\k<quote>[^}]+}`, 'g'))
return Array.from(matches, match => match[0]
.replace(/(?<=['"(])(https?:\/\/[^/]+|\/_fonts)\/[^")]+(\.[^".)]+)(?=['")])/g, '$1/file$2')
.replace(/(?<=['"(])(https?:\/\/[^/]+|\/_fonts)\/[^".)]+(?=['")])/g, '$1/file'),
)
}

function extractPreloadLinks(html?: string) {
return (html?.match(/<link[^>]+rel="preload"[^>]+>/g) || []).map(link => link.match(/href="([^"]+)"/)?.[1]?.replace(/\/_fonts\/[^")]+(\.[^".)]+)$/g, '/file$1'))
}
11 changes: 11 additions & 0 deletions test/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function extractFontFaces(fontFamily: string, html: string) {
const matches = html.matchAll(new RegExp(`@font-face\\s*{[^}]*font-family:\\s*(?<quote>['"])?${fontFamily}\\k<quote>[^}]+}`, 'g'))
return Array.from(matches, match => match[0]
.replace(/(?<=['"(])(https?:\/\/[^/]+|\/_fonts)\/[^")]+(\.[^".)]+)(?=['")])/g, '$1/file$2')
.replace(/(?<=['"(])(https?:\/\/[^/]+|\/_fonts)\/[^".)]+(?=['")])/g, '$1/file'),
)
}

export function extractPreloadLinks(html?: string) {
return (html?.match(/<link[^>]+rel="preload"[^>]+>/g) || []).map(link => link.match(/href="([^"]+)"/)?.[1]?.replace(/\/_fonts\/[^")]+(\.[^".)]+)$/g, '/file$1'))
}

0 comments on commit 572f3ca

Please sign in to comment.