Slide 1

Slide 1 text

Leveraging the complete Ember Toolbelt to improve things for all of us

Slide 2

Slide 2 text

Marco Otte-Witte @marcoow

Slide 3

Slide 3 text

simplabs.com @simplabs

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

How do we identify DOM elements in (acceptance and integration) tests?

Slide 7

Slide 7 text

{{! templates/components/blog-post.hbs}}

{{post.title}}

{{post.body}}

Slide 8

Slide 8 text

import { moduleForComponent, test } from 'ember-qunit'; import hbs from 'htmlbars-inline-precompile'; moduleForComponent('blog-post', 'blog post component', { integration: true }); test('it renders', function(assert) { … this.render(hbs`{{blog-post post=post}}`); assert.equal( this.$('h1').text().trim(), 'My awesome post' ); assert.equal( this.$('p').text().trim(), "You won't believe it, …" ); });

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

{{! templates/components/blog-post.hbs}} -

{{post.title}}

+

{{post.title}}

{{post.body}}

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

{{! templates/components/blog-post.hbs}}

{{post.title}}

{{post.body}}

Slide 14

Slide 14 text

import { moduleForComponent, test } from 'ember-qunit'; import hbs from 'htmlbars-inline-precompile'; moduleForComponent('blog-post', 'blog post component', { integration: true }); test('it renders', function(assert) { … this.render(hbs`{{blog-post post=post}}`); assert.equal( this.$('.js-post-title').text().trim(), 'My awesome post' ); assert.equal( this.$('.js-post-body').text().trim(), "You won't believe it, …" ); });

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

{{! templates/components/blog-post.hbs}} -

{{post.title}}

+
{{post.title}} -

{{post.body}}

+
{{post.body}}

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

.js-post-title { font-size: 20px; font-weight: bold; }

Slide 19

Slide 19 text

