|
| 1 | +# Testing |
| 2 | + |
| 3 | +> First version. Captures the testing approach today (Phase 1 of the project) and links out to the working configuration. Will be expanded as the test surface grows. |
| 4 | +
|
| 5 | +## Strategy |
| 6 | + |
| 7 | +MonkeyTab tests focus on **behavior that ships to consumers**: the public API of `@datasketch/monkeytab`, the field type registries, the in-memory adapter, and the rendering of the React components. |
| 8 | + |
| 9 | +We test the source directly (via the `@monkeytab/*` aliases configured in `vitest.config.ts`) so that what's tested matches what's built. We do **not** mock — tests use real adapters, real registries, and real React renders via `@testing-library/react`. |
| 10 | + |
| 11 | +The minimum bar is **15% line coverage**, enforced by `vitest.config.ts` thresholds. CI fails if coverage falls below the threshold. The current target is to grow from this floor as new tests are added. |
| 12 | + |
| 13 | +> **TODO**: raise the coverage target as the test suite matures (suggested next step: 30% after a full pass on `src/core/`). |
| 14 | +
|
| 15 | +## Test layout |
| 16 | + |
| 17 | +Tests live under [`tests/`](./tests/) and are picked up by Vitest's default glob `tests/**/*.test.{ts,tsx}`. There is no enforced sub-structure — flat files, `tests/unit/`, `tests/integration/`, and `tests/components/` are all fine. See [`tests/README.md`](./tests/README.md) for the conventions and example tests. |
| 18 | + |
| 19 | +## Sample test cases |
| 20 | + |
| 21 | +The test suite covers (or is targeted to cover) the following surface areas. This list is the working backlog for raising coverage past the 15% floor. |
| 22 | + |
| 23 | +| Area | What to test | Status | |
| 24 | +|---|---|---| |
| 25 | +| `<MonkeyTable>` render | Mounts via React Testing Library, headers from `columns` appear, mounts cleanly with empty rows / custom render / `editable: false` | ✅ in suite | |
| 26 | +| MemoryAdapter — schema | `listBases`, `getTable`, `info()` capabilities | ✅ in suite | |
| 27 | +| MemoryAdapter — CRUD | Create → query → get → update → delete round-trip | ✅ in suite | |
| 28 | +| MemoryAdapter — pagination | `queryRecords` with `offset` + `limit` slices correctly | ✅ in suite | |
| 29 | +| MemoryAdapter — schema mutation | `createField` / `deleteField` round-trip | ✅ in suite | |
| 30 | +| MemoryAdapter — error paths | `getRecord` throws on missing record | ✅ in suite | |
| 31 | +| Bundle import smoke | `dist/monkeytab.js` exports the documented public API | ✅ in suite | |
| 32 | +| SSR safety | Bundle imports cleanly in a no-DOM Node environment | ✅ in suite | |
| 33 | +| Public type contract | `dist/index.d.ts` compiles a consumer-style file via `tsc --noEmit` | ✅ in suite | |
| 34 | +| `compareValues` (sort) | numeric ordering, string ordering, null handling, mixed types | ⚠️ TODO | |
| 35 | +| `applySort` | single-column ascending/descending, multi-column tiebreakers | ⚠️ TODO | |
| 36 | +| `applyFilter` | each FilterOperator (eq, neq, lt/lte/gt/gte, contains, is_empty, etc.) per supported field type | ⚠️ TODO | |
| 37 | +| Field type registry | direct `lookup()` and override registration assertions | ⚠️ TODO | |
| 38 | +| `<MonkeyTable>` editing | `onChange` fires with new rows on cell edit | ⚠️ TODO | |
| 39 | +| Type-aware sort labels | correct label per field type | ⚠️ TODO | |
| 40 | +| Per-column control | `editable: false`, `sortable: false`, custom `width` exercised end-to-end | ⚠️ TODO | |
| 41 | + |
| 42 | +> **TODO**: each ⚠️ row is a good first issue for new contributors. |
| 43 | +
|
| 44 | +## Data structures under test |
| 45 | + |
| 46 | +The core data shapes are defined in `src/core/types.ts` and re-exported through `src/browser/types.d.ts`. Tests import them through the `@monkeytab/core` alias rather than reaching into source paths directly. |
| 47 | + |
| 48 | +Key shapes: |
| 49 | + |
| 50 | +- **`FieldSpec`** — `{ id, label, type, options? }` — describes a column |
| 51 | +- **`Row`** — `{ id, fields: Record<string, Value>, createdAt, updatedAt }` — a single record |
| 52 | +- **`TableSpec`** — `{ id, label, fields, primaryFieldId?, columnOrder?, rowOrder? }` — schema |
| 53 | +- **`FilterCondition`** / **`FilterGroup`** — query filters |
| 54 | +- **`SortSpec`** — sort directives |
| 55 | +- **`FieldOptions`** — discriminated union of per-type options (NumberFieldOptions, SingleSelectFieldOptions, etc.) |
| 56 | + |
| 57 | +See [`src/browser/types.d.ts`](./src/browser/types.d.ts) for the full surface. |
| 58 | + |
| 59 | +## Pull request workflow |
| 60 | + |
| 61 | +Every PR must: |
| 62 | + |
| 63 | +1. Pass `npm run build:check` (TypeScript strict-mode check on the source) |
| 64 | +2. Pass `npm test` (the full Vitest suite) |
| 65 | +3. Pass `npm run test:coverage` (which enforces the 15% thresholds) |
| 66 | +4. Pass `npm run build` (Vite library build, produces `dist/monkeytab.js`) |
| 67 | + |
| 68 | +CI runs all four steps automatically on push and on PR. The CI status badge is in [`README.md`](./README.md). The coverage report is uploaded as a CI artifact (`coverage-report`) and can be downloaded from the Actions run page. |
| 69 | + |
| 70 | +For the full PR submission flow, see [`CONTRIBUTING.md`](./CONTRIBUTING.md). The pull request template at [`.github/PULL_REQUEST_TEMPLATE.md`](./.github/PULL_REQUEST_TEMPLATE.md) is auto-applied to every new PR. |
| 71 | + |
| 72 | +## Running tests locally |
| 73 | + |
| 74 | +```bash |
| 75 | +npm install # one-time setup |
| 76 | +npm test # run the suite once |
| 77 | +npm run test:watch # interactive watch mode |
| 78 | +npm run test:coverage # run with coverage report (enforces 15% thresholds) |
| 79 | +npm run build:check # TypeScript type-check the source |
| 80 | +npm run build # Vite library build |
| 81 | +``` |
| 82 | + |
| 83 | +The HTML coverage report is generated at `coverage/index.html` after `test:coverage`. Open it in a browser to see line-by-line coverage. |
| 84 | + |
| 85 | +## Reporting test failures |
| 86 | + |
| 87 | +If a test fails on `main`, file a GitHub issue with: |
| 88 | + |
| 89 | +- The failing test name and file |
| 90 | +- The expected vs actual output |
| 91 | +- A minimal reproduction (what command you ran, what environment) |
| 92 | +- Whether the failure is reproducible or intermittent |
| 93 | + |
| 94 | +If a contributor's test catches a real bug, the failing test should be merged with the fix (so it becomes a regression check for the future). |
| 95 | + |
| 96 | +--- |
| 97 | + |
| 98 | +*Testing v0.1 · last updated 2026-04-08* |
0 commit comments