From 4cfa7756636fa50613b35815464b673965b92f1f Mon Sep 17 00:00:00 2001 From: Zhan Wang Date: Fri, 14 Jan 2022 21:49:33 -0800 Subject: [PATCH 1/2] update ember-test-helper-api-migration to support more case --- transforms/acceptance/README.md | 38 ++-- .../ember-test-helper-api-migration/README.md | 122 ++++++++++++- .../test-context-specifier.input.js | 38 ++++ .../test-context-specifier.output.js | 37 ++++ .../ember-test-helper-api-migration/index.js | 53 +++++- transforms/find/README.md | 4 +- transforms/integration/README.md | 167 +++++++++++++++--- transforms/native-dom/README.md | 83 ++++++++- transforms/this-render-migration/README.md | 7 +- .../update-triggerevent-file-param/README.md | 4 +- transforms/utils.js | 1 + 11 files changed, 496 insertions(+), 58 deletions(-) create mode 100644 transforms/ember-test-helper-api-migration/__testfixtures__/test-context-specifier.input.js create mode 100644 transforms/ember-test-helper-api-migration/__testfixtures__/test-context-specifier.output.js diff --git a/transforms/acceptance/README.md b/transforms/acceptance/README.md index 7a6d1f4..2e4d7e4 100644 --- a/transforms/acceptance/README.md +++ b/transforms/acceptance/README.md @@ -71,7 +71,7 @@ test('visiting /foo', function(assert) { ``` -**Output** ([andthen.input.js](transforms/acceptance/__testfixtures__/andthen.output.js)): +**Output** ([andthen.output.js](transforms/acceptance/__testfixtures__/andthen.output.js)): ```js import { visit } from '@ember/test-helpers'; import { test } from 'qunit'; @@ -110,7 +110,7 @@ test('visiting /foo', function(assert) { }); ``` -**Output** ([attr.input.js](transforms/acceptance/__testfixtures__/attr.output.js)): +**Output** ([attr.output.js](transforms/acceptance/__testfixtures__/attr.output.js)): ```js import { find } from '@ember/test-helpers'; import { test } from 'qunit'; @@ -145,7 +145,7 @@ test('visiting /foo', function(assert) { ``` -**Output** ([click.input.js](transforms/acceptance/__testfixtures__/click.output.js)): +**Output** ([click.output.js](transforms/acceptance/__testfixtures__/click.output.js)): ```js import { click, currentURL, findAll, visit } from '@ember/test-helpers'; import { test } from 'qunit'; @@ -198,7 +198,7 @@ test('function callback with one arg', function(assert) { ``` -**Output** ([each.input.js](transforms/acceptance/__testfixtures__/each.output.js)): +**Output** ([each.output.js](transforms/acceptance/__testfixtures__/each.output.js)): ```js import { findAll } from '@ember/test-helpers'; import { test } from 'qunit'; @@ -253,7 +253,7 @@ test('visiting /foo', function(assert) { ``` -**Output** ([fill-in.input.js](transforms/acceptance/__testfixtures__/fill-in.output.js)): +**Output** ([fill-in.output.js](transforms/acceptance/__testfixtures__/fill-in.output.js)): ```js import { fillIn, currentURL, findAll, visit } from '@ember/test-helpers'; import { test } from 'qunit'; @@ -285,7 +285,7 @@ test('visiting /foo', function(assert) { }); ``` -**Output** ([get-value.input.js](transforms/acceptance/__testfixtures__/get-value.output.js)): +**Output** ([get-value.output.js](transforms/acceptance/__testfixtures__/get-value.output.js)): ```js import { find } from '@ember/test-helpers'; import { test } from 'qunit'; @@ -315,7 +315,7 @@ test('transforms get() correctly', function(assert) { ``` -**Output** ([get.input.js](transforms/acceptance/__testfixtures__/get.output.js)): +**Output** ([get.output.js](transforms/acceptance/__testfixtures__/get.output.js)): ```js import { findAll } from '@ember/test-helpers'; import { test } from 'qunit'; @@ -345,7 +345,7 @@ test('visiting /foo', function(assert) { }); ``` -**Output** ([has-class.input.js](transforms/acceptance/__testfixtures__/has-class.output.js)): +**Output** ([has-class.output.js](transforms/acceptance/__testfixtures__/has-class.output.js)): ```js import { find } from '@ember/test-helpers'; import { test } from 'qunit'; @@ -376,7 +376,7 @@ test('visiting /foo', function(assert) { }); ``` -**Output** ([html.input.js](transforms/acceptance/__testfixtures__/html.output.js)): +**Output** ([html.output.js](transforms/acceptance/__testfixtures__/html.output.js)): ```js import { find } from '@ember/test-helpers'; import { test } from 'qunit'; @@ -412,7 +412,7 @@ test('visiting /foo', function(assert) { }); ``` -**Output** ([key-event.input.js](transforms/acceptance/__testfixtures__/key-event.output.js)): +**Output** ([key-event.output.js](transforms/acceptance/__testfixtures__/key-event.output.js)): ```js import { keyEvent, currentURL, visit } from '@ember/test-helpers'; import { test } from 'qunit'; @@ -442,7 +442,7 @@ test('visiting /foo', function(assert) { }); ``` -**Output** ([length.input.js](transforms/acceptance/__testfixtures__/length.output.js)): +**Output** ([length.output.js](transforms/acceptance/__testfixtures__/length.output.js)): ```js import { findAll } from '@ember/test-helpers'; import { test } from 'qunit'; @@ -496,7 +496,7 @@ test('visiting /twiddles', function(assert) { ``` -**Output** ([nested-in-and-then.input.js](transforms/acceptance/__testfixtures__/nested-in-and-then.output.js)): +**Output** ([nested-in-and-then.output.js](transforms/acceptance/__testfixtures__/nested-in-and-then.output.js)): ```js import { click } from '@ember/test-helpers'; import { test } from 'qunit'; @@ -536,7 +536,7 @@ test('visiting /foo', function(assert) { }); ``` -**Output** ([prop.input.js](transforms/acceptance/__testfixtures__/prop.output.js)): +**Output** ([prop.output.js](transforms/acceptance/__testfixtures__/prop.output.js)): ```js import { find } from '@ember/test-helpers'; import { test } from 'qunit'; @@ -567,7 +567,7 @@ test('visiting /foo', function(assert) { ``` -**Output** ([route-helpers.input.js](transforms/acceptance/__testfixtures__/route-helpers.output.js)): +**Output** ([route-helpers.output.js](transforms/acceptance/__testfixtures__/route-helpers.output.js)): ```js import { currentURL, currentPath, currentRouteName, visit } from '@ember/test-helpers'; import { test } from 'qunit'; @@ -605,7 +605,7 @@ test(':selected is replaced correctly', function(assert) { ``` -**Output** ([selected.input.js](transforms/acceptance/__testfixtures__/selected.output.js)): +**Output** ([selected.output.js](transforms/acceptance/__testfixtures__/selected.output.js)): ```js import { find, findAll } from '@ember/test-helpers'; import { test } from 'qunit'; @@ -639,7 +639,7 @@ test('visiting /foo', function(assert) { }); ``` -**Output** ([text.input.js](transforms/acceptance/__testfixtures__/text.output.js)): +**Output** ([text.output.js](transforms/acceptance/__testfixtures__/text.output.js)): ```js import { find } from '@ember/test-helpers'; import { test } from 'qunit'; @@ -673,7 +673,7 @@ test('visiting /foo', function(assert) { }); ``` -**Output** ([trigger-event.input.js](transforms/acceptance/__testfixtures__/trigger-event.output.js)): +**Output** ([trigger-event.output.js](transforms/acceptance/__testfixtures__/trigger-event.output.js)): ```js import { currentURL, blur, focus, triggerEvent, visit } from '@ember/test-helpers'; import { test } from 'qunit'; @@ -726,7 +726,7 @@ test('visiting /bar', async function(assert) { ``` -**Output** ([visit.input.js](transforms/acceptance/__testfixtures__/visit.output.js)): +**Output** ([visit.output.js](transforms/acceptance/__testfixtures__/visit.output.js)): ```js import { currentURL, visit } from '@ember/test-helpers'; import { test } from 'qunit'; @@ -755,4 +755,4 @@ test('visiting /bar', async function(assert) { }); ``` - \ No newline at end of file + \ No newline at end of file diff --git a/transforms/ember-test-helper-api-migration/README.md b/transforms/ember-test-helper-api-migration/README.md index 6064858..fd74193 100644 --- a/transforms/ember-test-helper-api-migration/README.md +++ b/transforms/ember-test-helper-api-migration/README.md @@ -18,6 +18,7 @@ ember-test-helpers-codemod ember-test-helper-api-migration path/of/files/ or/som * [basic](#basic) * [do-not-have-@ember-test-helpers-import](#do-not-have-@ember-test-helpers-import) * [do-not-have-ember-test-helpers-import](#do-not-have-ember-test-helpers-import) +* [test-context-specifier](#test-context-specifier) @@ -29,12 +30,24 @@ ember-test-helpers-codemod ember-test-helper-api-migration path/of/files/ or/som import { setApplication } from '@ember/test-helpers'; import { start } from 'ember-qunit'; import { setResolver } from 'ember-test-helpers'; + +setResolver(engineResolverFor('shared-components')); + +setApplication(Application.create(config.APP)); +preloadAssets(manifest).then(start); + ``` -**Output** ([basic.input.js](transforms/ember-test-helper-api-migration/__testfixtures__/basic.output.js)): +**Output** ([basic.output.js](transforms/ember-test-helper-api-migration/__testfixtures__/basic.output.js)): ```js import { setApplication, setResolver } from '@ember/test-helpers'; import { start } from 'ember-qunit'; + +setResolver(engineResolverFor('shared-components')); + +setApplication(Application.create(config.APP)); +preloadAssets(manifest).then(start); + ``` --- **do-not-have-@ember-test-helpers-import** @@ -43,12 +56,24 @@ import { start } from 'ember-qunit'; ```js import { start } from 'ember-qunit'; import { setResolver } from 'ember-test-helpers'; + +setResolver(engineResolverFor('shared-components')); + +setApplication(Application.create(config.APP)); +preloadAssets(manifest).then(start); + ``` -**Output** ([do-not-have-@ember-test-helpers-import.input.js](transforms/ember-test-helper-api-migration/__testfixtures__/do-not-have-@ember-test-helpers-import.output.js)): +**Output** ([do-not-have-@ember-test-helpers-import.output.js](transforms/ember-test-helper-api-migration/__testfixtures__/do-not-have-@ember-test-helpers-import.output.js)): ```js import { setResolver } from '@ember/test-helpers'; import { start } from 'ember-qunit'; + +setResolver(engineResolverFor('shared-components')); + +setApplication(Application.create(config.APP)); +preloadAssets(manifest).then(start); + ``` --- **do-not-have-ember-test-helpers-import** @@ -61,9 +86,10 @@ import hbs from 'htmlbars-inline-precompile'; moduleForComponent('foo-bar', 'Integration | Component | foo bar', { integration: true }); + ``` -**Output** ([do-not-have-ember-test-helpers-import.input.js](transforms/ember-test-helper-api-migration/__testfixtures__/do-not-have-ember-test-helpers-import.output.js)): +**Output** ([do-not-have-ember-test-helpers-import.output.js](transforms/ember-test-helper-api-migration/__testfixtures__/do-not-have-ember-test-helpers-import.output.js)): ```js import { moduleForComponent, test } from 'ember-qunit'; import hbs from 'htmlbars-inline-precompile'; @@ -71,5 +97,93 @@ import hbs from 'htmlbars-inline-precompile'; moduleForComponent('foo-bar', 'Integration | Component | foo bar', { integration: true }); + +``` +--- +**test-context-specifier** + +**Input** ([test-context-specifier.input.js](transforms/ember-test-helper-api-migration/__testfixtures__/test-context-specifier.input.js)): +```js +import { click } from '@ember/test-helpers'; +import { TestContext } from 'ember-test-helpers'; +import { setupRenderingTest } from 'ember-qunit'; +import { module, test } from 'qunit'; + + +module( + 'Integration | Component | c-pages/recommendations/collections/detail/collection-detail-card-list', + function (hooks) { + setupRenderingTest(hooks); + + /** @type {TestContext['setProperties']} */ + let setProperties; + + hooks.beforeEach(function () { + setProperties = this.setProperties; + + this.owner.register( + 'service:lls-content-library@configuration', + ConfigurationStub + ); + }); + + test('it renders detail view', async function (assert) { + await renderComponent({ + learningCollectionItems, + locale, + isEditable: false + }); + + assert + .dom(SELECTORS.DROPDOWN_BUTTON) + .doesNotExist( + 'It does not render dropdown button when `isEditable` is falsey' + ); + }); + } +); + +``` + +**Output** ([test-context-specifier.output.js](transforms/ember-test-helper-api-migration/__testfixtures__/test-context-specifier.output.js)): +```js +import { click, TestContext } from '@ember/test-helpers'; +import { setupRenderingTest } from 'ember-qunit'; +import { module, test } from 'qunit'; + + +module( + 'Integration | Component | c-pages/recommendations/collections/detail/collection-detail-card-list', + function (hooks) { + setupRenderingTest(hooks); + + /** @type {TestContext['setProperties']} */ + let setProperties; + + hooks.beforeEach(function () { + setProperties = this.setProperties; + + this.owner.register( + 'service:lls-content-library@configuration', + ConfigurationStub + ); + }); + + test('it renders detail view', async function (assert) { + await renderComponent({ + learningCollectionItems, + locale, + isEditable: false + }); + + assert + .dom(SELECTORS.DROPDOWN_BUTTON) + .doesNotExist( + 'It does not render dropdown button when `isEditable` is falsey' + ); + }); + } +); + ``` - \ No newline at end of file + \ No newline at end of file diff --git a/transforms/ember-test-helper-api-migration/__testfixtures__/test-context-specifier.input.js b/transforms/ember-test-helper-api-migration/__testfixtures__/test-context-specifier.input.js new file mode 100644 index 0000000..c67ac2b --- /dev/null +++ b/transforms/ember-test-helper-api-migration/__testfixtures__/test-context-specifier.input.js @@ -0,0 +1,38 @@ +import { click } from '@ember/test-helpers'; +import { TestContext } from 'ember-test-helpers'; +import { setupRenderingTest } from 'ember-qunit'; +import { module, test } from 'qunit'; + + +module( + 'Integration | Component | c-pages/recommendations/collections/detail/collection-detail-card-list', + function (hooks) { + setupRenderingTest(hooks); + + /** @type {TestContext['setProperties']} */ + let setProperties; + + hooks.beforeEach(function () { + setProperties = this.setProperties; + + this.owner.register( + 'service:lls-content-library@configuration', + ConfigurationStub + ); + }); + + test('it renders detail view', async function (assert) { + await renderComponent({ + learningCollectionItems, + locale, + isEditable: false + }); + + assert + .dom(SELECTORS.DROPDOWN_BUTTON) + .doesNotExist( + 'It does not render dropdown button when `isEditable` is falsey' + ); + }); + } +); diff --git a/transforms/ember-test-helper-api-migration/__testfixtures__/test-context-specifier.output.js b/transforms/ember-test-helper-api-migration/__testfixtures__/test-context-specifier.output.js new file mode 100644 index 0000000..93beff6 --- /dev/null +++ b/transforms/ember-test-helper-api-migration/__testfixtures__/test-context-specifier.output.js @@ -0,0 +1,37 @@ +import { click, TestContext } from '@ember/test-helpers'; +import { setupRenderingTest } from 'ember-qunit'; +import { module, test } from 'qunit'; + + +module( + 'Integration | Component | c-pages/recommendations/collections/detail/collection-detail-card-list', + function (hooks) { + setupRenderingTest(hooks); + + /** @type {TestContext['setProperties']} */ + let setProperties; + + hooks.beforeEach(function () { + setProperties = this.setProperties; + + this.owner.register( + 'service:lls-content-library@configuration', + ConfigurationStub + ); + }); + + test('it renders detail view', async function (assert) { + await renderComponent({ + learningCollectionItems, + locale, + isEditable: false + }); + + assert + .dom(SELECTORS.DROPDOWN_BUTTON) + .doesNotExist( + 'It does not render dropdown button when `isEditable` is falsey' + ); + }); + } +); diff --git a/transforms/ember-test-helper-api-migration/index.js b/transforms/ember-test-helper-api-migration/index.js index 2e80e7b..36897d4 100644 --- a/transforms/ember-test-helper-api-migration/index.js +++ b/transforms/ember-test-helper-api-migration/index.js @@ -1,10 +1,61 @@ +const { createImportStatement } = require('../utils'); const { getParser } = require('codemod-cli').jscodeshift; -const { addImportStatement, writeImportStatements } = require('../utils'); module.exports = function transformer(file, api) { const j = getParser(api); const root = j(file.source); + let _statementsToImport = new Set(); + + /** + * Adds (one or more) named imports to a private import statement collection. + * To write the import statements to a file, use writeImportStatements. + * + * @param j + * @param {array} imports + */ + function addImportStatement(imports) { + imports.forEach((method) => _statementsToImport.add(method)); + } + + /** + * Adds all collected named imports to an (existing or to be created) `import { namedImport } from '@ember/test-helpers'; + * To add named imports to the collection, use addImportStatement + * + * @param j + * @param root + */ + function writeImportStatements(j, root) { + if (_statementsToImport.size > 0) { + let body = root.get().value.program.body; + let importStatement = root.find(j.ImportDeclaration, { + source: { value: '@ember/test-helpers' }, + }); + + let statementsToImportArray = Array.from(_statementsToImport) + + if (importStatement.length === 0) { + importStatement = createImportStatement( + j, + '@ember/test-helpers', + 'default', + statementsToImportArray + ); + body.unshift(importStatement); + } else { + let existingSpecifiers = importStatement.get('specifiers'); + + statementsToImportArray.forEach((name) => { + if (existingSpecifiers.filter((exSp) => exSp.value.imported.name === name).length === 0) { + existingSpecifiers.push(j.importSpecifier(j.identifier(name))); + } + }); + } + } + + _statementsToImport = new Set(); + } + /** * Transform imports from ember-test-helpers to @ember/test-helpers */ diff --git a/transforms/find/README.md b/transforms/find/README.md index 853e29c..030bb1f 100644 --- a/transforms/find/README.md +++ b/transforms/find/README.md @@ -32,11 +32,11 @@ test('it renders', function(assert) { }); ``` -**Output** ([find.input.js](transforms/find/__testfixtures__/find.output.js)): +**Output** ([find.output.js](transforms/find/__testfixtures__/find.output.js)): ```js test('it renders', function(assert) { assert.equal(this.element.querySelector('.foo').textContent.trim(), 'bar'); assert.equal(this.element.querySelectorAll('.bar').length, 2); }); ``` - \ No newline at end of file + \ No newline at end of file diff --git a/transforms/integration/README.md b/transforms/integration/README.md index c6a4dc2..4cd6d76 100644 --- a/transforms/integration/README.md +++ b/transforms/integration/README.md @@ -33,6 +33,7 @@ ember-test-helpers-codemod integration path/of/files/ or/some**/*glob.js * [selected](#selected) * [set-value](#set-value) * [text](#text) +* [typescript](#typescript) @@ -82,7 +83,7 @@ test('and again', function(assert) { }); ``` -**Output** ([all.input.js](transforms/integration/__testfixtures__/all.output.js)): +**Output** ([all.output.js](transforms/integration/__testfixtures__/all.output.js)): ```js import { click, find, findAll, fillIn, blur, triggerEvent } from '@ember/test-helpers'; import { moduleForComponent, test } from 'ember-qunit'; @@ -146,7 +147,7 @@ test('it renders', function(assert) { ``` -**Output** ([attr.input.js](transforms/integration/__testfixtures__/attr.output.js)): +**Output** ([attr.output.js](transforms/integration/__testfixtures__/attr.output.js)): ```js import { find } from '@ember/test-helpers'; import { moduleForComponent, test } from 'ember-qunit'; @@ -187,7 +188,7 @@ test('it renders', function(assert) { ``` -**Output** ([click.input.js](transforms/integration/__testfixtures__/click.output.js)): +**Output** ([click.output.js](transforms/integration/__testfixtures__/click.output.js)): ```js import { find, findAll, click } from '@ember/test-helpers'; import { moduleForComponent, test } from 'ember-qunit'; @@ -240,9 +241,8 @@ test('it renders', function(assert) { ``` -**Output** ([default-component-test.input.js](transforms/integration/__testfixtures__/default-component-test.output.js)): +**Output** ([default-component-test.output.js](transforms/integration/__testfixtures__/default-component-test.output.js)): ```js -import { find } from '@ember/test-helpers'; import { moduleForComponent, test } from 'ember-qunit'; import hbs from 'htmlbars-inline-precompile'; @@ -257,7 +257,7 @@ test('it renders', function(assert) { this.render(hbs`{{foo-bar}}`); - assert.equal(find('*').textContent.trim(), ''); + assert.equal(this.element.textContent.trim(), ''); // Template block usage: this.render(hbs` @@ -266,7 +266,7 @@ test('it renders', function(assert) { {{/foo-barl}} `); - assert.equal(find('*').textContent.trim(), 'template block text'); + assert.equal(this.element.textContent.trim(), 'template block text'); }); ``` @@ -316,7 +316,7 @@ test('function callback with one arg', function(assert) { ``` -**Output** ([each.input.js](transforms/integration/__testfixtures__/each.output.js)): +**Output** ([each.output.js](transforms/integration/__testfixtures__/each.output.js)): ```js import { findAll } from '@ember/test-helpers'; import { moduleForComponent, test } from 'ember-qunit'; @@ -392,7 +392,7 @@ test('it renders', function(assert) { ``` -**Output** ([event.input.js](transforms/integration/__testfixtures__/event.output.js)): +**Output** ([event.output.js](transforms/integration/__testfixtures__/event.output.js)): ```js import { triggerEvent } from '@ember/test-helpers'; import { moduleForComponent, test } from 'ember-qunit'; @@ -443,7 +443,7 @@ test('it renders', function(assert) { ``` -**Output** ([focus.input.js](transforms/integration/__testfixtures__/focus.output.js)): +**Output** ([focus.output.js](transforms/integration/__testfixtures__/focus.output.js)): ```js import { focus } from '@ember/test-helpers'; import { moduleForComponent, test } from 'ember-qunit'; @@ -481,7 +481,7 @@ test('it renders', function(assert) { ``` -**Output** ([get-value.input.js](transforms/integration/__testfixtures__/get-value.output.js)): +**Output** ([get-value.output.js](transforms/integration/__testfixtures__/get-value.output.js)): ```js import { find } from '@ember/test-helpers'; import { moduleForComponent, test } from 'ember-qunit'; @@ -518,7 +518,7 @@ test('transforms get() correctly', function(assert) { ``` -**Output** ([get.input.js](transforms/integration/__testfixtures__/get.output.js)): +**Output** ([get.output.js](transforms/integration/__testfixtures__/get.output.js)): ```js import { findAll } from '@ember/test-helpers'; import { moduleForComponent, test } from 'ember-qunit'; @@ -555,7 +555,7 @@ test('it renders', function(assert) { ``` -**Output** ([has-class.input.js](transforms/integration/__testfixtures__/has-class.output.js)): +**Output** ([has-class.output.js](transforms/integration/__testfixtures__/has-class.output.js)): ```js import { find } from '@ember/test-helpers'; import { moduleForComponent, test } from 'ember-qunit'; @@ -596,7 +596,7 @@ test('it renders', function(assert) { ``` -**Output** ([html.input.js](transforms/integration/__testfixtures__/html.output.js)): +**Output** ([html.output.js](transforms/integration/__testfixtures__/html.output.js)): ```js import { find } from '@ember/test-helpers'; import { moduleForComponent, test } from 'ember-qunit'; @@ -657,12 +657,14 @@ test('it renders', function(assert) { assert.ok(this.$('.foo:radio').length); assert.ok(this.$('.foo:reset').length); assert.ok(this.$('.foo:selected').length); + assert.ok(this.$(':selected').length); assert.ok(this.$('.foo:submit').length); assert.ok(this.$('.foo:text').length); assert.ok(this.$('.foo:visible').length); assert.ok(this.$(JQEXTENSION_SELECTOR_AS_LOCAL_CONST).length); assert.ok(this.$(ANY_SELECTOR_AS_IMPORTED_CONST).length); + assert.ok(this.$(':eq(0)').length); assert.ok(this.$('.foo:eq(0)').length); assert.ok(this.$('.foo:eq(1)').length); assert.ok(this.$('.foo:first-child').length); @@ -673,7 +675,7 @@ test('it renders', function(assert) { ``` -**Output** ([jq-extensions.input.js](transforms/integration/__testfixtures__/jq-extensions.output.js)): +**Output** ([jq-extensions.output.js](transforms/integration/__testfixtures__/jq-extensions.output.js)): ```js import { find, findAll } from '@ember/test-helpers'; import { moduleForComponent, test } from 'ember-qunit'; @@ -711,12 +713,14 @@ test('it renders', function(assert) { assert.ok(this.$('.foo:radio').length); assert.ok(this.$('.foo:reset').length); assert.ok(findAll('.foo:checked').length); + assert.ok(this.$(':selected').length); assert.ok(this.$('.foo:submit').length); assert.ok(this.$('.foo:text').length); assert.ok(this.$('.foo:visible').length); assert.ok(this.$(JQEXTENSION_SELECTOR_AS_LOCAL_CONST).length); assert.ok(this.$(ANY_SELECTOR_AS_IMPORTED_CONST).length); + assert.ok(this.$(':eq(0)').length); assert.ok(findAll(find('.foo')).length); assert.ok(findAll('.foo')[1].length); assert.ok(findAll('.foo:first-child').length); @@ -747,7 +751,7 @@ test('it renders', function(assert) { ``` -**Output** ([key-event.input.js](transforms/integration/__testfixtures__/key-event.output.js)): +**Output** ([key-event.output.js](transforms/integration/__testfixtures__/key-event.output.js)): ```js import { keyEvent } from '@ember/test-helpers'; import { moduleForComponent, test } from 'ember-qunit'; @@ -785,7 +789,7 @@ test('it renders', function(assert) { ``` -**Output** ([length.input.js](transforms/integration/__testfixtures__/length.output.js)): +**Output** ([length.output.js](transforms/integration/__testfixtures__/length.output.js)): ```js import { findAll } from '@ember/test-helpers'; import { moduleForComponent, test } from 'ember-qunit'; @@ -822,7 +826,7 @@ test('it renders', function(assert) { ``` -**Output** ([prop.input.js](transforms/integration/__testfixtures__/prop.output.js)): +**Output** ([prop.output.js](transforms/integration/__testfixtures__/prop.output.js)): ```js import { find } from '@ember/test-helpers'; import { moduleForComponent, test } from 'ember-qunit'; @@ -868,7 +872,7 @@ test(':selected is replaced correctly', function(assert) { ``` -**Output** ([selected.input.js](transforms/integration/__testfixtures__/selected.output.js)): +**Output** ([selected.output.js](transforms/integration/__testfixtures__/selected.output.js)): ```js import { find, findAll } from '@ember/test-helpers'; import { moduleForComponent, test } from 'ember-qunit'; @@ -917,12 +921,20 @@ test('it renders', function(assert) { Ember.run(() => this.$('select').val('1').trigger('change')); Ember.run(() => this.$('#odd').val(10).trigger('input').trigger('blur')); this.$('#odd').val(10).trigger('input').trigger('blur'); + this.$('input:eq(0)') + .val('foo') + .trigger('keydown') + .focusout(); + this.$('input:eq(0)') + .val('foo') + .trigger('keydown') + .blur(); assert.ok(true); }); ``` -**Output** ([set-value.input.js](transforms/integration/__testfixtures__/set-value.output.js)): +**Output** ([set-value.output.js](transforms/integration/__testfixtures__/set-value.output.js)): ```js import { fillIn, blur } from '@ember/test-helpers'; import Ember from 'ember'; @@ -944,6 +956,14 @@ test('it renders', async function(assert) { Ember.run(() => this.$('select').val('1').trigger('change')); Ember.run(() => this.$('#odd').val(10).trigger('input').trigger('blur')); this.$('#odd').val(10).trigger('input').trigger('blur'); + this.$('input:eq(0)') + .val('foo') + .trigger('keydown') + .focusout(); + this.$('input:eq(0)') + .val('foo') + .trigger('keydown') + .blur(); assert.ok(true); }); @@ -966,9 +986,15 @@ test('it renders', function(assert) { assert.equal(this.$('.foo').text().trim(), ''); }); +test('it renders', function(assert) { + this.render(hbs`{{foo-bar}}`); + + assert.equal(this.$().text().trim(), ''); +}); + ``` -**Output** ([text.input.js](transforms/integration/__testfixtures__/text.output.js)): +**Output** ([text.output.js](transforms/integration/__testfixtures__/text.output.js)): ```js import { find } from '@ember/test-helpers'; import { moduleForComponent, test } from 'ember-qunit'; @@ -984,5 +1010,102 @@ test('it renders', function(assert) { assert.equal(find('.foo').textContent.trim(), ''); }); +test('it renders', function(assert) { + this.render(hbs`{{foo-bar}}`); + + assert.equal(this.element.textContent.trim(), ''); +}); + +``` +--- +**typescript** + +**Input** ([typescript.input.ts](transforms/integration/__testfixtures__/typescript.input.ts)): +```ts +import { click } from '@ember/test-helpers'; +import { moduleForComponent, test } from 'ember-qunit'; +import hbs from 'htmlbars-inline-precompile'; + +moduleForComponent('foo-bar', 'Integration | Component | foo bar', { + integration: true +}); + +function fillInHelper(value: string) { + this.$('.foo input').val(value); + this.$('.foo input').change(); +} + +test('it renders', async function(assert) { + this.render(hbs`{{foo-bar}}`); + + await click('.foo'); + assert.equal(this.$('.foo').attr('id'), 'foo'); + this.$('.foo input').val('bar').change(); + assert.equal(this.$('.foo').text().trim(), 'foo'); +}); + +test('it renders again', function(assert) { + this.render(hbs`{{foo-bar}}`); + + let selector = '.foo input'; + assert.equal(this.$(selector).length, 1); + assert.equal(this.$(selector).val(), 'foo'); + assert.ok(this.$('.foo').hasClass('selected')); +}); + +test('and again', function(assert) { + this.render(hbs`{{foo-bar}}`); + + this.$('foo').click(); + + fillInHelper.call(this, 'bar'); + assert.ok(this.$('.foo').hasClass('selected')); +}); + +``` + +**Output** ([typescript.output.ts](transforms/integration/__testfixtures__/typescript.output.ts)): +```ts +import { click, find, findAll, fillIn, blur, triggerEvent } from '@ember/test-helpers'; +import { moduleForComponent, test } from 'ember-qunit'; +import hbs from 'htmlbars-inline-precompile'; + +moduleForComponent('foo-bar', 'Integration | Component | foo bar', { + integration: true +}); + +async function fillInHelper(value: string) { + await fillIn('.foo input', value); + await triggerEvent('.foo input', 'change'); +} + +test('it renders', async function(assert) { + this.render(hbs`{{foo-bar}}`); + + await click('.foo'); + assert.equal(find('.foo').id, 'foo'); + await fillIn('.foo input', 'bar'); + await blur('.foo input'); + assert.equal(find('.foo').textContent.trim(), 'foo'); +}); + +test('it renders again', function(assert) { + this.render(hbs`{{foo-bar}}`); + + let selector = '.foo input'; + assert.equal(findAll(selector).length, 1); + assert.equal(find(selector).value, 'foo'); + assert.ok(find('.foo').classList.contains('selected')); +}); + +test('and again', async function(assert) { + this.render(hbs`{{foo-bar}}`); + + await click('foo'); + + fillInHelper.call(this, 'bar'); + assert.ok(find('.foo').classList.contains('selected')); +}); + ``` - \ No newline at end of file + \ No newline at end of file diff --git a/transforms/native-dom/README.md b/transforms/native-dom/README.md index d4f7fe5..1329336 100644 --- a/transforms/native-dom/README.md +++ b/transforms/native-dom/README.md @@ -16,6 +16,7 @@ ember-test-helpers-codemod native-dom path/of/files/ or/some**/*glob.js * [acceptance](#acceptance) +* [context-argument](#context-argument) * [double-import](#double-import) * [integration](#integration) * [prune-import](#prune-import) @@ -52,7 +53,7 @@ test('visiting /bar', async function(assert) { ``` -**Output** ([acceptance.input.js](transforms/native-dom/__testfixtures__/acceptance.output.js)): +**Output** ([acceptance.output.js](transforms/native-dom/__testfixtures__/acceptance.output.js)): ```js import { find, visit, currentURL, currentRouteName } from '@ember/test-helpers'; import { currentPath } from 'ember-native-dom-helpers'; @@ -77,6 +78,60 @@ test('visiting /bar', async function(assert) { assert.equal(currentRouteName(), 'bar.index'); }); +``` +--- +**context-argument** + +**Input** ([context-argument.input.js](transforms/native-dom/__testfixtures__/context-argument.input.js)): +```js +import { find, findAll, visit, click, fillIn } from 'ember-native-dom-helpers'; +import { test } from 'qunit'; +import moduleForAcceptance from '../../tests/helpers/module-for-acceptance'; + +moduleForAcceptance('click'); + +test('visiting /foo', async function(assert) { + await visit('/foo'); + + const foo = find('.foo'); + assert.equal(find('.bar', foo).textContent.trim(), 'bar'); + assert.equal(find('.bar', find('.foo')).textContent.trim(), 'bar'); + assert.equal(find('.bar', '.foo').textContent.trim(), 'bar'); + assert.equal(findAll('.bar', foo).length, 2); + assert.equal(findAll('.bar', find('.foo')).length, 2); + assert.equal(findAll('.bar', '.foo').length, 2); + + await click('button', foo); + await click('button', { shiftKey: true }); + await click('button', foo, { shiftKey: true }); +}); + +``` + +**Output** ([context-argument.output.js](transforms/native-dom/__testfixtures__/context-argument.output.js)): +```js +import { find, findAll, visit, click } from '@ember/test-helpers'; +import { test } from 'qunit'; +import moduleForAcceptance from '../../tests/helpers/module-for-acceptance'; + +moduleForAcceptance('click'); + +test('visiting /foo', async function(assert) { + await visit('/foo'); + + const foo = find('.foo'); + assert.equal(foo.querySelector('.bar').textContent.trim(), 'bar'); + assert.equal(find('.foo').querySelector('.bar').textContent.trim(), 'bar'); + assert.equal(find('.foo .bar').textContent.trim(), 'bar'); + assert.equal(foo.querySelectorAll('.bar').length, 2); + assert.equal(find('.foo').querySelectorAll('.bar').length, 2); + assert.equal(findAll('.foo .bar').length, 2); + + await click(foo.querySelector('button')); + await click('button', { shiftKey: true }); + await click(foo.querySelector('button'), { shiftKey: true }); +}); + ``` --- **double-import** @@ -86,12 +141,26 @@ test('visiting /bar', async function(assert) { import { click, currentURL } from 'ember-native-dom-helpers'; import { find, visit } from 'ember-native-dom-helpers'; +test('visiting /foo', async function(assert) { + await visit('/foo'); + await click('.foo'); + assert.ok(find('.foo')); + assert.ok(currentURL()); +}); + ``` -**Output** ([double-import.input.js](transforms/native-dom/__testfixtures__/double-import.output.js)): +**Output** ([double-import.output.js](transforms/native-dom/__testfixtures__/double-import.output.js)): ```js import { click, currentURL, find, visit } from '@ember/test-helpers'; +test('visiting /foo', async function(assert) { + await visit('/foo'); + await click('.foo'); + assert.ok(find('.foo')); + assert.ok(currentURL()); +}); + ``` --- **integration** @@ -167,7 +236,7 @@ test('and yet again', async function(assert) { ``` -**Output** ([integration.input.js](transforms/native-dom/__testfixtures__/integration.output.js)): +**Output** ([integration.output.js](transforms/native-dom/__testfixtures__/integration.output.js)): ```js import { click, @@ -243,11 +312,15 @@ test('and yet again', async function(assert) { ```js import { click } from 'ember-native-dom-helpers'; +click('.foo'); + ``` -**Output** ([prune-import.input.js](transforms/native-dom/__testfixtures__/prune-import.output.js)): +**Output** ([prune-import.output.js](transforms/native-dom/__testfixtures__/prune-import.output.js)): ```js import { click } from '@ember/test-helpers'; +click('.foo'); + ``` - \ No newline at end of file + \ No newline at end of file diff --git a/transforms/this-render-migration/README.md b/transforms/this-render-migration/README.md index 5787682..cb35cec 100644 --- a/transforms/this-render-migration/README.md +++ b/transforms/this-render-migration/README.md @@ -37,7 +37,7 @@ test('It handles switching selected option on click and fires onSelect event', a ``` -**Output** ([basic.input.js](transforms/this-render-migration/__testfixtures__/basic.output.js)): +**Output** ([basic.output.js](transforms/this-render-migration/__testfixtures__/basic.output.js)): ```js import { click, render } from '@ember/test-helpers'; @@ -48,6 +48,7 @@ test('It handles switching selected option on click and fires onSelect event', a `); }) + ``` --- **has-no-ember-test-helpers-import** @@ -64,7 +65,7 @@ test('It handles switching selected option on click and fires onSelect event', a ``` -**Output** ([has-no-ember-test-helpers-import.input.js](transforms/this-render-migration/__testfixtures__/has-no-ember-test-helpers-import.output.js)): +**Output** ([has-no-ember-test-helpers-import.output.js](transforms/this-render-migration/__testfixtures__/has-no-ember-test-helpers-import.output.js)): ```js import { render } from '@ember/test-helpers'; test('It handles switching selected option on click and fires onSelect event', async function(assert) { @@ -76,4 +77,4 @@ test('It handles switching selected option on click and fires onSelect event', a }) ``` - \ No newline at end of file + \ No newline at end of file diff --git a/transforms/update-triggerevent-file-param/README.md b/transforms/update-triggerevent-file-param/README.md index 216db74..02f0b02 100644 --- a/transforms/update-triggerevent-file-param/README.md +++ b/transforms/update-triggerevent-file-param/README.md @@ -33,7 +33,7 @@ test('test', async function(assert) { ``` -**Output** ([basic.input.js](transforms/update-triggerevent-file-param/__testfixtures__/basic.output.js)): +**Output** ([basic.output.js](transforms/update-triggerevent-file-param/__testfixtures__/basic.output.js)): ```js import { triggerEvent } from '@ember/test-helpers'; @@ -47,4 +47,4 @@ test('test', async function(assert) { }); ``` - \ No newline at end of file + \ No newline at end of file diff --git a/transforms/utils.js b/transforms/utils.js index 504bb99..fa2b544 100644 --- a/transforms/utils.js +++ b/transforms/utils.js @@ -552,6 +552,7 @@ module.exports = { isFindExpression, createFindExpression, createFindAllExpression, + createImportStatement, createQuerySelectorExpression, createQuerySelectorAllExpression, createClickExpression, From f4ee81673e65fc37b50dfe7aa55169eed902b8a5 Mon Sep 17 00:00:00 2001 From: Zhan Wang Date: Fri, 14 Jan 2022 22:12:33 -0800 Subject: [PATCH 2/2] trigger CI test