aboutsummaryrefslogtreecommitdiffstats
path: root/test/ng/directive/ngSwitchSpec.js
diff options
context:
space:
mode:
authorMisko Hevery2012-03-23 14:03:24 -0700
committerMisko Hevery2012-03-28 11:16:35 -0700
commit2430f52bb97fa9d682e5f028c977c5bf94c5ec38 (patch)
treee7529b741d70199f36d52090b430510bad07f233 /test/ng/directive/ngSwitchSpec.js
parent944098a4e0f753f06b40c73ca3e79991cec6c2e2 (diff)
downloadangular.js-2430f52bb97fa9d682e5f028c977c5bf94c5ec38.tar.bz2
chore(module): move files around in preparation for more modules
Diffstat (limited to 'test/ng/directive/ngSwitchSpec.js')
-rw-r--r--test/ng/directive/ngSwitchSpec.js93
1 files changed, 93 insertions, 0 deletions
diff --git a/test/ng/directive/ngSwitchSpec.js b/test/ng/directive/ngSwitchSpec.js
new file mode 100644
index 00000000..b4df109e
--- /dev/null
+++ b/test/ng/directive/ngSwitchSpec.js
@@ -0,0 +1,93 @@
+'use strict';
+
+describe('ng-switch', function() {
+ var element;
+
+
+ afterEach(function(){
+ dealoc(element);
+ });
+
+
+ it('should switch on value change', inject(function($rootScope, $compile) {
+ element = $compile(
+ '<div ng-switch="select">' +
+ '<div ng-switch-when="1">first:{{name}}</div>' +
+ '<div ng-switch-when="2">second:{{name}}</div>' +
+ '<div ng-switch-when="true">true:{{name}}</div>' +
+ '</div>')($rootScope);
+ expect(element.html()).toEqual(
+ '<!-- ngSwitchWhen: 1 --><!-- ngSwitchWhen: 2 --><!-- ngSwitchWhen: true -->');
+ $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(
+ '<ng:switch on="select">' +
+ '<div ng:switch-when="1">one</div>' +
+ '<div ng:switch-default>other</div>' +
+ '</ng:switch>')($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(
+ '<ng:switch on="url" change="name=\'works\'">' +
+ '<div ng-switch-when="a">{{name}}</div>' +
+ '</ng:switch>')($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(
+ '<ng:switch on="url">' +
+ '<div ng-switch-when="a">{{name}}</div>' +
+ '</ng:switch>')($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);
+ }));
+});