Skip to content

Commit

Permalink
rdflib-utils: allow to add turtle to ldp container mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
angelo-v committed Aug 26, 2024
1 parent 636d6af commit 2545b74
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
7 changes: 7 additions & 0 deletions utils/rdflib/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Changes

- [mockLdpContainer](https://solid-contrib.github.io/data-modules/rdflib-utils/functions/test_support.mockLdpContainer.html)
now allows to add additional turtle to include into the mock response

## 0.4.0

### Added
Expand Down
24 changes: 22 additions & 2 deletions utils/rdflib/src/test-support/mockResponses.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe("mockResponses", () => {
<> a ldp:Container, ldp:BasicContainer, ldp:Resource ;
.
`);
`);
});

it("mocks a container with contents", async () => {
Expand All @@ -32,7 +32,27 @@ describe("mockResponses", () => {
<> a ldp:Container, ldp:BasicContainer, ldp:Resource ;
ldp:contains <http://container.test/one>; ldp:contains <http://container.test/two>
.
`);
`);
});

it("mocks a container with contents and more turtle statements", async () => {
const fetch = jest.fn();
mockLdpContainer(
fetch,
"http://container.test/",
["http://container.test/one", "http://container.test/two"],
`<http://container.test/one> a ldp:Container .`,
);
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>
.
<http://container.test/one> a ldp:Container .`);
});
});
});
4 changes: 3 additions & 1 deletion utils/rdflib/src/test-support/mockResponses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ export function mockTurtleDocument(fetch: jest.Mock, url: string, ttl: string) {
* @param fetch - A mocked fetch function
* @param url - The URL to mock
* @param contains - List of URLs of documents contained in this container
* @param moreTurtle - Additional turtle to include into the response
*/
export function mockLdpContainer(
fetch: jest.Mock,
url: string,
contains: string[] = [],
moreTurtle: string = "",
) {
when(fetch)
.calledWith(url, expect.anything())
Expand All @@ -55,7 +57,7 @@ export function mockLdpContainer(
<> a ldp:Container, ldp:BasicContainer, ldp:Resource ;
${contains.map((it) => `ldp:contains <${it}>`).join("; ")}
.
`),
${moreTurtle}`),
} as Response);
}

Expand Down

0 comments on commit 2545b74

Please sign in to comment.