Skip to content

Commit 5b40e87

Browse files
IgorMinarmhevery
authored andcommitted
personalLog demo - initial version with spec
1 parent 1391f19 commit 5b40e87

File tree

4 files changed

+212
-0
lines changed

4 files changed

+212
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!doctype html>
2+
<html xmlns:ng="http://angularjs.org">
3+
<head>
4+
<title>Personal Log</title>
5+
<script type="text/javascript" src="../../src/angular-bootstrap.js" ng:autobind></script>
6+
<script type="text/javascript" src="personalLog.js"></script>
7+
</head>
8+
9+
10+
<body ng:controller="example.personalLog.LogCtrl">
11+
12+
<form action="" ng:submit="addLog(newMsg)">
13+
<input type="text" name="newMsg" />
14+
<input type="submit" value="add" />
15+
<input type="button" value="remove all" ng:click="rmLogs()" />
16+
</form>
17+
18+
<hr/>
19+
<h2>Logs:</h2>
20+
<ul>
21+
<li ng:repeat="log in logs">
22+
{{log.at | date:'yy-MM-dd HH:mm'}} {{log.msg}}
23+
[<a href="" ng:click="rmLog($index)">x</a>]
24+
</li>
25+
</ul>
26+
27+
</body>
28+
</html>

example/personalLog/personalLog.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//app namespace
2+
var example = {};
3+
example.personalLog = {};
4+
5+
6+
//name space isolating closure
7+
(function() {
8+
9+
var LOGS = 'logs';
10+
11+
/**
12+
* The controller for the personal log app.
13+
*/
14+
function LogCtrl($cookieStore) {
15+
var self = this,
16+
logs = self.logs = $cookieStore.get(LOGS) || [];
17+
18+
19+
/**
20+
* Adds newMsg to the logs array as a log, persists it and clears newMsg.
21+
*/
22+
this.addLog = function(msg) {
23+
var newMsg = msg || self.newMsg;
24+
if (!newMsg) return;
25+
var log = {
26+
at: new Date().getTime(),
27+
msg: newMsg
28+
}
29+
30+
logs.push(log);
31+
$cookieStore.put(LOGS, logs);
32+
self.newMsg = '';
33+
}
34+
35+
36+
/**
37+
* Persistently removes a log from logs.
38+
* @param {number} msgIdx Index of the log to remove.
39+
*/
40+
this.rmLog = function(msgIdx) {
41+
logs.splice(msgIdx,1);
42+
$cookieStore.put(LOGS, logs);
43+
}
44+
45+
46+
/**
47+
* Persistently removes all logs.
48+
*/
49+
this.rmLogs = function() {
50+
logs.splice(0);
51+
$cookieStore.remove(LOGS);
52+
}
53+
}
54+
55+
//inject
56+
LogCtrl.$inject = ['$cookieStore'];
57+
58+
//export
59+
example.personalLog.LogCtrl = LogCtrl;
60+
})();
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
describe('example.personalLog.LogCtrl', function() {
2+
var logCtrl;
3+
4+
function createNotesCtrl() {
5+
var scope = angular.scope();
6+
return scope.$new(example.personalLog.LogCtrl);
7+
}
8+
9+
10+
beforeEach(function() {
11+
logCtrl = createNotesCtrl();
12+
});
13+
14+
15+
it('should initialize notes with an empty array', function() {
16+
expect(logCtrl.logs).toEqual([]);
17+
});
18+
19+
20+
describe('addLog', function() {
21+
22+
beforeEach(function() {
23+
expect(logCtrl.logs).toEqual([]);
24+
});
25+
26+
27+
it('should add newMsg to logs as a log entry', function() {
28+
logCtrl.newMsg = 'first log message';
29+
logCtrl.addLog();
30+
31+
expect(logCtrl.logs.length).toBe(1);
32+
expect(logCtrl.logs[0].msg).toBe('first log message');
33+
34+
//one more msg, this time passed in as param
35+
logCtrl.addLog('second log message');
36+
37+
expect(logCtrl.logs.length).toBe(2);
38+
expect(logCtrl.logs[0].msg).toBe('first log message');
39+
expect(logCtrl.logs[1].msg).toBe('second log message');
40+
});
41+
42+
43+
it('should clear newMsg when log entry is persisted', function() {
44+
logCtrl.addLog('first log message');
45+
expect(logCtrl.newMsg).toBe('');
46+
});
47+
48+
49+
it('should store logs in the logs cookie', function() {
50+
expect(logCtrl.$cookies.logs).not.toBeDefined();
51+
logCtrl.addLog('first log message');
52+
expect(logCtrl.$cookies.logs).toBeTruthy();
53+
});
54+
55+
56+
it('should do nothing if newMsg is empty', function() {
57+
logCtrl.addLog('');
58+
expect(logCtrl.logs.length).toBe(0);
59+
});
60+
});
61+
62+
63+
describe('rmLog', function() {
64+
65+
beforeEach(function() {
66+
logCtrl.addLog('message1');
67+
logCtrl.addLog('message2');
68+
logCtrl.addLog('message3');
69+
logCtrl.addLog('message4');
70+
expect(logCtrl.logs.length).toBe(4);
71+
});
72+
73+
74+
it('should delete a message identified by index', function() {
75+
logCtrl.rmLog(1);
76+
expect(logCtrl.logs.length).toBe(3);
77+
78+
logCtrl.rmLog(2);
79+
expect(logCtrl.logs.length).toBe(2);
80+
expect(logCtrl.logs[0].msg).toBe('message1');
81+
expect(logCtrl.logs[1].msg).toBe('message3');
82+
});
83+
84+
85+
it('should update cookies when a log is deleted', function() {
86+
expect(logCtrl.$cookies.logs).toMatch(/\[\{.*?\}(,\{.*?\}){3}\]/);
87+
88+
logCtrl.rmLog(1);
89+
expect(logCtrl.$cookies.logs).toMatch(/\[\{.*?\}(,\{.*?\}){2}\]/);
90+
91+
logCtrl.rmLog(0);
92+
logCtrl.rmLog(0);
93+
logCtrl.rmLog(0);
94+
expect(logCtrl.$cookies.logs).toMatch(/\[\]/);
95+
});
96+
});
97+
98+
99+
describe('rmLogs', function() {
100+
101+
beforeEach(function() {
102+
logCtrl.addLog('message1');
103+
logCtrl.addLog('message2');
104+
logCtrl.addLog('message3');
105+
logCtrl.addLog('message4');
106+
expect(logCtrl.logs.length).toBe(4);
107+
});
108+
109+
110+
it('should remove all logs', function() {
111+
logCtrl.rmLogs();
112+
expect(logCtrl.logs).toEqual([]);
113+
});
114+
115+
116+
it('should remove logs cookie', function() {
117+
expect(logCtrl.$cookies.logs).toBeTruthy();
118+
logCtrl.rmLogs();
119+
expect(logCtrl.$cookies.logs).not.toBeDefined();
120+
});
121+
});
122+
});

jsTestDriver.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ load:
88
- src/Angular.js
99
- src/JSON.js
1010
- src/*.js
11+
- example/personalLog/*.js
1112
- test/testabilityPatch.js
1213
- src/scenario/Scenario.js
1314
- src/scenario/*.js
1415
- test/angular-mocks.js
1516
- test/scenario/*.js
1617
- test/*.js
18+
- example/personalLog/test/*.js
1719

1820
exclude:
1921
- test/jquery_alias.js

0 commit comments

Comments
 (0)