├── app
├── app.css
├── app.js
├── index.html
└── services
│ └── users
│ ├── users.js
│ └── users.spec.js
├── .gitignore
├── README.md
├── package.json
├── server.js
└── karma.conf.js
/app/app.css:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /node_modules
2 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ### Setup
2 |
3 | `nvm use 5.0`
4 |
5 | `npm install`
6 |
7 | `node server.js`
8 |
9 | ### Test
10 |
11 | `npm install -g karma-cli`
12 |
13 | `karma start`
--------------------------------------------------------------------------------
/app/app.js:
--------------------------------------------------------------------------------
1 | (function() {
2 | 'use strict';
3 |
4 | angular.module('meetIrl', [
5 | 'ui.router'
6 | ])
7 | .config(function($urlRouterProvider) {
8 | $urlRouterProvider.otherwise("/");
9 | });
10 | })();
11 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "scotch-angular-testing",
3 | "version": "1.0.0",
4 | "description": "Learn to test Angular code with Jasmine and Karma",
5 | "main": "server.js",
6 | "scripts": {
7 | "test": "karma start",
8 | "start": "node server.js"
9 | },
10 | "author": "Adam Morgan",
11 | "license": "ISC",
12 | "dependencies": {
13 | "body-parser": "^1.15.1",
14 | "express": "^4.13.4",
15 | "morgan": "^1.7.0",
16 | "path": "^0.12.7"
17 | },
18 | "devDependencies": {
19 | "angular": "^1.5.6",
20 | "angular-mocks": "^1.5.6",
21 | "angular-ui-router": "^0.3.0",
22 | "jasmine-core": "^2.4.1",
23 | "karma": "^0.13.22",
24 | "karma-chrome-launcher": "^1.0.1",
25 | "karma-jasmine": "^1.0.2",
26 | "karma-spec-reporter": "0.0.26"
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/server.js:
--------------------------------------------------------------------------------
1 | var express = require('express'),
2 | app = express(),
3 | bodyParser = require('body-parser'),
4 | morgan = require('morgan'),
5 | path = require('path');
6 |
7 | app.use(bodyParser.urlencoded({ extended: true }));
8 | app.use(bodyParser.json());
9 |
10 | app.use(function(req, res, next) {
11 | res.setHeader('Access-Control-Allow-Origin', '*');
12 | res.setHeader('Access-Control-Allow-Methods', 'GET, POST');
13 | res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With, content-type, Authorization');
14 | next();
15 | });
16 |
17 | app.use(morgan('dev'));
18 |
19 | app.use(express.static(__dirname + '/app'));
20 |
21 | app.get('*', function(req, res) {
22 | res.sendFile(path.join(__dirname + '/index.html'));
23 | });
24 |
25 | app.listen(8080);
26 | console.log('meet-irl is running on 8080');
27 |
--------------------------------------------------------------------------------
/app/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 | MeetIrl
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
30 |
31 | Hello, world!
32 |
33 |
34 |
--------------------------------------------------------------------------------
/app/services/users/users.js:
--------------------------------------------------------------------------------
1 | (function() {
2 | 'use strict';
3 |
4 | angular.module('api.users', [])
5 | .factory('Users', function() {
6 | var Users = {};
7 | var userList = [
8 | {
9 | id: '1',
10 | name: 'Jane',
11 | role: 'Designer',
12 | location: 'New York',
13 | twitter: 'gijane'
14 | },
15 | {
16 | id: '2',
17 | name: 'Bob',
18 | role: 'Developer',
19 | location: 'New York',
20 | twitter: 'billybob'
21 | },
22 | {
23 | id: '3',
24 | name: 'Jim',
25 | role: 'Developer',
26 | location: 'Chicago',
27 | twitter: 'jimbo'
28 | },
29 | {
30 | id: '4',
31 | name: 'Bill',
32 | role: 'Designer',
33 | location: 'LA',
34 | twitter: 'dabill'
35 | }
36 | ];
37 |
38 | Users.all = function() {
39 | return userList;
40 | };
41 |
42 | Users.findById = function(id) {
43 | return userList.find(function(user) {
44 | return user.id === id;
45 | });
46 | };
47 |
48 | return Users;
49 | });
50 | })();
51 |
--------------------------------------------------------------------------------
/app/services/users/users.spec.js:
--------------------------------------------------------------------------------
1 | describe('Users factory', function() {
2 | var Users;
3 | var userList = [
4 | {
5 | id: '1',
6 | name: 'Jane',
7 | role: 'Designer',
8 | location: 'New York',
9 | twitter: 'gijane'
10 | },
11 | {
12 | id: '2',
13 | name: 'Bob',
14 | role: 'Developer',
15 | location: 'New York',
16 | twitter: 'billybob'
17 | },
18 | {
19 | id: '3',
20 | name: 'Jim',
21 | role: 'Developer',
22 | location: 'Chicago',
23 | twitter: 'jimbo'
24 | },
25 | {
26 | id: '4',
27 | name: 'Bill',
28 | role: 'Designer',
29 | location: 'LA',
30 | twitter: 'dabill'
31 | }
32 | ];
33 | var singleUser = {
34 | id: '2',
35 | name: 'Bob',
36 | role: 'Developer',
37 | location: 'New York',
38 | twitter: 'billybob'
39 | };
40 |
41 | beforeEach(angular.mock.module('api.users'));
42 |
43 | beforeEach(inject(function(_Users_) {
44 | Users = _Users_;
45 | }));
46 |
47 | it('should exist', function() {
48 | expect(Users).toBeDefined();
49 | });
50 |
51 | describe('.all()', function() {
52 | it('should exist', function() {
53 | expect(Users.all).toBeDefined();
54 | });
55 |
56 | it('should return a hard-coded list of users', function() {
57 | expect(Users.all()).toEqual(userList);
58 | });
59 | });
60 |
61 | describe('.findById()', function() {
62 | it('should exist', function() {
63 | expect(Users.findById).toBeDefined();
64 | });
65 |
66 | it('should return one user object if it exists', function() {
67 | expect(Users.findById('2')).toEqual(singleUser);
68 | });
69 |
70 | it('should return undefined if the user cannot be found', function() {
71 | expect(Users.findById('ABC')).not.toBeDefined();
72 | });
73 | });
74 | });
75 |
--------------------------------------------------------------------------------
/karma.conf.js:
--------------------------------------------------------------------------------
1 | // Karma configuration
2 | // Generated on Wed Jun 01 2016 17:37:40 GMT-0400 (EDT)
3 |
4 | module.exports = function(config) {
5 | config.set({
6 |
7 | // base path that will be used to resolve all patterns (eg. files, exclude)
8 | basePath: '',
9 |
10 |
11 | // frameworks to use
12 | // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
13 | frameworks: ['jasmine'],
14 |
15 |
16 | // list of files / patterns to load in the browser
17 | files: [
18 | './node_modules/angular/angular.js',
19 | './node_modules/angular-ui-router/release/angular-ui-router.js',
20 | './node_modules/angular-mocks/angular-mocks.js',
21 | './app/services/users/users.js',
22 | './app/app.js',
23 | './app/services/users/users.spec.js'
24 | ],
25 |
26 |
27 | // list of files to exclude
28 | exclude: [
29 | ],
30 |
31 |
32 | // preprocess matching files before serving them to the browser
33 | // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
34 | preprocessors: {
35 | },
36 |
37 |
38 | // test results reporter to use
39 | // possible values: 'dots', 'progress'
40 | // available reporters: https://npmjs.org/browse/keyword/karma-reporter
41 | reporters: ['spec'],
42 |
43 |
44 | // web server port
45 | port: 9876,
46 |
47 |
48 | // enable / disable colors in the output (reporters and logs)
49 | colors: true,
50 |
51 |
52 | // level of logging
53 | // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
54 | logLevel: config.LOG_INFO,
55 |
56 |
57 | // enable / disable watching file and executing tests whenever any file changes
58 | autoWatch: true,
59 |
60 |
61 | // start these browsers
62 | // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
63 | browsers: ['Chrome'],
64 |
65 |
66 | // Continuous Integration mode
67 | // if true, Karma captures browsers, runs the tests and exits
68 | singleRun: false,
69 |
70 | // Concurrency level
71 | // how many browser should be started simultaneous
72 | concurrency: Infinity
73 | })
74 | }
75 |
--------------------------------------------------------------------------------