Skip to content

Commit

Permalink
chore: use a new fixture to fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Aug 7, 2024
1 parent 9a895fd commit f80e5b4
Show file tree
Hide file tree
Showing 20 changed files with 223 additions and 31 deletions.
2 changes: 1 addition & 1 deletion packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
"test:e2e:chrome": "playwright test",
"test:e2e:firefox": "playwright test --config playwright.firefox.config.js",
"test:types": "tsc --project tsconfig.tests.json",
"test:node": "astro-scripts test --parallel \"test/**/*.test.js\""
"test:node": "astro-scripts test \"test/**/*.test.js\""
},
"dependencies": {
"@astrojs/compiler": "^2.10.1",
Expand Down
1 change: 0 additions & 1 deletion packages/astro/src/content/data-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ export class DataStore {
}

hasCollection(collectionName: string) {
console.log(this.#collections.keys());
return this.#collections.has(collectionName);
}

Expand Down
54 changes: 27 additions & 27 deletions packages/astro/test/content-layer-render.test.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';

import { loadFixture } from './test-utils.js';
describe('Content Layer', () => {

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

describe('Dev', () => {
let devServer;
before(async () => {
fixture = await loadFixture({ root: './fixtures/content-layer/' });
devServer = await fixture.startDevServer();
});
let devServer;
before(async () => {
fixture = await loadFixture({ root: './fixtures/content-layer-rendering/' });
devServer = await fixture.startDevServer();
});

after(async () => {
devServer?.stop();
});
after(async () => {
devServer?.stop();
});

it('Render an MDX file', async () => {
const html = await fixture.fetch('/reptiles/iguana').then((r) => r.text());
it('Render an MDX file', async () => {
const html = await fixture.fetch('/reptiles/iguana').then((r) => r.text());

assert.match(html, /Iguana/);
assert.match(html, /This is a rendered entry/);
});
assert.match(html, /Iguana/);
assert.match(html, /This is a rendered entry/);
});
});

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

describe('Content Layer build', () => {
/** @type {import("./test-utils.js").Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({ root: './fixtures/content-layer/' });
fixture = await loadFixture({ root: './fixtures/content-layer/' });
await fixture.build();
});

it('Render an MDX file', async () => {
const html = await fixture.readFile('/reptiles/iguana/index.html');
it('Render an MDX file', async () => {
const html = await fixture.readFile('/reptiles/iguana/index.html');

assert.match(html, /Iguana/);
assert.match(html, /This is a rendered entry/);
});
assert.match(html, /Iguana/);
assert.match(html, /This is a rendered entry/);
});
});
4 changes: 2 additions & 2 deletions packages/astro/test/content-layer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ describe('Content Layer', () => {
assert.equal(json.increment.data.lastValue, 1);
await fixture.build();
const newJson = devalue.parse(await fixture.readFile('/collections.json'));
assert.equal(newJson.increment.data.lastValue, 3);
assert.equal(newJson.increment.data.lastValue, 2);
});

it('clears the store on new build with force flag', async () => {
let newJson = devalue.parse(await fixture.readFile('/collections.json'));
assert.equal(newJson.increment.data.lastValue, 3);
assert.equal(newJson.increment.data.lastValue, 2);
await fixture.build({ force: true }, {});
newJson = devalue.parse(await fixture.readFile('/collections.json'));
assert.equal(newJson.increment.data.lastValue, 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import mdx from '@astrojs/mdx';
import { defineConfig } from 'astro/config';
import { fileURLToPath } from 'node:url';

export default defineConfig({
integrations: [mdx()],
vite: {
resolve: {
alias: {
'@images': fileURLToPath(new URL('./images', import.meta.url))
}
},
}
});
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@test/content-layer-rendering",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*",
"@astrojs/mdx": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

<h2 data-components-export-applied="true"><slot /></h2>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<style>h3 { margin: 1rem }</style>
<h3><slot /></h3>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
import { CollectionEntry, getCollection } from 'astro:content';
import H3 from './H3.astro'
// Test for recursive `getCollection()` calls
const blog = await getCollection('blog');
type Props = {
content: CollectionEntry<'blog'>['data'];
};
const {
content: { title },
} = Astro.props;
---

<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<title>With Layout Prop</title>
</head>
<body data-layout-prop="true">
<h1>{title}</h1>
<H3>H3 inserted in the layout</H3>
<nav>
<ul>
{blog.map((post) => (
<li>
<a href={post.slug}>{post.data.title}</a>
</li>
))}
</ul>
</nav>
<slot />
</body>
</html>

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<p id="update-me">Hoisted script didn't update me :(</p>

<p id="update-me-inline">Inline script didn't update me :(</p>

<script>
document.querySelector('#update-me').innerText = 'Updated client-side with hoisted script!';
</script>

<script is:inline data-is-inline>
document.querySelector('#update-me-inline').innerText = 'Updated client-side with inline script!';
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { defineCollection, z, reference } from 'astro:content';
import { file, glob } from 'astro/loaders';

const reptiles = defineCollection({
type: 'experimental_content',
loader: glob({
pattern: '*.mdx',
base: new URL('../../content-outside-src-mdx', import.meta.url),
}),
schema: () =>
z.object({
title: z.string(),
description: z.string(),
publishedDate: z.coerce.date(),
tags: z.array(z.string()),
}),
});

export const collections = { reptiles };
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { getCollection, getEntry } from 'astro:content';
import * as devalue from 'devalue';

export async function GET() {
const customLoader = await getCollection('blog', (entry) => {
return entry.data.id < 6;
});
const fileLoader = await getCollection('dogs');

const dataEntry = await getEntry('dogs', 'beagle');

const simpleLoader = await getCollection('cats');

const entryWithReference = await getEntry('spacecraft', 'columbia-copy');
const referencedEntry = await getEntry(entryWithReference.data.cat);

const increment = await getEntry('increment', 'value');

return new Response(
devalue.stringify({
customLoader,
fileLoader,
dataEntry,
simpleLoader,
entryWithReference,
referencedEntry,
increment,
})
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
import { getCollection, getDataEntryById } from 'astro:content';
const blog = await getCollection('blog');
const first = await getDataEntryById('blog', 1);
const dogs = await getCollection('dogs');
const reptiles = await getCollection('reptiles');
---
<html>
<head>
<title>Index</title>
</head>
<body>

<h1>Dogs</h1>
<ul>
{dogs.map(dog => (
<li><a href={`/dogs/${dog.id}`}>{ dog.data?.breed }</a></li>
))}
</ul>
<h1>Blog Posts</h1>

<h2>{first.data.title}</h2>
<ul>
{blog.map(post => (
<li><a href={`/blog/${post.id}`}>{ post.data?.title }</a></li>
))}
</ul>

</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
import type { GetStaticPaths } from "astro";
import { getCollection, render } from "astro:content"
import { Image } from "astro:assets"
export const getStaticPaths = (async () => {
const collection = await getCollection("reptiles");
if(!collection) return []
return collection.map((reptile) => ({
params: {
slug: `/${reptile.id}`
},
props: {
reptile
}
}));
}) satisfies GetStaticPaths;
const { reptile } = Astro.props as any
const { Content } = await render(reptile)
---
<meta charset="utf-8">
<h1>{reptile.data.title}</h1>
<Content />
9 changes: 9 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 f80e5b4

Please sign in to comment.