import TagAssociationService from "../resources/tagAssociation/service"; import ITagAssociation from "../resources/tagAssociation"; describe("TagAssociationService", () => { it("should fetch a tag association by given id", () => { const fetchMock = jest.fn(() => new Promise((resolve => resolve({ data: { attributes: { "confidence_score": 0.34, "label": "Trekking", "score": 0.93 }, id: "c41e0fde-5e64-11e8-b625-2fe248649608", type: "tag-association", relationships: { target: { data: { id: "g-SPIT", type: "partner_activity" }, links: { related: "/partner-activities/g-SPIT" } } } } })))); const service = new TagAssociationService("access123"); service.http = { fetch: fetchMock, }; service.findById("c41e0fde-5e64-11e8-b625-2fe248649608") .then((tagV2: ITagAssociation) => { expect(tagV2.id).toBe("c41e0fde-5e64-11e8-b625-2fe248649608"); }) .catch((e) => console.error(e)); expect(fetchMock).toHaveBeenCalledWith("/tags/associations/c41e0fde-5e64-11e8-b625-2fe248649608", {"headers": {"Accept-Version": "v2", "Authorization": "Bearer access123"}}); }); it("should fetch tag associations", () => { const fetchMock = jest.fn(() => new Promise((resolve => resolve({ data: [ { attributes: { "confidence_score": 0.34, "label": "Trekking", "score": 0.93 }, id: "c41e0fde-5e64-11e8-b625-2fe248649608", type: "tag-association", relationships: { target: { data: { id: "g-SPIT", type: "partner_activity" }, links: { related: "/partner-activities/g-SPIT" } } } }, { attributes: { "confidence_score": 0.25, "label": "Adventure", "score": 0.83 }, id: "b44e0fde-5e64-11e8-b625-2fe248649608", type: "tag-association", relationships: { target: { data: { id: "g-SPIT", type: "partner_activity" }, links: { related: "/partner-activities/g-SPIT" } } } } ] })))); const service = new TagAssociationService("access123"); service.http = { fetch: fetchMock, }; service.find() .then((tags: ITagAssociation[]) => { expect(tags.length).toBe(2); expect(tags[0].id).toBe("c41e0fde-5e64-11e8-b625-2fe248649608"); expect(tags[1].id).toBe("b44e0fde-5e64-11e8-b625-2fe248649608"); }) .catch((e) => console.error(e)); expect(fetchMock).toHaveBeenCalledWith("/tags/associations", {"headers": {"Accept-Version": "v2", "Authorization": "Bearer access123"}}); }); });