diff --git a/src/generator.ts b/src/generator.ts index a01b0fd..db71918 100644 --- a/src/generator.ts +++ b/src/generator.ts @@ -129,6 +129,8 @@ export function defineGenerator({ Math.max(...templateCompiled.split("\n").map((line) => line.length)), ); + consola.log(border); + consola.log(targetFile.path()); consola.log(border); consola.log(""); consola.log(templateCompiled); @@ -177,13 +179,17 @@ export function defineGenerator({ } export function defineTestGenerator( - options: GeneratorOptions & { testsDir: string }, + options: GeneratorOptions & { testsDir: string; testsSubDir?: string }, ): Generator { return defineGenerator({ ...options, modifyTargetFile: (targetFile, args) => { if (args.path === undefined) { - targetFile.subDir = join("tests", options.testsDir, options.name + "s"); + targetFile.subDir = join( + "tests", + options.testsDir, + options.testsSubDir ?? options.name + "s", + ); } targetFile.name += "-test"; diff --git a/src/generators.ts b/src/generators.ts index 3a7ee32..36a3e78 100644 --- a/src/generators.ts +++ b/src/generators.ts @@ -62,4 +62,11 @@ export const generators: Generator[] = [ name: "service", testsDir: "unit", }), + + defineTestGenerator({ + args: [name(), path(), typescript()], + name: "acceptance", + testsDir: "acceptance", + testsSubDir: "", + }), ]; diff --git a/templates/acceptance-test/acceptance-test.js b/templates/acceptance-test/acceptance-test.js new file mode 100644 index 0000000..ea12390 --- /dev/null +++ b/templates/acceptance-test/acceptance-test.js @@ -0,0 +1,13 @@ +import { currentURL, visit } from '@ember/test-helpers'; +import { module, test } from 'qunit'; +import { setupApplicationTest } from '{{testHelpersImportPath}}'; + +module('Acceptance | {{name.path}}', function (hooks) { + setupApplicationTest(hooks); + + test('it visits /{{name.path}}', async function (assert) { + await visit('/{{name.path}}'); + + assert.strictEqual(currentURL(), '/{{name.path}}'); + }); +}); diff --git a/templates/acceptance-test/acceptance-test.ts b/templates/acceptance-test/acceptance-test.ts new file mode 100644 index 0000000..ea12390 --- /dev/null +++ b/templates/acceptance-test/acceptance-test.ts @@ -0,0 +1,13 @@ +import { currentURL, visit } from '@ember/test-helpers'; +import { module, test } from 'qunit'; +import { setupApplicationTest } from '{{testHelpersImportPath}}'; + +module('Acceptance | {{name.path}}', function (hooks) { + setupApplicationTest(hooks); + + test('it visits /{{name.path}}', async function (assert) { + await visit('/{{name.path}}'); + + assert.strictEqual(currentURL(), '/{{name.path}}'); + }); +}); diff --git a/test/__snapshots__/acceptance-test.test.ts.snap b/test/__snapshots__/acceptance-test.test.ts.snap new file mode 100644 index 0000000..fa5d0f7 --- /dev/null +++ b/test/__snapshots__/acceptance-test.test.ts.snap @@ -0,0 +1,69 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`generates a \`.js\` acceptance-test 1`] = ` +"import { currentURL, visit } from '@ember/test-helpers'; +import { module, test } from 'qunit'; +import { setupApplicationTest } from 'v2-addon/tests/helpers'; + +module('Acceptance | foo', function (hooks) { + setupApplicationTest(hooks); + + test('it visits /foo', async function (assert) { + await visit('/foo'); + + assert.strictEqual(currentURL(), '/foo'); + }); +}); +" +`; + +exports[`generates a \`.js\` acceptance-test at a custom path 1`] = ` +"import { currentURL, visit } from '@ember/test-helpers'; +import { module, test } from 'qunit'; +import { setupApplicationTest } from 'v2-addon/tests/helpers'; + +module('Acceptance | foo', function (hooks) { + setupApplicationTest(hooks); + + test('it visits /foo', async function (assert) { + await visit('/foo'); + + assert.strictEqual(currentURL(), '/foo'); + }); +}); +" +`; + +exports[`generates a \`.ts\` acceptance-test 1`] = ` +"import { currentURL, visit } from '@ember/test-helpers'; +import { module, test } from 'qunit'; +import { setupApplicationTest } from 'v2-addon/tests/helpers'; + +module('Acceptance | foo', function (hooks) { + setupApplicationTest(hooks); + + test('it visits /foo', async function (assert) { + await visit('/foo'); + + assert.strictEqual(currentURL(), '/foo'); + }); +}); +" +`; + +exports[`generates a \`.ts\` acceptance-test at a custom path 1`] = ` +"import { currentURL, visit } from '@ember/test-helpers'; +import { module, test } from 'qunit'; +import { setupApplicationTest } from 'v2-addon/tests/helpers'; + +module('Acceptance | foo', function (hooks) { + setupApplicationTest(hooks); + + test('it visits /foo', async function (assert) { + await visit('/foo'); + + assert.strictEqual(currentURL(), '/foo'); + }); +}); +" +`; diff --git a/test/acceptance-test.test.ts b/test/acceptance-test.test.ts new file mode 100644 index 0000000..6f5f401 --- /dev/null +++ b/test/acceptance-test.test.ts @@ -0,0 +1,46 @@ +import { afterEach, it } from "vitest"; +import { Package } from "./helpers.ts"; + +let pkg: Package; + +afterEach(() => pkg.cleanUp()); + +it("generates a `.js` acceptance-test", async (ctx) => { + pkg = await Package.create("v2-addon"); + + await pkg.gember("acceptance-test", "foo"); + + const content = await pkg.readFile("tests/acceptance/foo-test.js"); + + ctx.expect(content).toMatchSnapshot(); +}); + +it("generates a `.js` acceptance-test at a custom path", async (ctx) => { + pkg = await Package.create("v2-addon"); + + await pkg.gember("acceptance-test", "foo", "--path=tests/foo"); + + const content = await pkg.readFile("tests/foo/foo-test.js"); + + ctx.expect(content).toMatchSnapshot(); +}); + +it("generates a `.ts` acceptance-test", async (ctx) => { + pkg = await Package.create("v2-addon"); + + await pkg.gember("acceptance-test", "foo", "--ts"); + + const content = await pkg.readFile("tests/acceptance/foo-test.ts"); + + ctx.expect(content).toMatchSnapshot(); +}); + +it("generates a `.ts` acceptance-test at a custom path", async (ctx) => { + pkg = await Package.create("v2-addon"); + + await pkg.gember("acceptance-test", "foo", "--path=tests/foo", "--ts"); + + const content = await pkg.readFile("tests/foo/foo-test.ts"); + + ctx.expect(content).toMatchSnapshot(); +});