aboutsummaryrefslogtreecommitdiffstats
path: root/test/ScopeSpec.js
blob: 7e1a899f833b838eab8e0e2e2397ad8a0e7d0735 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
describe('scope/model', function(){

  it('should create a scope with parent', function(){
    var model = createScope({name:'Misko'});
    expect(model.name).toEqual('Misko');
  });

  it('should have $get/set$/parent$', function(){
    var parent = {};
    var model = createScope(parent);
    model.$set('name', 'adam');
    expect(model.name).toEqual('adam');
    expect(model.$get('name')).toEqual('adam');
    expect(model.$parent).toEqual(parent);
  });

  //$eval
  it('should eval function with correct this and pass arguments', function(){
    var model = createScope();
    model.$eval(function(name){
      this.name = name;
    }, 'works');
    expect(model.name).toEqual('works');
  });

  it('should eval expression with correct this', function(){
    var model = createScope();
    model.$eval('name="works"');
    expect(model.name).toEqual('works');
  });

  //$watch
  it('should watch an expression for change', function(){
    var model = createScope();
    model.oldValue = "";
    var nameCount = 0, evalCount = 0;
    model.name = 'adam';
    model.$watch('name', function(){ nameCount ++; });
    model.$watch(function(){return model.name;}, function(newValue, oldValue){
      this.newValue = newValue;
      this.oldValue = oldValue;
    });
    model.$onEval(function(){evalCount ++;});
    model.name = 'misko';
    model.$eval();
    expect(nameCount).toEqual(1);
    expect(evalCount).toEqual(1);
    expect(model.newValue).toEqual('misko');
    expect(model.oldValue).toEqual('adam');
  });

  it('should eval with no arguments', function(){
    var model = createScope();
    var count = 0;
    model.$onEval(function(){count++;});
    model.$eval();
    expect(count).toEqual(1);
  });

  //$bind
  it('should curry a function with respect to scope', function(){
    var model = createScope();
    model.name = 'misko';
    expect(model.$bind(function(){return this.name;})()).toEqual('misko');
  });

  //$behavior
  it('should behave as class', function(){
    function Printer(brand){
      this.brand = brand;
    };
    Printer.prototype.print = function(){
      this.printed = true;
    };
    var model = createScope({ name: 'parent' }, Printer, 'hp');
    expect(model.brand).toEqual('hp');
    model.print();
    expect(model.printed).toEqual(true);
  });



  //$tryEval
  it('should report error on element', function(){

  });

  it('should report error on visible element', function(){

  });

});