|
1 | 1 | import { LiveEntryNotFoundError } from "astro/content/runtime"; |
2 | 2 | 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"; |
4 | 5 | import { fetchEntry } from "../../src/loader/fetch-entry"; |
5 | 6 | import { PocketBaseAuthenticationError } from "../../src/types/errors"; |
6 | 7 | import { createLoaderOptions } from "../_mocks/create-loader-options"; |
7 | 8 | import { deleteCollection } from "../_mocks/delete-collection"; |
8 | 9 | import { insertCollection } from "../_mocks/insert-collection"; |
9 | | -import { insertEntry } from "../_mocks/insert-entry"; |
| 10 | +import { insertEntries, insertEntry } from "../_mocks/insert-entry"; |
10 | 11 |
|
11 | 12 | describe("fetchEntry", () => { |
12 | 13 | const options = createLoaderOptions({ collectionName: "_superusers" }); |
@@ -195,4 +196,115 @@ describe("fetchEntry", () => { |
195 | 196 | await deleteCollection(testOptions, superuserToken); |
196 | 197 | }); |
197 | 198 | }); |
| 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 | + }); |
198 | 310 | }); |
0 commit comments