import AssociationMapper from "../resources/association/mapper"; import {IAssociation} from "../resources/association/index"; describe("AssociationMapper", () => { it("should map an association to an association model", () => { const mapper = new AssociationMapper(); const model: IAssociation = mapper.toModel({ "type": "association", "relationships": {}, "id": "0100322a-5e5f-11e8-8ba0-db1638c712d6", "attributes": { "target_types": ["lodging"], "association_type": "to_one", "created_at": "2018-05-23T10:46:56.337288", "updated_at": "2018-05-23T10:46:56.337288", "status": "unmatched" } }); expect(model.id).toBe("0100322a-5e5f-11e8-8ba0-db1638c712d6"); expect(model.targetTypes).toContain("lodging"); expect(model.associationType).toBe("to_one"); expect(model.createdAt).toBe("2018-05-23T10:46:56.337288"); expect(model.updatedAt).toBe("2018-05-23T10:46:56.337288"); expect(model.status).toBe("unmatched"); }); it("properly assigns source to the association model", () => { const mapper = new AssociationMapper(); const model: IAssociation = mapper.toModel({ "type": "association", "relationships": { "source": { "data": { id: "1234", type: "poi" } } }, "id": "0100322a-5e5f-11e8-8ba0-db1638c712d6", "attributes": { "target_types": ["lodging"], "association_type": "to_one", "created_at": "2018-05-23T10:46:56.337288", "updated_at": "2018-05-23T10:46:56.337288", "status": "unmatched" } }, ([{ type: "poi", id: "1234", attributes: { name: "my POI" }, relationships: { "containing-place": { data: { type: "place", id: "362228" } } } }] as any)); expect(model.source.id).toBe("1234") }); it("properly assigns entries to the association model", () => { const mapper = new AssociationMapper(); const model: IAssociation = mapper.toModel({ "type": "association", "relationships": { "entries": { "data": [ { "type": "association-entry", "id": "1234" }, { "type": "association-entry", "id": "4321" } ], "links": { "related": "/associations/0100322a-5e5f-11e8-8ba0-db1638c712d/entries" } } }, "id": "0100322a-5e5f-11e8-8ba0-db1638c712d6", "attributes": { "target_types": ["lodging"], "association_type": "to_one", "created_at": "2018-05-23T10:46:56.337288", "updated_at": "2018-05-23T10:46:56.337288", "status": "unmatched" } }, ([ { type: "association-entry", id: "1234", attributes: { matches_source: true }, relationships: { target: { data: { type: "poi", id: "362228" } } } }, { type: "association-entry", id: "4321", attributes: { matches_source: true }, relationships: { target: { data: { type: "poi", id: "554122" } } } } ] as any)); const entries = model.entries; expect(entries).toHaveLength(2); expect(entries[0].id).toBe("1234"); expect(entries[1].id).toBe("4321"); }); it("properly assigns suggestions to the association model", () => { const mapper = new AssociationMapper(); const model: IAssociation = mapper.toModel({ "type": "association", "relationships": { "suggestions": { "data": [ { "type": "lodging", "id": "1234" }, { "type": "lodging", "id": "4321" } ], } }, "id": "0100322a-5e5f-11e8-8ba0-db1638c712d6", "attributes": { "target_types": ["lodging"], "association_type": "to_one", "created_at": "2018-05-23T10:46:56.337288", "updated_at": "2018-05-23T10:46:56.337288", "status": "unmatched" } }, ([ { type: "lodging", id: "1234", attributes: { name: "first" }, relationships: { "containing-place": { data: { id: "9999", type: "poi", } }, } }, { type: "lodging", id: "4321", attributes: { name: "second" }, relationships: { "containing-place": { data: { id: "9999", type: "poi", } }, } } ] as any)); const suggestions = model.suggestions; expect(suggestions).toHaveLength(2); expect(suggestions[0].id).toBe("1234"); expect(suggestions[1].id).toBe("4321"); }); });