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

[Glimmer2] Add more curly component tests #13236

Merged
merged 5 commits into from
Apr 3, 2016
Merged
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
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/* globals EmberDev */
import { set } from 'ember-metal/property_set';
import { Component } from '../../utils/helpers';
import { strip } from '../../utils/abstract-test-case';
import { moduleFor, RenderingTest } from '../../utils/test-case';
import { classes } from '../../utils/test-helpers';
import { htmlSafe } from 'ember-htmlbars/utils/string';

moduleFor('Components test: curly components', class extends RenderingTest {

Expand All @@ -18,6 +20,53 @@ moduleFor('Components test: curly components', class extends RenderingTest {
this.assertComponentElement(this.firstChild, { content: 'hello' });
}

['@htmlbars it can have a custom id and it is not bound']() {
this.registerComponent('foo-bar', { template: '{{id}} {{elementId}}' });

this.render('{{foo-bar id=customId}}', {
customId: 'bizz'
});

this.assertComponentElement(this.firstChild, { tagName: 'div', attrs: { id: 'bizz' }, content: 'bizz bizz' });

this.runTask(() => this.rerender());

this.assertComponentElement(this.firstChild, { tagName: 'div', attrs: { id: 'bizz' }, content: 'bizz bizz' });

this.runTask(() => set(this.context, 'customId', 'bar'));

this.assertComponentElement(this.firstChild, { tagName: 'div', attrs: { id: 'bizz' }, content: 'bar bizz' });

this.runTask(() => set(this.context, 'customId', 'bizz'));

this.assertComponentElement(this.firstChild, { tagName: 'div', attrs: { id: 'bizz' }, content: 'bizz bizz' });
}

['@htmlbars elementId cannot change'](assert) {
let component;
let FooBarComponent = Component.extend({
elementId: 'blahzorz',
init() {
this._super(...arguments);
component = this;
}
});

this.registerComponent('foo-bar', { ComponentClass: FooBarComponent, template: '{{elementId}}' });

this.render('{{foo-bar}}');

this.assertComponentElement(this.firstChild, { tagName: 'div', attrs: { id: 'blahzorz' }, content: 'blahzorz' });

if (EmberDev && !EmberDev.runningProdBuild) {
Copy link
Member

Choose a reason for hiding this comment

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

If only https://github.com/emberjs/ember.js/blob/master/packages/ember-views/lib/views/states/in_dom.js#L24 used assert this wouldn't be needed (you could use expectAssertion which does these checks internally).

This is good though, I'm just grumbling 😈

let willThrow = () => set(component, 'elementId', 'herpyderpy');

assert.throws(willThrow, /Changing a view's elementId after creation is not allowed/);

this.assertComponentElement(this.firstChild, { tagName: 'div', attrs: { id: 'blahzorz' }, content: 'blahzorz' });
}
}

['@test it can have a custom tagName']() {
let FooBarComponent = Component.extend({
tagName: 'foo-bar'
Expand Down Expand Up @@ -81,6 +130,32 @@ moduleFor('Components test: curly components', class extends RenderingTest {
this.assertComponentElement(this.firstChild, { tagName: 'div', attrs: { 'class': classes('ember-view foo bar') }, content: 'hello' });
}

['@htmlbars it should apply classBinding without condition always']() {
this.registerComponent('foo-bar', { template: 'hello' });

this.render('{{foo-bar classBinding=":foo"}}');

this.assertComponentElement(this.firstChild, { tagName: 'div', content: 'hello', attrs: { 'class': classes('foo ember-view') } });

this.runTask(() => this.rerender());

this.assertComponentElement(this.firstChild, { tagName: 'div', content: 'hello', attrs: { 'class': classes('foo ember-view') } });
}

['@htmlbars should not apply falsy class name']() {
this.registerComponent('foo-bar', { template: 'hello' });

this.render('{{foo-bar class=somethingFalsy}}', {
somethingFalsy: false
});

this.assertComponentElement(this.firstChild, { tagName: 'div', attrs: { class: 'ember-view' }, content: 'hello' });

this.runTask(() => this.rerender());

this.assertComponentElement(this.firstChild, { tagName: 'div', attrs: { class: 'ember-view' }, content: 'hello' });
}

['@test it can have custom classNames from constructor']() {
let FooBarComponent = Component.extend({
init() {
Expand Down Expand Up @@ -332,6 +407,14 @@ moduleFor('Components test: curly components', class extends RenderingTest {
this.assertComponentElement(this.nthChild(3), { tagName: 'div', attrs: { }, content: 'hello' });
}

['@htmlbars it should not allow attributeBindings to be set']() {
this.registerComponent('foo-bar', { template: 'hello' });

expectAssertion(() => {
this.render('{{foo-bar attributeBindings="one two"}}');
}, /Setting 'attributeBindings' via template helpers is not allowed/);
}

['@test it has an element']() {
let instance;

Expand Down Expand Up @@ -611,4 +694,104 @@ moduleFor('Components test: curly components', class extends RenderingTest {
assert.deepEqual(destroyed, { 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1 });
}

['@test should escape HTML in normal mustaches']() {
let component;
let FooBarComponent = Component.extend({
init() {
this._super(...arguments);
component = this;
},
output: 'you need to be more <b>bold</b>'
});

this.registerComponent('foo-bar', { ComponentClass: FooBarComponent, template: '{{output}}' });

this.render('{{foo-bar}}');

this.assertText('you need to be more <b>bold</b>');

this.runTask(() => this.rerender());

this.assertText('you need to be more <b>bold</b>');

this.runTask(() => set(component, 'output', 'you are so <i>super</i>'));

this.assertText('you are so <i>super</i>');

this.runTask(() => set(component, 'output', 'you need to be more <b>bold</b>'));
}

['@htmlbars should not escape HTML in triple mustaches'](assert) {
let component;
let FooBarComponent = Component.extend({
init() {
this._super(...arguments);
component = this;
},
output: 'you need to be more <b>bold</b>'
});

this.registerComponent('foo-bar', { ComponentClass: FooBarComponent, template: '{{{output}}}' });

this.render('{{foo-bar}}');

assert.strictEqual(this.firstChild.childNodes.length, 4);

this.assertElement(this.firstChild.childNodes[2], { tagName: 'b', content: 'bold' });

this.runTask(() => this.rerender());

assert.strictEqual(this.firstChild.childNodes.length, 4);

this.assertElement(this.firstChild.childNodes[2], { tagName: 'b', content: 'bold' });

this.runTask(() => set(component, 'output', 'you are so <i>super</i>'));

assert.strictEqual(this.firstChild.childNodes.length, 4);

this.assertElement(this.firstChild.childNodes[2], { tagName: 'i', content: 'super' });

this.runTask(() => set(component, 'output', 'you need to be more <b>bold</b>'));

assert.strictEqual(this.firstChild.childNodes.length, 4);

this.assertElement(this.firstChild.childNodes[2], { tagName: 'b', content: 'bold' });
}

['@htmlbars should not escape HTML if string is a htmlSafe'](assert) {
let component;
let FooBarComponent = Component.extend({
init() {
this._super(...arguments);
component = this;
},
output: htmlSafe('you need to be more <b>bold</b>')
});

this.registerComponent('foo-bar', { ComponentClass: FooBarComponent, template: '{{output}}' });

this.render('{{foo-bar}}');

assert.strictEqual(this.firstChild.childNodes.length, 4);

this.assertElement(this.firstChild.childNodes[2], { tagName: 'b', content: 'bold' });

this.runTask(() => this.rerender());

assert.strictEqual(this.firstChild.childNodes.length, 4);

this.assertElement(this.firstChild.childNodes[2], { tagName: 'b', content: 'bold' });

this.runTask(() => set(component, 'output', htmlSafe('you are so <i>super</i>')));

assert.strictEqual(this.firstChild.childNodes.length, 4);

this.assertElement(this.firstChild.childNodes[2], { tagName: 'i', content: 'super' });

this.runTask(() => set(component, 'output', htmlSafe('you need to be more <b>bold</b>')));

assert.strictEqual(this.firstChild.childNodes.length, 4);

this.assertElement(this.firstChild.childNodes[2], { tagName: 'b', content: 'bold' });
}
});