test: Firebase mock harness + createCampaign characterization

- src/__mocks__/firebase/*: jest manual mocks (app/auth/firestore)
- src/__mocks__/firebase/_mock-db.js: in-memory DB + call recorder
- src/setupTests.js: jest-dom, env stubs, crypto polyfill, DB reset
- src/App.characterization.test.js: createCampaign -> setDoc path/payload locked
- src/storage/contract.js (renamed from .test.js, helper not suite)

21 tests green (memory 19 + createCampaign 2).
This commit is contained in:
david raistrick
2026-06-28 17:59:50 -04:00
parent 12b24eb707
commit 84dd17e174
8 changed files with 273 additions and 1 deletions
+69
View File
@@ -0,0 +1,69 @@
// App.characterization.test.js
// Characterize App -> Firebase calls. Lock path + payload shape per action.
// Mock SDK, render AdminView, fire action, assert recorded calls.
// Purpose: refactor (path-shape rewrite) must not change these calls.
import React from 'react';
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import '@testing-library/jest-dom';
import { getCalls, MOCK_DB } from './__mocks__/firebase/_mock-db';
// import AFTER mocks resolve (jest auto-uses __mocks__/firebase/* via moduleNameMapper)
import App from './App';
// Helper: find first setDoc call matching path substring.
function findCall(fn, pathSub) {
return getCalls().find(c => c.fn === fn && (pathSub ? c.path.includes(pathSub) : true));
}
beforeEach(() => {
// App reads window.location at mount; ensure clean.
window.history.replaceState({}, '', '/');
window.open = jest.fn();
global.alert = jest.fn();
});
describe('App -> Firebase characterization: createCampaign', () => {
test('setDoc called with campaign path + correct payload shape', async () => {
render(<App />);
// Wait past auth (mock fires instantly) and campaign list load.
await waitFor(() => screen.getByText(/Create Campaign/i));
fireEvent.click(screen.getByRole('button', { name: /Create Campaign/i }));
// Modal: name input + create.
await waitFor(() => screen.getByLabelText(/Campaign Name/i));
fireEvent.change(screen.getByLabelText(/Campaign Name/i), { target: { value: 'Test Campaign' } });
fireEvent.click(screen.getByRole('button', { name: /^Create$/i }));
await waitFor(() => {
const call = findCall('setDoc', '/campaigns/');
expect(call).toBeDefined();
});
const call = findCall('setDoc', '/campaigns/');
expect(call.path).toMatch(/campaigns\/.+$/);
expect(call.data).toMatchObject({
name: 'Test Campaign',
playerDisplayBackgroundUrl: '',
players: [],
});
expect(call.data).toHaveProperty('ownerId');
expect(call.data).toHaveProperty('createdAt');
});
test('campaign path includes APP_ID namespace', async () => {
render(<App />);
await waitFor(() => screen.getByText(/Create Campaign/i));
fireEvent.click(screen.getByRole('button', { name: /Create Campaign/i }));
await waitFor(() => screen.getByLabelText(/Campaign Name/i));
fireEvent.change(screen.getByLabelText(/Campaign Name/i), { target: { value: 'NS Test' } });
fireEvent.click(screen.getByRole('button', { name: /^Create$/i }));
await waitFor(() => findCall('setDoc', '/campaigns/'));
const call = findCall('setDoc', '/campaigns/');
expect(call.path).toContain('artifacts/');
expect(call.path).toContain('/public/data/');
});
});