Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Component unit test example #20

Merged
merged 5 commits into from
May 5, 2015
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ View [the library](https://github.com/yapplabs/ember-key-responder) for more inf
* `ember test`
* `ember test --server`

## Unit Tests

When running unit tests on components that use ember-modal-dialog it is necessary to create and register the container for ember-modal-dialog to wormhole itself into. See this [example](https://github.com/yapplabs/ember-modal-dialog/tree/master/tests/unit/components/component-that-uses-modal-dialog-test.js) for how to set this up in a unit test.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to use a relative link here that will work on all branches.

Looks really good overall.. thanks!


## Building

* `ember build`
Expand Down
13 changes: 13 additions & 0 deletions tests/dummy/app/components/component-that-uses-modal-dialog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Ember from 'ember';

export default Ember.Component.extend({
isShowingModalDialog: false,
actions: {
openModalDialog: function(){
this.set('isShowingModalDialog', true);
},
closeModalDialog: function(){
this.set('isShowingModalDialog', false);
}
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{{#if isShowingModalDialog}}
{{#modal-dialog}}
<h1>Stop! Modal Time!</h1>
<button class="close" {{action 'closeModalDialog'}}>Close</button>
{{/modal-dialog}}
{{/if}}

<h1>A component that has a modal dialog</h1>
<button class="open" {{action 'openModalDialog'}}>Open Dialog</button>
44 changes: 44 additions & 0 deletions tests/unit/component/component-that-uses-modal-dialog-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { test, moduleForComponent } from 'ember-qunit';
/* global Ember */

moduleForComponent('component-that-uses-modal-dialog', {
/*
* Setting up the container for the modal dialog to wormhole into and
* injecting the id of that element into the modal-dialog component
*/
beforeEach: function(){
var modalContainerEl = document.createElement("div");
modalContainerEl.id = 'modal-overlays';
var rootEl = document.querySelector("#ember-testing");
rootEl.appendChild(modalContainerEl);
this.container.register('config:modals-container-id', 'modal-overlays', {instantiate: false});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

during migration ember app from 1.13 to 2.13 found unit test with similar code. it was probably copied from related discussion. could you please provide details is this test code still relevant and how I can do that in Ember 2.0

this.container.injection('component:modal-dialog', 'destinationElementId', 'config:modals-container-id');
},
needs: [
'component:modal-dialog',
'component:ember-modal-dialog-positioned-container',
'component:ember-wormhole'
]
});

test("it doesn't show component on start", function(assert){
assert.expect(1);

var component = this.subject();
// render the component
this.$();

// cannot use this.$ because the modal-dialog exists outside of the component
assert.equal(Ember.$('.ember-modal-dialog').length, 0);
});

test('it shows component when open modal clicked', function(assert){
assert.expect(1);

var component = this.subject();

this.$('button.open').click();

// cannot use this.$ because the modal-dialog exists outside of the component
assert.equal(Ember.$('.ember-modal-dialog').length, 1);
});