1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { LinkService } from "../link-service/link-service";
6 import { GroupsService } from "../groups-service/groups-service";
7 import { FavoriteService } from "./favorite-service";
8 import { LinkClass } from "models/link";
9 import { mockResourceService } from "services/common-service/common-resource-service.test";
10 import { FilterBuilder } from "services/api/filter-builder";
12 describe("FavoriteService", () => {
14 let linkService: LinkService;
15 let groupService: GroupsService;
18 linkService = mockResourceService(LinkService);
19 groupService = mockResourceService(GroupsService);
22 it("marks resource as favorite", async () => {
23 linkService.create = jest.fn().mockReturnValue(Promise.resolve({ uuid: "newUuid" }));
24 const favoriteService = new FavoriteService(linkService, groupService);
26 const newFavorite = await favoriteService.create({ userUuid: "userUuid", resource: { uuid: "resourceUuid", name: "resource" } });
28 expect(linkService.create).toHaveBeenCalledWith({
29 ownerUuid: "userUuid",
31 headUuid: "resourceUuid",
32 linkClass: LinkClass.STAR,
35 expect(newFavorite.uuid).toEqual("newUuid");
39 it("unmarks resource as favorite", async () => {
40 const list = jest.fn().mockReturnValue(Promise.resolve({ items: [{ uuid: "linkUuid" }] }));
41 const filters = new FilterBuilder()
42 .addEqual('owner_uuid', "userUuid")
43 .addEqual('head_uuid', "resourceUuid")
44 .addEqual('link_class', LinkClass.STAR);
45 linkService.list = list;
46 linkService.delete = jest.fn().mockReturnValue(Promise.resolve({ uuid: "linkUuid" }));
47 const favoriteService = new FavoriteService(linkService, groupService);
49 const newFavorite = await favoriteService.delete({ userUuid: "userUuid", resourceUuid: "resourceUuid" });
51 expect(list.mock.calls[0][0].filters).toEqual(filters.getFilters());
52 expect(linkService.delete).toHaveBeenCalledWith("linkUuid");
53 expect(newFavorite[0].uuid).toEqual("linkUuid");
56 it("lists favorite resources", async () => {
57 const list = jest.fn().mockReturnValue(Promise.resolve({ items: [{ headUuid: "headUuid" }] }));
58 const listFilters = new FilterBuilder()
59 .addEqual('owner_uuid', "userUuid")
60 .addEqual('link_class', LinkClass.STAR);
61 const contents = jest.fn().mockReturnValue(Promise.resolve({ items: [{ uuid: "resourceUuid" }] }));
62 const contentFilters = new FilterBuilder().addIn('uuid', ["headUuid"]);
63 linkService.list = list;
64 groupService.contents = contents;
65 const favoriteService = new FavoriteService(linkService, groupService);
67 const favorites = await favoriteService.list("userUuid");
69 expect(list.mock.calls[0][0].filters).toEqual(listFilters.getFilters());
70 expect(contents.mock.calls[0][0]).toEqual("userUuid");
71 expect(contents.mock.calls[0][1].filters).toEqual(contentFilters.getFilters());
72 expect(favorites).toEqual({ items: [{ uuid: "resourceUuid" }] });
75 it("checks if resources are present in favorites", async () => {
76 const list = jest.fn().mockReturnValue(Promise.resolve({ items: [{ headUuid: "foo" }] }));
77 const listFilters = new FilterBuilder()
78 .addIn("head_uuid", ["foo", "oof"])
79 .addEqual("owner_uuid", "userUuid")
80 .addEqual("link_class", LinkClass.STAR);
81 linkService.list = list;
82 const favoriteService = new FavoriteService(linkService, groupService);
84 const favorites = await favoriteService.checkPresenceInFavorites("userUuid", ["foo", "oof"]);
86 expect(list.mock.calls[0][0].filters).toEqual(listFilters.getFilters());
87 expect(favorites).toEqual({ foo: true, oof: false });