import Image from ‘next/image’;
query-string.test.js Test Suite Documentation
Overview
This Jest test suite validates the API’s handling of query string parameters across all entities defined in metadata-cache.json. It tests pagination, locale, ordering, searching (via MySearchFilter), and exact property matching to ensure query string functionality works correctly.
Test Scope
- Authenticates once before running tests to obtain a JWT token.
- Iterates over each entity and verifies the API’s response to various query string parameters.
- Performs detailed filtering tests on every property within each entity’s sample data.
- Includes positive and negative pagination tests and checks for appropriate error responses on invalid input.
- Uses snapshots extensively to validate response consistency and structure.
Test source
The list of endpoints to test is:
- Hardcoded for basic queryString expected on every entity
- Generated from Swagger for others Snapshots are based on prior run.
Key Functionalities Tested
1. Authentication Setup
- Authenticates before running entity-specific tests using environment credentials.
- Reuses the token for all subsequent requests.
2. Pagination Tests
- Tests different
itemsPerPagevalues:0,1,100, and-1.- Checks for expected response status codes (200 for valid values, 400 for invalid).
- Verifies that the number of items returned respects the pagination parameter.
- Tests
pageparameter values:0,1, and2.- Validates correct pagination behavior and error handling for invalid pages.
3. Locale Tests
- Tests various
_localequery string values such asfr,en, an invalid locale string, and empty locale. - Ensures the API responds with HTTP 200 and matches snapshots regardless of locale input.
4. Ordering Tests
- Tests ordering of results by
idin ascending and descending order. - Validates response structure and content via snapshots.
5. Search Tests (MySearchFilter)
The MySearchFilter provides free-flow search capabilities that compare search terms across multiple fields using OR clauses:
- Single-word search: Tests searching by ID (
search=1) and slug (search=slug-value) - Multi-word search: Tokenizes search terms (
?search=foo bar→["foo", "bar"]) and searches all matching fields with OR logic - Case-insensitive search: Wraps comparisons in LOWER() so
FOOBAR,foobar, andFoo Barall match - Shortcode ID support: If the entity supports
getShortcode(), searches like?search=A123match ID123 - Empty value handling: Empty search terms are discarded by the filter
- Snapshot validation: All responses are captured with snapshots for regression testing
This filter replaces the old ApplyFilter method and supports nested relations and translations.
6. Exact Query String Match on Properties
- For each filterable property (excluding technical fields like id, timestamps, and customProperties), the suite verifies that an exact query string match returns only the expected entity.
- Handles properties that are null by temporarily updating the entity via PUT before running the positive and negative filter checks.
7. Ordering Tests on All Properties
- For every property declared as orderable in the entity’s swaggerQS metadata, the suite verifies both ascending and descending ordering behavior.
- Confirms that at least two entities are returned and that non-null, non-object property values are correctly sorted in the specified direction.
8. Nullable Property Existence Tests
- For each nullable property listed in the entity’s metadata, the suite creates a dedicated entity with that property explicitly set to null and validates the exist[…] query string behavior.
- Verifies that exist[
]=true excludes entities with null for that property and exist[ ]=false includes them, ensuring correct handling of nullability-based filters.
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
- Extensive use of snapshots to detect changes in API response formatting and data content.
- Defensive handling for properties that are null or objects.
- The test suite ensures the API correctly supports complex filtering, sorting, pagination, and localization requirements.
- Includes a simple arithmetic test (
adds 1 + 1 to equal 2) as a baseline sanity check.
Last updated on