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

Add _ENABLE_BINDING_SUPPORT. #49

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
11 changes: 11 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ module.exports = {
env: {
embertest: true
}
},

// vendor files
{
files: ['vendor/**/*.js'],
globals: {
Ember: false
},
rules: {
'ember/new-module-imports': 'off'
}
}
]
};
3 changes: 2 additions & 1 deletion config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ module.exports = function(/* environment, appConfig */) {
_ENABLE_FREEZABLE_SUPPORT: true, // https://emberjs.com/deprecations/v1.x/#toc_ember-freezable
_ENABLE_COMPONENT_DEFAULTLAYOUT_SUPPORT: true, // https://www.emberjs.com/deprecations/v2.x/#toc_ember-component-defaultlayout
_ENABLE_CONTROLLER_WRAPPED_SUPPORT: true, // https://www.emberjs.com/deprecations/v1.x/#toc_objectcontroller
_ENABLE_PROPERTY_REQUIRED_SUPPORT: true // N/A
_ENABLE_PROPERTY_REQUIRED_SUPPORT: true, // N/A
_ENABLE_BINDING_SUPPORT: true, // https://www.emberjs.com/deprecations/v2.x/#toc_ember-binding
}
};
};
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ module.exports = {
this.import('vendor/string-fmt.js');
this.import('vendor/freezable.js');
this.import('vendor/component-defaultlayout.js');
this.import('vendor/binding.js');
},

