Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

Commit

Permalink
test(widgets): Adds simple unit and int tests.
Browse files Browse the repository at this point in the history
 - Sets up Karma to run some simple tests.
 - Sets up Protractor to run some simple tests.
 - Protractor tests use the Page Object pattern.

Closes GDC-41
  • Loading branch information
shanewilson authored and Shane Wilson committed Oct 6, 2014
1 parent 32c76c1 commit b6ad2ae
Show file tree
Hide file tree
Showing 19 changed files with 297 additions and 75 deletions.
10 changes: 7 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
dist/
.idea/

node_modules/
bower_components/
npm-debug.log
npm-debug.log*

.idea/
dist/
.tmp/
coverage/
report/

2 changes: 1 addition & 1 deletion app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta name="description" content="A front-end template that helps you build fast, modern mobile web apps.">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title ng-bind="pageTitle + ' | ngApp'"></title>
<title ng-bind="pageTitle + ' | GDC'"></title>

<!-- Add to homescreen for Chrome on Android -->
<meta name="mobile-web-app-capable" content="yes">
Expand Down
7 changes: 3 additions & 4 deletions app/scripts/core/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module ngApp.models {
page: number;
pages: number;
sort: string;
order: SortOrder;
order: string;
}

export class Pagination implements IPagination {
Expand All @@ -28,7 +28,7 @@ module ngApp.models {
page: number;
pages: number;
sort: string;
order: SortOrder;
order: string;

constructor(obj: any) {
this.count = obj.count;
Expand All @@ -38,8 +38,7 @@ module ngApp.models {
this.page = obj.page;
this.pages = obj.pages;
this.sort = obj.sort;
var o: string = obj.order;
this.order = SortOrder[o];
this.order = obj.order;
}
}

Expand Down
3 changes: 1 addition & 2 deletions app/scripts/home/templates/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<div class="container">
<h1>NG TS Boilerplate</h1>
<p>Angular + Typescript</p>
<h1>GDC Data Portal</h1>
</div>
56 changes: 56 additions & 0 deletions app/scripts/widgets/tests/widgets.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
describe('Widgets:', function () {

var WidgetsService, controller, httpBackend;

// Initialization of the AngularJS application before each test case
beforeEach(module('ngApp.widgets'));

// Injection of dependencies, $http will be mocked with $httpBackend
beforeEach(inject(function ($httpBackend) {
httpBackend = $httpBackend;
}));

describe('Controller:', function () {
it('should have widgets', inject(function ($controller) {
// Which HTTP requests do we expect to occur, and how do we response?
var widgets = [
{"name": "First User"},
{"name": "Second User"}
];

// Starting the controller
var wc = $controller('WidgetsController', {widgets: widgets});

// We expect the controller to put the right value onto the scope
expect(wc).to.have.property('widgets').with.length(2);
}));
});

describe('Service:', function () {
it('should get all widgets', inject(function (WidgetsService) {
sinon.spy(WidgetsService.ds, 'get');

var ws = {hits:[],facets:[],pagination:{}};
httpBackend.whenGET("/widgets").respond(ws);

WidgetsService.getWidgets();
httpBackend.flush();

expect(WidgetsService.ds.get).to.have.been.calledOnce;
expect(WidgetsService.ds.get).to.have.been.calledWith("");
}));

it('should get one widget by id', inject(function (WidgetsService) {
sinon.spy(WidgetsService.ds, 'get');

var w = {};
httpBackend.whenGET("/widgets/1").respond(w);

WidgetsService.getWidget(1);
httpBackend.flush();

expect(WidgetsService.ds.get).to.have.been.calledOnce;
expect(WidgetsService.ds.get).to.have.been.calledWith(1);
}));
});
});
14 changes: 2 additions & 12 deletions app/scripts/widgets/widgets.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,27 @@ module ngApp.widgets.services {
}

class WidgetsService implements IWidgetsService {
private static logWidget(id: string, params: Object) {
console.log("Received widget ", id, " request with params: ", params);
}

private static logWidgets(params: Object) {
console.log("Received widgets request with params: ", params);
}

private ds: restangular.IElement;

/* @ngInject */
constructor(Restangular: restangular.IService) {
this.ds = Restangular.all("projects");
this.ds = Restangular.all("widgets");
}

getWidget(id: string, params: Object = {}): ng.IPromise<Widget> {
WidgetsService.logWidget(id, params);
return this.ds.get(id, params).then(function (response) {
return new Widget(response);
});
}

getWidgets(params: Object = {}): ng.IPromise<Widgets> {
WidgetsService.logWidgets(params);
return this.ds.get("", params).then(function (response) {
return new Widgets(response);
});
}
}

angular
.module("widgets.services", ["widgets.models"])
.module("widgets.services", ["widgets.models", "restangular"])
.service("WidgetsService", WidgetsService);
}
31 changes: 31 additions & 0 deletions app/tests/home.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

var chai, chaiAsPromised, expect, ptor;

chai = require("chai");
chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
expect = chai.expect;

var HomePage = require('./pages/home.po.js');

describe('Home:', function () {
var page;

before(function () {
page = new HomePage();
ptor = protractor.getInstance();
});

it('should have a page title', function () {
ptor.getTitle().then(function (text) {
expect(text).to.equal("| GDC");
});
});

it('should have a header', function () {
page.header.getText().then(function(text){
expect(text).to.equal('GDC Data Portal');
});
});
});
15 changes: 0 additions & 15 deletions app/tests/integration/home/basic.tests.js

This file was deleted.

14 changes: 14 additions & 0 deletions app/tests/pages/home.po.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

var HomePage = function () {
browser.get('');
};

HomePage.prototype = Object.create({}, {
header: {
get: function () {
return element(by.css('h1'));
}}
});

module.exports = HomePage;
14 changes: 14 additions & 0 deletions app/tests/pages/widget.po.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

var WidgetPage = function () {
browser.get('widgets/PR1');
};

WidgetPage.prototype = Object.create({}, {
header: {
get: function () {
return element(by.css('h1'));
}}
});

module.exports = WidgetPage;
24 changes: 24 additions & 0 deletions app/tests/pages/widgets.po.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

var widgetsRepeater = by.repeater('w in wsc.widgets.hits');

var WidgetsPage = function () {
browser.get('widgets');
};

WidgetsPage.prototype = Object.create({}, {
header: {
get: function () {
return element(by.css('h1'));
}},
widgets: {
get: function () {
return element.all(widgetsRepeater);
}},
widget: {
value: function (id, field) {
return element(widgetsRepeater.row(id).column('w.' + field));
}}
});

module.exports = WidgetsPage;
9 changes: 0 additions & 9 deletions app/tests/unit/home/basic.tests.js

This file was deleted.

31 changes: 31 additions & 0 deletions app/tests/widget.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

var chai, chaiAsPromised, expect, ptor;

chai = require("chai");
chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
expect = chai.expect;

var WidgetPage = require('./pages/widget.po.js');

describe('Widget:', function () {
var page;

before(function () {
page = new WidgetPage();
ptor = protractor.getInstance();
});

it('should have a page title', function () {
ptor.getTitle().then(function (text) {
expect(text).to.equal("| GDC");
});
});

it('should have a header', function () {
page.header.getText().then(function(text){
expect(text).to.equal('PR1');
});
});
});
43 changes: 43 additions & 0 deletions app/tests/widgets.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

var chai, chaiAsPromised, expect, ptor;

chai = require("chai");
chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
expect = chai.expect;

var WidgetsPage = require('./pages/widgets.po.js');

describe('Widgets:', function () {
var page;

before(function () {
page = new WidgetsPage();
ptor = protractor.getInstance();
});

it('should have a page title', function () {
ptor.getTitle().then(function (text) {
expect(text).to.equal("| GDC");
});
});

it('should have a header', function () {
page.header.getText().then(function (text) {
expect(text).to.equal('Widgets');
});
});

describe('List:', function () {
it('should show a list widgets', function () {
page.widgets.then(function (rows) {
expect(rows.length).to.equal(5);
});

page.widget(0, 'id').getText().then(function (row) {
expect(row).to.equal('PR1');
});
});
});
});
Loading

0 comments on commit b6ad2ae

Please sign in to comment.