import BlacklistItemService from "../resources/blacklistItem/service"; import BlacklistItem from "../resources/blacklistItem"; import IBlacklistItem from "../resources/blacklistItem"; describe("BlacklistItemService", () => { it("should fetch a blacklistItem by given id", () => { const fetchMock = jest.fn(() => new Promise((resolve => resolve({ data: { attributes: { "created_at": "2018-05-23T10:46:56.337288", "updated_at": "2018-05-23T10:46:56.337288", }, id: "c41e0fde-5e64-11e8-b625-2fe248649608", type: "blacklist-item", relationships: { target: { data: { id: "g-SPIT", type: "partner_activity" }, links: { related: "/partner-activities/g-SPIT" } } } } })))); const service = new BlacklistItemService("access123"); service.http = { fetch: fetchMock, }; service.findById("c41e0fde-5e64-11e8-b625-2fe248649608") .then((blacklistItem: IBlacklistItem) => { expect(blacklistItem.id).toBe("c41e0fde-5e64-11e8-b625-2fe248649608"); }) .catch((e) => console.error(e)); expect(fetchMock).toHaveBeenCalledWith("/blacklist-items/c41e0fde-5e64-11e8-b625-2fe248649608", {"headers": {"Authorization": "Bearer access123"}}); }); it("should create a blacklist-item with given attributes", () => { const fetchMock = jest.fn(() => new Promise((resolve => resolve({ data: { attributes: { "created_at": "2018-05-23T10:46:56.337288", "updated_at": "2018-05-23T10:46:56.337288", }, id: "c41e0fde-5e64-11e8-b625-2fe248649608", type: "blacklist-item", relationships: { target: { data: { id: "351223", type: "poi" }, links: { related: "/pois/351223" } } } } })))); const service = new BlacklistItemService("access123"); service.http = { fetch: fetchMock, }; const blacklistItem: IBlacklistItem = new BlacklistItem({ "target": { id: "351223", type: "poi" } }); service.create(blacklistItem).then((result) => { expect(result.id).toBe("c41e0fde-5e64-11e8-b625-2fe248649608"); expect(result.target.id).toBe("351223"); expect(result.target.type).toBe("poi"); }) .catch((e) => console.error(e)); expect(fetchMock).toHaveBeenCalledWith("/blacklist-items", { "body": { "data": { "attributes": {}, "relationships": { "target": { "data": {"id": "351223", "type": "poi"} } }, "type": "blacklist-item" } }, "headers": {"Authorization": "Bearer access123"}, "method": "POST" } ); }); it("should delete a blacklist-item with given id", () => { const fetchMock = jest.fn(() => new Promise((resolve => resolve({ data: {} })))); const service = new BlacklistItemService("access123"); service.http = { fetch: fetchMock, }; service.destroy("c41e0fde-5e64-11e8-b625-2fe248649608"); expect(fetchMock).toHaveBeenCalledWith("/blacklist-items/c41e0fde-5e64-11e8-b625-2fe248649608", { "headers": { "Authorization": "Bearer access123" }, "method": "DELETE" } ); }); });