-
-
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.
Merge pull request #14979 from scalvert/router-service
Phase 3 of public Router Service - adding urlFor
- Loading branch information
Showing
5 changed files
with
314 additions
and
5 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
243 changes: 243 additions & 0 deletions
243
packages/ember/tests/routing/router_service_test/urlFor_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 |
---|---|---|
@@ -0,0 +1,243 @@ | ||
import { | ||
Controller, | ||
inject, | ||
String | ||
} from 'ember-runtime'; | ||
import { Component } from 'ember-glimmer'; | ||
import { Route, NoneLocation } from 'ember-routing'; | ||
import { | ||
get, | ||
set | ||
} from 'ember-metal'; | ||
import { | ||
RouterTestCase, | ||
moduleFor | ||
} from 'internal-test-helpers'; | ||
|
||
import { isFeatureEnabled } from 'ember-metal'; | ||
|
||
function setupController(app, name) { | ||
let controllerName = `${String.capitalize(name)}Controller`; | ||
|
||
Object.defineProperty(app, controllerName, { | ||
get() { | ||
throw new Error(`Generating a URL should not require instantiation of a ${controllerName}.`); | ||
} | ||
}); | ||
} | ||
|
||
function buildQueryParams(queryParams) { | ||
return { | ||
queryParams | ||
}; | ||
} | ||
|
||
if (isFeatureEnabled('ember-routing-router-service')) { | ||
moduleFor('Router Service - urlFor', class extends RouterTestCase { | ||
['@test RouterService#urlFor returns URL for simple route'](assert) { | ||
assert.expect(1); | ||
|
||
return this.visit('/').then(() => { | ||
let expectedURL = this.routerService.urlFor('parent.child'); | ||
|
||
assert.equal('/child', expectedURL); | ||
}); | ||
} | ||
|
||
['@test RouterService#urlFor returns URL for simple route with dynamic segments'](assert) { | ||
assert.expect(1); | ||
|
||
setupController(this.application, 'dynamic'); | ||
|
||
let dynamicModel = { id: 1, contents: 'much dynamicism' }; | ||
|
||
return this.visit('/').then(() => { | ||
let expectedURL = this.routerService.urlFor('dynamic', dynamicModel); | ||
|
||
assert.equal('/dynamic/1', expectedURL); | ||
}); | ||
} | ||
|
||
['@test RouterService#urlFor returns URL for simple route with basic query params'](assert) { | ||
assert.expect(1); | ||
|
||
let queryParams = buildQueryParams({ foo: 'bar' }); | ||
|
||
return this.visit('/').then(() => { | ||
let expectedURL = this.routerService.urlFor('parent.child', queryParams); | ||
|
||
assert.equal('/child?foo=bar', expectedURL); | ||
}); | ||
} | ||
|
||
['@test RouterService#urlFor returns URL for simple route with array as query params'](assert) { | ||
assert.expect(1); | ||
|
||
let queryParams = buildQueryParams({ selectedItems: ['a', 'b', 'c'] }); | ||
|
||
return this.visit('/').then(() => { | ||
let expectedURL = this.routerService.urlFor('parent.child', queryParams); | ||
|
||
assert.equal('/child?selectedItems[]=a&selectedItems[]=b&selectedItems[]=c', expectedURL); | ||
}); | ||
} | ||
|
||
['@test RouterService#urlFor returns URL for simple route with null query params'](assert) { | ||
assert.expect(1); | ||
|
||
let queryParams = buildQueryParams({ foo: null }); | ||
|
||
return this.visit('/').then(() => { | ||
let expectedURL = this.routerService.urlFor('parent.child', queryParams); | ||
|
||
assert.equal('/child', expectedURL); | ||
}); | ||
} | ||
|
||
['@test RouterService#urlFor returns URL for simple route with undefined query params'](assert) { | ||
assert.expect(1); | ||
|
||
let queryParams = buildQueryParams({ foo: undefined }); | ||
|
||
return this.visit('/').then(() => { | ||
let expectedURL = this.routerService.urlFor('parent.child', queryParams); | ||
|
||
assert.equal('/child', expectedURL); | ||
}); | ||
} | ||
|
||
['@test RouterService#urlFor returns URL for simple route with dynamic segments and basic query params'](assert) { | ||
assert.expect(1); | ||
|
||
let queryParams = buildQueryParams({ foo: 'bar' }); | ||
|
||
return this.visit('/').then(() => { | ||
let expectedURL = this.routerService.urlFor('dynamic', { id: 1 }, queryParams); | ||
|
||
assert.equal('/dynamic/1?foo=bar', expectedURL); | ||
}); | ||
} | ||
|
||
['@test RouterService#urlFor returns URL for simple route with dynamic segments and array as query params'](assert) { | ||
assert.expect(1); | ||
|
||
let queryParams = buildQueryParams({ selectedItems: ['a', 'b', 'c'] }); | ||
|
||
return this.visit('/').then(() => { | ||
let expectedURL = this.routerService.urlFor('dynamic', { id: 1 }, queryParams); | ||
|
||
assert.equal('/dynamic/1?selectedItems[]=a&selectedItems[]=b&selectedItems[]=c', expectedURL); | ||
}); | ||
} | ||
|
||
['@test RouterService#urlFor returns URL for simple route with dynamic segments and null query params'](assert) { | ||
assert.expect(1); | ||
|
||
let queryParams = buildQueryParams({ foo: null }); | ||
|
||
return this.visit('/').then(() => { | ||
let expectedURL = this.routerService.urlFor('dynamic', { id: 1 }, queryParams); | ||
|
||
assert.equal('/dynamic/1', expectedURL); | ||
}); | ||
} | ||
|
||
['@test RouterService#urlFor returns URL for simple route with dynamic segments and undefined query params'](assert) { | ||
assert.expect(1); | ||
|
||
let queryParams = buildQueryParams({ foo: undefined }); | ||
|
||
return this.visit('/').then(() => { | ||
let expectedURL = this.routerService.urlFor('dynamic', { id: 1 }, queryParams); | ||
|
||
assert.equal('/dynamic/1', expectedURL); | ||
}); | ||
} | ||
|
||
['@test RouterService#urlFor correctly transitions to route via generated path'](assert) { | ||
assert.expect(1); | ||
|
||
let expectedURL; | ||
|
||
return this.visit('/') | ||
.then(() => { | ||
expectedURL = this.routerService.urlFor('parent.child'); | ||
|
||
return this.routerService.transitionTo(expectedURL); | ||
}) | ||
.then(() => { | ||
assert.equal(expectedURL, this.routerService.get('currentURL')); | ||
}); | ||
} | ||
|
||
['@test RouterService#urlFor correctly transitions to route via generated path with dynamic segments'](assert) { | ||
assert.expect(1); | ||
|
||
let expectedURL; | ||
let dynamicModel = { id: 1 }; | ||
|
||
this.registerRoute('dynamic', Route.extend({ | ||
model() { | ||
return dynamicModel; | ||
} | ||
})); | ||
|
||
return this.visit('/') | ||
.then(() => { | ||
expectedURL = this.routerService.urlFor('dynamic', dynamicModel); | ||
|
||
return this.routerService.transitionTo(expectedURL); | ||
}) | ||
.then(() => { | ||
assert.equal(expectedURL, this.routerService.get('currentURL')); | ||
}); | ||
} | ||
|
||
['@test RouterService#urlFor correctly transitions to route via generated path with query params'](assert) { | ||
assert.expect(1); | ||
|
||
let expectedURL; | ||
let actualURL; | ||
let queryParams = buildQueryParams({ foo: 'bar' }); | ||
|
||
return this.visit('/') | ||
.then(() => { | ||
expectedURL = this.routerService.urlFor('parent.child', queryParams); | ||
|
||
return this.routerService.transitionTo(expectedURL); | ||
}) | ||
.then(() => { | ||
actualURL = `${this.routerService.get('currentURL')}?foo=bar`; | ||
|
||
assert.equal(expectedURL, actualURL); | ||
}); | ||
} | ||
|
||
['@test RouterService#urlFor correctly transitions to route via generated path with dynamic segments and query params'](assert) { | ||
assert.expect(1); | ||
|
||
let expectedURL; | ||
let actualURL; | ||
let queryParams = buildQueryParams({ foo: 'bar' }); | ||
let dynamicModel = { id: 1 }; | ||
|
||
this.registerRoute('dynamic', Route.extend({ | ||
model() { | ||
return dynamicModel; | ||
} | ||
})); | ||
|
||
return this.visit('/') | ||
.then(() => { | ||
expectedURL = this.routerService.urlFor('dynamic', dynamicModel, queryParams); | ||
|
||
return this.routerService.transitionTo(expectedURL); | ||
}) | ||
.then(() => { | ||
actualURL = `${this.routerService.get('currentURL')}?foo=bar`; | ||
|
||
assert.equal(expectedURL, actualURL); | ||
}); | ||
} | ||
}); | ||
} |
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