shelving/testmodule

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.

Fixtures

Basics

BASIC_SCHEMA is a DataSchema covering every common field type — string, number, choice, array, boolean, and a nested object. basic1basic9 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.

ts
import { BASIC_SCHEMA, basics, basic1 } from "shelving/test";

BASIC_SCHEMA.validate(basic1); // Passes.
basics.length;                 // 9, in unsorted order.

People

PERSON_SCHEMA is a smaller, more lifelike shape — a nested name object and a nullable birthday. person1person5 and the people array supply the data.

ts
import { PERSON_SCHEMA, people } from "shelving/test";

Collections

BASICS_COLLECTION and PEOPLE_COLLECTION wrap the two schemas as Collection definitions, ready to hand straight to a provider.

ts
import { MemoryDBProvider } from "shelving/db";
import { BASICS_COLLECTION, basic1 } from "shelving/test";

const provider = new MemoryDBProvider();
await provider.addItem(BASICS_COLLECTION, basic1);

Assertion helpers

HelperUse
expectOrderedItems()Assert an iterable of items has exactly these ids, in this order.
expectUnorderedItems()The same, but order does not matter.
EXPECT_PROMISELIKEA 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.

ts
import { expectOrderedItems } from "shelving/test";

const results = await provider.getQuery(BASICS_COLLECTION, { $order: "num" });
expectOrderedItems(results, ["basic1", "basic2", "basic3"]);

Functions

Go

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
Go

expectOrderedItems()function

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

Types

Go

BasicDatatype

Validated data shape of a test basic, inferred from BASIC_SCHEMA.

ValidatorType<typeof BASIC_SCHEMA>
Go

BasicItemtype

A test basic as a stored Item — BasicData plus a string id.

Item<string, BasicData>
Go

PersonDatatype

Validated data shape of a test person, inferred from PERSON_SCHEMA.

ValidatorType<typeof PERSON_SCHEMA>
Go

PersonItemtype

A test person as a stored Item — PersonData plus a string id.

Item<string, PersonData>

Constants

Go

BASIC_SCHEMAconstant

Schema for a test "basic" fixture, exercising string, number, choice, array, boolean, and nested-data props.

Go

basic1constant

Test basic fixture: str: "aaa", num: 100, group a, odd.

basic1: BasicItem
Go

basic2constant

Test basic fixture: str: "bbb", num: 200, group a, even.

basic2: BasicItem
Go

basic3constant

Test basic fixture: str: "ccc", num: 300, group a, odd.

basic3: BasicItem
Go

basic4constant

Test basic fixture: str: "ddd", num: 400, group b, even.

basic4: BasicItem
Go

basic5constant

Test basic fixture: str: "eee", num: 500, group b, odd.

basic5: BasicItem
Go

basic6constant

Test basic fixture: str: "fff", num: 600, group b, even.

basic6: BasicItem
Go

basic7constant

Test basic fixture: str: "ggg", num: 700, group c, odd.

basic7: BasicItem
Go

basic8constant

Test basic fixture: str: "hhh", num: 800, group c, even.

basic8: BasicItem
Go

basic9constant

Test basic fixture: str: "iii", num: 900, group c, odd.

basic9: BasicItem
Go

basicsconstant

Array of all nine test basic fixtures in a deliberately shuffled order, for exercising sort/query behaviour.

basics: ReadonlyArray<BasicItem>
Go

basic999constant

Standalone test basic data (no id): str: "zzz", num: 999, for use as new/unsaved data.

basic999: BasicData
Go

PERSON_SCHEMAconstant

Schema for a test "person" fixture, with a nested name and a nullable birthday.

Go

person1constant

Test person fixture: Dave Brook, born 1985-12-06.

person1: PersonItem
Go

person2constant

Test person fixture: Sally Callister, born 1973-11-19.

person2: PersonItem
Go

person3constant

Test person fixture: Sammy Canister, with no birthday (null).

person3: PersonItem
Go

person4constant

Test person fixture: Jilly Jones, with no birthday (null).

person4: PersonItem
Go

person5constant

Test person fixture: Terry Times, born 1964-08-01.

person5: PersonItem
Go

peopleconstant

Ordered array of all five test person fixtures (person1 through person5).

people: ReadonlyArray<PersonItem>
Go

EXPECT_PROMISELIKEconstant

Asymmetric matcher that expects an object matching PromiseLike (i.e. has a .then() method).

Go

BASICS_COLLECTIONconstant

Go

PEOPLE_COLLECTIONconstant