Skip to content

Commit

Permalink
Merge pull request #22 from solid-contrib/bookmarks-vanilla
Browse files Browse the repository at this point in the history
Add a test that parses our multi-format fixture
  • Loading branch information
michielbdejong authored Nov 24, 2023
2 parents 596e40f + 5cefa7c commit ec2b709
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 10 deletions.
4 changes: 4 additions & 0 deletions bookmarks/vanilla/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Bookmarks Data Module using vanilla JavaScript

# How to use this module

[Please add docs about how this data module uses the type index etc.]

## Development
```bash
git clone https://github.com/solid-contrib/data-modules
Expand Down
78 changes: 68 additions & 10 deletions bookmarks/vanilla/src/modules/Bookmark.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {
ThingPersisted,
buildThing,
createThing,
getLiteral,
getNamedNode,
getPodUrlAll,
getSolidDataset,
getThing,
Expand All @@ -14,14 +16,20 @@ import { getThingAll, removeThing } from "@inrupt/solid-client";
import {
BOOKMARK,
DCTERMS,
RDF
FOAF,
RDF,
RDFS
} from "@inrupt/vocab-common-rdf";


export interface IBookmark {
url: string
title: string
link: string
created?: string
updated?: string
creator?: string
topic?: string
}

export class Bookmark {
Expand Down Expand Up @@ -79,15 +87,7 @@ export class Bookmark {

const thing = getThing(ds, url)

if (thing) {
return {
url: thing.url,
title: getLiteral(thing, DCTERMS.title)?.value,
link: getLiteral(thing, BOOKMARK.recalls)?.value
} as IBookmark
}

return undefined
return thing ? this.mapBookmark(thing) : undefined
}

/**
Expand Down Expand Up @@ -187,4 +187,62 @@ export class Bookmark {

};


private static mapBookmark(thing: ThingPersisted): Bookmark {
const obj: IBookmark = {
url: thing.url,
title: this.mapTitle(thing),
link: this.mapLink(thing)
};
if (this.mapCreated(thing)) {
obj.created = this.mapCreated(thing);
}
if (this.mapUpdated(thing)) {
obj.updated = this.mapUpdated(thing);
}
if (this.mapCreator(thing)) {
obj.creator = this.mapCreator(thing);
}
if (this.mapTopic(thing)) {
obj.topic = this.mapTopic(thing);
}
return obj;
}
private static mapTitle(thing: ThingPersisted): string {
return (
getLiteral(thing, DCTERMS.title)?.value ??
getLiteral(thing, RDFS.label)?.value ??
""
);
}
private static mapLink(thing: ThingPersisted): string {
return (
getLiteral(thing, BOOKMARK.recalls)?.value ??
getNamedNode(thing, BOOKMARK.recalls)?.value ??
""
);
}
private static mapCreated(thing: ThingPersisted): string {
return getLiteral(thing, DCTERMS.created)?.value ?? ""
}
private static mapUpdated(thing: ThingPersisted): string {
return (
getLiteral(thing, "http://purl.org/dc/terms/updated")?.value ??
""
);
}
private static mapCreator(thing: ThingPersisted): string {
return (
getNamedNode(thing, DCTERMS.creator)?.value ??
getNamedNode(thing, FOAF.maker)?.value ??
""
);
}
private static mapTopic(thing: ThingPersisted): string {
return (
getNamedNode(thing, BOOKMARK.hasTopic)?.value ??
""
);
}

}
109 changes: 109 additions & 0 deletions bookmarks/vanilla/test/unit/Bookmark.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ describe("Bookmark", () => {
} as unknown as jest.Mocked<Session>;
});

// TODO: Improve existing tests to mock fetch and not getSolidDataset etc
// See https://github.com/solid-contrib/data-modules/issues/23

it("should return index url", async () => {
const podUrls = ["https://fake-pod.net/"];
const typeIndexUrl = "https://fake-pod.net/bookmarks/index.ttl";
Expand Down Expand Up @@ -154,6 +157,112 @@ describe("Bookmark", () => {

expect(Bookmark.getIndexUrl).toHaveBeenCalled();

expect(res).toEqual(expected);
});
it("should parse bookmarks in format one", async () => {
const indexUrl = "https://fake-pod.net/bookmarks/index.ttl";

const url = 'https://fake-pod.net/bookmarks/index.ttl#one';

const expected = {
url: 'https://fake-pod.net/bookmarks/index.ttl#one',
title: 'one',
link: 'http://example.com',
created: '2023-10-21T14:16:16Z',
updated: '2023-11-21T14:16:16Z',
creator: 'https://michielbdejong.solidcommunity.net/profile/card#me'
}

const responseObject: any = {
status: 200,
ok: true,
headers: {
get: (h: string) => (h == "Content-Type" ? "text/turtle" : undefined)
},
text: () => {
return Promise.resolve(loadFixture("bookmark-formats.ttl"));
}
};

jest.spyOn(Bookmark, "getIndexUrl").mockReturnValue(Promise.resolve(indexUrl));

jest.spyOn(session, "fetch").mockReturnValue(Promise.resolve(responseObject));
// jest.spyOn(inruptClient, "getThing").mockReturnValue(JSON.parse(loadFixture("things/one.json")));

const res = await Bookmark.get(url, session);

expect(Bookmark.getIndexUrl).toHaveBeenCalled();

expect(res).toEqual(expected);

});
it("should parse bookmarks in format two", async () => {
const indexUrl = "https://fake-pod.net/bookmarks/index.ttl";

const url = 'https://fake-pod.net/bookmarks/index.ttl#two';

const expected = {
url: 'https://fake-pod.net/bookmarks/index.ttl#two',
title: 'two',
link: 'http://example.com',
creator: 'https://michielbdejong.solidcommunity.net/profile/card#me'
}

const responseObject: any = {
status: 200,
ok: true,
headers: {
get: (h: string) => (h == "Content-Type" ? "text/turtle" : undefined)
},
text: () => {
return Promise.resolve(loadFixture("bookmark-formats.ttl"));
}
};

jest.spyOn(Bookmark, "getIndexUrl").mockReturnValue(Promise.resolve(indexUrl));

jest.spyOn(session, "fetch").mockReturnValue(Promise.resolve(responseObject));
// jest.spyOn(inruptClient, "getThing").mockReturnValue(JSON.parse(loadFixture("things/one.json")));

const res = await Bookmark.get(url, session);

expect(Bookmark.getIndexUrl).toHaveBeenCalled();

expect(res).toEqual(expected);
});
// FIXME: https://github.com/solid-contrib/data-modules/issues/24
it.only("should parse bookmarks in format three", async () => {
const indexUrl = "https://fake-pod.net/bookmarks/index.ttl";

const url = 'https://fake-pod.net/bookmarks/index.ttl#three';

const expected = {
url: 'https://fake-pod.net/bookmarks/index.ttl#three',
title: 'three',
link: 'http://example.com',
topic: 'http://wikipedia.org/sdfg'
}

const responseObject: any = {
status: 200,
ok: true,
headers: {
get: (h: string) => (h == "Content-Type" ? "text/turtle" : undefined)
},
text: () => {
return Promise.resolve(loadFixture("bookmark-formats.ttl"));
}
};

jest.spyOn(Bookmark, "getIndexUrl").mockReturnValue(Promise.resolve(indexUrl));

jest.spyOn(session, "fetch").mockReturnValue(Promise.resolve(responseObject));
// jest.spyOn(inruptClient, "getThing").mockReturnValue(JSON.parse(loadFixture("things/one.json")));

const res = await Bookmark.get(url, session);

expect(Bookmark.getIndexUrl).toHaveBeenCalled();

expect(res).toEqual(expected);
});
});
40 changes: 40 additions & 0 deletions bookmarks/vanilla/test/unit/fixtures/bookmark-formats.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
@prefix : <https://fake-pod.net/bookmarks/index.ttl#>.
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
@prefix bookm: <./>.
@prefix boo: <http://www.w3.org/2002/01/bookmark#>.
@prefix dc: <http://purl.org/dc/terms/>.
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
@prefix crdt: <http://soukai-solid.com/crdt>.
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

:one
a boo:Bookmark;
rdfs:label "one";
dc:created "2023-10-21T14:16:16Z"^^<http://www.w3.org/2001/XMLSchema#dateTime>;
dc:updated "2023-11-21T14:16:16Z"^^<http://www.w3.org/2001/XMLSchema#dateTime>;
dc:creator <https://michielbdejong.solidcommunity.net/profile/card#me>;
boo:recalls <http://example.com>.

:two
a boo:Bookmark;
dc:title "two";
foaf:maker <https://michielbdejong.solidcommunity.net/profile/card#me>;
boo:recalls "http://example.com".

:three
a boo:Bookmark;
rdfs:label "three";
boo:hasTopic <http://wikipedia.org/sdfg>;
boo:recalls <http://example.com>.

bookm:b93d9944-d54d-42f6-a39b-6ea3f9217763
a boo:Bookmark;
rdfs:label "sdf";
boo:hasTopic "sdfg";
boo:id "b93d9944-d54d-42f6-a39b-6ea3f9217763";
boo:recalls <http://example.com>.
bookm:b93d9944-d54d-42f6-a39b-6ea3f9217763-metadata
a crdt:Metadata;
crdt:createdAt "2023-11-21T12:50:32.051Z"^^xsd:dateTime;
crdt:resource bookm:b93d9944-d54d-42f6-a39b-6ea3f9217763;
crdt:updatedAt "2023-11-21T12:50:32.051Z"^^xsd:dateTime.

0 comments on commit ec2b709

Please sign in to comment.