Skip to content

Commit

Permalink
test(prettier): add simple prettier tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nate Moore committed Apr 20, 2021
1 parent 2bd1154 commit 4d54f8b
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 42 deletions.
72 changes: 32 additions & 40 deletions test/astro-prettier.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,40 @@ import { setup } from './helpers.js';

const Prettier = suite('Prettier formatting');

setup(Prettier, './fixtures/astro-expr');

Prettier('Can load page', async ({ readSrcFile }) => {
const src = await readSrcFile('/index.astro');
assert.not.type(src, 'undefined');

const result = format(src);
assert.not.type(result, 'undefined');
setup(Prettier, './fixtures/astro-prettier');

/**
* Utility to get `[src, out]` files
* @param name {string}
* @param ctx {any}
*/
const getFiles = async (name, { readFile }) => {
const [src, out] = await Promise.all([readFile(`/in/${name}.astro`), readFile(`/out/${name}.astro`)]);
return [src, out];
}

Prettier('can format a basic Astro file', async (ctx) => {
const [src, out] = await getFiles('basic', ctx);
assert.not.equal(src, out);

const formatted = format(src);
assert.equal(formatted, out);
});

// Prettier('Ignores characters inside of strings', async ({ runtime }) => {
// const result = await runtime.load('/strings');

// assert.equal(result.statusCode, 200);

// const $ = doc(result.contents);

// for (let col of ['red', 'yellow', 'blue']) {
// assert.equal($('#' + col).length, 1);
// }
// });

// Prettier('Ignores characters inside of line comments', async ({ runtime }) => {
// const result = await runtime.load('/line-comments');
// assert.equal(result.statusCode, 200);

// const $ = doc(result.contents);

// for (let col of ['red', 'yellow', 'blue']) {
// assert.equal($('#' + col).length, 1);
// }
// });

// Prettier('Ignores characters inside of multiline comments', async ({ runtime }) => {
// const result = await runtime.load('/multiline-comments');
// assert.equal(result.statusCode, 200);

// const $ = doc(result.contents);
Prettier('can format an Astro file with frontmatter', async (ctx) => {
const [src, out] = await getFiles('frontmatter', ctx);
assert.not.equal(src, out);

const formatted = format(src);
assert.equal(formatted, out);
});

// for (let col of ['red', 'yellow', 'blue']) {
// assert.equal($('#' + col).length, 1);
// }
// });
Prettier('can format an Astro file with embedded JSX expressions', async (ctx) => {
const [src, out] = await getFiles('embedded-expr', ctx);
assert.not.equal(src, out);

const formatted = format(src);
assert.equal(formatted, out);
});

Prettier.run();
13 changes: 13 additions & 0 deletions test/fixtures/astro-prettier/in/basic.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!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>Document</title>
</head>
<body>
<h1>
Hello world!</h1>
</body>
</html>
23 changes: 23 additions & 0 deletions test/fixtures/astro-prettier/in/embedded-expr.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
import Color from '../components/Color.jsx';
let title =
'My Site';
const colors = ['red', 'yellow', 'blue',];
---





<html lang="en">
<head>
<title>My site</title>
</head>
<body>
<h1>{title}</h1>

{colors.map(color => <div><Color name={color} /></div>)}
</body>
</html>
18 changes: 18 additions & 0 deletions test/fixtures/astro-prettier/in/frontmatter.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
import Color from '../components/Color.jsx';
let title =
'My Site';
const colors = ['red', 'yellow', 'blue',];
---


<html lang="en">
<head>
<title>My site</title>
</head>
<body>
<h1>{title}</h1>
</body>
</html>
12 changes: 12 additions & 0 deletions test/fixtures/astro-prettier/out/basic.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!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>Document</title>
</head>
<body>
<h1>Hello world!</h1>
</body>
</html>
22 changes: 22 additions & 0 deletions test/fixtures/astro-prettier/out/embedded-expr.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
import Color from "../components/Color.jsx";
let title = "My Site";
const colors = ["red", "yellow", "blue"];
---

<html lang="en">
<head>
<title>My site</title>
</head>
<body>
<h1>{title}</h1>

{colors.map((color) => (
<div>
<Color name={color} />
</div>
))}
</body>
</html>
16 changes: 16 additions & 0 deletions test/fixtures/astro-prettier/out/frontmatter.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
import Color from "../components/Color.jsx";
let title = "My Site";
const colors = ["red", "yellow", "blue"];
---

<html lang="en">
<head>
<title>My site</title>
</head>
<body>
<h1>{title}</h1>
</body>
</html>
4 changes: 2 additions & 2 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export function setup(Suite, fixturePath) {
}

context.runtime = runtime;
context.readSrcFile = async (path) => {
const resolved = fileURLToPath(new URL(`${fixturePath}/astro/pages${path}`, import.meta.url));
context.readFile = async (path) => {
const resolved = fileURLToPath(new URL(`${fixturePath}${path}`, import.meta.url));
return readFile(resolved).then(r => r.toString('utf-8'));
}
});
Expand Down

0 comments on commit 4d54f8b

Please sign in to comment.