If I'm writing unit tests for code using the npm package @inrupt/solid-client
that is trying to read from a Container, how do I mock that Container?
Technically, a Container is a regular Resource with a Thing for every Resource it contains, and one Thing identified by the Container's own URL that points to them.
Thus, the following code should do the trick:
// We're going to mock a Container at https://vincentt.inrupt.net/public/,
// that contains one Resource at https://vincentt.inrupt.net/public/child-one.ttl.
const containerUrl = "https://vincentt.inrupt.net/public/";
// Add a Thing for the child at
// https://vincentt.inrupt.net/public/child-one.ttl:
let childOneListing = createThing({ url: containerUrl + "child-one.ttl" });
childOneListing = addUrl(childOneListing, rdf.type, ldp.Resource);
childOneListing = addDatetime(childOneListing, dct.modified, new Date());
// Create a Thing identified by the Container's own URL,
// pointing to the Things representing its children:
let childrenIndex = createThing({ url: containerUrl });
childrenIndex = addUrl(childrenIndex, rdf.type, ldp.BasicContainer);
childrenIndex = addUrl(childrenIndex, rdf.type, ldp.Container);
// Add child-one.ttl to this index:
childrenIndex = addUrl(childrenIndex, ldp.contains, childOneListing);
// Now initialise a mock Resource for this Container for use in our unit test,
// and add the index and the child Resources' Things:
let mockContainer = mockSolidDatasetFrom(containerUrl);
mockContainer = setThing(mockContainer, childrenIndex);
mockContainer = setThing(mockContainer, childOneListing);
A CodeSandbox with the above code can be found here: https://codesandbox.io/s/ancient-wood-3u8m3?fontsize=14&hidenavigation=1&theme=dark