Skip to content

Commit

Permalink
test: frontmatter injection in layout
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Aug 10, 2022
1 parent 6ae2e4f commit 8f0d1e9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
const defaults = { title: 'Frontmatter not passed to layout!' }
const { frontmatter = defaults, content = defaults } = Astro.props;
---

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{frontmatter.title ?? content.title}</title>
</head>
<body>
<slot />
</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
layout: '../layouts/Base.astro'
---

# Page 1

Look at that!
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
layout: '../layouts/Base.astro'
---

# Page 2

## Table of contents
Expand Down
12 changes: 12 additions & 0 deletions packages/integrations/mdx/test/mdx-frontmatter-injection.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai';
import { parseHTML } from 'linkedom';
import { loadFixture } from '../../../astro/test/test-utils.js';

const FIXTURE_ROOT = new URL('./fixtures/mdx-frontmatter-injection/', import.meta.url);
Expand Down Expand Up @@ -41,4 +42,15 @@ describe('MDX frontmatter injection', () => {
expect(titles).to.contain('Overridden title');
expect(readingTimes).to.contain('1000 min read');
});

it('passes injected frontmatter to layouts', async () => {
const html1 = await fixture.readFile('/page-1/index.html');
const html2 = await fixture.readFile('/page-2/index.html');

const title1 = parseHTML(html1).document.querySelector('title');
const title2 = parseHTML(html2).document.querySelector('title');

expect(title1.innerHTML).to.equal('Page 1');
expect(title2.innerHTML).to.equal('Page 2');
});
});

0 comments on commit 8f0d1e9

Please sign in to comment.