import Image from ‘next/image’;
full-crud.as-admin.test.js Test Suite Documentation
Overview
This Jest test suite performs full integration testing of the CRUD (Create, Read, Update, Delete) operations for each entity defined in the metadata-cache.json. It ensures that the API behaves correctly across all basic operations for each resource, including validation of responses and state changes.
Test Scope
- Authenticates as an admin user to acquire a JWT token for authorized API requests.
- Covers all entities defined in the metadata cache.
- Verifies the listing, dropdown data, creation, retrieval, updating, and deletion of resources.
- Checks for proper handling of invalid input.
- Uses snapshots to verify response payloads after creation and update operations.
- Sanitizes sensitive or variable data before snapshot comparison using a custom
_sanitizefunction.
Test source
This suite uses the API metadata to generate valid body for POST / PUT requests. The list of endpoints to test is hardcoded (expected since we’re testing Create;Read;Update;Delete), but repeted for every entity. Snapshots are based on prior run.
Key Functionalities Tested
1. Authentication Setup
- Runs before all tests to log in with credentials from environment variables.
- Stores the received JWT token for authorization headers.
2. Listing Entities
- Makes a GET request to retrieve a paginated list.
- Asserts HTTP 200 status and ensures some results count is returned.
3. Dropdown Data Retrieval
- Makes a GET request to obtain entity dropdown options.
- Validates the response and number of items returned.
4. POST with Invalid Body
- Sends a POST request with an invalid payload.
- Expects a
400 Bad Requestresponse to verify input validation.
5. Create New Resource
- Sends a valid POST request to add a new resource using the sample body from metadata.
- Confirms creation with HTTP 201 status and captures new resource ID.
6. Validate New Resource Presence
- Checks that the new resource appears in both the listing and dropdown endpoints.
7. Retrieve New Resource by ID
- GET request for the new resource using the captured ID.
- Ensures the resource is returned with HTTP 200 and matches sanitized snapshot.
8. Update Resource
- Sends a PUT request with modified data to update the resource.
- Confirms successful update with HTTP 200.
9. Verify Updated Resource
- Retrieves the updated resource and verifies it against a sanitized snapshot.
10. Delete Resource
- Sends a DELETE request to remove the created resource.
- Accepts any successful status code from
200to299.
11. Validate Resource Removal
- Confirms that the deleted resource no longer appears in listings or dropdowns.
- Attempts to retrieve the deleted resource and expects a
404 Not Found.
Sanitization
The suite uses a _sanitize utility to replace sensitive fields with [SANITIZED] strings before snapshot comparisons. This protects sensitive data and ensures snapshots remain consistent across test runs even with dynamic or private data.
Environmental Setup
- Uses environment variables:
PHPR_BASE_URL- Base URL for API requests.PHPR_USERNAMEandPHPR_PASSWORD- Credentials for login.
- Loads test configuration from
metadata-cache.json.
Notes
- Tests run sequentially per entity to maintain state consistency.
- Listing counts are tracked to confirm changes after create/delete operations.
- Snapshot testing helps verify overall response structure alongside data correctness.
- Includes a simple arithmetic test (
adds 1 + 1 to equal 2) as a baseline sanity check.