import AssociationEntryService from "../resources/associationEntry/service"; import AssociationEntry from "../resources/associationEntry"; import IAssociationEntry from "../resources/associationEntry"; describe("AssociationEntryService", () => { it("should fetch an association entry by given id", () => { const fetchMock = jest.fn(() => new Promise((resolve => resolve({ data: { attributes: { "matches_source": true, "created_at": "2018-05-23T10:46:56.337288", "updated_at": "2018-05-23T10:46:56.337288", }, id: "c41e0fde-5e64-11e8-b625-2fe248649608", type: "association-entry", relationships: { target: { data: { id: "g-SPIT", type: "partner_activity" }, links: { related: "/partner-activities/g-SPIT" } } } } })))); const service = new AssociationEntryService("access123"); service.http = { fetch: fetchMock, }; service.findByEntryId("123", "c41e0fde-5e64-11e8-b625-2fe248649608") .then((associationEntry: IAssociationEntry) => { expect(associationEntry.id).toBe("c41e0fde-5e64-11e8-b625-2fe248649608"); }) .catch((e) => console.error(e)); expect(fetchMock).toHaveBeenCalledWith("/associations/123/entries/c41e0fde-5e64-11e8-b625-2fe248649608", {"headers": {"Authorization": "Bearer access123"}}); }); it("should create an association entry with given attributes", () => { const fetchMock = jest.fn(() => new Promise((resolve => resolve({ data: { attributes: { "matches_source": true, "created_at": "2018-05-23T10:46:56.337288", "updated_at": "2018-05-23T10:46:56.337288", }, id: "c41e0fde-5e64-11e8-b625-2fe248649608", type: "association-entry", relationships: { target: { data: { id: "g-SPIT", type: "partner_activity" }, links: { related: "/partner-activities/g-SPIT" } } } } })))); const service = new AssociationEntryService("access123"); service.http = { fetch: fetchMock, }; const entry = new AssociationEntry({ "matchesSource": true, "target": { id: "351223", type: "poi" } }); service.createAssociationEntry("123", entry).then((result) => { expect(result.id).toBe("c41e0fde-5e64-11e8-b625-2fe248649608"); expect(result.matchesSource).toBe(true); }) .catch((e) => console.error(e)); expect(fetchMock).toHaveBeenCalledWith("/associations/123/entries", { "body": { "data": { "attributes": { "matches_source": true }, "relationships": { "target": { "data": {"id": "351223", "type": "poi"} } }, "type": "association-entry"} }, "headers": {"Authorization": "Bearer access123"}, "method": "POST" } ); }); it("should bulk insert association entries", () => { const fetchMock = jest.fn(() => new Promise((resolve => resolve({ data: [ { attributes: { "matches_source": true, "created_at": "2018-05-23T10:46:56.337288", "updated_at": "2018-05-23T10:46:56.337288", }, id: "c41e0fde-5e64-11e8-b625-2fe248649608", type: "association-entry", relationships: { target: { data: { id: "1234", type: "poi" }, links: { related: "/pois/1234" } } } }, { attributes: { "matches_source": true, "created_at": "2018-05-23T10:46:56.337288", "updated_at": "2018-05-23T10:46:56.337288", }, id: "2656a698-6841-4ed9-b967-44ab00eb49f1", type: "association-entry", relationships: { target: { data: { id: "1234", type: "place" }, links: { related: "/places/1234" } } } } ] })))); const service = new AssociationEntryService("access123"); service.http = { fetch: fetchMock, }; const entries = [ new AssociationEntry({ "matchesSource": true, "target": { id: "1234", type: "place" } }), new AssociationEntry({ "matchesSource": true, "target": { id: "1234", type: "poi" } }) ]; service.bulkInsert("123", entries).then((entries) => { expect(entries).toHaveLength(2); expect(entries[0].id).toBe("c41e0fde-5e64-11e8-b625-2fe248649608"); expect(entries[1].id).toBe("2656a698-6841-4ed9-b967-44ab00eb49f1"); }) .catch((e) => console.error(e)); expect(fetchMock).toHaveBeenCalledWith("/associations/123/entries", { "body": { "data": [ { "attributes": { "matches_source": true }, "relationships": { "target": { "data": {"id": "1234", "type": "place"} } }, "type": "association-entry" }, { "attributes": {"matches_source": true}, "relationships": { "target": { "data": {"id": "1234", "type": "poi"} } }, "type": "association-entry" } ] }, "headers": {"Authorization": "Bearer access123"}, "method": "PUT" } ); }); it("should delete an association entry with given id", () => { const fetchMock = jest.fn(() => new Promise((resolve => resolve({ data: {} })))); const service = new AssociationEntryService("access123"); service.http = { fetch: fetchMock, }; service.deleteAssociationEntry("123", "c41e0fde-5e64-11e8-b625-2fe248649608"); expect(fetchMock).toHaveBeenCalledWith("/associations/123/entries/c41e0fde-5e64-11e8-b625-2fe248649608", { "body": { "data": {} }, "headers": {"Authorization": "Bearer access123"}, "method": "DELETE" } ); }); });