|
1 | 1 | describe('state', function () {
|
2 | 2 |
|
3 |
| - var locationProvider, templateParams; |
| 3 | + var stateProvider, locationProvider, templateParams; |
4 | 4 |
|
5 | 5 | beforeEach(module('ui.router', function($locationProvider) {
|
6 | 6 | locationProvider = $locationProvider;
|
@@ -33,6 +33,7 @@ describe('state', function () {
|
33 | 33 | state.onEnter = callbackLogger('onEnter');
|
34 | 34 | state.onExit = callbackLogger('onExit');
|
35 | 35 | });
|
| 36 | + stateProvider = $stateProvider; |
36 | 37 |
|
37 | 38 | $stateProvider
|
38 | 39 | .state('A', A)
|
@@ -478,4 +479,60 @@ describe('state', function () {
|
478 | 479 | expect(templateParams).toEqual({ item: "foo" });
|
479 | 480 | }));
|
480 | 481 | });
|
| 482 | + |
| 483 | + describe('provider decorators', function () { |
| 484 | + |
| 485 | + it('should return built-in decorators', function () { |
| 486 | + expect(stateProvider.decorator('parent')({ parent: A }).self.name).toBe("A"); |
| 487 | + }); |
| 488 | + |
| 489 | + it('should allow built-in decorators to be overridden', inject(function ($state, $q) { |
| 490 | + stateProvider.decorator('data', function(state) { |
| 491 | + return angular.extend(state.data || {}, { foo: "bar" }); |
| 492 | + }); |
| 493 | + stateProvider.state('AA', { parent: A, data: { baz: "true" } }); |
| 494 | + |
| 495 | + $state.transitionTo('AA'); |
| 496 | + $q.flush(); |
| 497 | + expect($state.current.data).toEqual({ baz: 'true', foo: 'bar' }); |
| 498 | + })); |
| 499 | + |
| 500 | + it('should allow new decorators to be added', inject(function ($state, $q) { |
| 501 | + stateProvider.decorator('custom', function(state) { |
| 502 | + return function() { return "Custom functionality for state '" + state + "'" }; |
| 503 | + }); |
| 504 | + stateProvider.state('decoratorTest', {}); |
| 505 | + |
| 506 | + $state.transitionTo('decoratorTest'); |
| 507 | + $q.flush(); |
| 508 | + expect($state.$current.custom()).toBe("Custom functionality for state 'decoratorTest'"); |
| 509 | + })); |
| 510 | + |
| 511 | + it('should allow built-in decorators to be extended', inject(function ($state, $q, $httpBackend) { |
| 512 | + stateProvider.decorator('views', function(state, parent) { |
| 513 | + var result = {}; |
| 514 | + |
| 515 | + angular.forEach(parent(state), function(config, name) { |
| 516 | + result[name] = angular.extend(config, { templateFactory: function() { |
| 517 | + return "Template for " + name; |
| 518 | + }}); |
| 519 | + }); |
| 520 | + return result; |
| 521 | + }); |
| 522 | + |
| 523 | + stateProvider.state('viewTest', { |
| 524 | + views: { |
| 525 | + viewA: {}, |
| 526 | + viewB: {} |
| 527 | + } |
| 528 | + }); |
| 529 | + |
| 530 | + $state.transitionTo('viewTest'); |
| 531 | + $q.flush(); |
| 532 | + |
| 533 | + expect($state.$current.views['viewA@'].templateFactory()).toBe('Template for viewA@'); |
| 534 | + expect($state.$current.views['viewB@'].templateFactory()).toBe('Template for viewB@'); |
| 535 | + })); |
| 536 | + |
| 537 | + }); |
481 | 538 | });
|
0 commit comments