Skip to content

Beautiful Open-Source Form Builder

Fix cached Google font previews in form Design (#1301)

Julien Nahum41c1816fb0c0
diff --git a/client/components/open/editors/GoogleFontPicker.vue b/client/components/open/editors/GoogleFontPicker.vue
index 0b22f548b..10ba3f8a0 100644
--- a/client/components/open/editors/GoogleFontPicker.vue
+++ b/client/components/open/editors/GoogleFontPicker.vue
@@ -157,11 +157,11 @@ watch(() => props.show, (show) => {
}
})
-watch(fonts, (newFonts) => {
- if (newFonts && newFonts.length > 0) {
+watch([fonts, () => props.show], ([newFonts, show]) => {
+ if (show && newFonts.length > 0) {
initializeVisibilityTracking()
}
-})
+}, { immediate: true })
const enrichedFonts = computed(() => {
return fuseResults.value && fuseResults.value.length > 0
diff --git a/client/test/e2e/opnform-flows.spec.ts b/client/test/e2e/opnform-flows.spec.ts
index 6cda780df..44e56d71d 100644
--- a/client/test/e2e/opnform-flows.spec.ts
+++ b/client/test/e2e/opnform-flows.spec.ts
@@ -558,6 +558,41 @@ test("editing an existing form title persists after save", async ({ page, reques
await expect(page.getByRole("heading", { name: updatedTitle, exact: true })).toBeVisible()
})
+test("font picker opens from Design and applies a font", async ({ page, request }) => {
+ const form = await apiCreateForm(request, { title: uniqueTitle("Font Picker") })
+ await page.route("**/fonts", route => route.fulfill({ json: ["Roboto", "Open Sans"] }))
+
+ const token = await apiLogin(request)
+ const baseURL = test.info().project.use.baseURL
+ if (!baseURL) throw new Error("Playwright baseURL is required")
+ await page.context().addCookies([{ name: "opnform_token", value: token, url: baseURL }])
+ await page.goto(`/forms/${form.slug}/edit`)
+ await expect(page.locator("#form-editor")).toBeVisible()
+
+ // Exercise the enabled UI without requiring a Google Fonts API key in CI.
+ await page.evaluate(() => {
+ const payload = (window as typeof window & {
+ __NUXT__: { state: Record<string, { services: { google: { fonts: boolean } } }> }
+ }).__NUXT__
+ payload.state["$sfeatureFlags"].services.google.fonts = true
+ })
+
+ const pageErrors: string[] = []
+ page.on("pageerror", error => pageErrors.push(error.message))
+
+ await page.getByText("Design", { exact: true }).click()
+ const fontFamily = page.getByText("Font Family", { exact: true }).locator("..")
+ await fontFamily.getByRole("button").click()
+ await expect(page.getByText("The quick brown fox jumped over the lazy dog").first()).toBeVisible()
+ await page.getByText("Roboto", { exact: true }).last().click()
+ await page.getByRole("button", { name: "Apply", exact: true }).click()
+
+ await expect(fontFamily.getByRole("button")).toContainText("Roboto")
+ await fontFamily.getByRole("button").click()
+ await expect(page.getByText("The quick brown fox jumped over the lazy dog").first()).toBeVisible()
+ expect(pageErrors).toEqual([])
+})
+
test("form save errors open the invalid field section and computed variable", async ({ page, request }) => {
const form = await apiCreateForm(request, {
title: uniqueTitle("Save Error Form"),
diff --git a/client/test/nuxt/google-font-picker.spec.ts b/client/test/nuxt/google-font-picker.spec.ts
new file mode 100644
index 000000000..8b41bdca1
--- /dev/null
+++ b/client/test/nuxt/google-font-picker.spec.ts
@@ -0,0 +1,86 @@
+import { describe, expect, it, vi } from 'vitest'
+import { flushPromises, mount } from '@vue/test-utils'
+import { useElementVisibility } from '@vueuse/core'
+import { nextTick } from 'vue'
+import type { Ref } from 'vue'
+import GoogleFontPicker from '~/components/open/editors/GoogleFontPicker.vue'
+
+const mockQuery = vi.hoisted(() => ({ data: null as Ref<string[]> | null }))
+
+vi.mock('~/composables/query/useContent', async () => {
+ const { ref } = await import('vue')
+
+ return {
+ useContent: () => {
+ const data = ref([])
+ mockQuery.data = data
+ return { fonts: { list: () => ({ data, isLoading: ref(false) }) } }
+ },
+ }
+})
+
+vi.mock('@vueuse/core', async (importOriginal) => {
+ const original = await importOriginal()
+ const { ref } = await import('vue')
+
+ return {
+ ...original,
+ useElementVisibility: vi.fn(() => ref(true)),
+ }
+})
+
+vi.mock('~/components/global/OverlayScrollbarsComponent.client.vue', async () => {
+ const { h, ref } = await import('vue')
+ return {
+ default: {
+ setup(_: unknown, { expose, slots }: { expose: (value: object) => void; slots: { default?: () => unknown } }) {
+ const root = ref<HTMLElement | null>(null)
+ expose({ getElement: () => root.value })
+ return () => h('div', { ref: root, class: 'font-scroll-container' }, slots.default?.())
+ },
+ },
+ }
+})
+
+function mountPicker(show = true) {
+ return mount(GoogleFontPicker, {
+ props: { show },
+ global: {
+ stubs: {
+ UModal: {
+ props: ['open'],
+ template: '<div v-if="open"><slot name="body" /><slot name="footer" /></div>',
+ },
+ TextInput: { template: '<input />' },
+ USkeleton: { template: '<div />' },
+ UButton: { template: '<button><slot /></button>' },
+ Icon: true,
+ },
+ },
+ })
+}
+
+describe('GoogleFontPicker', () => {
+ it('shows fonts when their response arrives', async () => {
+ const wrapper = mountPicker()
+ expect(wrapper.find('.font-scroll-container').exists()).toBe(true)
+ mockQuery.data!.value = ['Roboto']
+ await nextTick()
+ await flushPromises()
+
+ expect(vi.mocked(useElementVisibility).mock.calls.at(-1)?.[1]?.root).toBeInstanceOf(HTMLElement)
+ expect(wrapper.text()).toContain('The quick brown fox jumped over the lazy dog')
+ wrapper.unmount()
+ })
+
+ it('shows fonts already cached before the picker opens', async () => {
+ const wrapper = mountPicker(false)
+ mockQuery.data!.value = ['Roboto']
+ await nextTick()
+ await wrapper.setProps({ show: true })
+ await flushPromises()
+
+ expect(wrapper.text()).toContain('The quick brown fox jumped over the lazy dog')
+ wrapper.unmount()
+ })
+})