如何使用@inrupt/solid-client 模拟容器?

How do I mock a Container with @inrupt/solid-client?

如果我正在使用试图从容器中读取的 npm 包 @inrupt/solid-client 为代码编写单元测试,我该如何模拟该容器?

从技术上讲,容器是一种常规资源,它包含的每个资源都有一个 Thing,并且容器自己的 URL 标识的一个事物指向它们。

因此,下面的代码应该可以解决问题:

// 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);

可以在这里找到包含上述代码的 CodeSandbox:https://codesandbox.io/s/ancient-wood-3u8m3?fontsize=14&hidenavigation=1&theme=dark