- .js-post-title { + .js-post-header { font-size: 20px; font-weight: bold; } {{! templates/components/blog-post.hbs}} -

{{post.title}}

+

{{post.title}}

{{post.body}}

Slide 20

Slide 20 text

{{! templates/components/blog-post.hbs}}

{{post.title}}

{{post.body}}

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

{{! templates/components/blog-post.hbs}}

{{post.title}}

{{post.body}}

Slide 23

Slide 23 text

import { moduleForComponent, test } from 'ember-qunit'; import hbs from 'htmlbars-inline-precompile'; moduleForComponent('blog-post', 'blog post component', { integration: true }); test('it renders', function(assert) { … this.render(hbs`{{blog-post post=post}}`); assert.equal( this.$('[data-test-post-title]').text().trim(), 'My awesome post' ); assert.equal( this.$('[data-test-post-body]').text().trim(), "You won't believe it, …" ); });

Slide 24

Slide 24 text

{{! templates/components/blog-post.hbs}}

{{post.title}}

{{post.body}}

Slide 25

Slide 25 text

ember-test-selectors https://github.com/simplabs/ember-test-selectors

Slide 26

Slide 26 text

{{blog-post data-test-post-id=post.id}}

Slide 27

Slide 27 text

//app/components/blog-post.js import Ember from 'ember'; export default Ember.Component.extend({ post: null, 'data-test-post-id': Ember.computed.readOnly('post.id'), });

Slide 28

Slide 28 text

data-test-* everywhere

Slide 29

Slide 29 text

//app/components/blog-post.js import Ember from 'ember'; export default Ember.Component.extend({ post: null, 'data-test-post-id': Ember.computed.readOnly('post.id'), });

Slide 30

Slide 30 text

//app/components/blog-post.js import Ember from 'ember'; export default Ember.Component.extend({ post: null });

Slide 31

Slide 31 text

{{! templates/components/blog-post.hbs}}

{{post.title}}

{{post.body}}

Slide 32

Slide 32 text

{{! templates/components/blog-post.hbs}}

{{post.title}}

{{post.body}}

Slide 33

Slide 33 text

{{blog-post data-test-post}} {{#blog-post data-test-post}} {{/blog-post}}

Slide 34

Slide 34 text

{{blog-post}} {{#blog-post}} {{/blog-post}}

Slide 35

Slide 35 text

How does it all work?

Slide 36

Slide 36 text

» ember install ember-test-selectors

Slide 37

Slide 37 text

Thanks

Slide 38

Slide 38 text

…there's more

Slide 39

Slide 39 text

How does it all work internally?

Slide 40

Slide 40 text

//app/components/blog-post.js import Ember from 'ember'; export default Ember.Component.extend({ post: null, 'data-test-post-id': Ember.computed.readOnly('post.id'), });

Slide 41

Slide 41 text

/* global Ember */ (function() { var bindDataTestAttributes; Ember.Component.reopen({ init: function() { this._super.apply(this, arguments); if (!bindDataTestAttributes) { bindDataTestAttributes = require( 'ember-test-selectors/utils/bind-data-test-attributes' )['default']; } bindDataTestAttributes(this); } }); })();

Slide 42

Slide 42 text

included(app) { this._super.included.apply(this, arguments); … if (!this._stripTestSelectors) { app.import('vendor/ember-test-selectors/patch-component.js'); } }

Slide 43

Slide 43 text

//app/components/blog-post.js import Ember from 'ember'; export default Ember.Component.extend({ post: null, 'data-test-post-id': Ember.computed.readOnly('post.id'), });

Slide 44

Slide 44 text

included(app) { this._super.included.apply(this, arguments); … if (this._stripTestSelectors) { … if (checker.satisfies('^5.0.0')) { … app.options.babel.plugins.push( require('./strip-data-test-properties-plugin') ); } else if (checker.satisfies('^6.0.0-beta.1')) { … app.options.babel6.plugins.push( require('./strip-data-test-properties-plugin6') ); } } }

Slide 45

Slide 45 text

let TEST_SELECTOR_PREFIX = /data-test-.*/; function StripDataTestPropertiesPlugin() { return { visitor: { Property(path) { if (TEST_SELECTOR_PREFIX.test(path.node.key.value)) { path.remove(); } }, }, }; }

Slide 46

Slide 46 text

https://astexplorer.net

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

{{! templates/components/blog-post.hbs}}

{{post.title}}

{{post.body}}

Slide 49

Slide 49 text

included(app) { _setupPreprocessorRegistry(registry) { if (this._stripTestSelectors) { let StripTestSelectorsTransform = require( './strip-test-selectors' ); registry.add('htmlbars-ast-plugin', { name: 'strip-test-selectors', plugin: StripTestSelectorsTransform, baseDir() { return __dirname; } }); } … } }

Slide 50

Slide 50 text

Slide 51

Slide 51 text

StripTestSelectorsTransform.prototype.transform = function(ast) { let walker = new this.syntax.Walker(); walker.visit(ast, function(node) { if (node.type === 'ElementNode') { node.attributes = node.attributes.filter(function(attribute) { return !isTestSelector(attribute.name); }); } else if (node.type === 'MustacheStatement' || node.type === 'BlockStatement') { node.params = node.params.filter(function(param) { return !isTestSelector(param.original); }); node.hash.pairs = node.hash.pairs.filter(function(pair) { return !isTestSelector(pair.key); }); } }); return ast; };

Slide 52

Slide 52 text

{{! templates/components/blog-post.hbs}}

{{post.title}}

{{post.body}}

Slide 53

Slide 53 text

{{! templates/components/blog-post.hbs}}

{{post.title}}

{{post.body}}

Slide 54

Slide 54 text

StripTestSelectorsTransform.prototype.transform = function(ast) { let walker = new this.syntax.Walker(); walker.visit(ast, function(node) { if (node.type === 'ElementNode') { node.attributes = node.attributes.filter(function(attribute) { return !isTestSelector(attribute.name); }); } else if (node.type === 'MustacheStatement' || node.type === 'BlockStatement') { node.params = node.params.filter(function(param) { return !isTestSelector(param.original); }); node.hash.pairs = node.hash.pairs.filter(function(pair) { return !isTestSelector(pair.key); }); } }); return ast; };

Slide 55

Slide 55 text

{{blog-post data-test-post}} {{#blog-post data-test-post}} {{/blog-post}}

Slide 56

Slide 56 text

{{blog-post}} {{#blog-post}} {{/blog-post}}

Slide 57

Slide 57 text

StripTestSelectorsTransform.prototype.transform = function(ast) { let walker = new this.syntax.Walker(); walker.visit(ast, function(node) { if (node.type === 'ElementNode') { node.attributes = node.attributes.filter(function(attribute) { return !isTestSelector(attribute.name); }); } else if (node.type === 'MustacheStatement' || node.type === 'BlockStatement') { node.params = node.params.filter(function(param) { return !isTestSelector(param.original); }); node.hash.pairs = node.hash.pairs.filter(function(pair) { return !isTestSelector(pair.key); }); } }); return ast; };

Slide 58

Slide 58 text

{{blog-post data-test-post=post.id}} {{#blog-post data-test-post=post.id}} {{/blog-post}}

Slide 59

Slide 59 text

{{blog-post}} {{#blog-post}} {{/blog-post}}

Slide 60

Slide 60 text

https://embermap.com/video/ember-test-selectors

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

Thanks

Slide 63

Slide 63 text

simplabs.com @simplabs