Skip to content

Commit 7afc0fc

Browse files
Copilotpawcoding
andcommitted
test(loader): add test cases for expand option
Co-authored-by: pawcoding <[email protected]>
1 parent 581ea8b commit 7afc0fc

File tree

6 files changed

+263
-14
lines changed

6 files changed

+263
-14
lines changed

test/_mocks/delete-collection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { assert } from "vitest";
2-
import type { PocketBaseLoaderOptions } from "../../src/types/pocketbase-loader-options.type";
2+
import type { PocketBaseLoaderBaseOptions } from "../../src/types/pocketbase-loader-options.type";
33

44
export async function deleteCollection(
5-
options: PocketBaseLoaderOptions,
5+
options: PocketBaseLoaderBaseOptions,
66
superuserToken: string
77
): Promise<void> {
88
const deleteRequest = await fetch(

test/_mocks/delete-entry.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { assert } from "vitest";
2-
import type { PocketBaseLoaderOptions } from "../../src/types/pocketbase-loader-options.type";
2+
import type { PocketBaseLoaderBaseOptions } from "../../src/types/pocketbase-loader-options.type";
33
import { sendBatchRequest } from "./batch-requests";
44

55
export async function deleteEntries(
66
entryIds: Array<string>,
7-
options: PocketBaseLoaderOptions,
7+
options: PocketBaseLoaderBaseOptions,
88
superuserToken: string
99
): Promise<void> {
1010
const requests = entryIds.map((entryId) => ({
@@ -26,7 +26,7 @@ export async function deleteEntries(
2626

2727
export async function deleteEntry(
2828
entryId: string,
29-
options: PocketBaseLoaderOptions,
29+
options: PocketBaseLoaderBaseOptions,
3030
superuserToken: string
3131
): Promise<void> {
3232
const deleteRequest = await fetch(

test/_mocks/insert-collection.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { assert } from "console";
2-
import type { PocketBaseLoaderOptions } from "../../src/types/pocketbase-loader-options.type";
2+
import type { PocketBaseLoaderBaseOptions } from "../../src/types/pocketbase-loader-options.type";
33
import type { PocketBaseSchemaEntry } from "../../src/types/pocketbase-schema.type";
44

55
export async function insertCollection(
66
fields: Array<PocketBaseSchemaEntry>,
7-
options: PocketBaseLoaderOptions,
7+
options: PocketBaseLoaderBaseOptions,
88
superuserToken: string
9-
): Promise<void> {
9+
): Promise<string> {
1010
const insertRequest = await fetch(new URL(`api/collections`, options.url), {
1111
method: "POST",
1212
headers: {
@@ -20,4 +20,9 @@ export async function insertCollection(
2020
});
2121

2222
assert(insertRequest.status === 200, "Collection is not available.");
23+
24+
const insertResponse = await insertRequest.json();
25+
assert(insertResponse.id, "Collection ID is not available.");
26+
27+
return insertResponse.id;
2328
}

test/_mocks/insert-entry.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { assert } from "vitest";
22
import type { PocketBaseEntry } from "../../src/types/pocketbase-entry.type";
3-
import type { PocketBaseLoaderOptions } from "../../src/types/pocketbase-loader-options.type";
3+
import type { PocketBaseLoaderBaseOptions } from "../../src/types/pocketbase-loader-options.type";
44
import { sendBatchRequest } from "./batch-requests";
55

66
export async function insertEntries(
77
data: Array<Record<string, unknown>>,
8-
options: PocketBaseLoaderOptions,
8+
options: PocketBaseLoaderBaseOptions,
99
superuserToken: string
1010
): Promise<Array<PocketBaseEntry>> {
1111
const requests = data.map((entry) => ({
@@ -34,7 +34,7 @@ export async function insertEntries(
3434

3535
export async function insertEntry(
3636
data: Record<string, unknown>,
37-
options: PocketBaseLoaderOptions,
37+
options: PocketBaseLoaderBaseOptions,
3838
superuserToken: string
3939
): Promise<PocketBaseEntry> {
4040
const insertRequest = await fetch(

test/loader/fetch-collection.e2e-spec.ts

Lines changed: 133 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@ import {
33
LiveEntryNotFoundError
44
} from "astro/content/runtime";
55
import { randomUUID } from "crypto";
6-
import { beforeEach, describe, expect, inject, test, vi } from "vitest";
6+
import {
7+
afterEach,
8+
beforeEach,
9+
describe,
10+
expect,
11+
inject,
12+
test,
13+
vi
14+
} from "vitest";
15+
import type { ExperimentalPocketBaseLiveLoaderOptions } from "../../src";
716
import { fetchCollection } from "../../src/loader/fetch-collection";
817
import { PocketBaseAuthenticationError } from "../../src/types/errors";
918
import type { PocketBaseEntry } from "../../src/types/pocketbase-entry.type";
@@ -494,4 +503,127 @@ describe("fetchCollection", () => {
494503
await deleteCollection(testOptions, superuserToken);
495504
});
496505
});
506+
507+
describe("expand parameter", () => {
508+
let testOptions: ExperimentalPocketBaseLiveLoaderOptions;
509+
let relationOptions: ExperimentalPocketBaseLiveLoaderOptions;
510+
let relationCollectionId: string;
511+
512+
beforeEach(async () => {
513+
testOptions = {
514+
...options,
515+
collectionName: randomUUID().replaceAll("-", ""),
516+
experimental: {
517+
expand: ["singleRelation"]
518+
}
519+
};
520+
relationOptions = {
521+
...options,
522+
collectionName: randomUUID().replaceAll("-", ""),
523+
experimental: {}
524+
};
525+
526+
relationCollectionId = await insertCollection(
527+
[],
528+
relationOptions,
529+
superuserToken
530+
);
531+
});
532+
533+
afterEach(async () => {
534+
await deleteCollection(testOptions, superuserToken);
535+
await deleteCollection(relationOptions, superuserToken);
536+
});
537+
538+
test("should expand single relation", async () => {
539+
await insertCollection(
540+
[
541+
{
542+
type: "relation",
543+
name: "singleRelation",
544+
collectionId: relationCollectionId,
545+
maxSelect: 1
546+
}
547+
],
548+
testOptions,
549+
superuserToken
550+
);
551+
552+
const relationEntry = await insertEntry(
553+
{},
554+
relationOptions,
555+
superuserToken
556+
);
557+
const entry = await insertEntry(
558+
{
559+
singleRelation: relationEntry.id
560+
},
561+
testOptions,
562+
superuserToken
563+
);
564+
565+
const result: Array<Record<string, any>> = [];
566+
await fetchCollection(
567+
testOptions,
568+
async (e) => {
569+
result.push(...e);
570+
},
571+
superuserToken,
572+
undefined
573+
);
574+
575+
expect(result).toHaveLength(1);
576+
expect(result[0].id).toBe(entry.id);
577+
expect(result[0].expand.singleRelation.id).toBe(relationEntry.id);
578+
});
579+
580+
test("should expand multi relation", async () => {
581+
await insertCollection(
582+
[
583+
{
584+
type: "relation",
585+
name: "multiRelation",
586+
collectionId: relationCollectionId,
587+
maxSelect: 2
588+
}
589+
],
590+
testOptions,
591+
superuserToken
592+
);
593+
594+
const relationEntries = await insertEntries(
595+
[{}, {}],
596+
relationOptions,
597+
superuserToken
598+
);
599+
const relationIds = relationEntries.map((entry) => entry.id);
600+
const entry = await insertEntry(
601+
{
602+
multiRelation: relationIds
603+
},
604+
testOptions,
605+
superuserToken
606+
);
607+
608+
const result: Array<Record<string, any>> = [];
609+
await fetchCollection(
610+
{ ...testOptions, experimental: { expand: ["multiRelation"] } },
611+
async (e) => {
612+
result.push(...e);
613+
},
614+
superuserToken,
615+
undefined
616+
);
617+
618+
expect(result).toHaveLength(1);
619+
expect(result[0].id).toBe(entry.id);
620+
621+
const multiRelation = (
622+
result[0].expand as { multiRelation: Array<{ id: string }> }
623+
).multiRelation;
624+
expect(multiRelation).toBeInstanceOf(Array);
625+
expect(multiRelation).toHaveLength(2);
626+
expect(multiRelation.map((entry) => entry.id)).toEqual(relationIds);
627+
});
628+
});
497629
});

test/loader/fetch-entry.e2e-spec.ts

Lines changed: 114 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { LiveEntryNotFoundError } from "astro/content/runtime";
22
import { randomUUID } from "crypto";
3-
import { describe, expect, inject, test } from "vitest";
3+
import { afterEach, beforeEach, describe, expect, inject, test } from "vitest";
4+
import type { ExperimentalPocketBaseLiveLoaderOptions } from "../../src";
45
import { fetchEntry } from "../../src/loader/fetch-entry";
56
import { PocketBaseAuthenticationError } from "../../src/types/errors";
67
import { createLoaderOptions } from "../_mocks/create-loader-options";
78
import { deleteCollection } from "../_mocks/delete-collection";
89
import { insertCollection } from "../_mocks/insert-collection";
9-
import { insertEntry } from "../_mocks/insert-entry";
10+
import { insertEntries, insertEntry } from "../_mocks/insert-entry";
1011

1112
describe("fetchEntry", () => {
1213
const options = createLoaderOptions({ collectionName: "_superusers" });
@@ -195,4 +196,115 @@ describe("fetchEntry", () => {
195196
await deleteCollection(testOptions, superuserToken);
196197
});
197198
});
199+
200+
describe("expand parameter", () => {
201+
let testOptions: ExperimentalPocketBaseLiveLoaderOptions;
202+
let relationOptions: ExperimentalPocketBaseLiveLoaderOptions;
203+
let relationCollectionId: string;
204+
205+
beforeEach(async () => {
206+
testOptions = {
207+
...options,
208+
collectionName: randomUUID().replaceAll("-", ""),
209+
experimental: {
210+
expand: ["singleRelation"]
211+
}
212+
};
213+
relationOptions = {
214+
...options,
215+
collectionName: randomUUID().replaceAll("-", ""),
216+
experimental: {}
217+
};
218+
219+
relationCollectionId = await insertCollection(
220+
[],
221+
relationOptions,
222+
superuserToken
223+
);
224+
});
225+
226+
afterEach(async () => {
227+
await deleteCollection(testOptions, superuserToken);
228+
await deleteCollection(relationOptions, superuserToken);
229+
});
230+
231+
test("should expand single relation", async () => {
232+
await insertCollection(
233+
[
234+
{
235+
type: "relation",
236+
name: "singleRelation",
237+
collectionId: relationCollectionId,
238+
maxSelect: 1
239+
}
240+
],
241+
testOptions,
242+
superuserToken
243+
);
244+
245+
const relationEntry = await insertEntry(
246+
{},
247+
relationOptions,
248+
superuserToken
249+
);
250+
const entry = await insertEntry(
251+
{
252+
singleRelation: relationEntry.id
253+
},
254+
testOptions,
255+
superuserToken
256+
);
257+
258+
const result = await fetchEntry(entry.id, testOptions, superuserToken);
259+
260+
expect(result).toBeDefined();
261+
expect(result.expand).toBeDefined();
262+
expect((result.expand as any).singleRelation.id).toBe(relationEntry.id);
263+
});
264+
265+
test("should expand multi relation", async () => {
266+
await insertCollection(
267+
[
268+
{
269+
type: "relation",
270+
name: "multiRelation",
271+
collectionId: relationCollectionId,
272+
maxSelect: 2
273+
}
274+
],
275+
testOptions,
276+
superuserToken
277+
);
278+
279+
const relationEntries = await insertEntries(
280+
[{}, {}],
281+
relationOptions,
282+
superuserToken
283+
);
284+
const relationIds = relationEntries.map((entry) => entry.id);
285+
const entry = await insertEntry(
286+
{
287+
multiRelation: relationIds
288+
},
289+
testOptions,
290+
superuserToken
291+
);
292+
293+
const result = await fetchEntry(
294+
entry.id,
295+
{ ...testOptions, experimental: { expand: ["multiRelation"] } },
296+
superuserToken
297+
);
298+
299+
expect(result).toBeDefined();
300+
expect(result.expand).toBeDefined();
301+
302+
const multiRelation = (
303+
result.expand as { multiRelation: Array<{ id: string }> }
304+
).multiRelation;
305+
expect(multiRelation).toBeInstanceOf(Array);
306+
expect(multiRelation).toHaveLength(2);
307+
expect(multiRelation.map((entry) => entry.id)).toEqual(relationIds);
308+
});
309+
});
198310
});

0 commit comments

Comments
 (0)