expectUnorderedItems()function
Assert that a set of Item objects has exactly the expected .id values, in any order.
expectUnorderedItems(items: Iterable<Item<I, T>>, keys: Iterable<string> & NotString): void
Shared test fixtures and assertion helpers for Shelving's own unit tests.
This module exists so the test suite has one canonical set of sample data and matchers, instead of redefining ad-hoc name / age objects in every file. It is internal — not part of the public shelving package — but shelving/test is importable from test files within the library.
Reach for these fixtures whenever a test needs a schema, a collection, or a handful of realistic items — especially collection, provider, and query tests.
BASIC_SCHEMA is a DataSchema covering every common field type — string, number, choice, array, boolean, and a nested object. basic1 … basic9 are ready-made items. basics is those nine in a deliberately unsorted order, so a test that sorts or queries them actually proves something. basic999 is a tenth item kept out of the array — handy as an "add this" payload.
import { BASIC_SCHEMA, basics, basic1 } from "shelving/test";
BASIC_SCHEMA.validate(basic1); // Passes.
basics.length; // 9, in unsorted order.PERSON_SCHEMA is a smaller, more lifelike shape — a nested name object and a nullable birthday. person1 … person5 and the people array supply the data.
import { PERSON_SCHEMA, people } from "shelving/test";BASICS_COLLECTION and PEOPLE_COLLECTION wrap the two schemas as Collection definitions, ready to hand straight to a provider.
import { MemoryDBProvider } from "shelving/db";
import { BASICS_COLLECTION, basic1 } from "shelving/test";
const provider = new MemoryDBProvider();
await provider.addItem(BASICS_COLLECTION, basic1);| Helper | Use |
|---|---|
expectOrderedItems() | Assert an iterable of items has exactly these ids, in this order. |
expectUnorderedItems() | The same, but order does not matter. |
EXPECT_PROMISELIKE | A matcher for "this value looks like a promise". |
expectOrderedItems() and expectUnorderedItems() compare by id, so a failure reports the ids that were wrong — far more readable than a deep object diff. Both trim their own stack frame, so the reported failure points at your test.
import { expectOrderedItems } from "shelving/test";
const results = await provider.getQuery(BASICS_COLLECTION, { $order: "num" });
expectOrderedItems(results, ["basic1", "basic2", "basic3"]);Assert that a set of Item objects has exactly the expected .id values, in any order.
expectUnorderedItems(items: Iterable<Item<I, T>>, keys: Iterable<string> & NotString): void
Assert that a set of Item objects has exactly the expected .id values, in the exact given order.
expectOrderedItems(items: Iterable<Item<I, T>>, keys: Iterable<string> & NotString): void
Validated data shape of a test basic, inferred from BASIC_SCHEMA.
ValidatorType<typeof BASIC_SCHEMA>
A test basic as a stored Item — BasicData plus a string id.
Item<string, BasicData>
Validated data shape of a test person, inferred from PERSON_SCHEMA.
ValidatorType<typeof PERSON_SCHEMA>
A test person as a stored Item — PersonData plus a string id.
Item<string, PersonData>
Schema for a test "basic" fixture, exercising string, number, choice, array, boolean, and nested-data props.
Test basic fixture: str: "aaa", num: 100, group a, odd.
basic1: BasicItem
Test basic fixture: str: "bbb", num: 200, group a, even.
basic2: BasicItem
Test basic fixture: str: "ccc", num: 300, group a, odd.
basic3: BasicItem
Test basic fixture: str: "ddd", num: 400, group b, even.
basic4: BasicItem
Test basic fixture: str: "eee", num: 500, group b, odd.
basic5: BasicItem
Test basic fixture: str: "fff", num: 600, group b, even.
basic6: BasicItem
Test basic fixture: str: "ggg", num: 700, group c, odd.
basic7: BasicItem
Test basic fixture: str: "hhh", num: 800, group c, even.
basic8: BasicItem
Test basic fixture: str: "iii", num: 900, group c, odd.
basic9: BasicItem
Array of all nine test basic fixtures in a deliberately shuffled order, for exercising sort/query behaviour.
basics: ReadonlyArray<BasicItem>
Standalone test basic data (no id): str: "zzz", num: 999, for use as new/unsaved data.
basic999: BasicData
Schema for a test "person" fixture, with a nested name and a nullable birthday.
Test person fixture: Dave Brook, born 1985-12-06.
person1: PersonItem
Test person fixture: Sally Callister, born 1973-11-19.
person2: PersonItem
Test person fixture: Sammy Canister, with no birthday (null).
person3: PersonItem
Test person fixture: Jilly Jones, with no birthday (null).
person4: PersonItem
Test person fixture: Terry Times, born 1964-08-01.
person5: PersonItem
Ordered array of all five test person fixtures (person1 through person5).
people: ReadonlyArray<PersonItem>
Asymmetric matcher that expects an object matching PromiseLike (i.e. has a .then() method).