Skip to content

Commit

Permalink
feat(midway): rxPaginate page objects, with tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Droogans committed Feb 17, 2014
1 parent 30be1d4 commit 88c83ab
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 52 deletions.
3 changes: 1 addition & 2 deletions .jscs.json
Expand Up @@ -29,7 +29,6 @@
"disallowEmptyBlocks": true,
"disallowSpacesInsideParentheses": true,
"requireSpacesInsideObjectBrackets": "allButNested",
"disallowDanglingUnderscores": true,
"disallowSpaceAfterObjectKeys": true,
"requireCommaBeforeLineBreak": true,
"disallowLeftStickedOperators": [
Expand Down Expand Up @@ -99,4 +98,4 @@
"requireParamTypes": true
},
"excludeFiles": ["app/scripts/lib/**"]
}
}
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -62,6 +62,7 @@
"chai-as-promised": "~4.1.0",
"grunt-contrib-compress": "~0.6.1",
"grunt-contrib-cssmin": "~0.7.0",
"grunt-contrib-uglify": "~0.3.2"
"grunt-contrib-uglify": "~0.3.2",
"exceptions": "~0.1.1"
}
}
2 changes: 1 addition & 1 deletion src/rxPaginate/docs/rxPaginate.html
Expand Up @@ -23,4 +23,4 @@
</tr>
</tfoot>
</table>
</div>
</div>
48 changes: 14 additions & 34 deletions src/rxPaginate/docs/rxPaginate.js
Expand Up @@ -9,38 +9,18 @@ function rxPaginateCtrl ($scope, PageTracking) {
$scope.pager = PageTracking.createInstance();
$scope.pager.itemsPerPage = 3;

$scope.servers = [
{
'name': 'Server 1',
'os': 'Ubuntu 12.04'
},
{
'name': 'Server 2',
'os': 'Red Hat Enterprise Linux 6.4 '
},
{
'name': 'Server 3',
'os': 'Ubuntu 12.04'
},
{
'name': 'Server 4',
'os': 'CentOS 6.4'
},
{
'name': 'Server 5',
'os': 'Ubuntu 12.10'
},
{
'name': 'Server 6',
'os': 'Ubuntu 13.04'
},
{
'name': 'Server 7',
'os': 'Red Hat Enterprise Linux 6.4 '
},
{
'name': 'Server 8',
'os': 'Ubuntu 12.10'
var makeServers = function (serverCount) {
var servers = [];
var os = ['Ubuntu 12.04', 'Red Hat Enterprise Linux 6.4', 'CentOS 6.4', 'Ubuntu 13.04'];
for (var i = 1; i < serverCount + 1; i++) {
var server = {
id: i,
name: 'Server ' + i,
os: os[i % os.length]
};
servers.push(server);
}
];
}
return servers;
};
$scope.servers = makeServers(21);
}
52 changes: 44 additions & 8 deletions src/rxPaginate/docs/rxPaginate.midway.js
@@ -1,16 +1,52 @@
var demoPage = require('../../../utils/demo.page.js');
var rxPaginatePage = require('../rxPaginate.page.js').rxPaginate;
var pagination = require('../rxPaginate.page.js').rxPaginate;
var expect = require('chai').use(require('chai-as-promised')).expect;

// Add midway tests to run
describe('rxPaginate', function () {
var ptor = rxPaginatePage.driver;

it('beforeAll', function () {
before(function () {
demoPage.go();
var ptor = protractor.getInstance();
pagination = pagination.initialize(ptor.findElement(protractor.By.css('div.rx-paginate')));
});

// it.skip('should show element', function () {
// // will fail b/c there is no element being added in component.html
// expect(rxPaginatePage.rxPaginateElement.isDisplayed()).toEqual(true);
// });
});
it('should jump forward to page 7 using pagination', function () {
pagination.jumpToPage(7);
expect(pagination.getCurrentPageNumber()).to.eventually.equal(7);
});

it('should jump backward to page 2 using pagination', function () {
pagination.jumpToPage(2);
expect(pagination.getCurrentPageNumber()).to.eventually.equal(2);
});

it('should navigate forward one page at a time', function () {
pagination.nextPage();
expect(pagination.getCurrentPageNumber()).to.eventually.equal(3);
});

it('should navigate backwards one page at a time', function () {
pagination.previousPage();
pagination.previousPage();
expect(pagination.getCurrentPageNumber()).to.eventually.equal(1);
});

it('should navigate to the last page', function () {
var firstPage;
pagination.getCurrentPageNumber().then(function (page) {
firstPage = page;
});

pagination.lastPage();
pagination.getCurrentPageNumber().then(function (page) {
expect(page).to.be.above(firstPage);
});
});

it('should navigate to the first page', function () {
pagination.firstPage();
expect(pagination.getCurrentPageNumber()).to.eventually.equal(1);
});

});
161 changes: 155 additions & 6 deletions src/rxPaginate/rxPaginate.page.js
@@ -1,11 +1,160 @@
/*jshint node:true*/
var Page = require('astrolabe').Page;
var exceptions = require('exceptions');
var _ = require('lodash');

