Skip to content

Commit

Permalink
add nested json test
Browse files Browse the repository at this point in the history
  • Loading branch information
rgodha24 committed Sep 20, 2024
1 parent d3056dd commit 16a46cb
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/astro/test/content-layer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ describe('Content Layer', () => {
]);
});

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));
Expand Down
13 changes: 13 additions & 0 deletions packages/astro/test/fixtures/content-layer/src/content/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ const fish = defineCollection({
}),
});

const birds = defineCollection({
loader: file('src/data/birds.json', {
parser: (text) => JSON.parse(text).birds,
}),
schema: z.object({
id: z.string(),
name: z.string(),
breed: z.string(),
age: z.number(),
}),
});

// Absolute paths should also work
const absoluteRoot = new URL('../../content-outside-src', import.meta.url);

Expand Down Expand Up @@ -164,6 +176,7 @@ export const collections = {
dogs,
cats,
fish,
birds,
numbers,
spacecraft,
increment,
Expand Down
34 changes: 34 additions & 0 deletions packages/astro/test/fixtures/content-layer/src/data/birds.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"birds": [
{
"id": "bluejay",
"name": "Blue Jay",
"breed": "Cyanocitta cristata",
"age": 3
},
{
"id": "robin",
"name": "Robin",
"breed": "Turdus migratorius",
"age": 2
},
{
"id": "sparrow",
"name": "Sparrow",
"breed": "Passer domesticus",
"age": 1
},
{
"id": "cardinal",
"name": "Cardinal",
"breed": "Cardinalis cardinalis",
"age": 4
},
{
"id": "goldfinch",
"name": "Goldfinch",
"breed": "Spinus tristis",
"age": 2
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export async function GET() {

const tomlLoader = await getCollection('songs');

const nestedJsonLoader = await getCollection('birds');

return new Response(
devalue.stringify({
customLoader,
Expand All @@ -37,6 +39,7 @@ export async function GET() {
images,
yamlLoader,
tomlLoader,
nestedJsonLoader,
}),
);
}

0 comments on commit 16a46cb

Please sign in to comment.