From c3fa4cf8daf683c13d2b72eb41618f4195b2bd62 Mon Sep 17 00:00:00 2001 From: JohnDoe6345789 Date: Thu, 25 Dec 2025 16:22:44 +0000 Subject: [PATCH] Add seed data component initialization tests --- .../nextjs/src/seed-data/components.test.ts | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 frontends/nextjs/src/seed-data/components.test.ts diff --git a/frontends/nextjs/src/seed-data/components.test.ts b/frontends/nextjs/src/seed-data/components.test.ts new file mode 100644 index 000000000..633d1e953 --- /dev/null +++ b/frontends/nextjs/src/seed-data/components.test.ts @@ -0,0 +1,75 @@ +import { describe, it, expect, beforeEach, vi } from 'vitest' + +const { getComponentConfigs, addComponentConfig } = vi.hoisted(() => ({ + getComponentConfigs: vi.fn(), + addComponentConfig: vi.fn(), +})) + +vi.mock('@/lib/database', () => ({ + Database: { + getComponentConfigs, + addComponentConfig, + }, +})) + +import { initializeComponents } from '@/seed-data/components' + +const expectedConfigs = [ + { + id: 'config_home_heading', + componentId: 'node_home_heading', + props: { + level: 1, + children: 'Welcome to MetaBuilder', + }, + }, + { + id: 'config_home_subtitle', + componentId: 'node_home_subtitle', + props: { + children: 'Build multi-tenant applications without writing code', + }, + }, + { + id: 'config_home_button', + componentId: 'node_home_button', + props: { + children: 'Get Started', + variant: 'default', + size: 'lg', + }, + }, +] + +describe('initializeComponents', () => { + beforeEach(() => { + getComponentConfigs.mockReset() + addComponentConfig.mockReset() + }) + + it.each([ + { + name: 'skip seeding when configs exist', + existingConfigs: { existing: { id: 'existing' } }, + expectedAddCount: 0, + }, + { + name: 'seed defaults when no configs exist', + existingConfigs: {}, + expectedAddCount: expectedConfigs.length, + }, + ])('should $name', async ({ existingConfigs, expectedAddCount }) => { + getComponentConfigs.mockResolvedValue(existingConfigs) + + await initializeComponents() + + expect(getComponentConfigs).toHaveBeenCalledTimes(1) + expect(addComponentConfig).toHaveBeenCalledTimes(expectedAddCount) + + if (expectedAddCount > 0) { + expectedConfigs.forEach(config => { + expect(addComponentConfig).toHaveBeenCalledWith(expect.objectContaining(config)) + }) + } + }) +})