personalLog demo - initial version with spec

This commit is contained in:
Igor Minar
2010-10-22 22:13:14 -07:00
committed by Misko Hevery
parent 1391f19fb4
commit 5b40e87ac6
4 changed files with 212 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
<!doctype html>
<html xmlns:ng="http://angularjs.org">
<head>
<title>Personal Log</title>
<script type="text/javascript" src="../../src/angular-bootstrap.js" ng:autobind></script>
<script type="text/javascript" src="personalLog.js"></script>
</head>
<body ng:controller="example.personalLog.LogCtrl">
<form action="" ng:submit="addLog(newMsg)">
<input type="text" name="newMsg" />
<input type="submit" value="add" />
<input type="button" value="remove all" ng:click="rmLogs()" />
</form>
<hr/>
<h2>Logs:</h2>
<ul>
<li ng:repeat="log in logs">
{{log.at | date:'yy-MM-dd HH:mm'}} {{log.msg}}
[<a href="" ng:click="rmLog($index)">x</a>]
</li>
</ul>
</body>
</html>

View File

@@ -0,0 +1,60 @@
//app namespace
var example = {};
example.personalLog = {};
//name space isolating closure
(function() {
var LOGS = 'logs';
/**
* The controller for the personal log app.
*/
function LogCtrl($cookieStore) {
var self = this,
logs = self.logs = $cookieStore.get(LOGS) || [];
/**
* Adds newMsg to the logs array as a log, persists it and clears newMsg.
*/
this.addLog = function(msg) {
var newMsg = msg || self.newMsg;
if (!newMsg) return;
var log = {
at: new Date().getTime(),
msg: newMsg
}
logs.push(log);
$cookieStore.put(LOGS, logs);
self.newMsg = '';
}
/**
* Persistently removes a log from logs.
* @param {number} msgIdx Index of the log to remove.
*/
this.rmLog = function(msgIdx) {
logs.splice(msgIdx,1);
$cookieStore.put(LOGS, logs);
}
/**
* Persistently removes all logs.
*/
this.rmLogs = function() {
logs.splice(0);
$cookieStore.remove(LOGS);
}
}
//inject
LogCtrl.$inject = ['$cookieStore'];
//export
example.personalLog.LogCtrl = LogCtrl;
})();

View File

@@ -0,0 +1,122 @@
describe('example.personalLog.LogCtrl', function() {
var logCtrl;
function createNotesCtrl() {
var scope = angular.scope();
return scope.$new(example.personalLog.LogCtrl);
}
beforeEach(function() {
logCtrl = createNotesCtrl();
});
it('should initialize notes with an empty array', function() {
expect(logCtrl.logs).toEqual([]);
});
describe('addLog', function() {
beforeEach(function() {
expect(logCtrl.logs).toEqual([]);
});
it('should add newMsg to logs as a log entry', function() {
logCtrl.newMsg = 'first log message';
logCtrl.addLog();
expect(logCtrl.logs.length).toBe(1);
expect(logCtrl.logs[0].msg).toBe('first log message');
//one more msg, this time passed in as param
logCtrl.addLog('second log message');
expect(logCtrl.logs.length).toBe(2);
expect(logCtrl.logs[0].msg).toBe('first log message');
expect(logCtrl.logs[1].msg).toBe('second log message');
});
it('should clear newMsg when log entry is persisted', function() {
logCtrl.addLog('first log message');
expect(logCtrl.newMsg).toBe('');
});
it('should store logs in the logs cookie', function() {
expect(logCtrl.$cookies.logs).not.toBeDefined();
logCtrl.addLog('first log message');
expect(logCtrl.$cookies.logs).toBeTruthy();
});
it('should do nothing if newMsg is empty', function() {
logCtrl.addLog('');
expect(logCtrl.logs.length).toBe(0);
});
});
describe('rmLog', function() {
beforeEach(function() {
logCtrl.addLog('message1');
logCtrl.addLog('message2');
logCtrl.addLog('message3');
logCtrl.addLog('message4');
expect(logCtrl.logs.length).toBe(4);
});
it('should delete a message identified by index', function() {
logCtrl.rmLog(1);
expect(logCtrl.logs.length).toBe(3);
logCtrl.rmLog(2);
expect(logCtrl.logs.length).toBe(2);
expect(logCtrl.logs[0].msg).toBe('message1');
expect(logCtrl.logs[1].msg).toBe('message3');
});
it('should update cookies when a log is deleted', function() {
expect(logCtrl.$cookies.logs).toMatch(/\[\{.*?\}(,\{.*?\}){3}\]/);
logCtrl.rmLog(1);
expect(logCtrl.$cookies.logs).toMatch(/\[\{.*?\}(,\{.*?\}){2}\]/);
logCtrl.rmLog(0);
logCtrl.rmLog(0);
logCtrl.rmLog(0);
expect(logCtrl.$cookies.logs).toMatch(/\[\]/);
});
});
describe('rmLogs', function() {
beforeEach(function() {
logCtrl.addLog('message1');
logCtrl.addLog('message2');
logCtrl.addLog('message3');
logCtrl.addLog('message4');
expect(logCtrl.logs.length).toBe(4);
});
it('should remove all logs', function() {
logCtrl.rmLogs();
expect(logCtrl.logs).toEqual([]);
});
it('should remove logs cookie', function() {
expect(logCtrl.$cookies.logs).toBeTruthy();
logCtrl.rmLogs();
expect(logCtrl.$cookies.logs).not.toBeDefined();
});
});
});

View File

@@ -8,12 +8,14 @@ load:
- src/Angular.js
- src/JSON.js
- src/*.js
- example/personalLog/*.js
- test/testabilityPatch.js
- src/scenario/Scenario.js
- src/scenario/*.js
- test/angular-mocks.js
- test/scenario/*.js
- test/*.js
- example/personalLog/test/*.js
exclude:
- test/jquery_alias.js