本文介绍了茉莉测试用例匿名函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一个茉莉花code的变化,但我必须完成对匿名函数设置茉莉。我是新来这个。我试过,但我不能完全达到。请帮帮我。
HTML文件
< DIV纳克级={'mainPassdownFont mainPassdownNoteLayout noteEditBackground':isBeingEdited(注),mainPassdownFont mainPassdownNoteLayout':isNotBeingEdited(注)}>
< textarea的NG-ATTR-ID ={{注意 - '+ note.PassdownNoteID}}NG秀=isBeingEdited(注)级=mainPassdownFont passdownTextAreaLayout> {{note.NoteText}}< / textarea的>
< DIV NG隐藏=isBeingEdited(注)级=mainPassdownFont passdownTextAreaLayout>
< pre类=注pre> {{note.NoteText}}< / pre>
< / DIV> <输入类=passdownNoteInlineCheckboxNG-ATTR-ID ={{复选框 - '+ note.PassdownNoteID}}NG模型=cbModelNG变化=checkBoxChanged(注)NG核对= isClaimed(注)TYPE =复选框NAME =notesCheckboxVALUE =notesCheckbox>
< DIV NG秀=isClaimed(注)级=downArrowIcon passdownNoteDownArrowLayout>< / DIV>
< DIV NG秀=isClaimed(注)级=passdownAnnotationFont claimTextContainerLayout>
< p =类claimNameLayout> {{note.Created.By.Text}}< / P>
< p =类claimDateLayout> {{note.Created.Date.Local |日期:MMM D,Y H:mm的'}}< / P>
< / DIV>
< DIV NG隐藏=isClaimed(注)NG点击=claimNote(注)级=passdownAnnotationFont claimClickTextContainerLayout>
< p =类claimNameLayout>点击以权利要求1所述; / P>
< / DIV>
< DIV CLASS =passdownAnnotationFont authorTextContainerLayout>
< p =类claimNameLayout> {{note.LastModified.By.Text}}< / P>
< p =类claimDateLayout> {{note.LastModified.Date.Local |日期:MMM D,Y H:mm的'}}< / P>
< / DIV>
< DIV CLASS =actionIconContainerLayout>
< DIV CLASS =deleteIcon inlineActionIconLayoutNG点击=deleteNote(注)>< / DIV>
< DIV CLASS =editIcon inlineActionIconLayoutNG点击=editNote(注)>< / DIV>
< DIV CLASS =addIcon inlineActionIconLayoutNG点击=addSubNote(注)>< / DIV>
< / DIV>
< DIV CLASS =btnforpassdownTextAreaLayout>
<按钮NG-ATTR-ID ={{'btn-'+ note.PassdownNoteID}}类型=按钮级=BTN btnpassdownTextAreaLayoutNG秀=isBeingEdited(注)NG点击=completeEdit (注)>保存< /按钮>
<按钮NG-ATTR-ID ={{'btn-'+ note.PassdownNoteID}}类型=按钮级=BTN btnpassdownTextAreaLayoutNG秀=isBeingEdited(注)NG点击=的CancelEdit (注)>取消< /按钮>
< / DIV>
< / DIV> < / DIV>
< / DIV>
< / DIV>
JS修改
app.controller('passdownCtrl',
['$ rootScope','$范围,$ HTTP,$资源','$的位置,PassdownNotesData','$窗口','userService',
功能($ rootScope,$范围,$ HTTP,$资源,$位置,PassdownNotesData,$窗口,userService){$ scope.passdownNotesData = {reviewedDateUtc:01-01-2000T15:00,结果:[ ]};
$ scope.reviewDate = NULL;
$ scope.indexBeingEdited = -1;
$ scope.inputfocus = FALSE; $ scope.filterText =;
//去通过一个HTTP GET调用从服务器获取旅行。如果成功的话,分配给该响应
// $ scope.tripData。如果发生错误,将它们分配给tripData的UI找
//并提供用户一些可能采取的行动。
$ scope.passdownNotesData = PassdownNotesData.getData();
如果($ scope.passdownNotesData.results.length == 0){
PassdownNotesData.updatePassdownNotesData()
。然后(功能(结果){
$ scope.passdownNotesData =结果;
$ scope.reviewDate = parseBoldIQDate(result.reviewedDateUtc); //错误
},功能(错误){
警报('错误获取在控制器Passdown Notes数据');
});
}其他{
//$scope.selectedRequestIndex = 0;
//$scope.selectedRequest = $ scope.controllerRequestList.requestList [0];
} parseBoldIQDate =功能(dateStr){
VAR newDate =新的日期();
newDate.setUTCMonth(号码(dateStr.substr(0,2)) - 1);
newDate.setUTCDate(号码(dateStr.substr(3,2)));
newDate.setUTCFullYear(号码(dateStr.substr(6,4)));
newDate.setUTCHours(号码(dateStr.substr(11,2)));
newDate.setUTCMinutes(号码(dateStr.substr(14,2)));
返回newDate;
}; $ scope.noteState =功能(猫,checkOpen){
VAR容器名称=猫+'ContainerDiv';
VAR containerElement =的document.getElementById(容器名称);
如果(containerElement!= NULL){
VAR isOpen会= FALSE;
如果(containerElement.style.display ==块){
isOpen会= TRUE;
}
返回(isOpen会== checkOpen);
}
返回false;
};
getMaxNoteId =功能(){
//创建一个新的笔记结构,将其添加到列表中,坚持它,并把它在编辑模式
变种maxNoteId = 0;
对于(VAR I = 0; I< $ scope.passdownNotesData.results.length;我++){
如果($ scope.passdownNotesData.results [I] .PassdownNoteID> = maxNoteId){
maxNoteId = $ scope.passdownNotesData.results [I] .PassdownNoteID;
}
对于(VAR J = 0; J< $ scope.passdownNotesData.results [I] .SubNotes.length; J ++){
如果($ scope.passdownNotesData.results [I] .SubNotes [J] .PassdownNoteID> = maxNoteId){
maxNoteId = $ scope.passdownNotesData.results [I] .SubNotes [J] .PassdownNoteID;
}
}
}
返回maxNoteId;
};$ scope.addNote =功能(猫事件){
//创建一个新的笔记结构,将其添加到列表(本地和全球),坚持它,并把它在编辑模式 //创建新的笔记
VAR的currentdate =新的日期(); VAR newNote =新的对象();
newNote.PassdownNoteID = getMaxNoteId()+ 1;
newNote.Category =新的对象();
newNote.Category.Text =猫;
newNote.NoteText =默认注释文本;
newNote.Created =新的对象();
// newNote.Created.ClaimStatus =假;
newNote.Created.By =新的对象();
newNote.Created.By.ID = 0;
newNote.Created.By.Text =N / A;
newNote.Created.Date =新的对象();
newNote.Created.Date.Utc = createBoldIQDate(的currentdate);
newNote.Created.Date.Local =的currentdate; newNote.LastModified =新的对象();
newNote.LastModified.By =新的对象();
newNote.LastModified.By.ID = 17;
newNote.LastModified.By.Text = userService.getUser();
newNote.LastModified.Date =新的对象();
newNote.LastModified.Date.Utc = createBoldIQDate(的currentdate);
newNote.LastModified.Date.Local =的currentdate; newNote.SubNotes =新的Array(); //将它添加到本地和全局列表
//$scope.passdownNotesData.results[$scope.passdownNotesData.results.length] = newNote;
变种dataCopy = PassdownNotesData.getData();
dataCopy.results [dataCopy.results.length] = newNote; //更新的数据坚持到服务器
PassdownNotesData.writeUpdatesToServer(dataCopy); //将在编辑模式下显示
$ scope.indexBeingEdited = newNote.Id;
}; $ scope.getNumUnclaimedForCategory =函数(猫){
变种numUnclaimed = 0;
对于(VAR I = 0; I< $ scope.passdownNotesData.results.length;我++){
如果($ scope.passdownNotesData.results [I] .Category.Text ==猫){
//如果注释的主要部分进行了更新,加1,并呼吁它好。
如果($ scope.passdownNotesData.results [I] .IsActive ==假){
numUnclaimed ++;
}
}
}
返回numUnclaimed;
};
$ scope.checkBoxChanged =功能(注){
VAR elementStr =复选框 - + note.PassdownNoteID;
VAR checkboxElement =的document.getElementById(elementStr);
VAR VAL = checkboxElement.checked;
如果(VAL == FALSE){
//这里,我们将取消声明的说明
变种dataCopy = PassdownNotesData.getDatacompleteSubNoteEdit();
//更新真实的数据和我们的本地范围副本,并写出真正的东西回服务器
对于(VAR I = 0; I< dataCopy.results.length;我++){
如果(note.PassdownNoteID == dataCopy.results [I] .PassdownNoteID){
的console.log(这里面,note.PassdownNoteID);
dataCopy.results [I] .IsActive =假; //更新我们的本地副本
$ scope.passdownNotesData.results [I] .IsActive =假;
}
}
PassdownNotesData.writeUpdatesToServer(dataCopy); // $ scope.passdownNotesData); }其他{
$ scope.claimNote(注);
}
}; $ scope.editSubNote =功能(笔记本都){
$ scope.indexBeingEdited = subNote.PassdownNoteID;
VAR elementStr =subNote-+ subNote.PassdownNoteID;
VAR textAreaElement =的document.getElementById(elementStr);
$ scope.subNotesText = textAreaElement.value;
}; $ scope.isClaimed =功能(注){ 如果(note.isActive ==真){
返回true;
}其他{
返回false;
}
};
$ scope.claimNote =功能(注){
变种dataCopy = PassdownNotesData.getData();
//更新真实的数据和我们的本地范围副本,并写出真正的东西回服务器
对于(VAR I = 0; I< dataCopy.results.length;我++){
如果(note.PassdownNoteID == dataCopy.results [I] .PassdownNoteID){
dataCopy.results [I] .isActive =真;
dataCopy.results [I] .Created.By.ID = 17;
dataCopy.results [I] .Created.By.Text = $ scope.loggedInUser;
VAR的currentdate =新的日期();
dataCopy.results [I] .Created.Date.Utc = createBoldIQDate(的currentdate);
dataCopy.results [I] .Created.Date.Local =的currentdate;
$ scope.passdownNotesData.results [I] .Created.Date.Utc = dataCopy.results [I] .Created.Date.Utc;
$ scope.passdownNotesData.results [I] .Created.Date.Local =的currentdate;
}
}
PassdownNotesData.writeUpdatesToServer(dataCopy); // $ scope.passdownNotesData);
}; createBoldIQDate =功能(日期){
变种monthStr =(date.getUTCMonth()+ 1)的ToString();
如果(monthStr.length == 1){
monthStr =0+ monthStr;
}
变种dateStr =(date.getUTCDate())的toString();
如果(dateStr.length == 1){
dateStr =0+ dateStr;
}
变种yearStr =(date.getUTCFullYear())的toString();
。VAR hoursStr =(date.getUTCHours())的toString();
如果(hoursStr.length == 1){
hoursStr =0+ hoursStr;
}
变种minutesStr =(date.getUTCMinutes())的toString();
如果(minutesStr.length == 1){
minutesStr =0+ minutesStr;
}
变种dateStr = monthStr + - + dateStr + - + yearStr +的T+ hoursStr +:+ minutesStr; 返回dateStr;
};
$ scope.filterTrips =功能(注){
如果(note.Category.Text =='旅行'){返回true; }其他{返回false; }
};
$ scope.filterWeather =功能(注){
如果(note.Category.Text =='天气'){返回true; }其他{返回false; }
};
$ scope.filterMaintenance =功能(注){
如果(note.Category.Text =='维护'){返回true; }其他{返回false; }
};
$ scope.filterGeneral =功能(注){
如果(note.Category.Text =='一般'){返回true; }其他{返回false; }
}; $ scope.toggleNotesContainer =功能(猫事件){
VAR容器名称=猫+'ContainerDiv';
VAR containerElement =的document.getElementById(容器名称);
如果(containerElement!= NULL){
如果(containerElement.style.display =='无'){
containerElement.style.display =块;
如果(猫==跳闸){
变种buttonElement =的document.getElementById(passdownToggleHideShowButton);
buttonElement.innerHTML =隐藏所有;
}
}其他{
containerElement.style.display =无;
如果(猫==跳闸){
变种buttonElement =的document.getElementById(passdownToggleHideShowButton);
buttonElement.innerHTML =全部显示;
}
}
}
}; $ scope.toggleHideShowPassdown =功能(){
//我们将使用旅行容器作为是否为全部打开或关闭所有指标 // TODO - 当我们到达的时候,我们想动画这些容器的开启/关闭 变种tripsContainerElement =的document.getElementById('TripsContainerDiv');
如果(tripsContainerElement!= NULL){
如果(tripsContainerElement.style.display =='无'){
//这意味着我们需要打开所有的容器,并更改按钮上的文本
// 全部藏起来
变种buttonElement =的document.getElementById(passdownToggleHideShowButton);
buttonElement.innerHTML =隐藏所有; 变种weatherContainerElement =的document.getElementById('WeatherContainerDiv');
变种maintenanceContainerElement =的document.getElementById('MaintenanceContainerDiv');
变种GeneralContainerElement =的document.getElementById('GeneralContainerDiv');
tripsContainerElement.style.display =块;
weatherContainerElement.style.display =块;
maintenanceContainerElement.style.display =块;
GeneralContainerElement.style.display =块;
}其他{
//这意味着我们需要关闭所有的容器,并更改文本上的按钮
// 显示所有
变种buttonElement =的document.getElementById(passdownToggleHideShowButton);
buttonElement.innerHTML =全部显示; 变种weatherContainerElement =的document.getElementById('WeatherContainerDiv');
变种maintenanceContainerElement =的document.getElementById('MaintenanceContainerDiv');
变种GeneralContainerElement =的document.getElementById('GeneralContainerDiv');
tripsContainerElement.style.display =无;
weatherContainerElement.style.display =无;
maintenanceContainerElement.style.display =无;
GeneralContainerElement.style.display =无;
}
}
}; 变种containerElement =的document.getElementById(TripsContainerDiv);
containerElement.style.display =块;
containerElement =的document.getElementById(WeatherContainerDiv);
containerElement.style.display =块;
containerElement =的document.getElementById(MaintenanceContainerDiv);
containerElement.style.display =块;
containerElement =的document.getElementById(GeneralContainerDiv);
containerElement.style.display =块; $ scope.gotoPassdownScreen =功能(){
//$location.path('#/requests/legDetails');
};
}]);
茉莉花js文件。
描述('passdownnotes控制器规范',函数(){
VAR ctrlScope;
VAR rootScope;
VAR userServiceMock;
VAR PassdownNotesDataMock;
beforeEach(函数(){
模块(应用); //加载jiops模块
}); beforeEach(注(函数($ rootScope,$控制器,$ httpBackend,$ Q){
。ctrlScope = $ rootScope美元的新();
rootScope = $ rootScope;
PassdownNotesDataMock = {
updatePassdownNotesData:功能(){
变种推迟= $ q.defer();
deferred.resolve(mockPassdownNotesData);
返回deferred.promise;
},
的getData:功能(){
返回mockPassdownNotesData;
}
}; userServiceMock = {
的getUser:功能(){
变种推迟= $ q.defer();
deferred.resolve(mockUserData);
返回deferred.promise;
}
}; $控制器('passdownCtrl',{
$范围:ctrlScope,
PassdownNotesData:PassdownNotesDataMock,
userService:userServiceMock
});
})); 描述(passdownnotes控制器',函数(){
它('应该有正确的初始配置',函数(){
期待(ctrlScope.indexBeingEdited).toEqual(-1);
期待(ctrlScope.inputfocus).toBe(假);
期待(ctrlScope.reviewDate).toBe(NULL);
期待(ctrlScope.passdownNotesData).toEqual(0);
期待(ctrlScope.filterText).toBe();
});
});
});VAR mockPassdownNotesData =
{
结果:
{
创建:{
通过:{
ID:3,
文:开发人员,杰普森
},
日期:{
地方:07-02-2015T14:20:26,
TzAbbrev:MDT,
UTC:07-02-2015T20:20:26
}
},
IsActive:真实,
上一次更改:{
通过:{
ID:3,
文:开发人员,杰普森
},
日期:{
地方:07-02-2015T14:29:32,
TzAbbrev:MDT,
UTC:07-02-2015T20:29:32
}
},
NoteText:测试note3333
PassdownNoteID:1,
分配给:{
ID:0,
文本:
},
类别:{
ID:692,
文本:跳闸
},
DateAssigned:空,
截止日期:{
地方:07-19-2015T18:00,
TzAbbrev:MDT,
UTC:07-20-2015T00:00
},
到期日期:空,
SubNotes:[
{
创建:{
通过:{
ID:3,
文:开发人员,杰普森
},
日期:{
地方:07-02-2015T14:20:27,
TzAbbrev:MDT,
UTC:07-02-2015T20:20:27
}
},
IsActive:真实,
上一次更改:{
通过:{
ID:3,
文:开发人员,杰普森
},
日期:{
地方:07-02-2015T14:20:27,
TzAbbrev:MDT,
UTC:07-02-2015T20:20:27
}
},
NoteText:样品分注,
PassdownNoteID:2
}
]
},
{
创建:{
通过:{
ID:3,
文:开发人员,杰普森
},
日期:{
地方:07-07-2015T11:12:03,
TzAbbrev:MDT,
UTC:07-07-2015T17:12:03
}
},
IsActive:真实,
上一次更改:{
通过:{
ID:3,
文:开发人员,杰普森
},
日期:{
地方:07-07-2015T11:12:03,
TzAbbrev:MDT,
UTC:07-07-2015T17:12:03
}
},
NoteText:测试Passdown说明,
PassdownNoteID:3,
分配给:{
ID:302,
文:巴顿,巴顿
},
类别:{
ID:695,
文:常规
},
DateAssigned:{
地方:07-07-2015T11:12,
TzAbbrev:MDT,
UTC:07-07-2015T17:12
},
交货期:空,
到期日期:空,
SubNotes:[
{
创建:{
通过:{
ID:3,
文:开发人员,杰普森
},
日期:{
地方:07-07-2015T11:12:03,
TzAbbrev:MDT,
UTC:07-07-2015T17:12:03
}
},
IsActive:真实,
上一次更改:{
通过:{
ID:3,
文:开发人员,杰普森
},
日期:{
地方:07-07-2015T11:12:03,
TzAbbrev:MDT,
UTC:07-07-2015T17:12:03
}
},
NoteText:passdown笔记本都测试
PassdownNoteID:4
}
]
}
]
};
解决方案
app.controller('passdownCtrl',函数($范围,PassdownNotesData){
VAR VM =这一点; vm.passdownNotesData = PassdownNotesData.getData(); 如果(vm.passdownNotesData.results.length == 0){
PassdownNotesData.updatePassdownNotesData()
。然后(功能(结果){
vm.passdownNotesData =结果;
vm.reviewDate = parseBoldIQDate(result.reviewedDateUtc);
},功能(错误){
警报('错误获取在控制器Passdown Notes数据');
});
} VAR parseBoldIQDate =功能(dateStr){
VAR newDate =新的日期();
newDate.setUTCMonth(编号(dateStr.substr(0,2)) - 1);
newDate.setUTCDate(号码(dateStr.substr(3,2)));
newDate.setUTCFullYear(号码(dateStr.substr(6,4)));
newDate.setUTCHours(号码(dateStr.substr(11,2)));
newDate.setUTCMinutes(号码(dateStr.substr(14,2)));
返回newDate;
};
});描述(控制器:passdownCtrl',函数(){ 变量$范围,
$控制器,
$ Q,
递延,
PassdownNotesData,
CTRL,
窗口; beforeEach(angular.mock.module('requiredModule')); beforeEach(函数(){
angular.mock.module(函数($提供){
PassdownNotesData = jasmine.createSpyObj('PassdownNotesData',['的getData','updatePassdownNotesData']); $ provide.value('PassdownNotesData',PassdownNotesData);
});
}); beforeEach(函数(){
angular.mock.module(函数($提供){
窗口= jasmine.createSpyObj('窗口',['警告']); $ provide.value(窗口,窗口);
});
}); beforeEach(注(功能(_ $ rootScope_,_ $ controller_,_ $ Q_,_PassdownNotesData _){
。$ SCOPE = _ $ rootScope _ $新();
$控制器= _ $ controller_;
$ Q = _ $ Q_;
PassdownNotesData = _PassdownNotesData_;
递延= $ q.defer(); CTRL = $控制器('passdownCtrl作为CTRL',{
$范围:$范围,
PassdownNotesData:PassdownNotesData
});
})); 它('应该可以解决的承诺',函数(){
VAR数据= {
结果:[0]
} VAR的结果= {
名称:0,
reviewedDateUtc:'12'
}; PassdownNotesData.getData.and.returnValue(数据);
PassdownNotesData.updatePassdownNotesData.and.returnValue(deferred.promise); deferred.resolve(结果); $ $范围摘要()。 期待(ctrl.passdownNotesData).toBe(结果); //函数parseBoldIQDate必须被执行
// result.reviewedDateUtc值
期待(ctrl.reviewDate).toEqual(周四11月30日0 2时零零分50秒GMT + 0200(FLE标准时间)');
}); 它('应该拒绝承诺',函数(){
VAR数据= {
结果:[0]
} PassdownNotesData.getData.and.returnValue(数据);
PassdownNotesData.updatePassdownNotesData.and.returnValue(deferred.promise); deferred.reject({}); $ $范围摘要()。 期待(window.alert).toHaveBeenCalledWith('错误获取在控制器Passdown Notes数据');
});
});
I need a jasmine code changes, but I have to complete set of jasmine for the anonymous function. I am new to this. I tried but, I'm not able to achieve fully. Please help me.Html file
<div ng-class="{ 'mainPassdownFont mainPassdownNoteLayout noteEditBackground': isBeingEdited(note), 'mainPassdownFont mainPassdownNoteLayout': isNotBeingEdited(note) }" >
<textarea ng-attr-id="{{ 'note-' + note.PassdownNoteID }}" ng-show="isBeingEdited(note)" class="mainPassdownFont passdownTextAreaLayout">{{ note.NoteText }}</textarea>
<div ng-hide="isBeingEdited(note)" class="mainPassdownFont passdownTextAreaLayout">
<pre class="notePre">{{ note.NoteText }}</pre>
</div>
<input class="passdownNoteInlineCheckbox" ng-attr-id="{{ 'checkbox-' + note.PassdownNoteID }}" ng-model="cbModel" ng-change="checkBoxChanged(note)" ng-checked="isClaimed(note)" type="checkbox" name="notesCheckbox" value="notesCheckbox">
<div ng-show="isClaimed(note)" class="downArrowIcon passdownNoteDownArrowLayout" ></div>
<div ng-show="isClaimed(note)" class="passdownAnnotationFont claimTextContainerLayout" >
<p class="claimNameLayout">{{ note.Created.By.Text }}</p>
<p class="claimDateLayout">{{ note.Created.Date.Local | date : 'MMM d, y h:mm a' }}</p>
</div>
<div ng-hide="isClaimed(note)" ng-click="claimNote(note)" class="passdownAnnotationFont claimClickTextContainerLayout" >
<p class="claimNameLayout">Click to Claim</p>
</div>
<div class="passdownAnnotationFont authorTextContainerLayout" >
<p class="claimNameLayout">{{ note.LastModified.By.Text }}</p>
<p class="claimDateLayout">{{ note.LastModified.Date.Local | date : 'MMM d, y h:mm a' }}</p>
</div>
<div class="actionIconContainerLayout" >
<div class="deleteIcon inlineActionIconLayout" ng-click="deleteNote(note)"></div>
<div class="editIcon inlineActionIconLayout" ng-click="editNote(note)"></div>
<div class="addIcon inlineActionIconLayout" ng-click="addSubNote(note)"></div>
</div>
<div class="btnforpassdownTextAreaLayout">
<button ng-attr-id="{{ 'btn-' + note.PassdownNoteID }}" type="button" class="btn btnpassdownTextAreaLayout" ng-show="isBeingEdited(note)" ng-click="completeEdit(note)">Save</button>
<button ng-attr-id="{{ 'btn-' + note.PassdownNoteID }}" type="button" class="btn btnpassdownTextAreaLayout" ng-show="isBeingEdited(note)" ng-click="cancelEdit(note)">Cancel</button>
</div>
</div> </div>
</div>
</div>
js changes
app.controller('passdownCtrl',
['$rootScope', '$scope', '$http', '$resource', '$location', 'PassdownNotesData','$window', 'userService',
function($rootScope, $scope, $http, $resource, $location, PassdownNotesData, $window, userService) { $scope.passdownNotesData={ "reviewedDateUtc" : "01-01-2000T15:00", "results": [] };
$scope.reviewDate = null;
$scope.indexBeingEdited = -1;
$scope.inputfocus = false;
$scope.filterText = "";
// Go get trips from the server via an http Get Call. If successful, assign the response to
// $scope.tripData. If errors occur, assign them to the tripData for the UI to look for
// and provide the user some possible action.
$scope.passdownNotesData = PassdownNotesData.getData();
if ($scope.passdownNotesData.results.length == 0) {
PassdownNotesData.updatePassdownNotesData()
.then(function(result) {
$scope.passdownNotesData = result;
$scope.reviewDate = parseBoldIQDate(result.reviewedDateUtc); //error
}, function(error) {
alert('Error getting Passdown Notes data in controller');
});
} else {
//$scope.selectedRequestIndex = 0;
//$scope.selectedRequest = $scope.controllerRequestList.requestList[0];
}
parseBoldIQDate = function(dateStr) {
var newDate = new Date();
newDate.setUTCMonth(Number(dateStr.substr(0,2)) - 1);
newDate.setUTCDate(Number(dateStr.substr(3,2)));
newDate.setUTCFullYear(Number(dateStr.substr(6,4)));
newDate.setUTCHours(Number(dateStr.substr(11,2)));
newDate.setUTCMinutes(Number(dateStr.substr(14,2)));
return newDate;
};
$scope.noteState = function (cat, checkOpen) {
var containerName = cat + 'ContainerDiv';
var containerElement = document.getElementById(containerName);
if (containerElement != null) {
var isOpen = false;
if (containerElement.style.display == "block") {
isOpen = true;
}
return (isOpen == checkOpen);
}
return false;
};
getMaxNoteId = function() {
// create a new note structure, add it to the list, persist it, and put it in edit mode
var maxNoteId = 0;
for (var i = 0; i < $scope.passdownNotesData.results.length; i++ ) {
if ($scope.passdownNotesData.results[i].PassdownNoteID >= maxNoteId) {
maxNoteId = $scope.passdownNotesData.results[i].PassdownNoteID;
}
for (var j = 0; j < $scope.passdownNotesData.results[i].SubNotes.length; j++ ) {
if ($scope.passdownNotesData.results[i].SubNotes[j].PassdownNoteID >= maxNoteId) {
maxNoteId = $scope.passdownNotesData.results[i].SubNotes[j].PassdownNoteID;
}
}
}
return maxNoteId;
};
$scope.addNote = function(cat, event) {
// create a new note structure, add it to the list (local and global), persist it, and put it in edit mode
// create new note
var currentDate = new Date();
var newNote = new Object();
newNote.PassdownNoteID = getMaxNoteId() + 1;
newNote.Category = new Object();
newNote.Category.Text = cat;
newNote.NoteText = "Default Note Text";
newNote.Created = new Object();
// newNote.Created.ClaimStatus = "false";
newNote.Created.By = new Object();
newNote.Created.By.ID = 0;
newNote.Created.By.Text = "N/A";
newNote.Created.Date = new Object();
newNote.Created.Date.Utc = createBoldIQDate(currentDate);
newNote.Created.Date.Local = currentDate;
newNote.LastModified = new Object();
newNote.LastModified.By = new Object();
newNote.LastModified.By.ID = 17;
newNote.LastModified.By.Text = userService.getUser();
newNote.LastModified.Date = new Object();
newNote.LastModified.Date.Utc = createBoldIQDate(currentDate);
newNote.LastModified.Date.Local = currentDate;
newNote.SubNotes = new Array();
// Add it to local and global lists
//$scope.passdownNotesData.results[$scope.passdownNotesData.results.length] = newNote;
var dataCopy = PassdownNotesData.getData();
dataCopy.results[dataCopy.results.length] = newNote;
// Persist the updated data to the server
PassdownNotesData.writeUpdatesToServer(dataCopy);
// Put the display in Edit mode
$scope.indexBeingEdited = newNote.Id;
};
$scope.getNumUnclaimedForCategory = function(cat) {
var numUnclaimed = 0;
for (var i = 0; i < $scope.passdownNotesData.results.length; i++ ) {
if ($scope.passdownNotesData.results[i].Category.Text == cat) {
// If the main part of the note was updated, add 1 and call it good.
if ($scope.passdownNotesData.results[i].IsActive == "false") {
numUnclaimed++;
}
}
}
return numUnclaimed;
};
$scope.checkBoxChanged = function(note) {
var elementStr = "checkbox-" + note.PassdownNoteID;
var checkboxElement = document.getElementById(elementStr);
var val = checkboxElement.checked;
if (val == false) {
// Here we will unclaim the note
var dataCopy = PassdownNotesData.getDatacompleteSubNoteEdit();
// Update the real data 'and' our local scope copy, and write the real stuff back to the server
for (var i = 0; i < dataCopy.results.length; i++) {
if (note.PassdownNoteID == dataCopy.results[i].PassdownNoteID) {
console.log("inside here ", note.PassdownNoteID);
dataCopy.results[i].IsActive = "false";
//Update our local copy
$scope.passdownNotesData.results[i].IsActive = "false";
}
}
PassdownNotesData.writeUpdatesToServer(dataCopy); // $scope.passdownNotesData);
} else {
$scope.claimNote(note);
}
};
$scope.editSubNote = function(subNote) {
$scope.indexBeingEdited = subNote.PassdownNoteID;
var elementStr = "subNote-" + subNote.PassdownNoteID;
var textAreaElement = document.getElementById(elementStr);
$scope.subNotesText = textAreaElement.value;
};
$scope.isClaimed = function(note) {
if (note.isActive == "true") {
return true;
} else {
return false;
}
};
$scope.claimNote = function(note) {
var dataCopy = PassdownNotesData.getData();
// Update the real data 'and' our local scope copy, and write the real stuff back to the server
for (var i = 0; i < dataCopy.results.length; i++) {
if (note.PassdownNoteID == dataCopy.results[i].PassdownNoteID) {
dataCopy.results[i].isActive = "true";
dataCopy.results[i].Created.By.ID = 17;
dataCopy.results[i].Created.By.Text = $scope.loggedInUser;
var currentDate = new Date();
dataCopy.results[i].Created.Date.Utc = createBoldIQDate(currentDate);
dataCopy.results[i].Created.Date.Local = currentDate;
$scope.passdownNotesData.results[i].Created.Date.Utc = dataCopy.results[i].Created.Date.Utc;
$scope.passdownNotesData.results[i].Created.Date.Local = currentDate;
}
}
PassdownNotesData.writeUpdatesToServer(dataCopy); // $scope.passdownNotesData);
};
createBoldIQDate = function(date) {
var monthStr = (date.getUTCMonth() + 1).toString();
if (monthStr.length == 1) {
monthStr = "0" + monthStr;
}
var dateStr = (date.getUTCDate()).toString();
if (dateStr.length == 1) {
dateStr = "0" + dateStr;
}
var yearStr = (date.getUTCFullYear()).toString();
var hoursStr = (date.getUTCHours()).toString();
if (hoursStr.length == 1) {
hoursStr = "0" + hoursStr;
}
var minutesStr = (date.getUTCMinutes()).toString();
if (minutesStr.length == 1) {
minutesStr = "0" + minutesStr;
}
var dateStr = monthStr + "-" + dateStr + "-" + yearStr + "T" + hoursStr + ":" + minutesStr;
return dateStr;
};
$scope.filterTrips = function(note) {
if (note.Category.Text == 'Trips') { return true; } else { return false; }
};
$scope.filterWeather = function(note) {
if (note.Category.Text == 'Weather') { return true; } else { return false; }
};
$scope.filterMaintenance = function(note) {
if (note.Category.Text == 'Maintenance') { return true; } else { return false; }
};
$scope.filterGeneral = function(note) {
if (note.Category.Text == 'General') { return true; } else { return false; }
};
$scope.toggleNotesContainer = function(cat, event) {
var containerName = cat + 'ContainerDiv';
var containerElement = document.getElementById(containerName);
if (containerElement != null) {
if (containerElement.style.display == 'none') {
containerElement.style.display = 'block';
if (cat == "Trips") {
var buttonElement = document.getElementById("passdownToggleHideShowButton");
buttonElement.innerHTML = "Hide All";
}
} else {
containerElement.style.display = 'none';
if (cat == "Trips") {
var buttonElement = document.getElementById("passdownToggleHideShowButton");
buttonElement.innerHTML = "Show All";
}
}
}
};
$scope.toggleHideShowPassdown = function() {
// We will use the Trips container as the indicator of whether to "open All" or "Close all"
// TODO - when we get time, we want to animate the open/close of these containers
var tripsContainerElement = document.getElementById('TripsContainerDiv');
if (tripsContainerElement != null) {
if (tripsContainerElement.style.display == 'none') {
// This means we need to open all the containers, and change the text on the button to
// "hide all"
var buttonElement = document.getElementById("passdownToggleHideShowButton");
buttonElement.innerHTML = "Hide All";
var weatherContainerElement = document.getElementById('WeatherContainerDiv');
var maintenanceContainerElement = document.getElementById('MaintenanceContainerDiv');
var GeneralContainerElement = document.getElementById('GeneralContainerDiv');
tripsContainerElement.style.display = 'block';
weatherContainerElement.style.display = 'block';
maintenanceContainerElement.style.display = 'block';
GeneralContainerElement.style.display = 'block';
} else {
// This means we need to close all the containers, and change the text on the button to
// "show all"
var buttonElement = document.getElementById("passdownToggleHideShowButton");
buttonElement.innerHTML = "Show All";
var weatherContainerElement = document.getElementById('WeatherContainerDiv');
var maintenanceContainerElement = document.getElementById('MaintenanceContainerDiv');
var GeneralContainerElement = document.getElementById('GeneralContainerDiv');
tripsContainerElement.style.display = 'none';
weatherContainerElement.style.display = 'none';
maintenanceContainerElement.style.display = 'none';
GeneralContainerElement.style.display = 'none';
}
}
};
var containerElement = document.getElementById("TripsContainerDiv");
containerElement.style.display = 'block';
containerElement = document.getElementById("WeatherContainerDiv");
containerElement.style.display = 'block';
containerElement = document.getElementById("MaintenanceContainerDiv");
containerElement.style.display = 'block';
containerElement = document.getElementById("GeneralContainerDiv");
containerElement.style.display = 'block';
$scope.gotoPassdownScreen = function () {
//$location.path('#/requests/legDetails');
};
}]);
Jasmine js file..
describe('passdownnotes controller spec',function() {
var ctrlScope;
var rootScope;
var userServiceMock;
var PassdownNotesDataMock;
beforeEach(function() {
module('app'); // load jiops module
});
beforeEach(inject(function ($rootScope, $controller, $httpBackend, $q) {
ctrlScope = $rootScope.$new();
rootScope = $rootScope;
PassdownNotesDataMock = {
updatePassdownNotesData: function() {
var deferred = $q.defer();
deferred.resolve(mockPassdownNotesData);
return deferred.promise;
},
getData: function() {
return mockPassdownNotesData;
}
};
userServiceMock = {
getUser: function() {
var deferred = $q.defer();
deferred.resolve(mockUserData);
return deferred.promise;
}
};
$controller('passdownCtrl', {
$scope: ctrlScope,
PassdownNotesData:PassdownNotesDataMock,
userService:userServiceMock
});
}));
describe('passdownnotes controller',function() {
it('should have the correct initial configuration', function() {
expect(ctrlScope.indexBeingEdited).toEqual(-1);
expect(ctrlScope.inputfocus).toBe(false);
expect(ctrlScope.reviewDate).toBe(null);
expect(ctrlScope.passdownNotesData).toEqual(0);
expect(ctrlScope.filterText).toBe("");
});
});
});
var mockPassdownNotesData =
{
"results":[
{
"Created":{
"By":{
"ID":3,
"Text":"Developer, Jeppesen"
},
"Date":{
"Local":"07-02-2015T14:20:26",
"TzAbbrev":" MDT",
"Utc":"07-02-2015T20:20:26"
}
},
"IsActive":true,
"LastModified":{
"By":{
"ID":3,
"Text":"Developer, Jeppesen"
},
"Date":{
"Local":"07-02-2015T14:29:32",
"TzAbbrev":" MDT",
"Utc":"07-02-2015T20:29:32"
}
},
"NoteText":"test note3333",
"PassdownNoteID":1,
"AssignedTo":{
"ID":0,
"Text":""
},
"Category":{
"ID":692,
"Text":"Trips"
},
"DateAssigned":null,
"DueDate":{
"Local":"07-19-2015T18:00",
"TzAbbrev":" MDT",
"Utc":"07-20-2015T00:00"
},
"ExpirationDate":null,
"SubNotes":[
{
"Created":{
"By":{
"ID":3,
"Text":"Developer, Jeppesen"
},
"Date":{
"Local":"07-02-2015T14:20:27",
"TzAbbrev":" MDT",
"Utc":"07-02-2015T20:20:27"
}
},
"IsActive":true,
"LastModified":{
"By":{
"ID":3,
"Text":"Developer, Jeppesen"
},
"Date":{
"Local":"07-02-2015T14:20:27",
"TzAbbrev":" MDT",
"Utc":"07-02-2015T20:20:27"
}
},
"NoteText":"sample sub note",
"PassdownNoteID":2
}
]
},
{
"Created":{
"By":{
"ID":3,
"Text":"Developer, Jeppesen"
},
"Date":{
"Local":"07-07-2015T11:12:03",
"TzAbbrev":" MDT",
"Utc":"07-07-2015T17:12:03"
}
},
"IsActive":true,
"LastModified":{
"By":{
"ID":3,
"Text":"Developer, Jeppesen"
},
"Date":{
"Local":"07-07-2015T11:12:03",
"TzAbbrev":" MDT",
"Utc":"07-07-2015T17:12:03"
}
},
"NoteText":"Test Passdown Note",
"PassdownNoteID":3,
"AssignedTo":{
"ID":302,
"Text":"Button, Jenson"
},
"Category":{
"ID":695,
"Text":"General"
},
"DateAssigned":{
"Local":"07-07-2015T11:12",
"TzAbbrev":" MDT",
"Utc":"07-07-2015T17:12"
},
"DueDate":null,
"ExpirationDate":null,
"SubNotes":[
{
"Created":{
"By":{
"ID":3,
"Text":"Developer, Jeppesen"
},
"Date":{
"Local":"07-07-2015T11:12:03",
"TzAbbrev":" MDT",
"Utc":"07-07-2015T17:12:03"
}
},
"IsActive":true,
"LastModified":{
"By":{
"ID":3,
"Text":"Developer, Jeppesen"
},
"Date":{
"Local":"07-07-2015T11:12:03",
"TzAbbrev":" MDT",
"Utc":"07-07-2015T17:12:03"
}
},
"NoteText":"Test of passdown subnote",
"PassdownNoteID":4
}
]
}
]
};
解决方案
app.controller('passdownCtrl', function($scope, PassdownNotesData) {
var vm = this;
vm.passdownNotesData = PassdownNotesData.getData();
if (vm.passdownNotesData.results.length == 0) {
PassdownNotesData.updatePassdownNotesData()
.then(function(result) {
vm.passdownNotesData = result;
vm.reviewDate = parseBoldIQDate(result.reviewedDateUtc);
}, function(error) {
alert('Error getting Passdown Notes data in controller');
});
}
var parseBoldIQDate = function(dateStr) {
var newDate = new Date();
newDate.setUTCMonth(Number(dateStr.substr(0, 2)) - 1);
newDate.setUTCDate(Number(dateStr.substr(3, 2)));
newDate.setUTCFullYear(Number(dateStr.substr(6, 4)));
newDate.setUTCHours(Number(dateStr.substr(11, 2)));
newDate.setUTCMinutes(Number(dateStr.substr(14, 2)));
return newDate;
};
});
describe('Controller: passdownCtrl', function(){
var $scope,
$controller,
$q,
deferred,
PassdownNotesData,
ctrl,
window;
beforeEach(angular.mock.module('requiredModule'));
beforeEach(function(){
angular.mock.module(function($provide){
PassdownNotesData = jasmine.createSpyObj('PassdownNotesData', ['getData', 'updatePassdownNotesData']);
$provide.value('PassdownNotesData', PassdownNotesData);
});
});
beforeEach(function(){
angular.mock.module(function($provide){
window = jasmine.createSpyObj('window', ['alert']);
$provide.value('window', window);
});
});
beforeEach(inject(function(_$rootScope_, _$controller_, _$q_, _PassdownNotesData_){
$scope = _$rootScope_.$new();
$controller = _$controller_;
$q = _$q_;
PassdownNotesData = _PassdownNotesData_;
deferred = $q.defer();
ctrl = $controller('passdownCtrl as ctrl', {
$scope: $scope,
PassdownNotesData: PassdownNotesData
});
}));
it('should resolve promise', function(){
var data = {
results: ['0']
}
var result = {
name: '0',
reviewedDateUtc: '12'
};
PassdownNotesData.getData.and.returnValue(data);
PassdownNotesData.updatePassdownNotesData.and.returnValue(deferred.promise);
deferred.resolve(result);
$scope.$digest();
expect(ctrl.passdownNotesData).toBe(result);
// function parseBoldIQDate must be executed with
// result.reviewedDateUtc value
expect(ctrl.reviewDate).toEqual('Thu Nov 30 0 02:00:50 GMT+0200 (FLE Standard Time)');
});
it('should reject promise', function(){
var data = {
results: ['0']
}
PassdownNotesData.getData.and.returnValue(data);
PassdownNotesData.updatePassdownNotesData.and.returnValue(deferred.promise);
deferred.reject({});
$scope.$digest();
expect(window.alert).toHaveBeenCalledWith('Error getting Passdown Notes data in controller');
});
});
这篇关于茉莉测试用例匿名函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!