Adding e2e tests for the PersonalLog app

- added scenario runner
- added scenario specs
- cookie cleaning DSL
- making rmLog independent on ordering in the view
This commit is contained in:
Igor Minar 2010-10-23 21:40:27 -07:00
parent 943377a091
commit 01c52e92b1
5 changed files with 122 additions and 12 deletions

View file

@ -6,8 +6,10 @@
<script type="text/javascript" src="personalLog.js"></script>
</head>
<body ng:controller="example.personalLog.LogCtrl">
<!-- TODO: we need to expose $root so that we can delete cookies in the scenario runner, there
must be a better way to do this -->
<body ng:controller="example.personalLog.LogCtrl" ng:init="$window.$root = $root">
<form action="" ng:submit="addLog(newMsg)">
<input type="text" name="newMsg" />
@ -20,7 +22,7 @@
<ul>
<li ng:repeat="log in logs.$orderBy('-at')">
{{log.at | date:'yy-MM-dd HH:mm'}} {{log.msg}}
[<a href="" ng:click="rmLog($index)">x</a>]
[<a href="" ng:click="rmLog(log)">x</a>]
</li>
</ul>

View file

@ -51,10 +51,10 @@ function LogCtrl($cookieStore) {
/**
* Persistently removes a log from logs.
* @param {number} msgIdx Index of the log to remove.
* @param {object} log The log to remove.
*/
this.rmLog = function(msgIdx) {
logs.splice(msgIdx,1);
this.rmLog = function(log) {
angular.Array.remove(logs, log);
$cookieStore.put(LOGS, logs);
};

View file

@ -0,0 +1,98 @@
describe('personal log', function() {
beforeEach(function() {
navigateTo('../personalLog.html');
});
afterEach(function() {
clearCookies();
});
it('should create new logs and order them in reverse chronological order', function(){
//create first msg
input('newMsg').enter('my first message');
element('form input[type="submit"]').click();
expect(repeater('ul li').count()).toEqual(1);
expect(repeater('ul li').column('log.msg')).toEqual('my first message');
//create second msg
input('newMsg').enter('my second message');
element('form input[type="submit"]').click();
expect(repeater('ul li').count()).toEqual(2);
expect(repeater('ul li').column('log.msg')).toEqual(['my second message', 'my first message']);
});
it('should delete a log when user clicks on the related X link', function() {
//create first msg
input('newMsg').enter('my first message');
element('form input[type="submit"]').click();
//create second msg
input('newMsg').enter('my second message');
element('form input[type="submit"]').click();
expect(repeater('ul li').count()).toEqual(2);
element('ul li a:eq(1)').click();
expect(repeater('ul li').count()).toEqual(1);
expect(repeater('ul li').column('log.msg')).toEqual('my second message');
element('ul li a:eq(0)').click();
expect(repeater('ul li').count()).toEqual(0);
});
it('should delete all cookies when user clicks on "remove all" button', function() {
//create first msg
input('newMsg').enter('my first message');
element('form input[type="submit"]').click();
//create second msg
input('newMsg').enter('my second message');
element('form input[type="submit"]').click();
expect(repeater('ul li').count()).toEqual(2);
element('input[value="remove all"]').click();
expect(repeater('ul li').count()).toEqual(0);
});
it('should preserve logs over page reloads', function() {
input('newMsg').enter('my persistent message');
element('form input[type="submit"]').click();
expect(repeater('ul li').count()).toEqual(1);
navigateTo('about:blank');
navigateTo('../personalLog.html');
expect(repeater('ul li').column('log.msg')).toEqual('my persistent message');
expect(repeater('ul li').count()).toEqual(1);
});
});
/**
* DSL for deleting all cookies.
*/
angular.scenario.dsl('clearCookies', function() {
/**
* Deletes cookies by interacting with the cookie service within the application under test.
*/
return function() {
this.addFutureAction('clear all cookies', function($window, $document, done) {
//TODO: accessing angular services is pretty nasty, we need a better way to reach them
var $cookies = $window.$root.$cookies,
cookieName;
for (cookieName in $cookies) {
console.log('deleting cookie: ' + cookieName);
delete $cookies[cookieName];
}
$window.$root.$eval();
done();
});
};
});

View file

@ -0,0 +1,10 @@
<!doctype html">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Personal Log Scenario Runner</title>
<script type="text/javascript" src="../../../src/scenario/angular-bootstrap.js"></script>
<script type="text/javascript" src="personalLogScenario.js"></script>
</head>
<body>
</body>
</html>

View file

@ -72,10 +72,10 @@ describe('example.personalLog.LogCtrl', function() {
it('should delete a message identified by index', function() {
logCtrl.rmLog(1);
logCtrl.rmLog(logCtrl.logs[1]);
expect(logCtrl.logs.length).toBe(3);
logCtrl.rmLog(2);
logCtrl.rmLog(logCtrl.logs[2]);
expect(logCtrl.logs.length).toBe(2);
expect(logCtrl.logs[0].msg).toBe('message1');
expect(logCtrl.logs[1].msg).toBe('message3');
@ -85,12 +85,12 @@ describe('example.personalLog.LogCtrl', function() {
it('should update cookies when a log is deleted', function() {
expect(logCtrl.$cookies.logs).toMatch(/\[\{.*?\}(,\{.*?\}){3}\]/);
logCtrl.rmLog(1);
logCtrl.rmLog(logCtrl.logs[1]);
expect(logCtrl.$cookies.logs).toMatch(/\[\{.*?\}(,\{.*?\}){2}\]/);
logCtrl.rmLog(0);
logCtrl.rmLog(0);
logCtrl.rmLog(0);
logCtrl.rmLog(logCtrl.logs[0]);
logCtrl.rmLog(logCtrl.logs[0]);
logCtrl.rmLog(logCtrl.logs[0]);
expect(logCtrl.$cookies.logs).toMatch(/\[\]/);
});
});