forked from ionic-team/ionic-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathionicViewService.unit.js
777 lines (667 loc) · 33.5 KB
/
ionicViewService.unit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
describe('Ionic View Service', function() {
var viewService, rootScope, stateProvider, window;
beforeEach(module('ionic.service.view'));
beforeEach(module('ui.router'));
beforeEach(module(function ($stateProvider, $provide) {
stateProvider = $stateProvider;
$stateProvider
.state('home', { url: "/" })
.state('home.item', { url: "front/:id" })
.state('about', { url: "/about" })
.state('about.person', { url: "/person" })
.state('about.person.item', { url: "/id" })
.state('about.sidebar', {})
.state('about.sidebar.item', {})
.state('contact', { url: "/contact" })
.state('info', { url: "/info" })
.state('tabs', { abstract: true })
.state('tabs.tab1view1', {})
.state('tabs.tab1view2', {})
.state('tabs.tab1view3', {})
.state('tabs.tab2view1', {})
.state('tabs.tab2view2', {})
.state('tabs.tab2view3', {})
.state('tabs.tab3view1', {})
.state('tabs.tab3view2', {})
.state('tabs.tab3view3', {})
}));
beforeEach(inject(function($ionicViewService, $rootScope, $window, $location) {
viewService = $ionicViewService;
rootScope = $rootScope;
window = $window;
window.history.go = function(val) { return val };
}));
it('should do nothing if the same state happens', inject(function($state) {
var uiViewScope = {};
$state.go('home');
rootScope.$apply();
viewService.register(uiViewScope);
homeViewScope = {};
$state.go('home');
rootScope.$apply();
registerData = viewService.register(homeViewScope);
expect(registerData.navAction).toEqual('noChange');
expect(registerData.historyId).toEqual('root');
}));
it('should create a new view', inject(function($location, $state) {
$location.url('/home');
var view1Scope = {};
var rsp = viewService.register(view1Scope);
expect(rsp.navAction).toEqual('initialView');
expect(rsp.historyId).toEqual('root');
var currentView = viewService.getCurrentView();
expect(currentView.viewId).toBeDefined();
expect(currentView.index).toEqual(0);
expect(currentView.historyId).toBeDefined();
expect(currentView.backViewId).toEqual(null);
expect(currentView.forwardViewId).toEqual(null);
expect(currentView.url).toEqual('/home');
var hist = rootScope.$viewHistory.histories.root;
expect(hist.cursor).toEqual(0);
expect(hist.stack.length).toEqual(1);
}));
it('should register two sequential views', inject(function($state) {
$state.go('home');
rootScope.$apply();
expect(viewService.getCurrentStateName()).toEqual('home');
var view1Scope = {};
var rsp = viewService.register(view1Scope);
expect(rootScope.$viewHistory.currentView.stateName).toEqual('home');
expect(rsp.viewId).not.toBeUndefined();
expect(rootScope.$viewHistory.views[rsp.viewId].viewId).toEqual(rsp.viewId);
expect(viewService.getBackView()).toEqual(null);
expect(viewService.getForwardView()).toEqual(null);
expect(rootScope.$viewHistory.currentView.stateName).toEqual('home');
var currentView = viewService.getCurrentView();
expect(currentView.index).toEqual(0);
$state.go('about');
rootScope.$apply();
expect(viewService.getCurrentStateName()).toEqual('about');
rsp = viewService.register({});
expect(rsp.navAction).toEqual('newView');
expect(viewService.getCurrentView().stateName).toEqual('about');
expect(viewService.getBackView().stateName).toEqual('home');
expect(viewService.getForwardView()).toEqual(null);
var hist = rootScope.$viewHistory.histories.root;
expect(hist.cursor).toEqual(1);
expect(hist.stack.length).toEqual(2);
}));
it('should register views and go back to start', inject(function($state) {
$state.go('home');
rootScope.$apply();
var registerData = viewService.register({});
expect(viewService.getCurrentView().stateName).toEqual('home');
expect(viewService.getBackView()).toEqual(null);
expect(viewService.getForwardView()).toEqual(null);
expect(registerData.navDirection).toEqual(null);
expect(registerData.navAction).toEqual('initialView');
$state.go('about');
rootScope.$apply();
registerData = viewService.register({});
var currentView = viewService.getCurrentView();
var backView = viewService.getBackView();
var forwardView = viewService.getForwardView();
expect(currentView.stateName).toEqual('about');
expect(currentView.backViewId).toEqual(backView.viewId);
expect(backView.stateName).toEqual('home');
expect(forwardView).toEqual(null);
expect(registerData.navDirection).toEqual('forward');
expect(registerData.navAction).toEqual('newView');
expect(rootScope.$viewHistory.histories.root.cursor).toEqual(1);
expect(rootScope.$viewHistory.histories.root.stack.length).toEqual(2);
$state.go('contact');
rootScope.$apply();
registerData = viewService.register({});
currentView = viewService.getCurrentView();
//Set test value for remembered scroll
backView = viewService.getBackView();
forwardView = viewService.getForwardView();
expect(backView.stateName).toEqual('about');
expect(currentView.backViewId).toEqual(backView.viewId);
expect(viewService.getForwardView()).toEqual(null);
expect(registerData.navDirection).toEqual('forward');
expect(registerData.navAction).toEqual('newView');
expect(rootScope.$viewHistory.histories.root.cursor).toEqual(2);
expect(rootScope.$viewHistory.histories.root.stack.length).toEqual(3);
$state.go('about');
rootScope.$apply();
registerData = viewService.register({});
currentView = viewService.getCurrentView();
backView = viewService.getBackView();
forwardView = viewService.getForwardView();
expect(currentView.backViewId).toEqual(backView.viewId);
expect(currentView.forwardViewId).toEqual(forwardView.viewId);
expect(backView.stateName).toEqual('home');
expect(forwardView.stateName).toEqual('contact');
expect(registerData.navDirection).toEqual('back');
expect(registerData.navAction).toEqual('moveBack');
expect(rootScope.$viewHistory.histories.root.cursor).toEqual(1);
expect(rootScope.$viewHistory.histories.root.stack.length).toEqual(3);
$state.go('home');
rootScope.$apply();
registerData = viewService.register({});
currentView = viewService.getCurrentView();
backView = viewService.getBackView();
forwardView = viewService.getForwardView();
expect(currentView.stateName).toEqual('home');
expect(currentView.forwardViewId).toEqual(forwardView.viewId);
expect(backView).toEqual(null);
expect(forwardView.stateName).toEqual('about');
expect(registerData.navDirection).toEqual('back');
expect(registerData.navAction).toEqual('moveBack');
expect(rootScope.$viewHistory.histories.root.cursor).toEqual(0);
expect(rootScope.$viewHistory.histories.root.stack.length).toEqual(3);
}));
it('should register four views, and not go back to the first', inject(function($state) {
var homeViewScope = {};
$state.go('home');
rootScope.$apply();
var homeReg = viewService.register(homeViewScope);
expect(homeReg.navAction).toEqual('initialView');
expect(viewService.getCurrentStateName()).toEqual('home');
expect(viewService.getCurrentView().stateName).toEqual('home');
expect(viewService.getBackView()).toEqual(null);
expect(viewService.getForwardView()).toEqual(null);
expect(rootScope.$viewHistory.histories.root.cursor).toEqual(0);
expect(rootScope.$viewHistory.histories.root.stack.length).toEqual(1);
var aboutViewScope = {};
$state.go('about');
rootScope.$apply();
var aboutReg = viewService.register(aboutViewScope);
var currentView = viewService.getCurrentView();
var backView = viewService.getBackView();
var forwardView = viewService.getForwardView();
expect(aboutReg.navAction).toEqual('newView');
expect(currentView.viewId).toEqual(aboutReg.viewId);
expect(currentView.backViewId).toEqual(homeReg.viewId);
expect(currentView.forwardViewId).toEqual(null);
expect(backView.viewId).toEqual(homeReg.viewId);
expect(backView.forwardViewId).toEqual(currentView.viewId);
expect(rootScope.$viewHistory.histories.root.cursor).toEqual(1);
expect(rootScope.$viewHistory.histories.root.stack.length).toEqual(2);
var tab1Scope = {};
viewService.registerHistory(tab1Scope);
var tab1view1Scope = { $parent: tab1Scope };
$state.go('tabs.tab1view1');
rootScope.$apply();
var tab1view1Reg = viewService.register(tab1view1Scope);
expect(tab1view1Reg.navAction).toEqual('newView');
expect(rootScope.$viewHistory.histories[tab1Scope.$historyId].historyId).toEqual(tab1Scope.$historyId);
expect(rootScope.$viewHistory.histories[tab1Scope.$historyId].stack[0].viewId).toEqual(tab1view1Reg.viewId);
expect(rootScope.$viewHistory.histories[tab1Scope.$historyId].cursor).toEqual(0);
expect(rootScope.$viewHistory.histories[tab1Scope.$historyId].stack.length).toEqual(1);
expect(rootScope.$viewHistory.histories.root.cursor).toEqual(1);
expect(rootScope.$viewHistory.histories.root.stack.length).toEqual(2);
currentView = viewService.getCurrentView();
backView = viewService.getBackView();
forwardView = viewService.getForwardView();
expect(currentView.viewId).toEqual(tab1view1Reg.viewId);
expect(currentView.historyId).toEqual(tab1Scope.$historyId);
expect(currentView.historyId).toEqual(tab1view1Reg.historyId);
expect(currentView.backViewId).toEqual(aboutReg.viewId);
expect(currentView.forwardViewId).toEqual(null);
expect(backView.viewId).toEqual(aboutReg.viewId);
expect(backView.forwardViewId).toEqual(currentView.viewId);
$state.go('home');
rootScope.$apply();
var home2reg = viewService.register({});
expect(home2reg.navAction).toEqual('newView');
currentView = viewService.getCurrentView();
backView = viewService.getBackView();
forwardView = viewService.getForwardView();
expect(currentView.backViewId).toEqual(tab1view1Reg.viewId);
expect(currentView.forwardViewId).toEqual(null);
expect(backView.viewId).toEqual(tab1view1Reg.viewId);
expect(backView.forwardViewId).toEqual(currentView.viewId);
expect(rootScope.$viewHistory.histories.root.cursor).toEqual(2);
expect(rootScope.$viewHistory.histories.root.stack.length).toEqual(3);
expect(rootScope.$viewHistory.histories[tab1Scope.$historyId].cursor).toEqual(0);
expect(rootScope.$viewHistory.histories[tab1Scope.$historyId].stack.length).toEqual(1);
}));
it('should register views in the same history, go back, then overwrite the forward views', inject(function($state) {
var homeViewScope = {};
$state.go('home');
rootScope.$apply();
var homeReg = viewService.register(homeViewScope);
currentView = viewService.getCurrentView();
backView = viewService.getBackView();
forwardView = viewService.getForwardView();
expect(currentView.viewId).toEqual(homeReg.viewId);
expect(currentView.backViewId).toEqual(null);
expect(currentView.forwardViewId).toEqual(null);
expect(rootScope.$viewHistory.histories.root.cursor).toEqual(0);
expect(rootScope.$viewHistory.histories.root.stack.length).toEqual(1);
expect(homeReg.navAction).toEqual('initialView');
expect(homeReg.navDirection).toEqual(null);
var aboutScope = {};
$state.go('about');
rootScope.$apply();
var aboutReg = viewService.register(aboutScope);
currentView = viewService.getCurrentView();
backView = viewService.getBackView();
forwardView = viewService.getForwardView();
expect(currentView.viewId).toEqual(aboutReg.viewId);
expect(currentView.backViewId).toEqual(homeReg.viewId);
expect(currentView.forwardViewId).toEqual(null);
expect(backView.viewId).toEqual(homeReg.viewId);
expect(backView.forwardViewId).toEqual(currentView.viewId);
expect(rootScope.$viewHistory.histories.root.cursor).toEqual(1);
expect(rootScope.$viewHistory.histories.root.stack.length).toEqual(2);
expect(aboutReg.navAction).toEqual('newView');
expect(aboutReg.navDirection).toEqual('forward');
homeViewScope = {};
$state.go('home');
rootScope.$apply();
var homeReg2 = viewService.register(homeViewScope);
currentView = viewService.getCurrentView();
backView = viewService.getBackView();
forwardView = viewService.getForwardView();
expect(currentView.viewId).toEqual(homeReg.viewId);
expect(currentView.backViewId).toEqual(null);
expect(currentView.forwardViewId).toEqual(aboutReg.viewId);
expect(forwardView.viewId).toEqual(aboutReg.viewId);
expect(forwardView.backViewId).toEqual(currentView.viewId);
expect(rootScope.$viewHistory.histories.root.cursor).toEqual(0);
expect(rootScope.$viewHistory.histories.root.stack.length).toEqual(2);
expect(homeReg2.navAction).toEqual('moveBack');
expect(homeReg2.navDirection).toEqual('back');
// this should overwrite that we went to the "about" view
contactScope = {};
$state.go('contact');
rootScope.$apply();
var contactReg = viewService.register(contactScope);
currentView = viewService.getCurrentView();
backView = viewService.getBackView();
forwardView = viewService.getForwardView();
expect(currentView.backViewId).toEqual(homeReg.viewId);
expect(currentView.forwardViewId).toEqual(null);
expect(forwardView).toEqual(null);
expect(backView.viewId).toEqual(homeReg.viewId);
expect(backView.forwardViewId).toEqual(currentView.viewId);
expect(rootScope.$viewHistory.histories.root.cursor).toEqual(1);
expect(rootScope.$viewHistory.histories.root.stack.length).toEqual(2);
expect(contactReg.navAction).toEqual('newView');
expect(contactReg.navDirection).toEqual('forward');
}));
it('should go to a new history, come back out, go to same history and come back out', inject(function($state) {
var rootViewContainer = {};
$state.go('home');
rootScope.$apply();
var homeReg = viewService.register(rootViewContainer);
var currentView = viewService.getCurrentView();
expect(currentView.historyId).toEqual('root');
expect(rootScope.$viewHistory.histories.root.cursor).toEqual(0);
expect(homeReg.navAction).toEqual('initialView');
// each tab gets its own history in the tabs directive
// create a new tab and its history
var tabs1Container = { $parent: rootViewContainer };
viewService.registerHistory(tabs1Container);
expect(tabs1Container.$historyId).toBeDefined();
expect(rootViewContainer.$historyId).not.toEqual(tabs1Container.$historyId);
var originalTab1ViewId = tabs1Container.$historyId;
// the actual view within the tab renders
// nav to tab1 which has its own history
var tab1View = { $parent: tabs1Container };
$state.go('tabs.tab1view1');
rootScope.$apply();
var tab1view1Reg = viewService.register(tab1View);
currentView = viewService.getCurrentView();
expect(currentView.historyId).toEqual(tabs1Container.$historyId);
expect(rootScope.$viewHistory.histories[tabs1Container.$historyId].parentHistoryId).toEqual('root');
expect(rootScope.$viewHistory.histories[tabs1Container.$historyId].cursor).toEqual(0);
expect(rootScope.$viewHistory.histories[tabs1Container.$historyId].stack.length).toEqual(1);
expect(tab1view1Reg.navAction).toEqual('newView');
currentView = viewService.getCurrentView();
backView = viewService.getBackView();
forwardView = viewService.getForwardView();
expect(currentView.stateName).toEqual('tabs.tab1view1');
expect(currentView.viewId).toEqual(tab1view1Reg.viewId);
expect(currentView.backViewId).toEqual(homeReg.viewId);
expect(currentView.forwardViewId).toEqual(null);
expect(backView.stateName).toEqual('home');
expect(backView.backViewId).toEqual(null);
expect(backView.forwardViewId).toEqual(currentView.viewId);
expect(forwardView).toEqual(null);
// nav back to the home in the root history
homeViewScope = {};
$state.go('home');
rootScope.$apply();
var homeReg = viewService.register(homeViewScope);
expect(rootScope.$viewHistory.histories.root.cursor).toEqual(0);
expect(rootScope.$viewHistory.histories.root.stack.length).toEqual(1);
expect(homeReg.historyId).toEqual('root');
currentView = viewService.getCurrentView();
backView = viewService.getBackView();
forwardView = viewService.getForwardView();
expect(currentView.stateName).toEqual('home');
expect(currentView.backViewId).toEqual(null);
expect(currentView.forwardViewId).toEqual(tab1view1Reg.viewId);
expect(forwardView.stateName).toEqual('tabs.tab1view1');
expect(forwardView.viewId).toEqual(tab1view1Reg.viewId);
expect(forwardView.backViewId).toEqual(currentView.viewId);
expect(forwardView.forwardViewId).toEqual(null);
expect(rootScope.$viewHistory.histories.root.cursor).toEqual(0);
expect(rootScope.$viewHistory.histories.root.stack.length).toEqual(1);
// create a new tab and its history
tabs1Container = { $parent: rootViewContainer };
viewService.registerHistory(tabs1Container);
expect(originalTab1ViewId).not.toEqual(tabs1Container.$historyId);
tab1View = { $parent: tabs1Container };
$state.go('tabs.tab1view1');
rootScope.$apply();
tab1view1Reg = viewService.register(tab1View);
expect(tab1view1Reg.navAction).toEqual('moveForward');
expect(tab1view1Reg.navDirection).toEqual(null);
expect(tab1view1Reg.historyId).toEqual(originalTab1ViewId);
expect(originalTab1ViewId).toEqual(tabs1Container.$historyId);
expect(tab1view1Reg.historyId).not.toEqual('root');
expect(rootScope.$viewHistory.histories[tab1view1Reg.historyId].cursor).toEqual(0);
expect(rootScope.$viewHistory.histories[tab1view1Reg.historyId].stack.length).toEqual(1);
currentView = viewService.getCurrentView();
expect(currentView.historyId).toEqual(tabs1Container.$historyId);
expect(rootScope.$viewHistory.histories[tabs1Container.$historyId].cursor).toEqual(0);
currentView = viewService.getCurrentView();
backView = viewService.getBackView();
forwardView = viewService.getForwardView();
expect(currentView.stateName).toEqual('tabs.tab1view1');
expect(currentView.viewId).toEqual(tab1view1Reg.viewId);
expect(currentView.backViewId).toEqual(homeReg.viewId);
expect(currentView.forwardViewId).toEqual(null);
expect(backView.viewId).toEqual(homeReg.viewId);
expect(backView.stateName).toEqual('home');
expect(backView.backViewId).toEqual(null);
expect(backView.forwardViewId).toEqual(currentView.viewId);
expect(forwardView).toEqual(null);
expect(rootScope.$viewHistory.histories.root.cursor).toEqual(0);
}));
it('should nav to a history, move around in it, and come back', inject(function($state) {
// go to the first page
$state.go('home');
rootScope.$apply();
var homeReg = viewService.register({});
// each tab gets its own history in the tabs directive
var tab1Scope = { };
var tab2Scope = { };
var tab3Scope = { };
viewService.registerHistory(tab1Scope);
viewService.registerHistory(tab2Scope);
viewService.registerHistory(tab3Scope);
// the actual view renders
var tab1view1Scope = { $parent: tab1Scope };
$state.go('tabs.tab1view1');
rootScope.$apply();
var tab1view1ScopeReg = viewService.register(tab1view1Scope);
expect(viewService.getCurrentStateName()).toEqual('tabs.tab1view1');
expect(viewService.getBackView().stateName).toEqual('home');
expect(viewService.getForwardView()).toEqual(null);
var lastView = viewService.getCurrentView();
expect(lastView.index).toEqual(0);
expect(tab1view1ScopeReg.viewId).toEqual(lastView.viewId);
expect(tab1view1ScopeReg.navAction).toEqual('newView');
expect(tab1view1ScopeReg.navDirection).toEqual(null);
expect(rootScope.$viewHistory.histories[tab1view1ScopeReg.historyId].cursor).toEqual(0);
expect(rootScope.$viewHistory.histories[tab1view1ScopeReg.historyId].stack.length).toEqual(1);
// inside first tab, go to another list inside the same tab
var tab1view2Scope = { $parent: tab1Scope };
$state.go('tabs.tab1view2');
rootScope.$apply();
var tab1view2ScopeReg = viewService.register(tab1view2Scope);
expect(viewService.getCurrentStateName()).toEqual('tabs.tab1view2');
expect(viewService.getBackView().stateName).toEqual('tabs.tab1view1');
expect(viewService.getForwardView()).toEqual(null);
var lastView = viewService.getCurrentView();
expect(lastView.index).toEqual(1);
expect(tab1view2ScopeReg.viewId).toEqual(lastView.viewId);
expect(tab1view2ScopeReg.navAction).toEqual('newView');
expect(tab1view2ScopeReg.navDirection).toEqual('forward');
expect(rootScope.$viewHistory.histories[tab1view2ScopeReg.historyId].cursor).toEqual(1);
expect(rootScope.$viewHistory.histories[tab1view2ScopeReg.historyId].stack.length).toEqual(2);
// go back one within the tab
$state.go('tabs.tab1view1');
rootScope.$apply();
var tab1view1Scope2Reg = viewService.register(tab1view1Scope);
expect(viewService.getCurrentStateName()).toEqual('tabs.tab1view1');
expect(viewService.getBackView().stateName).toEqual('home');
expect(viewService.getForwardView().stateName).toEqual('tabs.tab1view2');
var lastView = viewService.getCurrentView();
expect(lastView.index).toEqual(0);
expect(tab1view1Scope2Reg.navAction).toEqual('moveBack');
expect(tab1view1Scope2Reg.navDirection).toEqual('back');
expect(rootScope.$viewHistory.histories[tab1view1Scope2Reg.historyId].cursor).toEqual(0);
expect(rootScope.$viewHistory.histories[tab1view1Scope2Reg.historyId].stack.length).toEqual(2);
// go back again, and should break out of the tab's history
$state.go('home');
rootScope.$apply();
var homeReg2 = viewService.register({});
expect(viewService.getCurrentStateName()).toEqual('home');
expect(homeReg2.historyId).toEqual('root');
expect(homeReg2.navAction).toEqual('moveBack');
expect(homeReg2.navDirection).toEqual(null);
expect(rootScope.$viewHistory.histories[homeReg2.historyId].cursor).toEqual(0);
expect(rootScope.$viewHistory.histories[homeReg2.historyId].stack.length).toEqual(1);
$state.go('about');
rootScope.$apply();
var aboutReg = viewService.register({});
expect(viewService.getCurrentStateName()).toEqual('about');
expect(aboutReg.historyId).toEqual('root');
expect(aboutReg.navAction).toEqual('newView');
expect(aboutReg.navDirection).toEqual('forward');
expect(rootScope.$viewHistory.histories[aboutReg.historyId].cursor).toEqual(1);
expect(rootScope.$viewHistory.histories[aboutReg.historyId].stack.length).toEqual(2);
}));
it('should change to history that already exists, and go to its last current view', inject(function($location, $state) {
// register tabs
var tab1Scope = {};
var tab2Scope = {};
viewService.registerHistory(tab1Scope);
viewService.registerHistory(tab2Scope);
var orgTab1HistoryId = tab1Scope.$historyId;
// render first view in tab1
var tab1view1Scope = { $parent: tab1Scope };
$state.go('tabs.tab1view1');
rootScope.$apply();
var registerData = viewService.register(tab1view1Scope);
expect(viewService.getCurrentStateName()).toEqual('tabs.tab1view1');
expect(rootScope.$viewHistory.histories[tab1Scope.$historyId].cursor).toEqual(0);
expect(rootScope.$viewHistory.histories[tab1Scope.$historyId].stack.length).toEqual(1);
expect(registerData.navAction).toEqual('initialView');
expect(registerData.navDirection).toEqual(null);
// render second view in tab1
var tab1view2Scope = { $parent: tab1Scope };
$state.go('tabs.tab1view2');
rootScope.$apply();
registerData = viewService.register(tab1view2Scope);
expect(viewService.getCurrentStateName()).toEqual('tabs.tab1view2');
expect(rootScope.$viewHistory.histories[tab1Scope.$historyId].cursor).toEqual(1);
expect(rootScope.$viewHistory.histories[tab1Scope.$historyId].stack.length).toEqual(2);
expect(registerData.navAction).toEqual('newView');
expect(registerData.navDirection).toEqual('forward');
currentView = viewService.getCurrentView();
expect(currentView.viewId).toEqual(registerData.viewId);
// go back to the first view again in tab 1
tab1view1Scope = { $parent: tab1Scope };
$state.go('tabs.tab1view1');
rootScope.$apply();
registerData = viewService.register(tab1view1Scope);
expect(viewService.getCurrentStateName()).toEqual('tabs.tab1view1');
currentView = viewService.getCurrentView();
expect(currentView.viewId).toEqual(registerData.viewId);
forwardView = viewService.getForwardView();
expect(currentView.forwardViewId).toEqual(rootScope.$viewHistory.histories[tab1Scope.$historyId].stack[1].viewId);
expect(forwardView.backViewId).toEqual(currentView.viewId);
expect(rootScope.$viewHistory.histories[tab1Scope.$historyId].cursor).toEqual(0);
expect(rootScope.$viewHistory.histories[tab1Scope.$historyId].stack.length).toEqual(2);
expect(registerData.navAction).toEqual('moveBack');
expect(registerData.navDirection).toEqual('back');
// render first view in tab2
var tab2view1Scope = { $parent: tab2Scope };
$state.go('tabs.tab2view1');
rootScope.$apply();
registerData = viewService.register(tab2view1Scope);
expect(viewService.getCurrentStateName()).toEqual('tabs.tab2view1');
expect(rootScope.$viewHistory.histories[tab2Scope.$historyId].cursor).toEqual(0);
expect(rootScope.$viewHistory.histories[tab2Scope.$historyId].stack.length).toEqual(1);
expect(registerData.navAction).toEqual('newView');
expect(registerData.navDirection).toEqual(null);
var tab2view1ViewId = registerData.viewId;
// tab1's forward history should be destroyed
expect(rootScope.$viewHistory.histories[tab1Scope.$historyId].stack.length).toEqual(1);
expect(rootScope.$viewHistory.histories[tab1Scope.$historyId].stack[0].forwardViewId).toEqual(registerData.viewId);
// go back to tab1, and it should load the first view of tab1
expect(tab1Scope.$historyId).toEqual(orgTab1HistoryId);
rootScope.$broadcast("viewState.changeHistory", { historyId: tab1Scope.$historyId, enableUrlChange: false });
rootScope.$apply();
expect(viewService.getCurrentStateName()).toEqual('tabs.tab1view1');
registerData = viewService.register(tab1view1Scope);
expect(registerData.navAction).toEqual('moveBack');
expect(registerData.navDirection).toEqual(null);
currentView = viewService.getCurrentView();
expect(currentView.viewId).toEqual(registerData.viewId);
expect(currentView.historyId).toEqual(orgTab1HistoryId);
expect(currentView.forwardViewId).toEqual(tab2view1ViewId);
expect(rootScope.$viewHistory.histories[tab1Scope.$historyId].cursor).toEqual(0);
expect(rootScope.$viewHistory.histories[tab1Scope.$historyId].stack.length).toEqual(1);
currentView = viewService.getCurrentView();
expect(currentView.stateName).toEqual('tabs.tab1view1');
expect(currentView.historyId).toEqual(tab1Scope.$historyId);
// go to view 2 in tab 1
tab1view2Scope = { $parent: tab1Scope };
$state.go('tabs.tab1view2');
rootScope.$apply();
registerData = viewService.register(tab1view2Scope);
expect(registerData.historyId).toEqual(orgTab1HistoryId);
var tab1view2ViewId = registerData.viewId;
expect(viewService.getCurrentStateName()).toEqual('tabs.tab1view2');
expect(rootScope.$viewHistory.histories[tab1Scope.$historyId].stack.length).toEqual(2);
expect(rootScope.$viewHistory.histories[tab1Scope.$historyId].cursor).toEqual(1);
expect(registerData.navAction).toEqual('newView');
expect(registerData.navDirection).toEqual('forward');
// go to view 1 in tab 2
tab2view1Scope = { $parent: tab2Scope };
$state.go('tabs.tab2view1');
rootScope.$apply();
registerData = viewService.register(tab2view1Scope);
expect(viewService.getCurrentStateName()).toEqual('tabs.tab2view1');
expect(rootScope.$viewHistory.histories[tab2Scope.$historyId].cursor).toEqual(0);
expect(registerData.navAction).toEqual('newView');
expect(registerData.navDirection).toEqual(null);
currentView = viewService.getCurrentView();
expect(currentView.backViewId).toEqual(tab1view2ViewId);
expect(currentView.forwardViewId).toEqual(null);
// should be remembered at the tab 1 view 2
rootScope.$broadcast("viewState.changeHistory", { historyId: tab1Scope.$historyId });
rootScope.$apply();
expect(viewService.getCurrentStateName()).toEqual('tabs.tab1view2');
expect(rootScope.$viewHistory.histories[tab1Scope.$historyId].cursor).toEqual(1);
}));
it('should go one level in tab1, vist tab2 and tab3, come back to tab1 and still be at spot', inject(function($location, $state) {
var tab1Container = {};
var tab2Container = {};
var tab3Container = {};
viewService.registerHistory(tab1Container);
viewService.registerHistory(tab2Container);
viewService.registerHistory(tab3Container);
// register tab1, view1
$state.go('tabs.tab1view1');
rootScope.$apply();
var tab1view1Reg = viewService.register(tab1Container);
expect(rootScope.$viewHistory.histories[tab1Container.$historyId].cursor).toEqual(0);
// register tab1, view2
$state.go('tabs.tab1view2');
rootScope.$apply();
var tab1view2Reg = viewService.register(tab1Container);
expect(rootScope.$viewHistory.histories[tab1Container.$historyId].cursor).toEqual(1);
currentView = viewService.getCurrentView();
expect(currentView.backViewId).toEqual(tab1view1Reg.viewId);
// register tab2, view1
$state.go('tabs.tab2view1');
rootScope.$apply();
var tab2view1Reg = viewService.register(tab2Container);
// register tab3, view1
$state.go('tabs.tab3view1');
rootScope.$apply();
var tab3view1Reg = viewService.register(tab3Container);
// register tab 1, view 2 again
$state.go('tabs.tab1view2');
rootScope.$apply();
var tab1view2Reg2 = viewService.register(tab1Container);
expect(tab1view2Reg2.navAction).toEqual('moveBack');
expect(tab1view2Reg2.viewId).toEqual(tab1view2Reg.viewId);
var currentViewId = tab1view2Reg.viewId;
expect(rootScope.$viewHistory.histories[tab1view2Reg.historyId].stack.length).toEqual(2);
backView = viewService.getBackView();
expect(backView).toBeDefined();
expect( Object.keys(rootScope.$viewHistory.views).length ).toEqual(4);
viewService.clearHistory();
expect( Object.keys(rootScope.$viewHistory.views).length ).toEqual(1);
expect(rootScope.$viewHistory.histories[tab1view2Reg.historyId].stack.length).toEqual(1);
backView = viewService.getBackView();
expect(backView).toEqual(null);
currentView = viewService.getCurrentView();
expect(currentView.viewId).toEqual(currentViewId);
}));
it('should init root viewHistory data', inject(function() {
expect(rootScope.$viewHistory.backView).toEqual(null);
expect(rootScope.$viewHistory.currentView).toEqual(null);
expect(rootScope.$viewHistory.forwardView).toEqual(null);
expect(rootScope.$viewHistory.histories).toEqual({
root: { historyId: 'root', parentHistoryId: null, stack: [], cursor: -1 }
});
}));
it('should create a viewService view', inject(function($location) {
var newView = viewService.createView();
expect(newView).toEqual(null);
newView = viewService.createView({ stateName: 'about', url: '/url', });
expect(newView.stateName).toEqual('about');
}));
it('should go() to a view', inject(function($location) {
var newView = viewService.createView({ stateName: 'about' });
newView.go();
rootScope.$apply();
expect($location.url()).toEqual('/about');
$location.url('/nochange');
newView = viewService.createView({ url: '/nochange' });
var result = newView.go();
expect(result).toEqual(null);
$location.url('/nochange')
newView = viewService.createView({ url: '/nochange' });
result = newView.go();
expect(result).toEqual(null);
newView = rootScope.$viewHistory.backView = viewService.createView({ url: '/url' });
result = newView.go();
expect(result).toEqual(-1);
newView = rootScope.$viewHistory.forwardView = viewService.createView({ url: '/url' });
result = newView.go();
expect(result).toEqual(1);
newView = viewService.createView({ url: '/url' });
newView.go();
expect($location.url()).toEqual('/url');
}));
it('should change history on event changeHistory', inject(function($location, $state) {
$location.url('/original');
rootScope.$broadcast("viewState.changeHistory");
expect($location.url()).toEqual('/original');
rootScope.$broadcast("viewState.changeHistory", { uiSref: 'about' });
expect($location.url()).toEqual('/about');
rootScope.$broadcast("viewState.changeHistory", { url: '/url' });
expect($location.url()).toEqual('/url');
rootScope.$viewHistory.histories['h123'] = { stack: [], cursor: -1 }
rootScope.$broadcast("viewState.changeHistory", { historyId: 'h123' });
expect($location.url()).toEqual('/url');
var newView = viewService.createView({ stateName: 'about' });
rootScope.$viewHistory.histories['h123'].stack.push(newView);
rootScope.$viewHistory.histories['h123'].cursor++;
rootScope.$broadcast("viewState.changeHistory", { historyId: 'h123' });
rootScope.$apply();
expect($state.current.name).toEqual('about');
}));
it('should update document title', inject(function($document) {
$document[0].title = 'Original Title';
rootScope.$broadcast("viewState.viewEnter");
expect($document[0].title).toEqual('Original Title');
rootScope.$broadcast("viewState.viewEnter", {});
expect($document[0].title).toEqual('Original Title');
rootScope.$broadcast("viewState.viewEnter", { title: 'New Title' });
expect($document[0].title).toEqual('New Title');
}));
});