import Image from ‘next/image’;
order-by-filter.test.js Test Suite Documentation
Overview
This Jest test suite validates the API’s order-by query string functionality across entities. It ensures server-side ordering works correctly by targeting slug fields, translatable titles, and multi-field combinations by creating test data and asserting precise sort sequences.
Test scope
- Authenticates as admin to obtain JWT token.
- Creates two resources per entity using metadata bodies.
- Verifies ascending/descending ordering on
slug,translations.title(if translatable), and combinedtranslations.title+slug. - Confirms created resources exist before ordering tests and cleans up after.
Test source
This suite uses the API metadata to generate valid body for POST / PUT requests. The list of endpoints to test is generated from metadata-cache.json (entities with order[slug] support).
Snapshots are not used; ordering is validated via local array sorting.
Key Functionalities Tested
1. Authentication Setup
- Runs before all tests per entity to log in with credentials from environment variables.
- Stores the received JWT token for authorization headers.
2. Test Data Creation
- Creates two new resources.
- Asserts HTTP 201 status and captures resource IDs for verification.
3. Validate Created Resources
- Makes GET requests to each new resource by ID.
- Asserts HTTP 200 status to ensure resources are accessible.
4. Order by Slug Ascending
- Sends GET with
?order[slug]=asc. - Asserts HTTP 200 and verifies
hydra:memberslugs match locally sorted ascending array.
5. Order by Slug Descending
- Sends GET with
?order[slug]=desc. - Asserts HTTP 200 and verifies
hydra:memberslugs match locally sorted descending array (viatoSorted().toReversed()).
6. Order by Translatable Title (If Supported)
- For entities with
apiTranslations:- Tests
?order[translations.title]=ascand?order[translations.title]=desc. - Extracts non-empty
parameterTitlevalues and asserts they match locally sorted arrays.
- Tests
7. Combined Ordering
- Sends GET with
?order[translations.title]=asc&order[slug]=asc. - Builds simplified response with
{id, slug, parameterTitle}. - Locally sorts by title then slug, asserts ID sequence matches.
8. Cleanup
- Runs after all tests to DELETE both created resources.
- Asserts HTTP 200 status for deletions.
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.
- Uses
array.prototype.tosortedfor precise local ordering validation. - Ensures server respects multi-field order precedence (title primary, slug secondary).
- Includes a simple arithmetic test (
adds 1 + 1 to equal 2) as a baseline sanity check.
Last updated on