Skip to content

Commit

Permalink
test: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Princesseuh committed Aug 12, 2024
1 parent dabf08a commit 0d2262f
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 0 deletions.
80 changes: 80 additions & 0 deletions packages/astro/test/content-intellisense.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { loadFixture } from './test-utils.js';

describe('Content Intellisense', () => {
/** @type {import("./test-utils.js").Fixture} */
let fixture;

/** @type {string[]} */
let collectionsDir = [];

/** @type {{collections: {hasSchema: boolean, name: string}[], entries: Record<string, string>}} */
let manifest = undefined;

before(async () => {
fixture = await loadFixture({ root: './fixtures/content-Intellisense/' });
await fixture.build();

collectionsDir = await fixture.readdir('../.astro/collections');
manifest = JSON.parse(await fixture.readFile('../.astro/collections/collections.json'));
});

it('generate JSON schemas for content collections', async () => {
assert.deepEqual(collectionsDir.includes('blog-cc.schema.json'), true);
});

it('generate JSON schemas for content layer', async () => {
assert.deepEqual(collectionsDir.includes('blog-cl.schema.json'), true);
});

it('manifest exists', async () => {
assert.notEqual(manifest, undefined);
});

it('manifest has content collections', async () => {
const manifestCollections = manifest.collections.map((collection) => collection.name);
assert.equal(
manifestCollections.includes('blog-cc'),
true,
"Expected 'blog-cc' collection in manifest",
);
});

it('manifest has content layer', async () => {
const manifestCollections = manifest.collections.map((collection) => collection.name);
assert.equal(
manifestCollections.includes('blog-cl'),
true,
"Expected 'blog-cl' collection in manifest",
);
});

it('has entries for content collections', async () => {
const collectionEntries = Object.entries(manifest.entries).filter((entry) =>
entry[0].includes(
'/astro/packages/astro/test/fixtures/content-intellisense/src/content/blog-cc/',
),
);
assert.equal(collectionEntries.length, 3, "Expected 3 entries for 'blog-cc' collection");
assert.equal(
collectionEntries.every((entry) => entry[1] === 'blog-cc'),
true,
"Expected 3 entries for 'blog-cc' collection to have 'blog-cc' as collection",
);
});

it('has entries for content layer', async () => {
const collectionEntries = Object.entries(manifest.entries).filter((entry) =>
entry[0].includes(
'/astro/packages/astro/test/fixtures/content-intellisense/src/content/blog-cl/',
),
);
assert.equal(collectionEntries.length, 3, "Expected 3 entries for 'blog-cl' collection");
assert.equal(
collectionEntries.every((entry) => entry[1] === 'blog-cl'),
true,
"Expected 3 entries for 'blog-cl' collection to have 'blog-cl' as collection name",
);
});
});
12 changes: 12 additions & 0 deletions packages/astro/test/fixtures/content-intellisense/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import markdoc from "@astrojs/markdoc";
import mdx from '@astrojs/mdx';
import { defineConfig } from 'astro/config';

// https://astro.build/config
export default defineConfig({
integrations: [mdx(), markdoc()],
experimental: {
contentLayer: true,
contentIntellisense: true
}
});
10 changes: 10 additions & 0 deletions packages/astro/test/fixtures/content-intellisense/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@test/content-intellisense",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*",
"@astrojs/mdx": "workspace:*",
"@astrojs/markdoc": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: "Markdown"
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: "MDX"
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: "Markdoc"
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: "Markdown"
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: "MDX"
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: "Markdoc"
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { glob } from 'astro/loaders';
import { defineCollection, z } from 'astro:content';

const blogCC = defineCollection({
type: 'content',
schema: z.object({
title: z.string(),
description: z.string().optional(),
}),
});

const blogCL = defineCollection({
// By default the ID is a slug, generated from the path of the file relative to `base`
loader: glob({ pattern: "**/*", base: "./src/blog-cl" }),
schema: z.object({
title: z.string(),
description: z.string().optional(),
}),
});

export const collections = {
"blog-cc": blogCC,
"blog-cl": blogCL,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function stripRenderFn(entryWithRender) {
const { render, ...entry } = entryWithRender;
return entry;
}

export function stripAllRenderFn(collection = []) {
return collection.map(stripRenderFn);
}
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0d2262f

Please sign in to comment.