Skip to content

Commit 73bb2da

Browse files
committed
Fix collections test
1 parent 4164eac commit 73bb2da

File tree

7 files changed

+361
-361
lines changed

7 files changed

+361
-361
lines changed

packages/hub/src/lib/add-collection-item.spec.ts

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,83 @@ import { it, describe, expect } from "vitest";
22

33
import { TEST_HUB_URL, TEST_ACCESS_TOKEN, TEST_USER } from "../test/consts";
44
import { addCollectionItem } from "./add-collection-item";
5-
import { listCollections } from "./list-collections";
65
import { collectionInfo } from "./collection-info";
76
import { deleteCollectionItem } from "./delete-collection-item";
7+
import { createCollection } from "./create-collection";
8+
import { deleteCollection } from "./delete-collection";
89

910
describe("addCollectionItem", () => {
1011
it("should add a item to a collection", async () => {
1112
let slug: string = "";
12-
let itemId: string = "";
13+
14+
const randomString = crypto.randomUUID();
15+
const title = `Test Collection ${randomString}`;
1316

1417
try {
15-
for await (const entry of listCollections({
16-
search: { owner: [TEST_USER] },
17-
limit: 1,
18+
const result = await createCollection({
19+
collection: {
20+
title,
21+
namespace: TEST_USER,
22+
description: "This is a test collection",
23+
private: false,
24+
},
25+
accessToken: TEST_ACCESS_TOKEN,
1826
hubUrl: TEST_HUB_URL,
19-
})) {
20-
slug = entry.slug;
21-
break;
22-
}
27+
});
28+
29+
slug = result.slug;
30+
31+
expect(result.slug.startsWith(`${TEST_USER}/test-collection-${randomString}`)).toBe(true);
2332

2433
await addCollectionItem({
34+
accessToken: TEST_ACCESS_TOKEN,
35+
hubUrl: TEST_HUB_URL,
2536
slug,
2637
item: {
27-
type: "model",
28-
id: "quanghuynt14/TestAddCollectionItem",
38+
type: "collection",
39+
// temporary, later the slug should work on its own
40+
id: result.slug.slice(-24),
2941
},
3042
note: "This is a test item",
31-
accessToken: TEST_ACCESS_TOKEN,
32-
hubUrl: TEST_HUB_URL,
3343
});
3444

35-
const collection = await collectionInfo({
45+
const items = await collectionInfo({
3646
slug,
3747
accessToken: TEST_ACCESS_TOKEN,
3848
hubUrl: TEST_HUB_URL,
3949
});
4050

41-
const item = collection.items.find((item) => item.id === "quanghuynt14/TestAddCollectionItem");
42-
43-
expect(item).toBeDefined();
51+
expect(items.items.length).toBe(1);
52+
expect(items.items[0].type).toBe("collection");
53+
// temporary, later the slug should work on its own right?
54+
expect(items.items[0].id).toBe(result.slug.slice(-24));
55+
expect(items.items[0].note).toEqual({
56+
html: "This is a test item",
57+
text: "This is a test item",
58+
});
4459

45-
itemId = item?._id || "";
46-
} finally {
4760
await deleteCollectionItem({
4861
slug,
49-
itemId,
62+
itemId: items.items[0]._id,
5063
accessToken: TEST_ACCESS_TOKEN,
5164
hubUrl: TEST_HUB_URL,
5265
});
66+
67+
const items2 = await collectionInfo({
68+
slug,
69+
accessToken: TEST_ACCESS_TOKEN,
70+
hubUrl: TEST_HUB_URL,
71+
});
72+
73+
expect(items2.items.length).toBe(0);
74+
} finally {
75+
if (slug) {
76+
await deleteCollection({
77+
slug,
78+
accessToken: TEST_ACCESS_TOKEN,
79+
hubUrl: TEST_HUB_URL,
80+
});
81+
}
5382
}
5483
});
5584
});

packages/hub/src/lib/add-collection-item.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ export async function addCollectionItem(
2727
fetch?: typeof fetch;
2828
} & Partial<CredentialsParams>
2929
): Promise<void> {
30+
if (!params.slug) {
31+
throw new TypeError("slug is required");
32+
}
33+
3034
const accessToken = checkCredentials(params);
3135

3236
const res = await (params.fetch ?? fetch)(`${params.hubUrl ?? HUB_URL}/api/collections/${params.slug}/items`, {

0 commit comments

Comments
 (0)