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

Closes softlayer/sl-ember-test-helpers#156 #158

Merged
merged 20 commits into from
Jan 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
97acf3f
Closes softlayer/sl-ember-test-helpers#156
theoshu Nov 20, 2015
1feb16e
Changed API interface a little to accomodate for event firing + added…
azizpunjani Nov 25, 2015
aa4a74d
Merge pull request #1 from azizpunjani/issue156
theoshu Nov 30, 2015
372494c
code review changes: documentation improvements and dependency modifi…
theoshu Dec 3, 2015
d85d173
Fixed lint issues. Updated docs
azizpunjani Dec 7, 2015
162d652
Added backticks for code
azizpunjani Dec 7, 2015
9fed923
Merge pull request #2 from azizpunjani/issue156
theoshu Dec 8, 2015
29daac2
Fixed lint issues + added test
azizpunjani Dec 8, 2015
ff53328
Added deleted tests back
azizpunjani Dec 8, 2015
df08451
Merge pull request #3 from azizpunjani/issue156
theoshu Dec 8, 2015
2790a44
Install ember-sinon as an addon.
azizpunjani Dec 14, 2015
f9e3ce9
import sinon.js when in test or development mode.
azizpunjani Dec 14, 2015
948f7b5
Merge pull request #4 from azizpunjani/issue156
theoshu Dec 14, 2015
02f9063
Fixed issues outlined in review
azizpunjani Dec 18, 2015
4936a45
Merge pull request #5 from azizpunjani/issue156
theoshu Dec 28, 2015
fc4a11e
Use anonymous functions instead of arrow functions for backward compa…
azizpunjani Jan 13, 2016
9688ba4
Merge pull request #6 from azizpunjani/issue156
theoshu Jan 13, 2016
9c69755
Changed link number
azizpunjani Jan 21, 2016
2766f87
Merge branch 'master' of github.com:softlayer/sl-ember-test-helpers i…
azizpunjani Jan 21, 2016
b2afc7c
Merge pull request #7 from azizpunjani/issue156
theoshu Jan 21, 2016
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### 1.10.0