exports.rxPaginate = Page.create({
// Elements
rxPaginateElement: {
get: function () {
return this.findElement(this.by.id('rxPaginateElement'));
var page = {

lnkCurrentPage: {
get: function () { return this.rxPaginate.findElement(this.by.css('.pagination .active a')); }
},

tblPagination: {
get: function () { return this.rxPaginate.findElements(this.by.css('.pagination li a')); }
},

jumpToPage: {
value: function (pageNumber) {
var page = this;
if (pageNumber < 1) {
page.NoSuchPageException.thro('Page number must be >= 1');
}

return this.getPageNumbers().then(function (pageNumbers) {
var pageIndex = _.indexOf(pageNumbers, pageNumber);
if (pageIndex === -1) {
// The page is not on the current page numbers list.
// Let's navigate around and try again.
if (_.min(pageNumbers) > pageNumber) {
// The lowest available page is still too big.
page.jumpToLowestAvailablePage();
// Try again.
page.jumpToPage(pageNumber);
} else {
return page.getCurrentPageNumber().then(function (currentPage) {
if (_.last(pageNumbers) == currentPage) {
// We are at the last page, and we still need to go higher.
var message = pageNumber + ' exceeds max page of ' + _.last(pageNumbers);
page.NoSuchPageException.thro(message);
} else {
page.jumpToHighestAvailablePage();
// Try again.
page.jumpToPage(pageNumber);
}
});
}
} else {
// Our target page is somewhere in the available pages list.
return page.tblPagination.then(function (pagination) {
// pageIndex + 2 will offset the ["First", "Prev"] links.
return pagination[pageIndex + 2].then(function (page) {
page.click();
});
});
}
});
}
},

firstPage: {
value: function () {
return this.tblPagination.then(function (pages) {
return pages[0].then(function (firstPage) {
firstPage.click();
});
});
}
},

previousPage: {
value: function () {
return this.tblPagination.then(function (pages) {
return pages[1].then(function (previousPage) {
previousPage.click();
});
});
}
},

nextPage: {
value: function () {
return this.tblPagination.then(function (pages) {
return pages[pages.length - 2].then(function (nextPage) {
nextPage.click();
});
});
}
},

lastPage: {
value: function () {
return this.tblPagination.then(function (pages) {
return _.last(pages).then(function (lastPage) {
lastPage.click();
});
});
}
},

getCurrentPageNumber: {
value: function () {
return this.lnkCurrentPage.then(function (currentPage) {
return currentPage.getText().then(function (text) {
return parseInt(text, 10);
});
});
}
},

getPageNumbers: {
value: function () {
// Return a list of page numbers available to paginate to.
var pages = [];
var css = 'a[ng-click$="pageNumber = n"]';
return this.driver.findElements(this.by.css(css)).then(function (pageNumbers) {
_.forEach(pageNumbers, function (pageNumber) {
return pageNumber.then(function (number) {
return number.getText().then(function (n) {
pages.push(parseInt(n, 10));
});
});
});
return pages;
});
}
},

jumpToLowestAvailablePage: {
value: function () {
return this.tblPagination.then(function (pagination) {
return pagination[2].then(function (lowestLink) {
lowestLink.click();
});
});
}
},

jumpToHighestAvailablePage: {
value: function () {
return this.tblPagination.then(function (pagination) {
return pagination[pagination.length - 3].then(function (highestLink) {
highestLink.click();
});
});
}
},

NoSuchPageException: {
get: function () { return new exceptions.Exception('No such page'); }
}

};

exports.rxPaginate = {
initialize: function (rxPaginationElement) {
page.rxPaginate = {
get: function () { return rxPaginationElement; }
};
return Page.create(page);
}
});
};

0 comments on commit 88c83ab

Please sign in to comment.