Skip to content

Commit 108414a

Browse files
authored
Merge pull request #65 from bertdeblock/acceptance-test-generator
Add an `acceptance-test` generator
2 parents a3b785a + b0dbf8e commit 108414a

File tree

6 files changed

+156
-2
lines changed

6 files changed

+156
-2
lines changed

src/generator.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ export function defineGenerator({
129129
Math.max(...templateCompiled.split("\n").map((line) => line.length)),
130130
);
131131

132+
consola.log(border);
133+
consola.log(targetFile.path());
132134
consola.log(border);
133135
consola.log("");
134136
consola.log(templateCompiled);
@@ -177,13 +179,17 @@ export function defineGenerator({
177179
}
178180

179181
export function defineTestGenerator(
180-
options: GeneratorOptions & { testsDir: string },
182+
options: GeneratorOptions & { testsDir: string; testsSubDir?: string },
181183
): Generator {
182184
return defineGenerator({
183185
...options,
184186
modifyTargetFile: (targetFile, args) => {
185187
if (args.path === undefined) {
186-
targetFile.subDir = join("tests", options.testsDir, options.name + "s");
188+
targetFile.subDir = join(
189+
"tests",
190+
options.testsDir,
191+
options.testsSubDir ?? options.name + "s",
192+
);
187193
}
188194

189195
targetFile.name += "-test";

src/generators.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,11 @@ export const generators: Generator[] = [
6262
name: "service",
6363
testsDir: "unit",
6464
}),
65+
66+
defineTestGenerator({
67+
args: [name(), path(), typescript()],
68+
name: "acceptance",
69+
testsDir: "acceptance",
70+
testsSubDir: "",
71+
}),
6572
];
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { currentURL, visit } from '@ember/test-helpers';
2+
import { module, test } from 'qunit';
3+
import { setupApplicationTest } from '{{testHelpersImportPath}}';
4+
5+
module('Acceptance | {{name.path}}', function (hooks) {
6+
setupApplicationTest(hooks);
7+
8+
test('it visits /{{name.path}}', async function (assert) {
9+
await visit('/{{name.path}}');
10+
11+
assert.strictEqual(currentURL(), '/{{name.path}}');
12+
});
13+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { currentURL, visit } from '@ember/test-helpers';
2+
import { module, test } from 'qunit';
3+
import { setupApplicationTest } from '{{testHelpersImportPath}}';
4+
5+
module('Acceptance | {{name.path}}', function (hooks) {
6+
setupApplicationTest(hooks);
7+
8+
test('it visits /{{name.path}}', async function (assert) {
9+
await visit('/{{name.path}}');
10+
11+
assert.strictEqual(currentURL(), '/{{name.path}}');
12+
});
13+
});
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`generates a \`.js\` acceptance-test 1`] = `
4+
"import { currentURL, visit } from '@ember/test-helpers';
5+
import { module, test } from 'qunit';
6+
import { setupApplicationTest } from 'v2-addon/tests/helpers';
7+
8+
module('Acceptance | foo', function (hooks) {
9+
setupApplicationTest(hooks);
10+
11+
test('it visits /foo', async function (assert) {
12+
await visit('/foo');
13+
14+
assert.strictEqual(currentURL(), '/foo');
15+
});
16+
});
17+
"
18+
`;
19+
20+
exports[`generates a \`.js\` acceptance-test at a custom path 1`] = `
21+
"import { currentURL, visit } from '@ember/test-helpers';
22+
import { module, test } from 'qunit';
23+
import { setupApplicationTest } from 'v2-addon/tests/helpers';
24+
25+
module('Acceptance | foo', function (hooks) {
26+
setupApplicationTest(hooks);
27+
28+
test('it visits /foo', async function (assert) {
29+
await visit('/foo');
30+
31+
assert.strictEqual(currentURL(), '/foo');
32+
});
33+
});
34+
"
35+
`;
36+
37+
exports[`generates a \`.ts\` acceptance-test 1`] = `
38+
"import { currentURL, visit } from '@ember/test-helpers';
39+
import { module, test } from 'qunit';
40+
import { setupApplicationTest } from 'v2-addon/tests/helpers';
41+
42+
module('Acceptance | foo', function (hooks) {
43+
setupApplicationTest(hooks);
44+
45+
test('it visits /foo', async function (assert) {
46+
await visit('/foo');
47+
48+
assert.strictEqual(currentURL(), '/foo');
49+
});
50+
});
51+
"
52+
`;
53+
54+
exports[`generates a \`.ts\` acceptance-test at a custom path 1`] = `
55+
"import { currentURL, visit } from '@ember/test-helpers';
56+
import { module, test } from 'qunit';
57+
import { setupApplicationTest } from 'v2-addon/tests/helpers';
58+
59+
module('Acceptance | foo', function (hooks) {
60+
setupApplicationTest(hooks);
61+
62+
test('it visits /foo', async function (assert) {
63+
await visit('/foo');
64+
65+
assert.strictEqual(currentURL(), '/foo');
66+
});
67+
});
68+
"
69+
`;

test/acceptance-test.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { afterEach, it } from "vitest";
2+
import { Package } from "./helpers.ts";
3+
4+
let pkg: Package;
5+
6+
afterEach(() => pkg.cleanUp());
7+
8+
it("generates a `.js` acceptance-test", async (ctx) => {
9+
pkg = await Package.create("v2-addon");
10+
11+
await pkg.gember("acceptance-test", "foo");
12+
13+
const content = await pkg.readFile("tests/acceptance/foo-test.js");
14+
15+
ctx.expect(content).toMatchSnapshot();
16+
});
17+
18+
it("generates a `.js` acceptance-test at a custom path", async (ctx) => {
19+
pkg = await Package.create("v2-addon");
20+
21+
await pkg.gember("acceptance-test", "foo", "--path=tests/foo");
22+
23+
const content = await pkg.readFile("tests/foo/foo-test.js");
24+
25+
ctx.expect(content).toMatchSnapshot();
26+
});
27+
28+
it("generates a `.ts` acceptance-test", async (ctx) => {
29+
pkg = await Package.create("v2-addon");
30+
31+
await pkg.gember("acceptance-test", "foo", "--ts");
32+
33+
const content = await pkg.readFile("tests/acceptance/foo-test.ts");
34+
35+
ctx.expect(content).toMatchSnapshot();
36+
});
37+
38+
it("generates a `.ts` acceptance-test at a custom path", async (ctx) => {
39+
pkg = await Package.create("v2-addon");
40+
41+
await pkg.gember("acceptance-test", "foo", "--path=tests/foo", "--ts");
42+
43+
const content = await pkg.readFile("tests/foo/foo-test.ts");
44+
45+
ctx.expect(content).toMatchSnapshot();
46+
});

0 commit comments

Comments
 (0)