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

Avoid polyfills / ENV setup in Ember 2.x scenarios. #50

Merged
merged 4 commits into from
Jan 9, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
23 changes: 23 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
/* eslint-env node */
'use strict'

const VersionChecker = require('ember-cli-version-checker');

module.exports = {
name: 'ember-2-legacy',

init() {
this._super && this._super.init.apply(this, arguments);

let checker = new VersionChecker(this);
this.emberVersion = checker.forEmber();
},

config() {
// do nothing if running with Ember 2.x
if (this.emberVersion.lt('3.0.0-alpha.0')) {
return;
}

return this._super.config.apply(this, arguments);
},

included() {
this._super.included.apply(this, arguments);

// do nothing if running with Ember 2.x
if (this.emberVersion.lt('3.0.0-alpha.0')) {
return;
}

this.import('vendor/ember-k.js');
this.import('vendor/safe-string.js');
this.import('vendor/enumerable-contains.js');
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"test": "ember try:each"
},
"dependencies": {
"ember-cli-babel": "^6.6.0"
"ember-cli-babel": "^6.6.0",
"ember-cli-version-checker": "^2.1.0"
},
"devDependencies": {
"broccoli-asset-rev": "^2.4.5",
Expand All @@ -36,8 +37,8 @@
"ember-cli-uglify": "^2.0.0",
"ember-export-application-global": "^2.0.0",
"ember-load-initializers": "^1.0.0",
"ember-resolver": "^4.0.0",
"ember-qunit-assert-helpers": "^0.2.1",
"ember-resolver": "^4.0.0",
"ember-source": "http://builds.emberjs.com/beta/shas/08bc55d20f76f874b6a7ceefd4007dde1aa7c9dc.tgz",
"eslint-plugin-ember": "^5.0.0",
"eslint-plugin-node": "^5.2.1",
Expand Down
79 changes: 50 additions & 29 deletions tests/unit/immediate-observer-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Ember from 'ember';
import Object from '@ember/object';
import { get, set, computed } from '@ember/object';
import { run } from '@ember/runloop';
import EmberObject from '@ember/object';
import { module, test } from 'qunit';

let originalObserver;
Expand All @@ -16,41 +18,60 @@ module('Ember.immediateObserver', {
}
});

test('that Ember.immediateObserver is deprecated', function(assert) {
assert.expectDeprecation(() => {
const Thing = Object.extend({
bar: null,
baz: null,
test('immediate observers should fire synchronously', function(assert) {
let observerCalled = 0;

foo: Ember.immediateObserver('bar', function() {
this.set('baz', 'helloworld');
})
});
// explicitly create a run loop so we do not inadvertently
// trigger deferred behavior
run(function() {
let obj;
assert.expectDeprecation(() => {
obj = EmberObject.extend({
fooDidChange: Ember.immediateObserver('foo', function() {
observerCalled++;
assert.equal(get(this, 'foo'), 'barbaz', 'newly set value is immediately available');
}),

const instance = Thing.create();
instance.set('bar', 'start');
foo: computed({
get: function() { return 'yes hello this is foo'; },
set: function(key, value) { return value; }
})
}).create();
}, /Usage of `Ember.immediateObserver` is deprecated/);

assert.equal(instance.get('baz'), 'helloworld');
}, 'Usage of `Ember.immediateObserver` is deprecated, use `observer` instead');
});
assert.equal(get(obj, 'foo'), 'yes hello this is foo', 'precond - computed property returns a value');
assert.equal(observerCalled, 0, 'observer has not yet been called');

test('that Ember.immediateObserver calls Ember.observer', function(assert) {
assert.expect(1);
set(obj, 'foo', 'barbaz');

// eslint-disable-next-line ember/new-module-imports
Ember.observer = () => {
assert.ok(true, 'immediateObserver calls observer');
};
assert.equal(observerCalled, 1, 'observer was called once');
});
});

const Thing = Object.extend({
bar: null,
foo: Ember.immediateObserver('bar', function() {})
});
test('that Ember.immediateObserver generally works', function(assert) {
assert.expect(1);

const instance = Thing.create();
instance.set('bar', 'start');
const Thing = EmberObject.extend({
bar: null,
foo: Ember.immediateObserver('bar', function() {
assert.ok(true, 'was called!');
})
});

const instance = Thing.create();
instance.set('bar', 'start');
});

test('that function prototype correctly gets polyfilled', function(assert) {
assert.deepEqual(Function.prototype.observesImmediately, Ember.immediateObserver);
test('Function.prototype.observesImmediately generally works', function(assert) {
assert.expect(1);

const Thing = EmberObject.extend({
bar: null,
foo: function() {
assert.ok(true, 'was called!');
}.observesImmediately('bar'),
});

const instance = Thing.create();
instance.set('bar', 'start');
});
29 changes: 24 additions & 5 deletions tests/unit/underscore-actions-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,30 @@ import Route from '@ember/routing/route';
import Controller from '@ember/controller';
import Component from '@ember/component';
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';

module('Underscore _actions');
module('Underscore _actions', function(hooks) {
setupTest(hooks);

test('that _actions are correctly added to Routes, Controllers, and Components', function(assert) {
assert.ok(Route.prototype.hasOwnProperty('_actions'), '_actions is defined on Ember.Route');
assert.ok(Controller.prototype.hasOwnProperty('_actions'), '_actions is defined on Ember.Controller');
assert.ok(Component.prototype.hasOwnProperty('_actions'), '_actions is defined on Ember.Component');


function confirmActionsAreHandled(type, Klass) {
test(`_actions defined on ${type} at extend time is properly handled`, function(assert) {
function definedAction() { }
this.owner.register(`${type}:foo`, Klass.extend({
// eslint-disable-next-line
_actions: {
foo: definedAction,
}
}));

let instance = this.owner.factoryFor(`${type}:foo`).create();

assert.strictEqual(instance.actions.foo, definedAction);
});
}

confirmActionsAreHandled('route', Route);
confirmActionsAreHandled('controller', Controller);
confirmActionsAreHandled('component', Component);
});
4 changes: 3 additions & 1 deletion vendor/immediate-observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
}

if (EmberENV.EXTEND_PROTOTYPES.Function === true) {
Function.prototype.observesImmediately = _Ember.immediateObserver;
Function.prototype.observesImmediately = function(...dependentKeys) {
return _Ember.immediateObserver(...dependentKeys, this);
};
}
})();