Skip to content

Commit

Permalink
add map<id, data> yaml test
Browse files Browse the repository at this point in the history
  • Loading branch information
rgodha24 committed Sep 25, 2024
1 parent 6052036 commit 340778f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 13 deletions.
4 changes: 2 additions & 2 deletions packages/astro/src/content/loaders/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface FileOptions {
* */
parser?: (
text: string,
) => Record<string, unknown> | Array<{ id: string } & Record<string, unknown>>;
) => Record<string, Record<string, unknown>> | Array<Record<string, unknown>>;
}

/**
Expand Down Expand Up @@ -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<Record<string, unknown>>;
let data: Array<Record<string, unknown>> | Record<string, Record<string, unknown>>;

try {
const contents = await fs.readFile(filePath, 'utf-8');
Expand Down
51 changes: 51 additions & 0 deletions packages/astro/test/content-layer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
21 changes: 11 additions & 10 deletions packages/astro/test/fixtures/content-layer/src/data/fish.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 340778f

Please sign in to comment.