|
1 | 1 | import Controller from '@ember/controller';
|
| 2 | +import { Component } from '@ember/-internals/glimmer'; |
2 | 3 | import { RouterTestCase, moduleFor } from 'internal-test-helpers';
|
3 | 4 | import Service, { service } from '@ember/service';
|
4 | 5 |
|
@@ -168,5 +169,47 @@ moduleFor(
|
168 | 169 | assert.deepEqual(qp.queryParams, { sort: 'ascending' });
|
169 | 170 | });
|
170 | 171 | }
|
| 172 | + |
| 173 | + ['@test RouterService#isActive works reliably during component rendering before router initialization']( |
| 174 | + assert |
| 175 | + ) { |
| 176 | + assert.expect(1); |
| 177 | + |
| 178 | + // This simulates the scenario where isActive is called during component rendering |
| 179 | + // before the router has been fully set up, which used to throw an error |
| 180 | + |
| 181 | + let componentInstance; |
| 182 | + |
| 183 | + this.addTemplate('parent.index', '{{foo-component}}'); |
| 184 | + |
| 185 | + this.addComponent('foo-component', { |
| 186 | + ComponentClass: class extends Component { |
| 187 | + @service('router') |
| 188 | + routerService; |
| 189 | + |
| 190 | + init() { |
| 191 | + super.init(); |
| 192 | + componentInstance = this; |
| 193 | + } |
| 194 | + |
| 195 | + get isRouteActive() { |
| 196 | + // This used to throw "Cannot read properties of undefined (reading 'isActiveIntent')" |
| 197 | + // before setupRouter() was added to the isActive method |
| 198 | + return this.routerService.isActive('parent.child'); |
| 199 | + } |
| 200 | + }, |
| 201 | + template: `{{this.isRouteActive}}`, |
| 202 | + }); |
| 203 | + |
| 204 | + return this.visit('/').then(() => { |
| 205 | + // The test passes if no error is thrown during rendering |
| 206 | + // and isActive returns a boolean value |
| 207 | + assert.strictEqual( |
| 208 | + typeof componentInstance.isRouteActive, |
| 209 | + 'boolean', |
| 210 | + 'isActive should return a boolean value without throwing an error' |
| 211 | + ); |
| 212 | + }); |
| 213 | + } |
171 | 214 | }
|
172 | 215 | );
|
0 commit comments