Skip to content

Commit e3ba1bf

Browse files
committed
Merge pull request #937 from meenie/tests/uiView
test(uiView): Added in tests for ngClass, ngRepeat, and animations.
2 parents 6969e90 + 7d76705 commit e3ba1bf

File tree

1 file changed

+237
-38
lines changed

1 file changed

+237
-38
lines changed

test/viewDirectiveSpec.js

+237-38
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ describe('uiView', function () {
1010
var depends = ['ui.router'];
1111

1212
try {
13-
angular.module('ngAnimateMock');
14-
depends.push('ngAnimateMock');
13+
angular.module('ngAnimate');
14+
depends.push('ngAnimate', 'ngAnimateMock');
1515
} catch(e) {
1616
angular.module('mock.animate', []).value('$animate', null);
1717
module('mock.animate');
@@ -83,6 +83,19 @@ describe('uiView', function () {
8383
this.someProperty = "value"
8484
},
8585
controllerAs: "vm"
86+
},
87+
lState = {
88+
views: {
89+
view1: {
90+
template: 'view1'
91+
},
92+
view2: {
93+
template: 'view2'
94+
},
95+
view3: {
96+
template: 'view3'
97+
}
98+
}
8699
};
87100

88101
beforeEach(module(function ($stateProvider) {
@@ -98,6 +111,7 @@ describe('uiView', function () {
98111
.state('i', iState)
99112
.state('j', jState)
100113
.state('k', kState)
114+
.state('l', lState)
101115
}));
102116

103117
beforeEach(inject(function ($rootScope, _$compile_) {
@@ -229,42 +243,6 @@ describe('uiView', function () {
229243
// verify if the initial view has been updated
230244
expect(elem.find('li').length).toBe(scope.items.length);
231245
}));
232-
233-
// related to issue #857
234-
it('should handle ui-view inside ng-if', inject(function ($state, $q, $compile) {
235-
// ngIf does not exist in 1.0.8
236-
if (angular.version.full === '1.0.8') return;
237-
238-
scope.someBoolean = false;
239-
elem.append($compile('<div ng-if="someBoolean"><ui-view></ui-view></div>')(scope));
240-
241-
$state.transitionTo(aState);
242-
$q.flush();
243-
244-
// Verify there is no ui-view in the DOM
245-
expect(elem.find('ui-view').length).toBe(0);
246-
247-
// Turn on the div that holds the ui-view
248-
scope.someBoolean = true;
249-
scope.$digest();
250-
251-
// Verify that the ui-view is there and it has the correct content
252-
expect(elem.find('ui-view').text()).toBe(aState.template);
253-
254-
// Turn off the ui-view
255-
scope.someBoolean = false;
256-
scope.$digest();
257-
258-
// Verify there is no ui-view in the DOM
259-
expect(elem.find('ui-view').length).toBe(0);
260-
261-
// Turn on the div that holds the ui-view once again
262-
scope.someBoolean = true;
263-
scope.$digest();
264-
265-
// Verify that the ui-view is there and it has the correct content
266-
expect(elem.find('ui-view').text()).toBe(aState.template);
267-
}));
268246
});
269247

270248
describe('autoscroll attribute', function () {
@@ -327,4 +305,225 @@ describe('uiView', function () {
327305

328306
expect(elem.text()).toBe('value');
329307
}));
308+
309+
describe('play nicely with other directives', function() {
310+
// related to issue #857
311+
it('should work with ngIf', inject(function ($state, $q, $compile) {
312+
// ngIf does not exist in 1.0.8
313+
if (angular.version.full === '1.0.8') return;
314+
315+
scope.someBoolean = false;
316+
elem.append($compile('<div ng-if="someBoolean"><ui-view></ui-view></div>')(scope));
317+
318+
$state.transitionTo(aState);
319+
$q.flush();
320+
321+
// Verify there is no ui-view in the DOM
322+
expect(elem.find('ui-view').length).toBe(0);
323+
324+
// Turn on the div that holds the ui-view
325+
scope.someBoolean = true;
326+
scope.$digest();
327+
328+
// Verify that the ui-view is there and it has the correct content
329+
expect(elem.find('ui-view').text()).toBe(aState.template);
330+
331+
// Turn off the ui-view
332+
scope.someBoolean = false;
333+
scope.$digest();
334+
335+
// Verify there is no ui-view in the DOM
336+
expect(elem.find('ui-view').length).toBe(0);
337+
338+
// Turn on the div that holds the ui-view once again
339+
scope.someBoolean = true;
340+
scope.$digest();
341+
342+
// Verify that the ui-view is there and it has the correct content
343+
expect(elem.find('ui-view').text()).toBe(aState.template);
344+
}));
345+
346+
it ('should work with ngClass', inject(function($state, $q, $compile) {
347+
scope.showClass = false;
348+
elem.append($compile('<div><ui-view ng-class="{\'someClass\': showClass}"></ui-view></div>')(scope));
349+
350+
expect(elem.find('ui-view')).not.toHaveClass('someClass');
351+
352+
scope.showClass = true;
353+
scope.$digest();
354+
355+
expect(elem.find('ui-view')).toHaveClass('someClass');
356+
357+
scope.showClass = false;
358+
scope.$digest();
359+
360+
expect(elem.find('ui-view')).not.toHaveClass('someClass');
361+
}));
362+
363+
describe ('working with ngRepeat', function() {
364+
// ngRepeat does not work properly with uiView in 1.0.8 & 1.1.5
365+
if (['1.0.8', '1.1.5'].indexOf(angular.version.full) !== -1) return;
366+
367+
it ('should have correct number of uiViews', inject(function($state, $q, $compile) {
368+
elem.append($compile('<div><ui-view ng-repeat="view in views" name="{{view}}"></ui-view></div>')(scope));
369+
370+
// Should be no ui-views in DOM
371+
expect(elem.find('ui-view').length).toBe(0);
372+
373+
// Lets add 3
374+
scope.views = ['view1', 'view2', 'view3'];
375+
scope.$digest();
376+
377+
// Should be 3 ui-views in the DOM
378+
expect(elem.find('ui-view').length).toBe(scope.views.length);
379+
380+
// Lets add one more - yay two-way binding
381+
scope.views.push('view4');
382+
scope.$digest();
383+
384+
// Should have 4 ui-views
385+
expect(elem.find('ui-view').length).toBe(scope.views.length);
386+
387+
// Lets remove 2 ui-views from the DOM
388+
scope.views.pop();
389+
scope.views.pop();
390+
scope.$digest();
391+
392+
// Should have 2 ui-views
393+
expect(elem.find('ui-view').length).toBe(scope.views.length);
394+
}));
395+
396+
it ('should populate each view with content', inject(function($state, $q, $compile) {
397+
elem.append($compile('<div><ui-view ng-repeat="view in views" name="{{view}}"></ui-view></div>')(scope));
398+
399+
$state.transitionTo(lState);
400+
$q.flush();
401+
402+
expect(elem.find('ui-view').length).toBe(0);
403+
404+
scope.views = ['view1', 'view2'];
405+
406+
scope.$digest();
407+
408+
var uiViews = elem.find('ui-view');
409+
410+
expect(uiViews.eq(0).text()).toBe(lState.views.view1.template);
411+
expect(uiViews.eq(1).text()).toBe(lState.views.view2.template);
412+
expect(uiViews.eq(2).length).toBe(0);
413+
414+
scope.views.push('view3');
415+
scope.$digest();
416+
417+
uiViews = elem.find('ui-view');
418+
419+
expect(uiViews.eq(0).text()).toBe(lState.views.view1.template);
420+
expect(uiViews.eq(1).text()).toBe(lState.views.view2.template);
421+
expect(uiViews.eq(2).text()).toBe(lState.views.view3.template);
422+
}));
423+
});
424+
});
425+
426+
describe('AngularJS 1.2.* Animations', function() {
427+
// Only doing tests for AngularJS 1.2.*
428+
if (['1.0.8', '1.1.5'].indexOf(angular.version.full) !== -1) return;
429+
430+
it ('should do transition animations', inject(function($state, $q, $compile, $animate) {
431+
var content = 'Initial Content',
432+
animation;
433+
elem.append($compile('<div><ui-view>' + content + '</ui-view></div>')(scope));
434+
435+
// Enter Animation
436+
animation = $animate.queue.shift();
437+
expect(animation.event).toBe('enter');
438+
expect(animation.element.text()).toBe(content);
439+
440+
$state.transitionTo(aState);
441+
$q.flush();
442+
443+
// Enter Animation
444+
animation = $animate.queue.shift();
445+
expect(animation.event).toBe('enter');
446+
expect(animation.element.text()).toBe(aState.template);
447+
// Leave Animation
448+
animation = $animate.queue.shift();
449+
expect(animation.event).toBe('leave');
450+
expect(animation.element.text()).toBe(content);
451+
452+
$state.transitionTo(bState);
453+
$q.flush();
454+
455+
// Enter Animation
456+
animation = $animate.queue.shift();
457+
expect(animation.event).toBe('enter');
458+
expect(animation.element.text()).toBe(bState.template);
459+
// Leave Animation
460+
animation = $animate.queue.shift();
461+
expect(animation.event).toBe('leave');
462+
expect(animation.element.text()).toBe(aState.template);
463+
464+
// No more animations
465+
expect($animate.queue.length).toBe(0);
466+
}));
467+
468+
it ('should do ngClass animations', inject(function($state, $q, $compile, $animate) {
469+
scope.classOn = false;
470+
var content = 'Initial Content',
471+
className = 'yay',
472+
animation;
473+
elem.append($compile('<div><ui-view ng-class="{\'' + className + '\': classOn}">' + content + '</ui-view></div>')(scope));
474+
// Don't care about enter class
475+
$animate.queue.shift();
476+
477+
scope.classOn = true;
478+
scope.$digest();
479+
480+
animation = $animate.queue.shift();
481+
expect(animation.event).toBe('addClass');
482+
expect(animation.element.text()).toBe(content);
483+
484+
scope.classOn = false;
485+
scope.$digest();
486+
487+
animation = $animate.queue.shift();
488+
expect(animation.event).toBe('removeClass');
489+
expect(animation.element.text()).toBe(content);
490+
491+
// No more animations
492+
expect($animate.queue.length).toBe(0);
493+
}));
494+
495+
it ('should do ngIf animations', inject(function($state, $q, $compile, $animate) {
496+
scope.shouldShow = false;
497+
var content = 'Initial Content',
498+
animation;
499+
elem.append($compile('<div><ui-view ng-if="shouldShow">' + content + '</ui-view></div>')(scope));
500+
501+
// No animations yet
502+
expect($animate.queue.length).toBe(0);
503+
504+
scope.shouldShow = true;
505+
scope.$digest();
506+
507+
// $ViewDirective enter animation - Basically it's just the <!-- uiView --> comment
508+
animation = $animate.queue.shift();
509+
expect(animation.event).toBe('enter');
510+
expect(animation.element.text()).toBe('');
511+
512+
// $ViewDirectiveFill enter animation - The second uiView directive that files in the content
513+
animation = $animate.queue.shift();
514+
expect(animation.event).toBe('enter');
515+
expect(animation.element.text()).toBe(content);
516+
517+
scope.shouldShow = false;
518+
scope.$digest();
519+
520+
// uiView leave animation
521+
animation = $animate.queue.shift();
522+
expect(animation.event).toBe('leave');
523+
expect(animation.element.text()).toBe(content);
524+
525+
// No more animations
526+
expect($animate.queue.length).toBe(0);
527+
}));
528+
});
330529
});

0 commit comments

Comments
 (0)