Initial commit
This commit is contained in:
3
packages/test-utils/eslint.config.js
Normal file
3
packages/test-utils/eslint.config.js
Normal file
@@ -0,0 +1,3 @@
|
||||
import { react } from '@dwellops/config/eslint';
|
||||
|
||||
export default react;
|
||||
33
packages/test-utils/package.json
Normal file
33
packages/test-utils/package.json
Normal file
@@ -0,0 +1,33 @@
|
||||
{
|
||||
"name": "@dwellops/test-utils",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./src/index.ts",
|
||||
"default": "./src/index.ts"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"lint": "eslint src",
|
||||
"typecheck": "tsc --noEmit"
|
||||
},
|
||||
"dependencies": {
|
||||
"@dwellops/types": "workspace:*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "catalog:",
|
||||
"eslint": "catalog:",
|
||||
"@types/node": "catalog:",
|
||||
"@dwellops/config": "workspace:*",
|
||||
"vitest": "catalog:",
|
||||
"@testing-library/react": "catalog:",
|
||||
"@testing-library/user-event": "catalog:",
|
||||
"@testing-library/jest-dom": "catalog:",
|
||||
"react": "catalog:",
|
||||
"react-dom": "catalog:",
|
||||
"@types/react": "catalog:",
|
||||
"@types/react-dom": "catalog:"
|
||||
}
|
||||
}
|
||||
96
packages/test-utils/src/factories.ts
Normal file
96
packages/test-utils/src/factories.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
import type {
|
||||
AuthUser,
|
||||
Hoa,
|
||||
HoaId,
|
||||
Membership,
|
||||
MembershipId,
|
||||
Unit,
|
||||
UnitId,
|
||||
UserId,
|
||||
} from '@dwellops/types';
|
||||
|
||||
let _idCounter = 1;
|
||||
|
||||
/**
|
||||
* Generates a deterministic test ID string.
|
||||
* Resets on module re-import; do not rely on specific values across test files.
|
||||
*/
|
||||
function nextId(prefix: string): string {
|
||||
return `${prefix}_test_${String(_idCounter++).padStart(4, '0')}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the ID counter. Call in beforeEach if deterministic IDs matter.
|
||||
*/
|
||||
export function resetIdCounter(): void {
|
||||
_idCounter = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a mock AuthUser. All fields are overridable.
|
||||
*
|
||||
* @param overrides - Partial AuthUser fields to override.
|
||||
*/
|
||||
export function makeUser(overrides: Partial<AuthUser> = {}): AuthUser {
|
||||
return {
|
||||
id: nextId('user') as UserId,
|
||||
email: 'test@example.com',
|
||||
name: 'Test User',
|
||||
emailVerified: true,
|
||||
image: null,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a mock Hoa record.
|
||||
*
|
||||
* @param overrides - Partial Hoa fields to override.
|
||||
*/
|
||||
export function makeHoa(overrides: Partial<Hoa> = {}): Hoa {
|
||||
const id = nextId('hoa') as HoaId;
|
||||
return {
|
||||
id,
|
||||
name: 'Test HOA',
|
||||
slug: `test-hoa-${id}`,
|
||||
description: null,
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a mock Unit record.
|
||||
*
|
||||
* @param overrides - Partial Unit fields to override.
|
||||
*/
|
||||
export function makeUnit(overrides: Partial<Unit> = {}): Unit {
|
||||
return {
|
||||
id: nextId('unit') as UnitId,
|
||||
hoaId: nextId('hoa') as HoaId,
|
||||
identifier: '101',
|
||||
address: null,
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a mock Membership record.
|
||||
*
|
||||
* @param overrides - Partial Membership fields to override.
|
||||
*/
|
||||
export function makeMembership(overrides: Partial<Membership> = {}): Membership {
|
||||
return {
|
||||
id: nextId('membership') as MembershipId,
|
||||
userId: nextId('user') as UserId,
|
||||
hoaId: nextId('hoa') as HoaId,
|
||||
unitId: null,
|
||||
role: 'OWNER',
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
2
packages/test-utils/src/index.ts
Normal file
2
packages/test-utils/src/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './factories';
|
||||
export * from './render';
|
||||
25
packages/test-utils/src/render.tsx
Normal file
25
packages/test-utils/src/render.tsx
Normal file
@@ -0,0 +1,25 @@
|
||||
import type { ReactElement } from 'react';
|
||||
import { render as tlRender } from '@testing-library/react';
|
||||
import type { RenderOptions, RenderResult } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
|
||||
export type { RenderResult };
|
||||
|
||||
/**
|
||||
* Wrapped render helper that sets up userEvent alongside the render.
|
||||
* Prefer this over @testing-library/react render directly.
|
||||
*
|
||||
* @param ui - The React element to render.
|
||||
* @param options - Optional Testing Library render options.
|
||||
* @returns Render result plus a `user` userEvent instance.
|
||||
*/
|
||||
export function render(
|
||||
ui: ReactElement,
|
||||
options?: RenderOptions,
|
||||
): RenderResult & { user: ReturnType<typeof userEvent.setup> } {
|
||||
const user = userEvent.setup();
|
||||
const result = tlRender(ui, options);
|
||||
return { ...result, user };
|
||||
}
|
||||
|
||||
export { screen, within, fireEvent, waitFor, act } from '@testing-library/react';
|
||||
10
packages/test-utils/tsconfig.json
Normal file
10
packages/test-utils/tsconfig.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"extends": "@dwellops/config/tsconfig/react-library.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./src",
|
||||
"noEmit": true,
|
||||
"lib": ["ES2022", "DOM"]
|
||||
},
|
||||
"include": ["src"]
|
||||
}
|
||||
Reference in New Issue
Block a user