import AssociationService from "../resources/association/service"; import Association from "../resources/association"; import IAssociation from "../resources/association"; describe("AssociationService", () => { it("should fetch an association by given id", () => { const fetchMock = jest.fn(() => new Promise((resolve => resolve({ data: { attributes: { "target_types": ["poi", "place"], "association_type": "to_many", "created_at": "2018-05-23T10:46:56.337288", "updated_at": "2018-05-23T10:46:56.337288", "status": "matched" }, id: "c41e0fde-5e64-11e8-b625-2fe248649608", type: "association", relationships: { source: { data: { id: "g-SPIT", type: "partner_activity" }, links: { related: "/partner-activities/g-SPIT" } } } } })))); const service = new AssociationService("access123"); service.http = { fetch: fetchMock, }; service.findById("c41e0fde-5e64-11e8-b625-2fe248649608") .then((association: IAssociation) => { expect(association.id).toBe("c41e0fde-5e64-11e8-b625-2fe248649608"); }) .catch((e) => console.error(e)); expect(fetchMock).toHaveBeenCalledWith("/associations/c41e0fde-5e64-11e8-b625-2fe248649608", {"headers": {"Authorization": "Bearer access123"}}); }); it("should create an association with given attributes", () => { const fetchMock = jest.fn(() => new Promise((resolve => resolve({ data: { attributes: { "target_types": ["poi"], "association_type": "to_one", "created_at": "2018-05-23T10:46:56.337288", "updated_at": "2018-05-23T10:46:56.337288", "status": "unmatched" }, id: "c41e0fde-5e64-11e8-b625-2fe248649608", type: "association", relationships: { source: { data: { id: "g-SPIT", type: "partner-activity" }, links: { related: "/partner-activities/g-SPIT" } } } } })))); const service = new AssociationService("access123"); service.http = { fetch: fetchMock, }; const association: IAssociation = new Association({ "targetTypes": ["poi"], "associationType": "to_one", "status": "unmatched", "source": { id: "351223", type: "poi" } }); service.create(association).then((result) => { expect(result.id).toBe("c41e0fde-5e64-11e8-b625-2fe248649608"); expect(result.associationType).toBe("to_one"); }) .catch((e) => console.error(e)); expect(fetchMock).toHaveBeenCalledWith("/associations", { "body": { "data": { "attributes": { "association_type": "to_one", "status": "unmatched", "target_types": ["poi"] }, "relationships": { "source": { "data": {"id": "351223", "type": "poi"} } }, "type": "association" } }, "headers": {"Authorization": "Bearer access123"}, "method": "POST" } ); }); it("should update an association with given attributes", () => { const fetchMock = jest.fn(() => new Promise((resolve => resolve({ data: { attributes: { "target_types": ["poi"], "association_type": "to_one", "created_at": "2018-05-23T10:46:56.337288", "updated_at": "2018-05-23T10:46:56.337288", "status": "matched" }, id: "c41e0fde-5e64-11e8-b625-2fe248649608", type: "association", relationships: { source: { data: { id: "g-SPIT", type: "partner-activity" }, links: { related: "/partner-activities/g-SPIT" } } } } })))); const service = new AssociationService("access123"); service.http = { fetch: fetchMock, }; const attributes: IAssociation = new Association({ "id": "c41e0fde-5e64-11e8-b625-2fe248649608", "status": "matched" }); service.update(attributes).then((association: IAssociation) => { expect(association.id).toBe("c41e0fde-5e64-11e8-b625-2fe248649608") expect(association.status).toBe("matched") }) .catch((e) => console.error(e)); expect(fetchMock).toHaveBeenCalledWith("/associations/c41e0fde-5e64-11e8-b625-2fe248649608", { "body": { "data": { "attributes": { "status": "matched" }, "id": "c41e0fde-5e64-11e8-b625-2fe248649608", "type": "association" } }, "headers": { "Authorization": "Bearer access123" }, "method": "PATCH" } ); }); it("should delete an association with given id", () => { const fetchMock = jest.fn(() => new Promise((resolve => resolve({ data: {} })))); const service = new AssociationService("access123"); service.http = { fetch: fetchMock, }; service.destroy("c41e0fde-5e64-11e8-b625-2fe248649608") expect(fetchMock).toHaveBeenCalledWith("/associations/c41e0fde-5e64-11e8-b625-2fe248649608", { "headers": { "Authorization": "Bearer access123" }, "method": "DELETE" } ) }); });