Skip to content

Commit

Permalink
rdflib-utils: allow to mock LDP container contents
Browse files Browse the repository at this point in the history
  • Loading branch information
angelo-v committed Jul 29, 2024
1 parent a056e85 commit c6538cf
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
38 changes: 38 additions & 0 deletions utils/rdflib/src/test-support/mockResponses.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { mockLdpContainer } from "./mockResponses";

describe("mockResponses", () => {
describe("mockLdpContainer", () => {
it("mocks a container without contents", async () => {
const fetch = jest.fn();
mockLdpContainer(fetch, "http://container.test/");
const result = await fetch("http://container.test/", {});
expect(await result.text()).toEqual(`
@prefix dc: <http://purl.org/dc/terms/>.
@prefix ldp: <http://www.w3.org/ns/ldp#>.
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
<> a ldp:Container, ldp:BasicContainer, ldp:Resource ;
.
`);
});

it("mocks a container with contents", async () => {
const fetch = jest.fn();
mockLdpContainer(fetch, "http://container.test/", [
"http://container.test/one",
"http://container.test/two",
]);
const result = await fetch("http://container.test/", {});
expect(await result.text()).toEqual(`
@prefix dc: <http://purl.org/dc/terms/>.
@prefix ldp: <http://www.w3.org/ns/ldp#>.
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
<> a ldp:Container, ldp:BasicContainer, ldp:Resource ;
ldp:contains <http://container.test/one>; ldp:contains <http://container.test/two>
.
`);
});
});
});
11 changes: 9 additions & 2 deletions utils/rdflib/src/test-support/mockResponses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ export function mockTurtleDocument(fetch: jest.Mock, url: string, ttl: string) {
* Mock a LDP container at the given URL
* @param fetch - A mocked fetch function
* @param url - The URL to mock
* @param contains - List of URLs of documents contained in this container
*/
export function mockLdpContainer(fetch: jest.Mock, url: string) {
export function mockLdpContainer(
fetch: jest.Mock,
url: string,
contains: string[] = [],
) {
when(fetch)
.calledWith(url, expect.anything())
.mockResolvedValue({
Expand All @@ -47,7 +52,9 @@ export function mockLdpContainer(fetch: jest.Mock, url: string) {
@prefix ldp: <http://www.w3.org/ns/ldp#>.
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
<> a ldp:Container, ldp:BasicContainer, ldp:Resource .
<> a ldp:Container, ldp:BasicContainer, ldp:Resource ;
${contains.map((it) => `ldp:contains <${it}>`).join("; ")}
.
`),
} as Response);
}
Expand Down

0 comments on commit c6538cf

Please sign in to comment.