Skip to content

Commit

Permalink
Fix styles/attributes tied to ember-application container. (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
fotinakis authored Feb 21, 2018
1 parent 0a82959 commit d2143d1
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 4 deletions.
13 changes: 11 additions & 2 deletions addon/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function setAttributeValues(dom) {

// Limit scope to inputs only as textareas do not retain their value when cloned
let elems = dom.find(
`input[type=text], input[type=search], input[type=tel], input[type=url], input[type=email],
`input[type=text], input[type=search], input[type=tel], input[type=url], input[type=email],
input[type=password], input[type=number], input[type=checkbox], input[type=radio]`
);

Expand Down Expand Up @@ -77,10 +77,19 @@ export function percySnapshot(name, options) {
let scope = options.scope;

// Create a full-page DOM snapshot from the current testing page.
// TODO(fotinakis): more memory-efficient way to do this?
let domCopy = $('html').clone();
let testingContainer = domCopy.find('#ember-testing');

// Copy attributes from Ember's rootElement to the DOM snapshot <body> tag. Some applications rely
// on setting attributes on the Ember rootElement (for example, to drive dynamic per-route
// styling). In tests these attributes are added to the #ember-testing container and would be lost
// in the DOM hoisting below, so we copy them to the to the snapshot's <body> tag to
// make sure that they persist in the DOM snapshot.
let attributesToCopy = testingContainer.prop('attributes');
$.each(attributesToCopy, function() {
domCopy.attr(this.name, this.value);
});

if (scope) {
snapshotRoot = testingContainer.find(scope);
} else {
Expand Down
8 changes: 8 additions & 0 deletions tests/acceptance/dummy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,11 @@ test('enableJavaScript option can pass through', function(assert) {
});
percySnapshot(assert, {enableJavaScript: true});
});

test('attributes on rootElement are copied to the DOM snapshot', function(assert) {
visit('/test-route-styles');
andThen(function() {
assert.equal(currentURL(), '/test-route-styles');
});
percySnapshot(assert);
});
9 changes: 8 additions & 1 deletion tests/dummy/app/router.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import $ from 'jquery';
import { on } from '@ember/object/evented';
import EmberRouter from '@ember/routing/router';
import config from './config/environment';

const Router = EmberRouter.extend({
location: config.locationType,
rootURL: config.rootURL
rootURL: config.rootURL,

addDataRoute: on('didTransition', function() {
$('.ember-application').attr('data-route', this.currentRouteName);
}),
});

Router.map(function() {
this.route('test-route-styles');
});

export default Router;
10 changes: 9 additions & 1 deletion tests/dummy/app/styles/app.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
body {
font-size: 26px;
}

.DummyBox {
background: lightblue;
padding: 20px;
width: 100%;
height: 300px;
}
}

[data-route="test-route-styles"] .DummyBox {
background: lightgreen;
}
3 changes: 3 additions & 0 deletions tests/dummy/app/templates/test-route-styles.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{#dummy-box}}
This box should be green, styled by the body tag data-route attribute.
{{/dummy-box}}
4 changes: 4 additions & 0 deletions tests/helpers/destroy-app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { run } from '@ember/runloop';
import $ from 'jquery';

export default function destroyApp(application) {
// Strip data attributes added by the router so they don't leak between tests.
$('.ember-application').attr('data-route', null);

run(application, 'destroy');
}

0 comments on commit d2143d1

Please sign in to comment.