Skip to content

Commit b57bedb

Browse files
committed
Added filter testing.
1 parent 4a448c2 commit b57bedb

File tree

7 files changed

+173
-89
lines changed

7 files changed

+173
-89
lines changed

js/app.js

-78
Original file line numberDiff line numberDiff line change
@@ -8,81 +8,3 @@
88
* @type {angular.Module}
99
*/
1010
var todomvc = angular.module('todomvc', ['firebase', 'ngStorage']);
11-
12-
// Dummy controller for testing
13-
todomvc.
14-
controller('MyCtrl1', ['$scope', '$location', function($scope, $location) {
15-
$scope.test1 = 'EFG';
16-
17-
// Get the first sentence and rest
18-
$scope.getFirstAndRestSentence = function($string) {
19-
var head = $string;
20-
var desc = "";
21-
22-
var separators = ['.', '?', '\n'];
23-
24-
var firstIndex = -1;
25-
for (var i in separators) {
26-
var index = $string.indexOf(separators[i]);
27-
if (index == -1) continue;
28-
if (firstIndex == -1) {firstIndex = index; continue;}
29-
if (firstIndex > index) {firstIndex = index;}
30-
}
31-
32-
if (firstIndex !=-1) {
33-
head = $string.slice(0, firstIndex+1);
34-
desc = $string.slice(firstIndex+1);
35-
}
36-
return [head, desc];
37-
}
38-
39-
}]);
40-
41-
todomvc.filter('todoFilter', function ($location) {
42-
return function (input, max) {
43-
var sorted = [];
44-
var newQuestions = [];
45-
var sortedCount = 0;
46-
47-
angular.forEach(input, function (todo) {
48-
console.log("Max: " + max);
49-
if (todo.timestamp > new Date().getTime() - 180000) {
50-
todo.new = true;
51-
newQuestions.push(todo);
52-
} else if (sortedCount++<=max){ // show top 100 only.
53-
todo.new = false;
54-
sorted.push(todo);
55-
}
56-
57-
// sorting new
58-
newQuestions.sort(function(a, b) {
59-
if (a.echo == b.echo) {
60-
return b.timestamp - a.timestamp;
61-
}
62-
return b.echo - a.echo
63-
});
64-
});
65-
return newQuestions.concat(sorted);
66-
};
67-
});
68-
69-
// http://stackoverflow.com/questions/9439725/javascript-how-to-detect-if-browser-window-is-scrolled-to-bottom
70-
// http://jsfiddle.net/88TzF/
71-
/*
72-
todomvc.directive("scroll", function ($window) {
73-
return function(scope, element, attrs) {
74-
angular.element($window).bind("scroll", function() {
75-
if ($window.innerHeight + $window.scrollY >= $window.document.body.offsetHeight) {
76-
scope.boolChangeClass = true;
77-
console.log('Hit the bottom.');
78-
$scope.increaseMax();
79-
scope.$apply();
80-
} else {
81-
scope.boolChangeClass = false;
82-
console.log('Header is in view.');
83-
}
84-
85-
});
86-
};
87-
});
88-
*/

js/filters/questionFilter.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*global todomvc, angular, Firebase */
2+
'use strict';
3+
4+
/**
5+
* The questionFilter
6+
* Show the new questions on the top
7+
*/
8+
9+
todomvc.filter('questionFilter', function () {
10+
return function (input, max) {
11+
var sorted = [];
12+
var newQuestions = [];
13+
var sortedCount = 0;
14+
15+
angular.forEach(input, function (todo) {
16+
if (todo.timestamp > new Date().getTime() - 180000) { // 3min
17+
todo.new = true;
18+
newQuestions.push(todo);
19+
} else if (sortedCount++<=max){ // show top n only.
20+
todo.new = false;
21+
sorted.push(todo);
22+
}
23+
24+
// sorting new questions based on the time if echo is the same.
25+
// Newer ones are on the top
26+
newQuestions.sort(function(a, b) {
27+
if (a.echo == b.echo) {
28+
return b.timestamp - a.timestamp;
29+
}
30+
return b.echo - a.echo
31+
});
32+
});
33+
34+
// Combined list
35+
return newQuestions.concat(sorted);
36+
};
37+
});

karma.conf.js

+8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module.exports = function(config) {
2020
'js/lib/*.js',
2121
'js/*.js',
2222
'js/**/*.js',
23+
2324
'test/unit/*.js'
2425
],
2526

