@@ -2,56 +2,119 @@ describe('state', function () {
2
2
3
3
beforeEach ( module ( 'ui.state' ) ) ;
4
4
5
- describe ( '(transitions and related promises)' , function ( ) {
6
- var A = { data : { } } ;
7
-
8
- beforeEach ( module ( function ( $stateProvider ) {
9
- $stateProvider . state ( 'A' , A )
10
- } ) ) ;
5
+ var A = { data : { } } , B = { } , C = { } ;
6
+
7
+ beforeEach ( module ( function ( $stateProvider ) {
8
+ $stateProvider
9
+ . state ( 'A' , A )
10
+ . state ( 'B' , B )
11
+ . state ( 'C' , C ) ;
12
+ } ) ) ;
13
+
14
+ var log = '' ;
15
+ function eventLogger ( event , to , from ) {
16
+ log += event . name + '(' + to . name + ',' + from . name + ');' ;
17
+ }
18
+
19
+ beforeEach ( inject ( function ( $rootScope ) {
20
+ $rootScope . $on ( '$stateChangeStart' , eventLogger ) ;
21
+ $rootScope . $on ( '$stateChangeSuccess' , eventLogger ) ;
22
+ } ) ) ;
23
+
24
+ function $get ( what ) {
25
+ return jasmine . getEnv ( ) . currentSpec . $injector . get ( what ) ;
26
+ }
11
27
12
- it ( '.current is always defined' , inject ( function ( $state ) {
28
+ function initStateTo ( state ) {
29
+ var $state = $get ( '$state' ) , $q = $get ( '$q' ) ;
30
+ $state . transitionTo ( state , { } ) ;
31
+ $q . flush ( ) ;
32
+ expect ( $state . current ) . toBe ( state ) ;
33
+ log = '' ;
34
+ }
35
+
36
+
37
+ describe ( '.current' , function ( ) {
38
+ it ( 'is always defined' , inject ( function ( $state ) {
13
39
expect ( $state . current ) . toBeDefined ( ) ;
14
40
} ) ) ;
15
41
16
- it ( '.$current is always defined' , inject ( function ( $state ) {
42
+ it ( 'updates asynchronously as the transitionTo() promise is resolved' , inject ( function ( $state , $q ) {
43
+ var trans = $state . transitionTo ( A , { } ) ;
44
+ expect ( $state . current ) . not . toBe ( A ) ;
45
+ $q . flush ( ) ;
46
+ expect ( $state . current ) . toBe ( A ) ;
47
+ } ) ) ;
48
+ } ) ;
49
+
50
+
51
+ describe ( '$current' , function ( ) {
52
+ it ( 'is always defined' , inject ( function ( $state ) {
17
53
expect ( $state . $current ) . toBeDefined ( ) ;
18
54
} ) ) ;
19
55
20
- it ( '.$current wraps the raw state object' , inject ( function ( $state , $q ) {
56
+ it ( 'wraps the raw state object' , inject ( function ( $state , $q ) {
21
57
$state . transitionTo ( A , { } ) ;
22
58
$q . flush ( ) ;
23
59
expect ( $state . $current . data ) . toBe ( A . data ) ; // 'data' is reserved for app use
24
60
} ) ) ;
61
+ } ) ;
62
+
63
+
64
+ describe ( '.transition' , function ( ) {
65
+ it ( 'is null when no transition is taking place' , inject ( function ( $state , $q ) {
66
+ expect ( $state . transition ) . toBeNull ( ) ;
67
+ $state . transitionTo ( A , { } ) ;
68
+ $q . flush ( ) ;
69
+ expect ( $state . transition ) . toBeNull ( ) ;
70
+ } ) ) ;
71
+
72
+ it ( 'is the current transition' , inject ( function ( $state , $q ) {
73
+ var trans = $state . transitionTo ( A , { } ) ;
74
+ expect ( $state . transition ) . toBe ( trans ) ;
75
+ } ) ) ;
76
+ } ) ;
25
77
26
- it ( '.transitionTo() returns a promise for the target state' , inject ( function ( $state , $q ) {
78
+
79
+ describe ( '.transitionTo()' , function ( ) {
80
+ it ( 'returns a promise for the target state' , inject ( function ( $state , $q ) {
27
81
var trans = $state . transitionTo ( A , { } ) ;
28
82
$q . flush ( ) ;
29
83
expect ( resolvedValue ( trans ) ) . toBe ( A ) ;
30
84
} ) ) ;
31
85
32
- it ( '.current updates asynchronously as the transitionTo() promise is resolved' , inject ( function ( $state , $q ) {
33
- var trans = $state . transitionTo ( A , { } ) ;
34
- expect ( $state . current ) . not . toBe ( A ) ;
86
+ it ( 'is a no-op when passing the current state and identical parameters' , inject ( function ( $state , $q ) {
87
+ initStateTo ( A ) ;
88
+ var trans = $state . transitionTo ( A , { } ) ; // no-op
89
+ expect ( trans ) . toBeDefined ( ) ; // but we still get a valid promise
35
90
$q . flush ( ) ;
91
+ expect ( resolvedValue ( trans ) ) . toBe ( A ) ;
36
92
expect ( $state . current ) . toBe ( A ) ;
93
+ expect ( log ) . toBe ( '' ) ;
37
94
} ) ) ;
38
95
39
- it ( '.$transition is always the current or last transition' , inject ( function ( $state , $q ) {
40
- expect ( $state . $transition ) . toBeDefined ( ) ;
96
+ it ( 'aborts pending transitions (last call wins)' , inject ( function ( $state , $q ) {
97
+ initStateTo ( A ) ;
98
+ var superseded = $state . transitionTo ( B , { } ) ;
99
+ $state . transitionTo ( C , { } ) ;
41
100
$q . flush ( ) ;
42
- expect ( resolvedValue ( $state . $transition ) ) . toBe ( $state . current ) ;
43
- var trans = $state . transitionTo ( A , { } ) ;
44
- expect ( $state . $transition ) . toBeDefined ( ) ;
45
- expect ( $state . $transition ) . toBe ( trans ) ;
46
- $q . flush ( ) ;
47
- expect ( $state . $transition ) . toBe ( trans ) ;
101
+ expect ( $state . current ) . toBe ( C ) ;
102
+ expect ( resolvedError ( superseded ) ) . toBeDefined ( ) ;
103
+ expect ( log ) . toBe (
104
+ '$stateChangeStart(B,A);' +
105
+ '$stateChangeStart(C,A);' +
106
+ '$stateChangeSuccess(C,A);' ) ;
48
107
} ) ) ;
49
108
50
- it ( '.transition is null when no transition is taking place' , inject ( function ( $state , $q ) {
51
- expect ( $state . transition ) . toBeNull ( ) ;
109
+ it ( 'aborts pending transitions even when going back to the curren state' , inject ( function ( $state , $q ) {
110
+ initStateTo ( A ) ;
111
+ var superseded = $state . transitionTo ( B , { } ) ;
52
112
$state . transitionTo ( A , { } ) ;
53
113
$q . flush ( ) ;
54
- expect ( $state . transition ) . toBeNull ( ) ;
114
+ expect ( $state . current ) . toBe ( A ) ;
115
+ expect ( resolvedError ( superseded ) ) . toBeDefined ( ) ;
116
+ expect ( log ) . toBe (
117
+ '$stateChangeStart(B,A);' ) ;
55
118
} ) ) ;
56
119
} ) ;
57
120
} ) ;
0 commit comments