'use strict';
describe('ngSwitch', function() {
var element;
afterEach(function(){
dealoc(element);
});
it('should switch on value change', inject(function($rootScope, $compile) {
element = $compile(
'
' +
'
first:{{name}}
' +
'
second:{{name}}
' +
'
true:{{name}}
' +
'
')($rootScope);
expect(element.html()).toEqual(
'');
$rootScope.select = 1;
$rootScope.$apply();
expect(element.text()).toEqual('first:');
$rootScope.name="shyam";
$rootScope.$apply();
expect(element.text()).toEqual('first:shyam');
$rootScope.select = 2;
$rootScope.$apply();
expect(element.text()).toEqual('second:shyam');
$rootScope.name = 'misko';
$rootScope.$apply();
expect(element.text()).toEqual('second:misko');
$rootScope.select = true;
$rootScope.$apply();
expect(element.text()).toEqual('true:misko');
}));
it('should switch on switch-when-default', inject(function($rootScope, $compile) {
element = $compile(
'' +
'one
' +
'other
' +
'')($rootScope);
$rootScope.$apply();
expect(element.text()).toEqual('other');
$rootScope.select = 1;
$rootScope.$apply();
expect(element.text()).toEqual('one');
}));
it('should call change on switch', inject(function($rootScope, $compile) {
element = $compile(
'' +
'{{name}}
' +
'')($rootScope);
$rootScope.url = 'a';
$rootScope.$apply();
expect($rootScope.name).toEqual('works');
expect(element.text()).toEqual('works');
}));
it('should properly create and destroy child scopes', inject(function($rootScope, $compile) {
element = $compile(
'' +
'{{name}}
' +
'')($rootScope);
$rootScope.$apply();
var getChildScope = function() { return element.find('div').scope(); };
expect(getChildScope()).toBeUndefined();
$rootScope.url = 'a';
$rootScope.$apply();
var child1 = getChildScope();
expect(child1).toBeDefined();
spyOn(child1, '$destroy');
$rootScope.url = 'x';
$rootScope.$apply();
expect(getChildScope()).toBeUndefined();
expect(child1.$destroy).toHaveBeenCalledOnce();
$rootScope.url = 'a';
$rootScope.$apply();
var child2 = getChildScope();
expect(child2).toBeDefined();
expect(child2).not.toBe(child1);
}));
});