|
| 1 | +import { v4 as uuid } from "uuid"; |
| 2 | +import { Fetcher, graph, UpdateManager } from "rdflib"; |
| 3 | +import { ContactsModuleRdfLib } from "./ContactsModuleRdfLib"; |
| 4 | +import { |
| 5 | + mockNotFound, |
| 6 | + mockTurtleResponse, |
| 7 | +} from "../test-support/mockResponses"; |
| 8 | +import { expectPatchRequest } from "../test-support/expectRequests"; |
| 9 | + |
| 10 | +jest.mock("uuid"); |
| 11 | + |
| 12 | +describe("create new group", () => { |
| 13 | + it("creates group resource", async () => { |
| 14 | + const authenticatedFetch = jest.fn(); |
| 15 | + |
| 16 | + (uuid as jest.Mock).mockReturnValueOnce( |
| 17 | + "b4e9fd85-3b38-4db7-8599-d0eda0b2ac74", |
| 18 | + ); |
| 19 | + |
| 20 | + const store = graph(); |
| 21 | + const fetcher = new Fetcher(store, { |
| 22 | + fetch: authenticatedFetch, |
| 23 | + }); |
| 24 | + const updater = new UpdateManager(store); |
| 25 | + const contacts = new ContactsModuleRdfLib({ |
| 26 | + store, |
| 27 | + fetcher, |
| 28 | + updater, |
| 29 | + }); |
| 30 | + |
| 31 | + mockTurtleResponse( |
| 32 | + authenticatedFetch, |
| 33 | + "https://pod.test/alice/contacts/index.ttl", |
| 34 | + ` |
| 35 | + @prefix vcard: <http://www.w3.org/2006/vcard/ns#>. |
| 36 | + @prefix ab: <http://www.w3.org/ns/pim/ab#>. |
| 37 | + @prefix dc: <http://purl.org/dc/elements/1.1/>. |
| 38 | + @prefix xsd: <http://www.w3.org/2001/XMLSchema#>. |
| 39 | + |
| 40 | + <#this> a vcard:AddressBook; |
| 41 | + dc:title "Alice's contacts"; |
| 42 | + vcard:nameEmailIndex <people.ttl>; |
| 43 | + vcard:groupIndex <groups.ttl>. |
| 44 | +`, |
| 45 | + ); |
| 46 | + |
| 47 | + mockTurtleResponse( |
| 48 | + authenticatedFetch, |
| 49 | + "https://pod.test/alice/contacts/groups.ttl", |
| 50 | + "", |
| 51 | + ); |
| 52 | + |
| 53 | + mockNotFound( |
| 54 | + authenticatedFetch, |
| 55 | + "https://pod.test/alice/contacts/Group/b4e9fd85-3b38-4db7-8599-d0eda0b2ac74/index.ttl", |
| 56 | + ); |
| 57 | + |
| 58 | + const createdUri = await contacts.createNewGroup({ |
| 59 | + addressBookUri: "https://pod.test/alice/contacts/index.ttl#this", |
| 60 | + groupName: "best friends", |
| 61 | + }); |
| 62 | + |
| 63 | + expect(createdUri).toEqual( |
| 64 | + "https://pod.test/alice/contacts/Group/b4e9fd85-3b38-4db7-8599-d0eda0b2ac74/index.ttl#this", |
| 65 | + ); |
| 66 | + expectPatchRequest( |
| 67 | + authenticatedFetch, |
| 68 | + "https://pod.test/alice/contacts/Group/b4e9fd85-3b38-4db7-8599-d0eda0b2ac74/index.ttl", |
| 69 | + `INSERT DATA { <https://pod.test/alice/contacts/Group/b4e9fd85-3b38-4db7-8599-d0eda0b2ac74/index.ttl#this> <http://www.w3.org/2006/vcard/ns#fn> "best friends" . |
| 70 | +<https://pod.test/alice/contacts/Group/b4e9fd85-3b38-4db7-8599-d0eda0b2ac74/index.ttl#this> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2006/vcard/ns#Group> . |
| 71 | + }`, |
| 72 | + ); |
| 73 | + }); |
| 74 | +}); |
0 commit comments