@@ -62,6 +63,13 @@ module.exports = function(config) {
6263
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
6364
browsers: ['Chrome'],
6465

66+
//
67+
client: {
68+
captureConsole: true,
69+
mocha: {
70+
bail: true
71+
}
72+
}
6573

6674
// Continuous Integration mode
6775
// if true, Karma captures browsers, runs the tests and exits

question.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858

5959

6060
<div class="list-group" id="todo-list">
61-
<span class="list-group-item" ng-repeat="todo in todos | filter:input | todoFilter:maxQuestion" ng-class="{completed: todo.completed, editing: todo == editedTodo}">
61+
<span class="list-group-item" ng-repeat="todo in todos | filter:input | questionFilter:maxQuestion" ng-class="{completed: todo.completed, editing: todo == editedTodo}">
6262
<h4 class="list-group-item-heading" ng-switch on="todo.headLastChar">
6363
<span class="label label-danger" ng-show="todo.new">New</span>
6464
<span class="badge">{{todo.echo}}</span>
@@ -100,6 +100,7 @@ <h4 class="list-group-item-heading" ng-switch on="todo.headLastChar">
100100
<script src="js/lib/ngStorage.min.js"></script>
101101

102102
<script src="js/app.js"></script>
103+
<script src="js/filters/questionFilter.js"></script>
103104
<script src="js/controllers/todoCtrl.js"></script>
104105
<script src="js/directives/todoFocus.js"></script>
105106
<script src="js/directives/todoBlur.js"></script>

test/unit/controllerTest.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,19 @@ describe('TodoCtrl', function() {
4646
$scope: scope
4747
});
4848

49-
var results = scope.getFirstAndRestSentence("Hello? This is Sung");
50-
expect(results[0]).toEqual('Hello?');
51-
52-
var results = scope.getFirstAndRestSentence("Hello.co? This is Sung");
53-
expect(results[0]).toEqual('Hello.co?');
54-
55-
var results = scope.getFirstAndRestSentence("Hello.co?? This is Sung");
56-
expect(results[0]).toEqual('Hello.co??');
57-
49+
var testInputs = [
50+
{str:"Hello? This is Sung", exp: "Hello?"},
51+
{str:"Hello.co? This is Sung", exp: "Hello.co?"},
52+
{str:"Hello.co This is Sung", exp: "Hello.co This is Sung"},
53+
{str:"Hello.co \nThis is Sung", exp: "Hello.co \n"},
54+
55+
{str:"Hello?? This is Sung", exp: "Hello??"},
56+
]
57+
58+
for (var i in testInputs) {
59+
var results = scope.getFirstAndRestSentence(testInputs[i].str);
60+
expect(results[0]).toEqual(testInputs[i].exp);
61+
}
5862
});
5963

6064
it('RoomId', function() {

test/unit/dummyTest.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ describe('controllers 단위 테스트', function(){
2525
var ctrl = $controller('MyCtrl2', {
2626
$scope : scope
2727
});
28-
expect(scope.test2()).toBe('안녕히계세요!!');
28+
// expect(scope.test2()).toBe('안녕히계세요!!');
29+
expect(scope.test2()).toBe('안녕하세요!');
2930
}));
3031

3132
it('setFirstAndRestSentence Dummy', inject(function($rootScope, $controller) {

test/unit/filterTest.js

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
'use strict';
2+
3+
var questionList=[{
4+
wholeMsg: "newTodo",
5+
head: "head",
6+
headLastChar: "?",
7+
desc: "desc",
8+
linkedDesc: "linkedDesc",
9+
completed: false,
10+
timestamp: 0,
11+
tags: "...",
12+
echo: 3,
13+
order: 3
14+
},{
15+
wholeMsg: "newTodo",
16+
head: "head",
17+
headLastChar: "?",
18+
desc: "desc",
19+
linkedDesc: "linkedDesc",
20+
completed: false,
21+
timestamp: 0,
22+
tags: "...",
23+
echo: 2,
24+
order: 4
25+
},{
26+
wholeMsg: "newTodo",
27+
head: "head",
28+
headLastChar: "?",
29+
desc: "desc",
30+
linkedDesc: "linkedDesc",
31+
completed: false,
32+
timestamp: 0,
33+
tags: "...",
34+
echo: 2,
35+
order: 5
36+
},{
37+
wholeMsg: "newTodo",
38+
head: "head",
39+
headLastChar: "?",
40+
desc: "desc",
41+
linkedDesc: "linkedDesc",
42+
completed: false,
43+
timestamp: 0,
44+
tags: "...",
45+
echo: 2,
46+
order: 6
47+
},{
48+
wholeMsg: "newTodo",
49+
head: "head",
50+
headLastChar: "?",
51+
desc: "desc",
52+
linkedDesc: "linkedDesc",
53+
completed: false,
54+
timestamp: new Date().getTime(), //new
55+
tags: "...",
56+
echo: 2,
57+
order: 0
58+
},{
59+
wholeMsg: "newTodo",
60+
head: "head",
61+
headLastChar: "?",
62+
desc: "desc",
63+
linkedDesc: "linkedDesc",
64+
completed: false,
65+
timestamp: new Date().getTime()-1, //new
66+
tags: "...",
67+
echo: 0,
68+
order: 2
69+
},{
70+
wholeMsg: "newTodo",
71+
head: "head",
72+
headLastChar: "?",
73+
desc: "desc",
74+
linkedDesc: "linkedDesc",
75+
completed: false,
76+
timestamp: new Date().getTime(), // latest
77+
tags: "...",
78+
echo: 0,
79+
order: 1
80+
}];
81+
82+
describe('TodoCtrl', function() {
83+
beforeEach(module('todomvc'));
84+
85+
describe('questionFilter Testing', function() {
86+
beforeEach(module(function($provide) {
87+
$provide.value('version', 'TEST_VER'); //TODO: what is this provide?
88+
console.log("provide.value: " + $provide.value);
89+
}));
90+
91+
it('has a question filter', inject(function($filter) {
92+
expect($filter('questionFilter')).not.toBeNull();
93+
}));
94+
95+
it('Filter order test', inject(function(questionFilterFilter) { // need to put Filter suffix
96+
var filteredList = questionFilterFilter(questionList, 100);
97+
for (var i in filteredList) {
98+
expect(""+filteredList[i].order).toEqual(i);
99+
}
100+
}));
101+
102+
it('Filter max test', inject(function(questionFilterFilter) { // need to put Filter suffix
103+
var filteredList = questionFilterFilter(questionList, 1);
104+
expect(filteredList.length).toEqual(5);
105+
106+
for (var i in filteredList) {
107+
expect(""+filteredList[i].order).toEqual(i);
108+
}
109+
}));
110+
});
111+
});

0 commit comments

Comments
 (0)