From 18ec2644c19ff5477b36b86a8d04114c95b79004 Mon Sep 17 00:00:00 2001 From: Eli Flanagan Date: Mon, 4 Jun 2018 16:24:56 -0400 Subject: [PATCH] naive rename existing render calls Also account for the `context` lifecycle hook. --- .../fourteen-testing-api/32.input.js | 22 +++++ .../fourteen-testing-api/32.output.js | 20 ++++ .../fourteen-testing-api/32a.input.js | 25 +++++ .../fourteen-testing-api/32a.output.js | 24 +++++ fourteen-testing-api.js | 99 ++++++++++++++++++- 5 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 __testfixtures__/fourteen-testing-api/32.input.js create mode 100644 __testfixtures__/fourteen-testing-api/32.output.js create mode 100644 __testfixtures__/fourteen-testing-api/32a.input.js create mode 100644 __testfixtures__/fourteen-testing-api/32a.output.js diff --git a/__testfixtures__/fourteen-testing-api/32.input.js b/__testfixtures__/fourteen-testing-api/32.input.js new file mode 100644 index 0000000..23bba6b --- /dev/null +++ b/__testfixtures__/fourteen-testing-api/32.input.js @@ -0,0 +1,22 @@ +import { expect } from 'chai'; +import { describe, it, beforeEach } from 'mocha'; +import { setupComponentTest } from 'ember-mocha'; +import hbs from 'htmlbars-inline-precompile'; + + +describe('GravatarImageComponent', function() { + setupComponentTest('gravatar-image', { + integration: true + }); + + let foo, fred, render; + beforeEach(function() { + render = () => + this.render(hbs`{{gravatar-image foo=(action 'bar')}}`); + }); + + it('renders', function() { + expect(this.$('img')).to.exist; + render(); + }); +}); diff --git a/__testfixtures__/fourteen-testing-api/32.output.js b/__testfixtures__/fourteen-testing-api/32.output.js new file mode 100644 index 0000000..75296b6 --- /dev/null +++ b/__testfixtures__/fourteen-testing-api/32.output.js @@ -0,0 +1,20 @@ +import { expect } from 'chai'; +import { describe, it, beforeEach } from 'mocha'; +import { setupRenderingTest } from 'ember-mocha'; +import { render } from '@ember/test-helpers'; +import hbs from 'htmlbars-inline-precompile'; + + +describe('GravatarImageComponent', function() { + setupRenderingTest(); + + let foo, fred, render2; + beforeEach(function() { + render2 = async () => render(hbs`{{gravatar-image foo=(action 'bar')}}`); + }); + + it('renders', async function() { + expect(this.$('img')).to.exist; + await render2(); + }); +}); diff --git a/__testfixtures__/fourteen-testing-api/32a.input.js b/__testfixtures__/fourteen-testing-api/32a.input.js new file mode 100644 index 0000000..e754e2f --- /dev/null +++ b/__testfixtures__/fourteen-testing-api/32a.input.js @@ -0,0 +1,25 @@ +import { expect } from 'chai'; +import { + beforeEach, + context, + describe, + it, +} from 'mocha'; +import { setupComponentTest } from 'ember-mocha'; +import hbs from 'htmlbars-inline-precompile'; + +describe('Integration | Component | my-component', function () { + setupComponentTest('my-component', { + integration: true, + }); + + context('when in some situation or another', function () { + beforeEach(function() { + this.render(hbs`{{my-component}}`); + }); + + it('should do the thing', function () { + expect(true).to.be.true; + }); + }); +}); \ No newline at end of file diff --git a/__testfixtures__/fourteen-testing-api/32a.output.js b/__testfixtures__/fourteen-testing-api/32a.output.js new file mode 100644 index 0000000..497342a --- /dev/null +++ b/__testfixtures__/fourteen-testing-api/32a.output.js @@ -0,0 +1,24 @@ +import { expect } from 'chai'; +import { + beforeEach, + context, + describe, + it, +} from 'mocha'; +import { setupRenderingTest } from 'ember-mocha'; +import { render } from '@ember/test-helpers'; +import hbs from 'htmlbars-inline-precompile'; + +describe('Integration | Component | my-component', function () { + setupRenderingTest(); + + context('when in some situation or another', function () { + beforeEach(async function() { + await render(hbs`{{my-component}}`); + }); + + it('should do the thing', function () { + expect(true).to.be.true; + }); + }); +}); \ No newline at end of file diff --git a/fourteen-testing-api.js b/fourteen-testing-api.js index f216082..f9c5f6e 100755 --- a/fourteen-testing-api.js +++ b/fourteen-testing-api.js @@ -126,6 +126,7 @@ module.exports = function(file, api) { { expression: { callee: { name: "before" } } }, { expression: { callee: { name: "beforeEach" } } }, { expression: { callee: { name: "afterEach" } } }, + { expression: { callee: { name: "context" } } }, { expression: { callee: { name: "after" } } } ]; @@ -528,6 +529,9 @@ module.exports = function(file, api) { }); }); + if (specifiers.size === 0) { + specifiers.add("render"); + } ensureImportWithSpecifiers({ source: "@ember/test-helpers", anchor: "ember-mocha", @@ -671,6 +675,7 @@ module.exports = function(file, api) { replaceApplicationTestVariableDeclarator(path, moduleInfo.testVarDeclarationName) } + _relabelExistingRenders(path); _removeUnusedLifecycleHooks(path) }) } @@ -685,8 +690,97 @@ module.exports = function(file, api) { }); } + function _relabelExistingRenders(path) { + const hasExisting = j(path).find(j.VariableDeclarator, { + id: { + name: "render" + } + }).filter(p => { + return p.parentPath.parentPath.value.kind === "let"; + }); + + const curHook = j(path); + // transforms render function expressions into async + ["render"].forEach(type => { + curHook.find(j.AssignmentExpression, { + left: { + type: "Identifier", + name: "render" + }, + right: { + type: "ArrowFunctionExpression" + } + }) + .forEach(p => { + // mark the right hand expression as an async function + p.value.right.async = true; + }); + curHook.find(j.CallExpression, { + callee: { + object: { + type: "ThisExpression" + }, + property: { + name: type + } + } + }).forEach(p => { + let expression = p.get("expression"); + p.replace(j.callExpression(j.identifier(type), expression.node.arguments)); + }); + }); + + const newAlias = "render2"; + const finder = a => a.type === "VariableDeclarator" && a.id.name === "render"; + j(path).find(j.VariableDeclaration, { + kind: "let" + }) + .filter(p => { + return p.value.declarations.find(finder); + }) + .forEach(p => { + try { + const { declarations } = p.value; + const relevant = declarations.find(finder); + relevant.id.name = newAlias; + } catch (err) { } // eslint-disable-line no-empty + }); + if (hasExisting.size() === 1) { + j(path).find(j.AssignmentExpression, { + left: { + type: "Identifier", + name: "render" + }, + right: { + type: "ArrowFunctionExpression" + } + }).forEach(p => { + p.node.left.name = newAlias; + }); + + // rename renders in it blocks + j(path).find(j.ExpressionStatement, { + expression: { + type: "CallExpression", + callee: { + name: "render" + } + } + }) + .forEach(p => { + p.node.expression.callee.name = newAlias; + let expression = p.get("expression"); + let awaitExpression = j.awaitExpression( + j.callExpression(j.identifier(newAlias), expression.node.arguments) + ); + p.scope.node.async = true; + expression.replace(awaitExpression); + }) + } +} + function _removeUnusedLifecycleHooks(path) { - ['beforeEach', 'afterEach', 'before', 'after'].forEach(name => { + ['beforeEach', 'afterEach', 'before', 'after', 'context',].forEach(name => { j(path) .find(j.ExpressionStatement, { expression: { @@ -696,6 +790,9 @@ module.exports = function(file, api) { } }) .forEach(node => { + if (!node.value.expression.arguments[0].body) { + return; + } if (!node.value.expression.arguments[0].body.body.length >= 1) { j(node).remove() }