treeForVendor(rawVendorTree) {
Expand Down
1 change: 0 additions & 1 deletion tests/dummy/config/targets.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module.exports = {
browsers: [
'ie 9',
'last 1 Chrome versions',
'last 1 Firefox versions',
'last 1 Safari versions'
Expand Down
277 changes: 277 additions & 0 deletions tests/unit/binding-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,277 @@
import Ember from 'ember';
import EmberObject, { observer } from '@ember/object';
import Component from '@ember/component';
import { run } from '@ember/runloop';
import { set, get } from '@ember/object';
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';

module('Ember.Binding', function(hooks) {

hooks.afterEach(function() {
// prevent test leakage
run.cancelTimers();
});

module('Ember.bind', function(hooks) {
let fromObject, toObject, binding, root;

hooks.beforeEach(function(assert) {
fromObject = EmberObject.create({ value: 'start' });
toObject = EmberObject.create({ value: 'end' });
root = { fromObject: fromObject, toObject: toObject };
run(() => {
assert.expectDeprecation(() => {
binding = Ember.bind(root, 'toObject.value', 'fromObject.value');
}, /`Ember\.Binding` is deprecated./);
});
});

test('binding should have synced on connect', function(assert) {
assert.equal(get(toObject, 'value'), 'start', 'toObject.value should match fromObject.value');
});

test('fromObject change should propagate to toObject only after flush', function(assert) {
run(() => {
set(fromObject, 'value', 'change');
assert.equal(get(toObject, 'value'), 'start');
});
assert.equal(get(toObject, 'value'), 'change');
});

test('toObject change should propagate to fromObject only after flush', function(assert) {
run(() => {
set(toObject, 'value', 'change');
assert.equal(get(fromObject, 'value'), 'start');
});
assert.equal(get(fromObject, 'value'), 'change');
});

test('deferred observing during bindings', function(assert) {
// setup special binding
fromObject = EmberObject.create({
value1: 'value1',
value2: 'value2'
});

toObject = EmberObject.extend({
observer: observer('value1', 'value2', function() {
assert.equal(get(this, 'value1'), 'CHANGED', 'value1 when observer fires');
assert.equal(get(this, 'value2'), 'CHANGED', 'value2 when observer fires');
this.callCount++;
})
}).create({
value1: 'value1',
value2: 'value2',

callCount: 0
});

let root = { fromObject: fromObject, toObject: toObject };
run(function () {
assert.expectDeprecation(() => {
Ember.bind(root, 'toObject.value1', 'fromObject.value1');
}, /`Ember\.Binding` is deprecated./);

assert.expectDeprecation(() => {
Ember.bind(root, 'toObject.value2', 'fromObject.value2');
}, /`Ember\.Binding` is deprecated./);

// change both value1 + value2, then flush bindings. observer should only
// fire after bindings are done flushing.
set(fromObject, 'value1', 'CHANGED');
set(fromObject, 'value2', 'CHANGED');
});

assert.equal(toObject.callCount, 2, 'should call observer twice');
});

test('binding disconnection actually works', function(assert) {
binding.disconnect(root);
run(function () {
set(fromObject, 'value', 'change');
});
assert.equal(get(toObject, 'value'), 'start');
});

test('chained binding', function(assert) {
let first, second, third;
run(function() {
first = EmberObject.create({ output: 'first' });

second = EmberObject.extend({
inputDidChange: observer('input', function() {
set(this, 'output', get(this, 'input'));
})
}).create({
input: 'second',
output: 'second'
});

third = EmberObject.create({ input: 'third' });

root = { first: first, second: second, third: third };

assert.expectDeprecation(() => {
Ember.bind(root, 'second.input', 'first.output');
}, /`Ember\.Binding` is deprecated./);

assert.expectDeprecation(() => {
Ember.bind(root, 'second.output', 'third.input');
}, /`Ember\.Binding` is deprecated./);
});

run(function() {
set(first, 'output', 'change');
assert.equal('change', get(first, 'output'), 'first.output');
assert.ok('change' !== get(third, 'input'), 'third.input');
});

assert.equal('change', get(first, 'output'), 'first.output');
assert.equal('change', get(second, 'input'), 'second.input');
assert.equal('change', get(second, 'output'), 'second.output');
assert.equal('change', get(third, 'input'), 'third.input');
});
});

module('Ember.Object.prototype.bind', function(hooks) {
let testObject, fromObject, TestObject;

hooks.beforeEach(function() {
TestObject = EmberObject.extend({
foo: 'bar',
bar: 'foo',
extraObject: null
});

testObject = EmberObject.create({
foo: 'bar',
bar: 'foo',
extraObject: null
});

fromObject = EmberObject.create({
bar: 'foo',
extraObject: null
});

Ember.lookup['TestNamespace'] = {
fromObject: fromObject,
testObject: testObject
};
});

test('bind(.bar) should bind to relative path', function(assert) {
run(() => {
assert.expectDeprecation(() => {
// create binding
testObject.bind('foo', 'bar');
}, /`Ember.Binding` is deprecated/);

// now make a change to see if the binding triggers.
set(testObject, 'bar', 'changedValue');
});

assert.equal('changedValue', get(testObject, 'foo'), 'testObject.foo');
});

test('bind(TestNamespace.fromObject.bar) should follow absolute path', function(assert) {
run(() => {
assert.expectDeprecation(() => {
// create binding
testObject.bind('foo', 'TestNamespace.fromObject.bar');
}, /`Ember.Binding` is deprecated/);

// now make a change to see if the binding triggers.
set(fromObject, 'bar', 'changedValue');
});

assert.equal('changedValue', get(testObject, 'foo'), 'testObject.foo');
});

test('fooBinding: .bar should bind to relative path', function(assert) {
run(() => {
assert.expectDeprecation(() => {
// create binding
testObject = TestObject.extend({
fooBinding: 'bar'
}).create();
}, /`Ember.Binding` is deprecated/);

// now make a change to see if the binding triggers.
set(testObject, 'bar', 'changedValue');
});

assert.equal('changedValue', get(testObject, 'foo'), 'testObject.foo');
});

test('fooBinding: TestNamespace.fromObject.bar should follow absolute path', function(assert) {
run(() => {
assert.expectDeprecation(() => {
// create binding
testObject = TestObject.extend({
fooBinding: 'TestNamespace.fromObject.bar'
}).create();
}, /`Ember.Binding` is deprecated/);

// now make a change to see if the binding triggers.
set(fromObject, 'bar', 'changedValue');
});

assert.equal('changedValue', get(testObject, 'foo'), 'testObject.foo');
});
});

module('rendering', function(hooks) {
setupRenderingTest(hooks);

hooks.beforeEach(function(assert) {
this.assertText = function(expectedText) {
assert.strictEqual(this.element.textContent, expectedText, `content should be: \`${expectedText}\``);
};
});

test('should accept bindings as a string or an Ember.Binding', async function() {
let FooBarComponent = Component.extend({
twoWayTestBinding: Ember.Binding.from('direction'),
stringTestBinding: 'direction',
twoWayObjectTestBinding: Ember.Binding.from('displacement.distance'),
stringObjectTestBinding: 'displacement.distance'
});

this.owner.register('component:foo-bar', FooBarComponent);
this.owner.register('template:components/foo-bar', hbs`two way: {{twoWayTest}}, string: {{stringTest}}, object: {{twoWayObjectTest}}, string object: {{stringObjectTest}}`);

this.set('direction', 'down');
this.set('displacement', { distance: 10 });

await render(hbs`{{foo-bar direction=direction displacement=displacement}}`);

this.assertText('two way: down, string: down, object: 10, string object: 10');

this.set('direction', 'up');

this.assertText('two way: up, string: up, object: 10, string object: 10');

this.set('displacement.distance', 20);

this.assertText('two way: up, string: up, object: 20, string object: 20');

run(() => {
set(this, 'direction', 'right');
set(this, 'displacement.distance', 30);
});

this.assertText('two way: right, string: right, object: 30, string object: 30');

run(() => {
set(this, 'direction', 'down');
set(this, 'displacement', { distance: 10 });
});

this.assertText('two way: down, string: down, object: 10, string object: 10');
});
});
});
Loading