|
| 1 | +/*jshint browser: true, indent: 2 */ |
| 2 | +/*global describe: false, it: false, beforeEach: false, expect: false, resolvedValue: false, module: false, inject: false, angular: false */ |
| 3 | + |
| 4 | +describe('uiView', function () { |
| 5 | + 'use strict'; |
| 6 | + |
| 7 | + var scope, $compile, elem; |
| 8 | + |
| 9 | + beforeEach(module('ui.state')); |
| 10 | + |
| 11 | + var aState = { |
| 12 | + template: 'aState template' |
| 13 | + }, |
| 14 | + bState = { |
| 15 | + template: 'bState template' |
| 16 | + }, |
| 17 | + cState = { |
| 18 | + views: { |
| 19 | + 'cview': { |
| 20 | + template: 'cState cview template' |
| 21 | + } |
| 22 | + } |
| 23 | + }, |
| 24 | + dState = { |
| 25 | + views: { |
| 26 | + 'dview1': { |
| 27 | + template: 'dState dview1 template' |
| 28 | + }, |
| 29 | + 'dview2': { |
| 30 | + template: 'dState dview2 template' |
| 31 | + } |
| 32 | + } |
| 33 | + }, |
| 34 | + eState = { |
| 35 | + template: '<div ui-view="eview" class="eview"></div>' |
| 36 | + }, |
| 37 | + fState = { |
| 38 | + views: { |
| 39 | + 'eview': { |
| 40 | + template: 'fState eview template' |
| 41 | + } |
| 42 | + } |
| 43 | + }, |
| 44 | + gState = { |
| 45 | + template: '<div ui-view="inner"><span ng-class="{ test: true }">{{content}}</span></div>' |
| 46 | + }, |
| 47 | + hState = { |
| 48 | + views: { |
| 49 | + 'inner': { |
| 50 | + template: 'hState inner template' |
| 51 | + } |
| 52 | + } |
| 53 | + }; |
| 54 | + |
| 55 | + beforeEach(module(function ($stateProvider) { |
| 56 | + $stateProvider |
| 57 | + .state('a', aState) |
| 58 | + .state('b', bState) |
| 59 | + .state('c', cState) |
| 60 | + .state('d', dState) |
| 61 | + .state('e', eState) |
| 62 | + .state('e.f', fState) |
| 63 | + .state('g', gState) |
| 64 | + .state('g.h', hState); |
| 65 | + })); |
| 66 | + |
| 67 | + beforeEach(inject(function ($rootScope, _$compile_) { |
| 68 | + scope = $rootScope.$new(); |
| 69 | + $compile = _$compile_; |
| 70 | + elem = angular.element('<div>'); |
| 71 | + })); |
| 72 | + |
| 73 | + describe('linking ui-directive', function () { |
| 74 | + it('anonymous ui-view should be replaced with the template of the current $state', inject(function ($state, $q) { |
| 75 | + elem.append($compile('<div ui-view></div>')(scope)); |
| 76 | + |
| 77 | + $state.transitionTo(aState); |
| 78 | + $q.flush(); |
| 79 | + |
| 80 | + expect(elem.text()).toBe(aState.template); |
| 81 | + })); |
| 82 | + |
| 83 | + it('named ui-view should be replaced with the template of the current $state', inject(function ($state, $q) { |
| 84 | + elem.append($compile('<div ui-view="cview"></div>')(scope)); |
| 85 | + |
| 86 | + $state.transitionTo(cState); |
| 87 | + $q.flush(); |
| 88 | + |
| 89 | + expect(elem.text()).toBe(cState.views.cview.template); |
| 90 | + })); |
| 91 | + |
| 92 | + it('ui-view should be updated after transition to another state', inject(function ($state, $q) { |
| 93 | + elem.append($compile('<div ui-view></div>')(scope)); |
| 94 | + |
| 95 | + $state.transitionTo(aState); |
| 96 | + $q.flush(); |
| 97 | + |
| 98 | + expect(elem.text()).toBe(aState.template); |
| 99 | + |
| 100 | + $state.transitionTo(bState); |
| 101 | + $q.flush(); |
| 102 | + |
| 103 | + expect(elem.text()).toBe(bState.template); |
| 104 | + })); |
| 105 | + |
| 106 | + it('should handle NOT nested ui-views', inject(function ($state, $q) { |
| 107 | + elem.append($compile('<div ui-view="dview1" class="dview1"></div><div ui-view="dview2" class="dview2"></div>')(scope)); |
| 108 | + |
| 109 | + $state.transitionTo(dState); |
| 110 | + $q.flush(); |
| 111 | + |
| 112 | + expect(elem[0].querySelector('.dview1').innerText).toBe(dState.views.dview1.template); |
| 113 | + expect(elem[0].querySelector('.dview2').innerText).toBe(dState.views.dview2.template); |
| 114 | + })); |
| 115 | + |
| 116 | + it('should handle nested ui-views (testing two levels deep)', inject(function ($state, $q) { |
| 117 | + elem.append($compile('<div ui-view class="view"></div>')(scope)); |
| 118 | + |
| 119 | + $state.transitionTo(fState); |
| 120 | + $q.flush(); |
| 121 | + |
| 122 | + expect(elem[0].querySelector('.view').querySelector('.eview').innerText).toBe(fState.views.eview.template); |
| 123 | + })); |
| 124 | + }); |
| 125 | + |
| 126 | + describe('handling initial view', function () { |
| 127 | + it('initial view should be compiled if the view is empty', inject(function ($state, $q) { |
| 128 | + var content = 'inner content'; |
| 129 | + |
| 130 | + elem.append($compile('<div ui-view></div>')(scope)); |
| 131 | + scope.$apply('content = "' + content + '"'); |
| 132 | + |
| 133 | + $state.transitionTo(gState); |
| 134 | + $q.flush(); |
| 135 | + |
| 136 | + expect(elem[0].querySelector('.test').innerText).toBe(content); |
| 137 | + })); |
| 138 | + |
| 139 | + it('initial view should be put back after removal of the view', inject(function ($state, $q) { |
| 140 | + var content = 'inner content'; |
| 141 | + |
| 142 | + elem.append($compile('<div ui-view></div>')(scope)); |
| 143 | + scope.$apply('content = "' + content + '"'); |
| 144 | + |
| 145 | + $state.transitionTo(hState); |
| 146 | + $q.flush(); |
| 147 | + |
| 148 | + expect(elem.text()).toBe(hState.views.inner.template); |
| 149 | + |
| 150 | + // going to the parent state which makes the inner view empty |
| 151 | + $state.transitionTo(gState); |
| 152 | + $q.flush(); |
| 153 | + |
| 154 | + expect(elem[0].querySelector('.test').innerText).toBe(content); |
| 155 | + })); |
| 156 | + }); |
| 157 | + |
| 158 | +}); |
0 commit comments