Giter Site home page Giter Site logo

fivetanley / ember-test-helpers-codemod Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ember-codemods/ember-test-helpers-codemod

0.0 2.0 0.0 260 KB

Codemod to transform your Ember tests to use @ember/test-helpers

License: MIT License

JavaScript 98.17% TypeScript 1.83%

ember-test-helpers-codemod's Introduction

ember-test-helpers-codemod

Build Status Build status npm version

A jscodeshift based codemod to help migrating your jQuery or ember-native-dom-helpers based Ember tests to @ember/test-helpers.

Please note that this will not be able to cover all possible cases how jQuery based tests can be written. Given the dynamic nature of JavaScript and the extensive API and fluent interface of jQuery, this would be impossible. Instead this codemod focuses to cover the most common usages and do those transformations that it can safely do. So it is likely that you will have to manually migrate some usages this tool cannot cover!

Usage

WARNING: jscodeshift, and thus this codemod, edits your files in place. It does not make a copy. Make sure your code is checked into a source control repository like Git and that you have no outstanding changes to commit before running this tool.

cd my-ember-app-or-addon
npx ember-test-helpers-codemod integration tests/integration
npx ember-test-helpers-codemod acceptance tests/acceptance
npx ember-test-helpers-codemod native-dom tests

Transformations

Integrations tests

This addon will perform the following transformations suitable for integration tests:

Before After Transform
this.$('.foo').attr('id') find('.foo').id attr.js
this.$('.foo').attr('data-test') find('.foo').getAttribute('data-test') attr.js
this.$('.foo').click() await click('.foo') click.js
this.$('.foo').change() (and more events) await triggerEvent('.foo', 'change') trigger-shortcut.js
this.$('.foo').trigger('input') await triggerEvent('.foo', 'input') trigger.js
this.$('.foo').focus() await focus('.foo') focus.js
this.$('.foo').val() find('.foo').value get-value.js
this.$('div').hasClass('foo') find('div').classList.contains('foo') has-class.js
this.$('.foo').trigger('click') await click('.foo') key-event.js
this.$('.foo').trigger('keydown', { keyCode: 13 }) await keyEvent('.foo', 'keydown', 13) key-event.js
this.$('.foo').length findAll('.foo').length length.js
this.$('.foo').prop('tagName') find('.foo').tagName prop.js
this.$('.foo').val('foo') await fillIn('.foo', 'foo') set-value.js
this.$('.foo').val('bar').change() await fillIn('.foo', 'foo'); await blur('.foo'); set-value.js
this.$('.foo').val('bar').trigger('input') await fillIn('.foo', 'foo') set-value.js
this.$('.foo').text() find('.foo').textContent text.js
this.$('.foo').html() find('.foo').innerHTML html.js
this.$('.foo').html('foo') find('.foo').innerHTML = 'foo' html.js
this.$('.foo').each((index, elem) => {...}) findAll('.foo').forEach((elem, index) => {...}) each.js
this.$('.foo').get(3) findAll('.foo')[3] get.js

If you want to run only selected transforms on your code, you can pick just the needed transform:

jscodeshift -t path/to/ember-test-helpers-codemod/transforms/integration/transforms/click.js tests/integration

Acceptance tests

These transformations are available for acceptance tests:

Before After Transform
find('.foo').attr('id') find('.foo').id attr.js
find('.foo').attr('data-test') find('.foo').getAttribute('data-test') attr.js
click('.foo') await click('.foo') click.js
fillIn('#bar', 'baz') await fillIn('#bar', 'baz') fill-in.js
triggerEvent('input', 'focus') await focus('.foo') trigger-event.js
triggerEvent('input', 'blur') await blur('.foo') trigger-event.js
triggerEvent('input', 'mouseenter') await triggerEvent('input', 'mouseenter') trigger-event.js
find('.foo').val() find('.foo').value get-value.js
find('div').hasClass('foo') find('div').classList.contains('foo') has-class.js
keyEvent('#bar', 'keypress', 13); await keyEvent('.foo', 'keydown', 13) key-event.js
find('.foo').length findAll('.foo').length length.js
find('.foo').prop('tagName') find('.foo').tagName prop.js
find('.foo').text() find('.foo').textContent text.js
find('.foo').html() find('.foo').innerHTML html.js
find('.foo').html('foo') find('.foo').innerHTML = 'foo' html.js
find('.foo').each((index, elem) => {...}) findAll('.foo').forEach((elem, index) => {...}) each.js
find('.foo').get(3) findAll('.foo')[3] get.js

If you want to run only selected transforms on your code, you can pick just the needed transform:

jscodeshift -t ../ember-test-helpers-codemod/transforms/acceptance/transforms/click.js tests/integration

ember-native-dom-helpers tests

These transformations are available for tests based on ember-native-dom-helpers:

Before After Transform
import { click, find, findAll, fillIn, focus, blur, triggerEvent, keyEvent, waitFor, waitUntil } from 'ember-native-dom-helpers'; import { click, find, findAll, fillIn, focus, blur, triggerEvent, triggerKeyEvent, waitFor, waitUntil } from '@ember/test-helpers';
find('.foo', context) context.querySelector('.foo')
find('.foo', '.context') find('.context .foo')
findAll('.foo', context) context.querySelectorAll('.foo')
click('.foo', context) click(context.querySelector('.foo'))
click('.foo', context, { shiftKey: true }) click(context.querySelector('.foo'), { shiftKey: true })

Replace find/findAll

By default this codemod will use the find() and findAll() helpers from @ember/test-helpers where required. If you want to use the native query functions this.element.querySelector() / this.element.querySelectorAll() instead, you can use the find.js transform after you have run the other transformations:

npx ember-test-helpers-codemod find tests
Before After Transform
find('.foo') this.element.querySelector('.foo') find.js
findAll('.foo') this.element.querySelectorAll('.foo') find.js

Note that this will require all instances of find/findAll to have the correct this context, otherwise you will run into Cannot read property 'querySelector' of undefined exceptions, as this.element will not be defined. This can happen outside of the main test function, for example inside of custom test helper functions.

ember-test-helpers-codemod's People

Contributors

simonihmig avatar scalvert avatar cibernox avatar nlfurniss avatar dwickern avatar eflanagan0 avatar knownasilya avatar dingoeatingfuzz avatar rwjblue avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.