forked from angular/router
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(1.x): make pipeline configurable
- Loading branch information
Showing
5 changed files
with
254 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
describe('$pipeline', function () { | ||
|
||
var elt, | ||
$compile, | ||
$rootScope, | ||
$router, | ||
$templateCache, | ||
$controllerProvider; | ||
|
||
beforeEach(function() { | ||
module('ng'); | ||
module('ngNewRouter'); | ||
module(function(_$controllerProvider_) { | ||
$controllerProvider = _$controllerProvider_; | ||
}); | ||
}); | ||
|
||
it('should allow reconfiguration', function () { | ||
module(function($pipelineProvider, $provide) { | ||
$pipelineProvider.config([ | ||
'$setupRoutersStep', | ||
'$initLocalsStep', | ||
'myCustomStep', | ||
'$initControllersStep', | ||
'$runCanDeactivateHookStep', | ||
'$runCanActivateHookStep', | ||
'$loadTemplatesStep', | ||
'$activateStep' | ||
]); | ||
|
||
$provide.value('myCustomStep', function (instruction) { | ||
return instruction.router.traverseInstruction(instruction, function (instruction) { | ||
return instruction.locals.custom = 'hello!' | ||
}); | ||
}); | ||
}); | ||
|
||
inject(function(_$compile_, _$rootScope_, _$router_, _$templateCache_) { | ||
$compile = _$compile_; | ||
$rootScope = _$rootScope_; | ||
$router = _$router_; | ||
$templateCache = _$templateCache_; | ||
}); | ||
|
||
put('one', '<div>{{one.custom}}</div>'); | ||
$controllerProvider.register('OneController', function (custom) { | ||
this.custom = custom; | ||
}); | ||
|
||
$router.config([ | ||
{ path: '/', component: 'one' } | ||
]); | ||
|
||
compile('<ng-viewport></ng-viewport>'); | ||
|
||
$router.navigate('/'); | ||
$rootScope.$digest(); | ||
|
||
expect(elt.text()).toBe('hello!'); | ||
}); | ||
|
||
function put (name, template) { | ||
$templateCache.put(componentTemplatePath(name), [200, template, {}]); | ||
} | ||
|
||
function compile(template) { | ||
elt = $compile('<div>' + template + '</div>')($rootScope); | ||
$rootScope.$digest(); | ||
return elt; | ||
} | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Helpers to keep tests DRY | ||
*/ | ||
|
||
function componentTemplatePath(name) { | ||
return './components/' + dashCase(name) + '/' + dashCase(name) + '.html'; | ||
} | ||
|
||
function componentControllerName(name) { | ||
return name[0].toUpperCase() + name.substr(1) + 'Controller'; | ||
} | ||
|
||
function dashCase(str) { | ||
return str.replace(/([A-Z])/g, function ($1) { | ||
return '-' + $1.toLowerCase(); | ||
}); | ||
} | ||
|
||
function boringController (model, value) { | ||
return function () { | ||
this[model] = value; | ||
}; | ||
} | ||
|
||
function provideHelpers(fn, preInject) { | ||
return function () { | ||
var elt, | ||
$compile, | ||
$rootScope, | ||
$router, | ||
$templateCache, | ||
$controllerProvider; | ||
|
||
module('ng'); | ||
module('ngNewRouter'); | ||
module(function(_$controllerProvider_) { | ||
$controllerProvider = _$controllerProvider_; | ||
}); | ||
|
||
inject(function(_$compile_, _$rootScope_, _$router_, _$templateCache_) { | ||
$compile = _$compile_; | ||
$rootScope = _$rootScope_; | ||
$router = _$router_; | ||
$templateCache = _$templateCache_; | ||
}); | ||
|
||
function registerComponent(name, template, config) { | ||
if (!template) { | ||
template = ''; | ||
} | ||
var ctrl; | ||
if (!config) { | ||
ctrl = function () {}; | ||
} else if (angular.isArray(config)) { | ||
ctrl = function () {}; | ||
ctrl.$routeConfig = config; | ||
} else if (typeof config === 'function') { | ||
ctrl = config; | ||
} else { | ||
ctrl = function () {}; | ||
ctrl.prototype = config; | ||
} | ||
$controllerProvider.register(componentControllerName(name), ctrl); | ||
put(name, template); | ||
} | ||
|
||
|
||
function put (name, template) { | ||
$templateCache.put(componentTemplatePath(name), [200, template, {}]); | ||
} | ||
|
||
function compile(template) { | ||
var elt = $compile('<div>' + template + '</div>')($rootScope); | ||
$rootScope.$digest(); | ||
return elt; | ||
} | ||
|
||
fn({ | ||
registerComponent: registerComponent, | ||
$router: $router, | ||
put: put, | ||
compile: compile | ||
}) | ||
} | ||
} |