Testing AngularJS scope variables

leog

I'm trying to implement a test that relies on a scope variable. I want to enable ng-switch-when to resolve an expression. This is what I'm trying to do (UPDATE using $rootScope):

it('should switch on array changes', inject(function($rootScope, $compile) {
  element = $compile(
    '<div ng-switch="select">' +
      '<div ng-switch-when="test[0]">test[0]:{{test[0]}}</div>' +
    '</div>')($rootScope);
  expect(element.html()).toEqual('<!-- ngSwitchWhen: test[0] -->');
  $rootScope.test = ["leog"];
  $rootScope.select = "leog";
  $rootScope.$apply();
  expect(element.text()).toEqual('test[0]:leog');
}));

My problem is that the implementation I have for this to work does not get the scope variable "test" to evaluate and work as I expect. Here is the implementation:

var ngSwitchWhenDirective = ngDirective({
  transclude: 'element',
  priority: 800,
  require: '^ngSwitch',
  compile: function(element, attrs) {
    return function(scope, element, attr, ctrl, $transclude) {
      var expr = scope.$eval(attrs.ngSwitchWhen),
          ngSwitchWhen = expr !== undefined ? expr : attrs.ngSwitchWhen;
      ctrl.cases['!' + ngSwitchWhen] = (ctrl.cases['!' + ngSwitchWhen] || []);
      ctrl.cases['!' + ngSwitchWhen].push({ transclude: $transclude, element: element });
    };
  }
});

Does anybody knows what I'm doing wrong? Any help will be appreciated.

Thanks in advance!

UPDATE

Just to clarify, this is an example of how ng-switch it's being tested from the Angular team. Just to show that I'm doing my test in a similar way but not having the expected result.

And also, I forgot to reverse my code to $rootScope, what you've been seeing up until now was one of my attempts to make this work creating a new scope to avoid relying on $rootScope for a change.

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');
}));
leog

So, after moving some code around I found that the variables not involved on making $rootScope change to test the feature, should be defined before compiling the element to test. Thanks @Jonathan for the hint.

Here is the working code with additional test cases:

it('should evaluate expressions to match switch value', inject(function($rootScope, $compile) {
  $rootScope.array = ["leog", ".me"];
  $rootScope.obj = {"key": "value"};
  $rootScope.$apply();
  element = $compile(
    '<div ng-switch="select">' +
      '<div ng-switch-when="obj.key">obj.key:{{obj.key}}</div>' +
      '<div ng-switch-when="array[0]">array[0]:{{array[0]}}</div>' +
      '<div ng-switch-when="array[1]">array[1]:{{array[1]}}</div>' +
    '</div>')($rootScope);
  expect(element.html()).toEqual('<!-- ngSwitchWhen: obj.key -->' +
                                 '<!-- ngSwitchWhen: array[0] -->' +
                                 '<!-- ngSwitchWhen: array[1] -->');
  $rootScope.select = "value1";
  $rootScope.$apply();
  expect(element.text()).toEqual('obj.key:value');
  $rootScope.select = "leog";
  $rootScope.$apply();
  expect(element.text()).toEqual('array[0]:leog');
  $rootScope.select = ".me";
  $rootScope.$apply();
  expect(element.text()).toEqual('array[1]:.me');
}));

Thanks you all.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Setting dynamic scope variables in AngularJs - scope.<some_string>

From Java

Access scope variables from a filter in AngularJS

From Dev

Angularjs passing variables to a directive with an inherited (not isolated) scope

From Dev

Unit testing AngularJS Directive with isolated scope - how to get bindings to output?

From Dev

Auto-updating scope variables in angularjs

From Dev

Is it possible to parse a string with scope variables angularjs ?

From Dev

AngularJS: Initialize nested scope variables in controller

From Dev

Angularjs $scope variables not updating in view

From Dev

AngularJS set ng-controller with scope variables

From Dev

Initialize $scope variables for multiple controllers - AngularJS

From Dev

AngularJS: accessing scope variables in $routeProvider

From Dev

Create dynamic scope variables in AngularJs inside loop

From Dev

AngularJs scope variables in console

From Dev

How to add Dynamic Scope variables to a for loop in angularjs?

From Dev

How to test function out of scope and this in AngularJS Unit Testing

From Dev

Changing AngularJS scope variables from an inside function

From Dev

Unit Testing $scope.$on AngularJS

From Dev

AngularJS: Unit testing controller returns "TypeError: $scope.$watch is not a function"

From Dev

Ionic: AngularJS Variables not Updating DOM with $scope

From Dev

can't bind scope variables in angularjs

From Dev

Angularjs $scope variables not updating in view

From Dev

Passing data to out of scope variables in angularjs

From Dev

Auto Set fields with $scope variables - AngularJS

From Dev

AngularJS: accessing scope variables in $routeProvider

From Dev

Angularjs: Testing controller function, which changes $scope

From Dev

AngularJS - Scope variables in Angular ui router

From Dev

AngularJS set scope variables in Karma test

From Dev

AngularJS - variables not updating in view (not using scope)

From Dev

How exactly are scope variables in angularjs altered?

Related Related

  1. 1

    Setting dynamic scope variables in AngularJs - scope.<some_string>

  2. 2

    Access scope variables from a filter in AngularJS

  3. 3

    Angularjs passing variables to a directive with an inherited (not isolated) scope

  4. 4

    Unit testing AngularJS Directive with isolated scope - how to get bindings to output?

  5. 5

    Auto-updating scope variables in angularjs

  6. 6

    Is it possible to parse a string with scope variables angularjs ?

  7. 7

    AngularJS: Initialize nested scope variables in controller

  8. 8

    Angularjs $scope variables not updating in view

  9. 9

    AngularJS set ng-controller with scope variables

  10. 10

    Initialize $scope variables for multiple controllers - AngularJS

  11. 11

    AngularJS: accessing scope variables in $routeProvider

  12. 12

    Create dynamic scope variables in AngularJs inside loop

  13. 13

    AngularJs scope variables in console

  14. 14

    How to add Dynamic Scope variables to a for loop in angularjs?

  15. 15

    How to test function out of scope and this in AngularJS Unit Testing

  16. 16

    Changing AngularJS scope variables from an inside function

  17. 17

    Unit Testing $scope.$on AngularJS

  18. 18

    AngularJS: Unit testing controller returns "TypeError: $scope.$watch is not a function"

  19. 19

    Ionic: AngularJS Variables not Updating DOM with $scope

  20. 20

    can't bind scope variables in angularjs

  21. 21

    Angularjs $scope variables not updating in view

  22. 22

    Passing data to out of scope variables in angularjs

  23. 23

    Auto Set fields with $scope variables - AngularJS

  24. 24

    AngularJS: accessing scope variables in $routeProvider

  25. 25

    Angularjs: Testing controller function, which changes $scope

  26. 26

    AngularJS - Scope variables in Angular ui router

  27. 27

    AngularJS set scope variables in Karma test

  28. 28

    AngularJS - variables not updating in view (not using scope)

  29. 29

    How exactly are scope variables in angularjs altered?

HotTag

Archive