From 340778f303eb7fce21c1e8e174c6980dea0af29f Mon Sep 17 00:00:00 2001 From: Rohan Godha Date: Tue, 24 Sep 2024 14:52:59 -0400 Subject: [PATCH] add map yaml test --- packages/astro/src/content/loaders/file.ts | 4 +- packages/astro/test/content-layer.test.js | 51 +++++++++++++++++++ .../content-layer/src/content/config.ts | 1 - .../fixtures/content-layer/src/data/fish.yaml | 21 ++++---- 4 files changed, 64 insertions(+), 13 deletions(-) diff --git a/packages/astro/src/content/loaders/file.ts b/packages/astro/src/content/loaders/file.ts index ed1701e11108..d109f95b6994 100644 --- a/packages/astro/src/content/loaders/file.ts +++ b/packages/astro/src/content/loaders/file.ts @@ -11,7 +11,7 @@ export interface FileOptions { * */ parser?: ( text: string, - ) => Record | Array<{ id: string } & Record>; + ) => Record> | Array>; } /** @@ -46,7 +46,7 @@ export function file(fileName: string, options?: FileOptions): Loader { } async function syncData(filePath: string, { logger, parseData, store, config }: LoaderContext) { - let data: Array>; + let data: Array> | Record>; try { const contents = await fs.readFile(filePath, 'utf-8'); diff --git a/packages/astro/test/content-layer.test.js b/packages/astro/test/content-layer.test.js index b37c65507ed8..97d4925ca1e5 100644 --- a/packages/astro/test/content-layer.test.js +++ b/packages/astro/test/content-layer.test.js @@ -95,6 +95,57 @@ describe('Content Layer', () => { json.probes.every(({ id }) => !id.startsWith('voyager')), 'Voyager probes should not be included', ); + + it('Returns nested json `file()` loader collection', async () => { + assert.ok(json.hasOwnProperty('nestedJsonLoader')); + assert.ok(Array.isArray(json.nestedJsonLoader)); + + const ids = json.nestedJsonLoader.map((item) => item.data.id); + assert.deepEqual(ids, ['bluejay', 'robin', 'sparrow', 'cardinal', 'goldfinch']); + }); + + it('Returns yaml `file()` loader collection', async () => { + assert.ok(json.hasOwnProperty('yamlLoader')); + assert.ok(Array.isArray(json.yamlLoader)); + + const ids = json.yamlLoader.map((item) => item.data.id); + assert.deepEqual(ids, [ + 'bubbles', + 'finn', + 'shadow', + 'spark', + 'splash', + 'nemo', + 'angel-fish', + 'gold-stripe', + 'blue-tail', + 'bubble-buddy', + ]); + }); + + it('Returns toml `file()` loader collection', async () => { + assert.ok(json.hasOwnProperty('tomlLoader')); + assert.ok(Array.isArray(json.tomlLoader)); + + const ids = json.tomlLoader.map((item) => item.data.id); + assert.deepEqual(ids, [ + 'crown', + 'nikes-on-my-feet', + 'stars', + 'never-let-me-down', + 'no-church-in-the-wild', + 'family-ties', + 'somebody', + 'honest', + ]); + }); + + it('Returns nested json `file()` loader collection', async () => { + assert.ok(json.hasOwnProperty('nestedJsonLoader')); + assert.ok(Array.isArray(json.nestedJsonLoader)); + + const ids = json.nestedJsonLoader.map((item) => item.data.id); + assert.deepEqual(ids, ['bluejay', 'robin', 'sparrow', 'cardinal', 'goldfinch']); }); it('Returns data entry by id', async () => { diff --git a/packages/astro/test/fixtures/content-layer/src/content/config.ts b/packages/astro/test/fixtures/content-layer/src/content/config.ts index 4b05f4e4dce8..58f368349300 100644 --- a/packages/astro/test/fixtures/content-layer/src/content/config.ts +++ b/packages/astro/test/fixtures/content-layer/src/content/config.ts @@ -122,7 +122,6 @@ const cats = defineCollection({ const fish = defineCollection({ loader: file('src/data/fish.yaml'), schema: z.object({ - id: z.string(), name: z.string(), breed: z.string(), age: z.number(), diff --git a/packages/astro/test/fixtures/content-layer/src/data/fish.yaml b/packages/astro/test/fixtures/content-layer/src/data/fish.yaml index 5a49d9983c03..a9ac4e4352b4 100644 --- a/packages/astro/test/fixtures/content-layer/src/data/fish.yaml +++ b/packages/astro/test/fixtures/content-layer/src/data/fish.yaml @@ -1,40 +1,41 @@ -- id: "bubbles" +# map of ids to data +bubbles: name: "Bubbles" breed: "Goldfish" age: 2 -- id: "finn" +finn: name: "Finn" breed: "Betta" age: 1 -- id: "shadow" +shadow: name: "Shadow" breed: "Catfish" age: 3 -- id: "spark" +spark: name: "Spark" breed: "Tetra" age: 1 -- id: "splash" +splash: name: "Splash" breed: "Guppy" age: 2 -- id: "nemo" +nemo: name: "Nemo" breed: "Clownfish" age: 3 -- id: "angel-fish" +angel-fish: name: "Angel Fish" breed: "Angelfish" age: 4 -- id: "gold-stripe" +gold-stripe: name: "Gold Stripe" breed: "Molly" age: 1 -- id: "blue-tail" +blue-tail: name: "Blue Tail" breed: "Swordtail" age: 2 -- id: "bubble-buddy" +bubble-buddy: name: "Bubble Buddy" breed: "Betta" age: 3