|
| 1 | +import { observer } from 'ember-metal/mixin'; |
| 2 | +import Controller from 'ember-runtime/controllers/controller'; |
| 3 | +import { RenderingTest, moduleFor } from '../../utils/test-case'; |
| 4 | +import { set } from 'ember-metal/property_set'; |
| 5 | +import EmberRouter from 'ember-routing/system/router'; |
| 6 | + |
| 7 | + |
| 8 | +moduleFor('@htmlbars Helpers test: {{render}}', class extends RenderingTest { |
| 9 | + ['@test should render given template']() { |
| 10 | + this.owner.register('controller:home', Controller.extend()); |
| 11 | + this.registerTemplate('home', '<p>BYE</p>'); |
| 12 | + |
| 13 | + this.render(`<h1>HI</h1>{{render 'home'}}`); |
| 14 | + |
| 15 | + this.assertText('HIBYE'); |
| 16 | + } |
| 17 | + |
| 18 | + ['@test should render nested helpers']() { |
| 19 | + this.owner.register('controller:home', Controller.extend()); |
| 20 | + this.owner.register('controller:foo', Controller.extend()); |
| 21 | + this.owner.register('controller:bar', Controller.extend()); |
| 22 | + this.owner.register('controller:baz', Controller.extend()); |
| 23 | + |
| 24 | + this.registerTemplate('home', '<p>BYE</p>'); |
| 25 | + this.registerTemplate('foo', `<p>FOO</p>{{render 'bar'}}`); |
| 26 | + this.registerTemplate('bar', `<p>BAR</p>{{render 'baz'}}`); |
| 27 | + this.registerTemplate('baz', `<p>BAZ</p>`); |
| 28 | + |
| 29 | + this.render('<h1>HI</h1>{{render \'foo\'}}'); |
| 30 | + this.assertText('HIFOOBARBAZ'); |
| 31 | + } |
| 32 | + |
| 33 | + ['@test should have assertion if neither template nor view exists']() { |
| 34 | + this.owner.register('controller:oops', Controller.extend()); |
| 35 | + |
| 36 | + expectAssertion(() => { |
| 37 | + this.render(`<h1>HI</h1>{{render 'oops'}}`); |
| 38 | + }, 'You used `{{render \'oops\'}}`, but \'oops\' can not be found as a template.'); |
| 39 | + } |
| 40 | + |
| 41 | + ['@test should render given template with a supplied model']() { |
| 42 | + this.owner.register('controller:post', Controller.extend()); |
| 43 | + this.registerTemplate('post', '<p>{{model.title}}</p>'); |
| 44 | + |
| 45 | + expectDeprecation(() => { |
| 46 | + this.render(`<h1>HI</h1>{{render 'post' post}}`, { |
| 47 | + post: { |
| 48 | + title: `It's Simple Made Easy` |
| 49 | + } |
| 50 | + }); |
| 51 | + }, /Please refactor [\w\{\}"` ]+ to a component/); |
| 52 | + |
| 53 | + this.assertText(`HIIt's Simple Made Easy`); |
| 54 | + |
| 55 | + this.runTask(() => this.rerender()); |
| 56 | + |
| 57 | + this.assertText(`HIIt's Simple Made Easy`); |
| 58 | + |
| 59 | + this.runTask(() => set(this.context, 'post.title', `Rails is omakase`)); |
| 60 | + |
| 61 | + this.assertText(`HIRails is omakase`); |
| 62 | + |
| 63 | + this.runTask(() => set(this.context, 'post', { title: `It's Simple Made Easy` })); |
| 64 | + |
| 65 | + this.assertText(`HIIt's Simple Made Easy`); |
| 66 | + } |
| 67 | + |
| 68 | + ['@test with a supplied model should not fire observers on the controller']() { |
| 69 | + this.owner.register('controller:post', Controller.extend()); |
| 70 | + this.registerTemplate('post', '<p>{{model.title}}</p>'); |
| 71 | + |
| 72 | + let postDidChange = 0; |
| 73 | + expectDeprecation(() => { |
| 74 | + this.render(`<h1>HI</h1>{{render 'post' post}}`, { |
| 75 | + postDidChange: observer('post', function() { |
| 76 | + postDidChange++; |
| 77 | + }), |
| 78 | + post: { |
| 79 | + title: `It's Simple Made Easy` |
| 80 | + } |
| 81 | + }); |
| 82 | + }, /Please refactor [\w\{\}"` ]+ to a component/); |
| 83 | + |
| 84 | + this.assertText(`HIIt's Simple Made Easy`); |
| 85 | + |
| 86 | + this.runTask(() => this.rerender()); |
| 87 | + |
| 88 | + this.assertText(`HIIt's Simple Made Easy`); |
| 89 | + } |
| 90 | + |
| 91 | + ['@test should raise an error when a given controller name does not resolve to a controller']() { |
| 92 | + this.registerTemplate('home', '<p>BYE</p>'); |
| 93 | + this.owner.register('controller:posts', Controller.extend()); |
| 94 | + expectAssertion(() => { |
| 95 | + this.render(`<h1>HI</h1>{{render "home" controller="postss"}}`); |
| 96 | + }, /The controller name you supplied \'postss\' did not resolve to a controller./); |
| 97 | + } |
| 98 | + |
| 99 | + ['@test should render with given controller'](assert) { |
| 100 | + this.registerTemplate('home', '{{uniqueId}}'); |
| 101 | + |
| 102 | + let id = 0; |
| 103 | + let model = {}; |
| 104 | + |
| 105 | + this.owner.register('controller:posts', Controller.extend({ |
| 106 | + init() { |
| 107 | + this._super(...arguments); |
| 108 | + this.uniqueId = id++; |
| 109 | + this.set('model', model); |
| 110 | + } |
| 111 | + })); |
| 112 | + |
| 113 | + this.render('{{render "home" controller="posts"}}'); |
| 114 | + let renderedController = this.owner.lookup('controller:posts'); |
| 115 | + let uniqueId = renderedController.get('uniqueId'); |
| 116 | + let renderedModel = renderedController.get('model'); |
| 117 | + |
| 118 | + assert.equal(uniqueId, 0); |
| 119 | + assert.equal(renderedModel, model); |
| 120 | + this.assertText('0'); |
| 121 | + |
| 122 | + this.runTask(() => this.rerender()); |
| 123 | + |
| 124 | + assert.equal(uniqueId, 0); |
| 125 | + assert.equal(renderedModel, model); |
| 126 | + this.assertText('0'); |
| 127 | + } |
| 128 | + |
| 129 | + ['@test should render templates with models multiple times'](assert) { |
| 130 | + this.owner.register('controller:post', Controller.extend()); |
| 131 | + |
| 132 | + this.registerTemplate('post', '<p>{{model.title}}</p>'); |
| 133 | + expectDeprecation(() => { |
| 134 | + this.render(`<h1>HI</h1> {{render 'post' post1}} {{render 'post' post2}}`, { |
| 135 | + post1: { |
| 136 | + title: 'Me First' |
| 137 | + }, |
| 138 | + post2: { |
| 139 | + title: 'Then me' |
| 140 | + } |
| 141 | + }); |
| 142 | + }, /Please refactor [\w\{\}"` ]+ to a component/); |
| 143 | + |
| 144 | + this.assertText('HI Me First Then me'); |
| 145 | + |
| 146 | + this.runTask(() => this.rerender()); |
| 147 | + |
| 148 | + this.assertText('HI Me First Then me'); |
| 149 | + |
| 150 | + this.runTask(() => set(this.context, 'post1.title', 'I am new')); |
| 151 | + |
| 152 | + this.assertText('HI I am new Then me'); |
| 153 | + |
| 154 | + this.runTask(() => set(this.context, 'post1', { title: 'Me First' })); |
| 155 | + |
| 156 | + this.assertText('HI Me First Then me'); |
| 157 | + } |
| 158 | + |
| 159 | + ['@test should not treat invocations with falsy contexts as context-less'](assert) { |
| 160 | + this.registerTemplate('post', '<p>{{#unless model.zero}}NOTHING{{/unless}}</p>'); |
| 161 | + this.owner.register('controller:post', Controller.extend()); |
| 162 | + |
| 163 | + expectDeprecation(() => { |
| 164 | + this.render(`<h1>HI</h1> {{render 'post' zero}} {{render 'post' nonexistent}}`, { |
| 165 | + model: { |
| 166 | + zero: false |
| 167 | + } |
| 168 | + }); |
| 169 | + }, /Please refactor [\w\{\}"` ]+ to a component/); |
| 170 | + |
| 171 | + assert.ok(this.$().text().match(/^HI ?NOTHING ?NOTHING$/)); |
| 172 | + } |
| 173 | + |
| 174 | + ['@test should render templates both with and without models'](assert) { |
| 175 | + this.registerTemplate('post', `<p>Title:{{model.title}}</p>`); |
| 176 | + this.owner.register('controller:post', Controller.extend()); |
| 177 | + |
| 178 | + let post = { |
| 179 | + title: 'Rails is omakase' |
| 180 | + }; |
| 181 | + expectDeprecation(() => { |
| 182 | + this.render(`<h1>HI</h1> {{render 'post'}} {{render 'post' post}}`, { |
| 183 | + post |
| 184 | + }); |
| 185 | + }, /Please refactor [\w\{\}"` ]+ to a component/); |
| 186 | + |
| 187 | + assert.ok(this.$().text().match(/^HI ?Title: ?Title:Rails is omakase$/)); |
| 188 | + |
| 189 | + this.runTask(() => this.rerender()); |
| 190 | + |
| 191 | + assert.ok(this.$().text().match(/^HI ?Title: ?Title:Rails is omakase$/)); |
| 192 | + |
| 193 | + this.runTask(() => set(this.context, 'post.title', 'Simple Made Easy')); |
| 194 | + |
| 195 | + assert.ok(this.$().text().match(/^HI ?Title: ?Title:Simple Made Easy$/)); |
| 196 | + |
| 197 | + this.runTask(() => set(this.context, 'post', { title: 'Rails is omakase' })); |
| 198 | + |
| 199 | + assert.ok(this.$().text().match(/^HI ?Title: ?Title:Rails is omakase$/)); |
| 200 | + } |
| 201 | + |
| 202 | + ['@test works with dot notation']() { |
| 203 | + this.registerTemplate('blog.post', '{{uniqueId}}'); |
| 204 | + |
| 205 | + let id = 0; |
| 206 | + this.owner.register('controller:blog.post', Controller.extend({ |
| 207 | + init() { |
| 208 | + this._super(...arguments); |
| 209 | + this.uniqueId = id++; |
| 210 | + } |
| 211 | + })); |
| 212 | + |
| 213 | + this.render('{{render "blog.post"}}'); |
| 214 | + |
| 215 | + this.assertText(`0`); |
| 216 | + } |
| 217 | + |
| 218 | + ['@test throws an assertion if called with an unquoted template name']() { |
| 219 | + this.registerTemplate('home', '<p>BYE</p>'); |
| 220 | + |
| 221 | + expectAssertion(() => { |
| 222 | + this.render('<h1>HI</h1>{{render home}}'); |
| 223 | + }, 'The first argument of {{render}} must be quoted, e.g. {{render "sidebar"}}.'); |
| 224 | + } |
| 225 | + |
| 226 | + ['@test throws an assertion if called with a literal for a model']() { |
| 227 | + this.registerTemplate('home', '<p>BYE</p>'); |
| 228 | + expectAssertion(() => { |
| 229 | + this.render('<h1>HI</h1>{{render "home" "model"}}', { |
| 230 | + model: { |
| 231 | + title: 'Simple Made Easy' |
| 232 | + } |
| 233 | + }); |
| 234 | + }, 'The second argument of {{render}} must be a path, e.g. {{render "post" post}}.'); |
| 235 | + } |
| 236 | + |
| 237 | + ['@test should render a template without a model only once']() { |
| 238 | + this.owner.register('controller:home', Controller.extend()); |
| 239 | + this.owner.register('router:main', EmberRouter.extend()); |
| 240 | + this.registerTemplate('home', '<p>BYE</p>'); |
| 241 | + |
| 242 | + expectAssertion(() => { |
| 243 | + this.render(`<h1>HI</h1>{{render 'home'}}<hr/>{{render 'home'}}`); |
| 244 | + }, /\{\{render\}\} helper once/i); |
| 245 | + } |
| 246 | + |
| 247 | + ['@test should set router as target when action not found on parentController is not found'](assert) { |
| 248 | + let postController; |
| 249 | + this.registerTemplate('post', 'post template'); |
| 250 | + this.owner.register('controller:post', Controller.extend({ |
| 251 | + init() { |
| 252 | + this._super(...arguments); |
| 253 | + postController = this; |
| 254 | + } |
| 255 | + })); |
| 256 | + |
| 257 | + let routerStub = { |
| 258 | + send(actionName) { |
| 259 | + assert.equal(actionName, 'someAction'); |
| 260 | + assert.ok(true, 'routerStub#send called'); |
| 261 | + } |
| 262 | + }; |
| 263 | + |
| 264 | + this.owner.register('router:main', routerStub, { instantiate: false }); |
| 265 | + |
| 266 | + expectDeprecation(() => { |
| 267 | + this.render(`{{render 'post' post1}}`); |
| 268 | + }, /Please refactor [\w\{\}"` ]+ to a component/); |
| 269 | + |
| 270 | + postController.send('someAction'); |
| 271 | + } |
| 272 | +}); |
0 commit comments