* [#156](https://github.com/softlayer/sl-ember-test-helpers/issues/156) [ENHANCEMENT] Add `globalLibraries` helper
* [#160](https://github.com/softlayer/sl-ember-test-helpers/pull/160) BUGFIX Lock down version of jQuery in bower.json
* [#145](https://github.com/softlayer/sl-ember-test-helpers/pull/146) DOCUMENTATION Update *CONTRIBUTING.md* to add linting guidance
* [#155](https://github.com/softlayer/sl-ember-test-helpers/pull/155) INTERNAL Upgrade `ember-qunit` dependency
Expand Down
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,34 @@ The call to `requires` returns an object:
}
```

### Global Libraries

Use this helper in your unit tests to determine if a component called globally-scoped `Ember.$`, `$` or `jQuery`. You must wrap your component with references to the setup and removal functions.

```
const component = this.subject();

globalLibraries.setupSpies();

globalLibraries.triggerEvents( component );

assert.notOk(
globalLibraries.called(),
'There are no references to Ember.$, $ or jQuery'
);

globalLibraries.restoreSpies();
```

The `triggerEvents()` function takes a `component` as an argument and triggers the following events on it: `willInsertElement`, `didInsertElement`, `willClearRender` and `willDestroyElement`. Triggering of the various events
will ensure code that has handlers attached to those events will be exercised, making the helper more effective at detecting global references.

The call to `called()` returns a boolean that is the result of the sinon spies detecting `Ember.$`, `$`, or `jQuery`. Validate `false` to verify that the code within the component does not have global references to
`Ember.$`, `$` or `jQuery`.

```
<boolean: true if the spy detects a reference to the global scope, false if not>
```

## Asynchronous

Expand Down
11 changes: 5 additions & 6 deletions blueprints/sl-ember-test-helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,17 @@ module.exports = {
var thirdText = ' "contains",' + EOL + ' "requires",';
var thirdLocationText = '"predef": [' + EOL;

// Import statement
return this.insertIntoFile( firstFile, firstText, { after: firstLocationText } )

// Execution of registration function
.then( function() {
return this.insertIntoFile( secondFile, secondText, { after: secondLocationText } );
}.bind(this))

// .jshintrc file
}.bind( this ))
.then( function() {
return this.insertIntoFile( thirdFile, thirdText, { after: thirdLocationText } );
}.bind(this));
}.bind( this ))
.then( function() {
return this.addAddonToProject( 'ember-sinon' );
}.bind( this ));
},

normalizeEntityName: function() {}
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
'use strict';

module.exports = {
name: 'sl-ember-test-helpers'
name: 'sl-ember-test-helpers'
};
6 changes: 4 additions & 2 deletions test-support/helpers/sl/register-test-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import Ember from 'ember';
import {
contains,
AjaxHelper,
requires
requires,
globalLibraries
} from './synchronous';

export default function() {
Ember.Test.registerHelper( 'contains', contains );
Ember.Test.registerHelper( 'Ajax', AjaxHelper );
Ember.Test.registerHelper( 'requires', requires );
}
Ember.Test.registerHelper( 'globalLibraries', globalLibraries );
}
6 changes: 4 additions & 2 deletions test-support/helpers/sl/synchronous.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import AjaxHelper from './synchronous/ajax';
import contains from './synchronous/contains';
import requires from './synchronous/requires';
import * as globalLibraries from './synchronous/global-libraries';

export {
AjaxHelper,
contains,
requires
};
requires,
globalLibraries
};
43 changes: 43 additions & 0 deletions test-support/helpers/sl/synchronous/global-libraries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import Ember from 'ember';
import sinon from 'sinon';

export let called;
export let jqueryAliasSpy;
export let jquerySpy;
export let emberJquerySpy;

export function setupSpies() {
jqueryAliasSpy = sinon.spy( window, '$' );
jquerySpy = sinon.spy( window, 'jQuery' );
emberJquerySpy = sinon.spy( Ember, '$' );
}

export function triggerEvents( component ) {
Ember.run( () => {
[
'willInsertElement',
'didInsertElement',
'willClearRender',
'willDestroyElement'
].map( ( event ) => {
component.trigger( event );
});
});
}

export function called() {
return jqueryAliasSpy.called || jquerySpy.called || emberJquerySpy.called;
}

export function restoreSpies() {
window.$.restore();
window.jQuery.restore();
Ember.$.restore();
}

export function resetSpies() {
jqueryAliasSpy.reset();
jquerySpy.reset();
emberJquerySpy.reset();
}

102 changes: 102 additions & 0 deletions tests/unit/helpers/sl/synchronous/global-libaries-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import Ember from 'ember';
import { test } from 'ember-qunit';
import * as globalLibraries from '../../../../helpers/sl/synchronous/global-libraries';
import sinon from 'sinon';

module( 'Unit | Helpers | sl/synchronous/global-libraries' );

test( 'it exists', function( assert ) {
assert.ok(
globalLibraries,
'it exists'
);
});

test( 'Passes when global libraries are not referenced', function( assert ) {
const component = Ember.Object.extend().create({
trigger: function() {}
});

globalLibraries.setupSpies();

globalLibraries.triggerEvents( component );

assert.notOk(
globalLibraries.called()
);

globalLibraries.restoreSpies();
});

test( 'triggerEvents triggers respective events', function( assert ) {
const events = {
'willInsertElement': sinon.spy(),
'didInsertElement': sinon.spy(),
'willClearRender': sinon.spy(),
'willDestroyElement': sinon.spy()
};

const component = {
trigger: function( eventName ) {
this.events[ eventName ]();
},

events
};

globalLibraries.triggerEvents( component );

Object.keys( events ).forEach( ( eventName ) => {
const spy = events[ eventName ];
assert.ok(
spy.called,
`${ eventName } was triggered`
);
});
});

test( 'called() returns true when $ is referenced', function( assert ) {
globalLibraries.setupSpies();

window.$();

assert.ok(
globalLibraries.called()
);

globalLibraries.restoreSpies();
});

test( 'called() returns true when jQuery is referenced', function( assert ) {
globalLibraries.setupSpies();

window.jQuery();

assert.ok(
globalLibraries.called()
);

globalLibraries.restoreSpies();
});

test( 'called() returns true when Ember.$ is referenced', function( assert ) {
globalLibraries.setupSpies();

Ember.$();

assert.ok(
globalLibraries.called()
);

globalLibraries.restoreSpies();
});

test( 'called() returns false when global libraries are not referenced', function( assert ) {
globalLibraries.setupSpies();

assert.notOk(
globalLibraries.called()
);

globalLibraries.restoreSpies();
});