70 lines
2.7 KiB
JavaScript
70 lines
2.7 KiB
JavaScript
|
|
// 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/');
|
||
|
|
});
|
||
|
|
});
|