mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-24 03:55:49 +08:00
major refactoring of scope
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
describe("input widget", function(){
|
||||
|
||||
var compile, element, scope, model;
|
||||
var compile, element, scope;
|
||||
|
||||
beforeEach(function() {
|
||||
scope = null;
|
||||
@@ -8,10 +8,8 @@ describe("input widget", function(){
|
||||
var compiler = new Compiler(angularTextMarkup, angularAttrMarkup, angularDirective, angularWidget);
|
||||
compile = function(html) {
|
||||
element = jqLite(html);
|
||||
var view = compiler.compile(element)(element);
|
||||
view.init();
|
||||
scope = view.scope;
|
||||
model = scope.state;
|
||||
scope = compiler.compile(element)(element);
|
||||
scope.$init();
|
||||
};
|
||||
});
|
||||
|
||||
@@ -22,35 +20,35 @@ describe("input widget", function(){
|
||||
|
||||
it('should input-text auto init and handle keyup/change events', function(){
|
||||
compile('<input type="Text" name="name" value="Misko" ng-action="count = count + 1" ng-init="count=0"/>');
|
||||
expect(scope.get('name')).toEqual("Misko");
|
||||
expect(scope.get('count')).toEqual(0);
|
||||
expect(scope.$get('name')).toEqual("Misko");
|
||||
expect(scope.$get('count')).toEqual(0);
|
||||
|
||||
scope.set('name', 'Adam');
|
||||
scope.updateView();
|
||||
scope.$set('name', 'Adam');
|
||||
scope.$eval();
|
||||
expect(element.val()).toEqual("Adam");
|
||||
|
||||
element.val('Shyam');
|
||||
element.trigger('keyup');
|
||||
expect(scope.get('name')).toEqual('Shyam');
|
||||
expect(scope.get('count')).toEqual(1);
|
||||
expect(scope.$get('name')).toEqual('Shyam');
|
||||
expect(scope.$get('count')).toEqual(1);
|
||||
|
||||
element.val('Kai');
|
||||
element.trigger('change');
|
||||
expect(scope.get('name')).toEqual('Kai');
|
||||
expect(scope.get('count')).toEqual(2);
|
||||
expect(scope.$get('name')).toEqual('Kai');
|
||||
expect(scope.$get('count')).toEqual(2);
|
||||
});
|
||||
|
||||
it("should process ng-format", function(){
|
||||
compile('<input type="Text" name="list" value="a,b,c" ng-format="list"/>');
|
||||
expect(scope.get('list')).toEqual(['a', 'b', 'c']);
|
||||
expect(scope.$get('list')).toEqual(['a', 'b', 'c']);
|
||||
|
||||
scope.set('list', ['x', 'y', 'z']);
|
||||
scope.updateView();
|
||||
scope.$set('list', ['x', 'y', 'z']);
|
||||
scope.$eval();
|
||||
expect(element.val()).toEqual("x, y, z");
|
||||
|
||||
element.val('1, 2, 3');
|
||||
element.trigger('keyup');
|
||||
expect(scope.get('list')).toEqual(['1', '2', '3']);
|
||||
expect(scope.$get('list')).toEqual(['1', '2', '3']);
|
||||
});
|
||||
|
||||
it("should process ng-validation", function(){
|
||||
@@ -58,8 +56,8 @@ describe("input widget", function(){
|
||||
expect(element.hasClass('ng-validation-error')).toBeTruthy();
|
||||
expect(element.attr('ng-error')).toEqual('Not a number');
|
||||
|
||||
scope.set('price', '123');
|
||||
scope.updateView();
|
||||
scope.$set('price', '123');
|
||||
scope.$eval();
|
||||
expect(element.hasClass('ng-validation-error')).toBeFalsy();
|
||||
expect(element.attr('ng-error')).toBeFalsy();
|
||||
|
||||
@@ -74,8 +72,8 @@ describe("input widget", function(){
|
||||
expect(element.hasClass('ng-validation-error')).toBeTruthy();
|
||||
expect(element.attr('ng-error')).toEqual('Required');
|
||||
|
||||
scope.set('price', 'xxx');
|
||||
scope.updateView();
|
||||
scope.$set('price', 'xxx');
|
||||
scope.$eval();
|
||||
expect(element.hasClass('ng-validation-error')).toBeFalsy();
|
||||
expect(element.attr('ng-error')).toBeFalsy();
|
||||
|
||||
@@ -87,41 +85,41 @@ describe("input widget", function(){
|
||||
|
||||
it("should process ng-required", function() {
|
||||
compile('<textarea name="name">Misko</textarea>');
|
||||
expect(scope.get('name')).toEqual("Misko");
|
||||
expect(scope.$get('name')).toEqual("Misko");
|
||||
|
||||
scope.set('name', 'Adam');
|
||||
scope.updateView();
|
||||
scope.$set('name', 'Adam');
|
||||
scope.$eval();
|
||||
expect(element.val()).toEqual("Adam");
|
||||
|
||||
element.val('Shyam');
|
||||
element.trigger('keyup');
|
||||
expect(scope.get('name')).toEqual('Shyam');
|
||||
expect(scope.$get('name')).toEqual('Shyam');
|
||||
|
||||
element.val('Kai');
|
||||
element.trigger('change');
|
||||
expect(scope.get('name')).toEqual('Kai');
|
||||
expect(scope.$get('name')).toEqual('Kai');
|
||||
});
|
||||
|
||||
it('should call ng-action on button click', function(){
|
||||
compile('<input type="button" value="Click Me" ng-action="clicked = true"/>');
|
||||
element.click();
|
||||
expect(scope.get('clicked')).toEqual(true);
|
||||
expect(scope.$get('clicked')).toEqual(true);
|
||||
});
|
||||
|
||||
it('should support button alias', function(){
|
||||
compile('<button ng-action="clicked = true">Click Me</button>');
|
||||
element.click();
|
||||
expect(scope.get('clicked')).toEqual(true);
|
||||
expect(scope.$get('clicked')).toEqual(true);
|
||||
});
|
||||
|
||||
it('should type="checkbox"', function(){
|
||||
compile('<input type="checkbox" name="checkbox" checked ng-action="action = true"/>');
|
||||
expect(scope.get('checkbox')).toEqual(true);
|
||||
expect(scope.$get('checkbox')).toEqual(true);
|
||||
element.click();
|
||||
expect(scope.get('checkbox')).toEqual(false);
|
||||
expect(scope.get('action')).toEqual(true);
|
||||
expect(scope.$get('checkbox')).toEqual(false);
|
||||
expect(scope.$get('action')).toEqual(true);
|
||||
element.click();
|
||||
expect(scope.get('checkbox')).toEqual(true);
|
||||
expect(scope.$get('checkbox')).toEqual(true);
|
||||
});
|
||||
|
||||
it('should type="radio"', function(){
|
||||
@@ -131,21 +129,21 @@ describe("input widget", function(){
|
||||
'</div>');
|
||||
var a = element[0].childNodes[0];
|
||||
var b = element[0].childNodes[1];
|
||||
expect(model.chose).toEqual('B');
|
||||
expect(model.clicked).not.toBeDefined();
|
||||
model.chose = 'A';
|
||||
model.$updateView();
|
||||
expect(scope.chose).toEqual('B');
|
||||
expect(scope.clicked).not.toBeDefined();
|
||||
scope.chose = 'A';
|
||||
scope.$eval();
|
||||
expect(a.checked).toEqual(true);
|
||||
|
||||
model.chose = 'B';
|
||||
model.$updateView();
|
||||
scope.chose = 'B';
|
||||
scope.$eval();
|
||||
expect(a.checked).toEqual(false);
|
||||
expect(b.checked).toEqual(true);
|
||||
expect(model.clicked).not.toBeDefined();
|
||||
expect(scope.clicked).not.toBeDefined();
|
||||
|
||||
jqLite(a).click();
|
||||
expect(model.chose).toEqual('A');
|
||||
expect(model.clicked).toEqual(1);
|
||||
expect(scope.chose).toEqual('A');
|
||||
expect(scope.clicked).toEqual(1);
|
||||
});
|
||||
|
||||
it('should type="select-one"', function(){
|
||||
@@ -154,10 +152,10 @@ describe("input widget", function(){
|
||||
'<option>A</option>' +
|
||||
'<option selected>B</option>' +
|
||||
'</select>');
|
||||
expect(model.selection).toEqual('B');
|
||||
model.selection = 'A';
|
||||
model.$updateView();
|
||||
expect(model.selection).toEqual('A');
|
||||
expect(scope.selection).toEqual('B');
|
||||
scope.selection = 'A';
|
||||
scope.$eval();
|
||||
expect(scope.selection).toEqual('A');
|
||||
expect(element[0].childNodes[0].selected).toEqual(true);
|
||||
});
|
||||
|
||||
@@ -167,14 +165,14 @@ describe("input widget", function(){
|
||||
'<option>A</option>' +
|
||||
'<option selected>B</option>' +
|
||||
'</select>');
|
||||
expect(model.selection).toEqual(['B']);
|
||||
model.selection = ['A'];
|
||||
model.$updateView();
|
||||
expect(scope.selection).toEqual(['B']);
|
||||
scope.selection = ['A'];
|
||||
scope.$eval();
|
||||
expect(element[0].childNodes[0].selected).toEqual(true);
|
||||
});
|
||||
|
||||
it('should report error on missing field', function(){
|
||||
|
||||
//compile('<input type="text"/>');
|
||||
});
|
||||
|
||||
it('should report error on assignment error', function(){
|
||||
|
||||
Reference in New Issue
Block a user