-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE ember-glimmer-template-only-components] Implement template-o…
…nly components RFC
- Loading branch information
1 parent
cb8aad2
commit 0fd6f64
Showing
9 changed files
with
249 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
packages/ember-glimmer/lib/component-managers/template-only.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { VersionedPathReference } from '@glimmer/reference'; | ||
import { CompiledDynamicProgram, ComponentDefinition, NULL_REFERENCE } from '@glimmer/runtime'; | ||
import { Opaque } from '@glimmer/util'; | ||
import Environment from '../environment'; | ||
import { OwnedTemplate, WrappedTemplateFactory } from '../template'; | ||
import AbstractManager from './abstract'; | ||
|
||
class TemplateOnlyComponentLayoutCompiler { | ||
static id = 'template-only'; | ||
|
||
constructor(public template: WrappedTemplateFactory) { | ||
} | ||
|
||
compile(builder: any) { | ||
// TODO: use fromLayout | ||
builder.wrapLayout(this.template); | ||
} | ||
} | ||
|
||
export default class TemplateOnlyComponentManager extends AbstractManager<null> { | ||
create(): null { | ||
return null; | ||
} | ||
|
||
layoutFor({ template }: TemplateOnlyComponentDefinition, _: null, env: Environment): CompiledDynamicProgram { | ||
return env.getCompiledBlock(TemplateOnlyComponentLayoutCompiler, template); | ||
} | ||
|
||
getSelf(): VersionedPathReference<Opaque> { | ||
return NULL_REFERENCE; | ||
} | ||
|
||
getDestructor() { | ||
return null; | ||
} | ||
} | ||
|
||
const MANAGER = new TemplateOnlyComponentManager(); | ||
|
||
export class TemplateOnlyComponentDefinition extends ComponentDefinition<null> { | ||
constructor(name: string, public template: OwnedTemplate) { | ||
super(name, MANAGER, null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
220 changes: 154 additions & 66 deletions
220
packages/ember-glimmer/tests/integration/components/template-only-components-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,196 @@ | ||
import { moduleFor, RenderingTest } from '../../utils/test-case'; | ||
import { classes } from '../../utils/test-helpers'; | ||
import { EMBER_GLIMMER_TEMPLATE_ONLY_COMPONENTS } from 'ember/features'; | ||
|
||
class TemplateOnlyComponentsTest extends RenderingTest { | ||
registerComponent(name, template) { | ||
super.registerComponent(name, { template, ComponentClass: null }); | ||
} | ||
} | ||
|
||
['@test it can render a template-only component']() { | ||
this.registerComponent('foo-bar', 'hello'); | ||
if (EMBER_GLIMMER_TEMPLATE_ONLY_COMPONENTS) { | ||
moduleFor('Components test: template-only components (glimmer components)', class extends TemplateOnlyComponentsTest { | ||
['@test it can render a template-only component']() { | ||
this.registerComponent('foo-bar', 'hello'); | ||
|
||
this.render('{{foo-bar}}'); | ||
this.render('{{foo-bar}}'); | ||
|
||
this.assertComponentElement(this.firstChild, { content: 'hello' }); | ||
this.assertInnerHTML('hello'); | ||
|
||
this.assertStableRerender(); | ||
} | ||
this.assertStableRerender(); | ||
} | ||
|
||
['@feature(ember-glimmer-named-arguments) it can render named arguments']() { | ||
this.registerComponent('foo-bar', '|{{@foo}}|{{@bar}}|'); | ||
['@feature(ember-glimmer-named-arguments) it can render named arguments']() { | ||
this.registerComponent('foo-bar', '|{{@foo}}|{{@bar}}|'); | ||
|
||
this.render('{{foo-bar foo=foo bar=bar}}', { | ||
foo: 'foo', bar: 'bar' | ||
}); | ||
this.render('{{foo-bar foo=foo bar=bar}}', { | ||
foo: 'foo', bar: 'bar' | ||
}); | ||
|
||
this.assertComponentElement(this.firstChild, { content: '|foo|bar|' }); | ||
this.assertInnerHTML('|foo|bar|'); | ||
|
||
this.assertStableRerender(); | ||
this.assertStableRerender(); | ||
|
||
this.runTask(() => this.context.set('foo', 'FOO')); | ||
this.runTask(() => this.context.set('foo', 'FOO')); | ||
|
||
this.assertComponentElement(this.firstChild, { content: '|FOO|bar|' }); | ||
this.assertInnerHTML('|FOO|bar|'); | ||
|
||
this.runTask(() => this.context.set('bar', 'BAR')); | ||
this.runTask(() => this.context.set('bar', 'BAR')); | ||
|
||
this.assertComponentElement(this.firstChild, { content: '|FOO|BAR|' }); | ||
this.assertInnerHTML('|FOO|BAR|'); | ||
|
||
this.runTask(() => this.context.setProperties({ foo: 'foo', bar: 'bar' })); | ||
this.runTask(() => this.context.setProperties({ foo: 'foo', bar: 'bar' })); | ||
|
||
this.assertComponentElement(this.firstChild, { content: '|foo|bar|' }); | ||
} | ||
this.assertInnerHTML('|foo|bar|'); | ||
} | ||
|
||
['@test it renders named arguments as reflected properties']() { | ||
this.registerComponent('foo-bar', '|{{foo}}|{{this.bar}}|'); | ||
['@test it does not reflected arguments as properties']() { | ||
this.registerComponent('foo-bar', '|{{foo}}|{{this.bar}}|'); | ||
|
||
this.render('{{foo-bar foo=foo bar=bar}}', { | ||
foo: 'foo', bar: 'bar' | ||
}); | ||
this.render('{{foo-bar foo=foo bar=bar}}', { | ||
foo: 'foo', bar: 'bar' | ||
}); | ||
|
||
this.assertComponentElement(this.firstChild, { content: '|foo|bar|' }); | ||
this.assertInnerHTML('|||'); | ||
|
||
this.assertStableRerender(); | ||
this.assertStableRerender(); | ||
|
||
this.runTask(() => this.context.set('foo', 'FOO')); | ||
this.runTask(() => this.context.set('foo', 'FOO')); | ||
|
||
this.assertComponentElement(this.firstChild, { content: '|FOO|bar|' }); | ||
this.assertInnerHTML('|||'); | ||
|
||
this.runTask(() => this.context.set('bar', null)); | ||
this.runTask(() => this.context.set('bar', null)); | ||
|
||
this.assertComponentElement(this.firstChild, { content: '|FOO||' }); | ||
this.assertInnerHTML('|||'); | ||
|
||
this.runTask(() => this.context.setProperties({ foo: 'foo', bar: 'bar' })); | ||
this.runTask(() => this.context.setProperties({ foo: 'foo', bar: 'bar' })); | ||
|
||
this.assertComponentElement(this.firstChild, { content: '|foo|bar|' }); | ||
} | ||
this.assertInnerHTML('|||'); | ||
} | ||
|
||
['@test it has curly component features']() { | ||
this.registerComponent('foo-bar', 'hello'); | ||
['@test it does not have curly component features']() { | ||
this.registerComponent('foo-bar', 'hello'); | ||
|
||
this.render('{{foo-bar tagName="p" class=class}}', { | ||
class: 'foo bar' | ||
}); | ||
this.render('{{foo-bar tagName="p" class=class}}', { | ||
class: 'foo bar' | ||
}); | ||
|
||
this.assertComponentElement(this.firstChild, { | ||
tagName: 'p', | ||
attrs: { class: classes('foo bar ember-view') }, | ||
content: 'hello' | ||
}); | ||
this.assertInnerHTML('hello'); | ||
|
||
this.assertStableRerender(); | ||
|
||
this.runTask(() => this.context.set('class', 'foo')); | ||
this.assertStableRerender(); | ||
|
||
this.assertComponentElement(this.firstChild, { | ||
tagName: 'p', | ||
attrs: { class: classes('foo ember-view') }, | ||
content: 'hello' | ||
}); | ||
this.runTask(() => this.context.set('class', 'foo')); | ||
|
||
this.runTask(() => this.context.set('class', null)); | ||
this.assertInnerHTML('hello'); | ||
|
||
this.assertComponentElement(this.firstChild, { | ||
tagName: 'p', | ||
attrs: { class: classes('ember-view') }, | ||
content: 'hello' | ||
}); | ||
this.runTask(() => this.context.set('class', null)); | ||
|
||
this.runTask(() => this.context.set('class', 'foo bar')); | ||
this.assertInnerHTML('hello'); | ||
|
||
this.assertComponentElement(this.firstChild, { | ||
tagName: 'p', | ||
attrs: { class: classes('foo bar ember-view') }, | ||
content: 'hello' | ||
}); | ||
} | ||
} | ||
this.runTask(() => this.context.set('class', 'foo bar')); | ||
|
||
this.assertInnerHTML('hello'); | ||
} | ||
}); | ||
} else { | ||
moduleFor('Components test: template-only components (curly components)', class extends TemplateOnlyComponentsTest { | ||
['@test it can render a template-only component']() { | ||
this.registerComponent('foo-bar', 'hello'); | ||
|
||
this.render('{{foo-bar}}'); | ||
|
||
this.assertComponentElement(this.firstChild, { content: 'hello' }); | ||
|
||
this.assertStableRerender(); | ||
} | ||
|
||
['@feature(ember-glimmer-named-arguments) it can render named arguments']() { | ||
this.registerComponent('foo-bar', '|{{@foo}}|{{@bar}}|'); | ||
|
||
this.render('{{foo-bar foo=foo bar=bar}}', { | ||
foo: 'foo', bar: 'bar' | ||
}); | ||
|
||
this.assertComponentElement(this.firstChild, { content: '|foo|bar|' }); | ||
|
||
this.assertStableRerender(); | ||
|
||
this.runTask(() => this.context.set('foo', 'FOO')); | ||
|
||
this.assertComponentElement(this.firstChild, { content: '|FOO|bar|' }); | ||
|
||
this.runTask(() => this.context.set('bar', 'BAR')); | ||
|
||
this.assertComponentElement(this.firstChild, { content: '|FOO|BAR|' }); | ||
|
||
this.runTask(() => this.context.setProperties({ foo: 'foo', bar: 'bar' })); | ||
|
||
this.assertComponentElement(this.firstChild, { content: '|foo|bar|' }); | ||
} | ||
|
||
moduleFor('Components test: template-only components', class extends TemplateOnlyComponentsTest {}); | ||
['@test it renders named arguments as reflected properties']() { | ||
this.registerComponent('foo-bar', '|{{foo}}|{{this.bar}}|'); | ||
|
||
this.render('{{foo-bar foo=foo bar=bar}}', { | ||
foo: 'foo', bar: 'bar' | ||
}); | ||
|
||
this.assertComponentElement(this.firstChild, { content: '|foo|bar|' }); | ||
|
||
this.assertStableRerender(); | ||
|
||
this.runTask(() => this.context.set('foo', 'FOO')); | ||
|
||
this.assertComponentElement(this.firstChild, { content: '|FOO|bar|' }); | ||
|
||
this.runTask(() => this.context.set('bar', null)); | ||
|
||
this.assertComponentElement(this.firstChild, { content: '|FOO||' }); | ||
|
||
this.runTask(() => this.context.setProperties({ foo: 'foo', bar: 'bar' })); | ||
|
||
this.assertComponentElement(this.firstChild, { content: '|foo|bar|' }); | ||
} | ||
|
||
['@test it has curly component features']() { | ||
this.registerComponent('foo-bar', 'hello'); | ||
|
||
this.render('{{foo-bar tagName="p" class=class}}', { | ||
class: 'foo bar' | ||
}); | ||
|
||
this.assertComponentElement(this.firstChild, { | ||
tagName: 'p', | ||
attrs: { class: classes('foo bar ember-view') }, | ||
content: 'hello' | ||
}); | ||
|
||
this.assertStableRerender(); | ||
|
||
this.runTask(() => this.context.set('class', 'foo')); | ||
|
||
this.assertComponentElement(this.firstChild, { | ||
tagName: 'p', | ||
attrs: { class: classes('foo ember-view') }, | ||
content: 'hello' | ||
}); | ||
|
||
this.runTask(() => this.context.set('class', null)); | ||
|
||
this.assertComponentElement(this.firstChild, { | ||
tagName: 'p', | ||
attrs: { class: classes('ember-view') }, | ||
content: 'hello' | ||
}); | ||
|
||
this.runTask(() => this.context.set('class', 'foo bar')); | ||
|
||
this.assertComponentElement(this.firstChild, { | ||
tagName: 'p', | ||
attrs: { class: classes('foo bar ember-view') }, | ||
content: 'hello' | ||
}); | ||
} | ||
}); | ||
} |
Oops, something went wrong.