Skip to content

Commit

Permalink
chore(tests): Add ngrx test infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeRyanDev committed Jun 8, 2016
1 parent f067cdb commit a583221
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 10 deletions.
46 changes: 39 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Users Environment Variables
.lock-wscript

# OS generated files #
.DS_Store
ehthumbs.db
Icon?
Expand All @@ -6,9 +31,8 @@ Thumbs.db
# Node Files #
/node_modules
/bower_components
npm-debug.log

# Typing #
# Typing TSD #
/src/typings/tsd/
/typings/
/tsd_typings/
Expand All @@ -17,8 +41,16 @@ npm-debug.log
/dist
/public/__build__/
/src/*/__build__/
/__build__/**
/public/dist/
/src/*/dist/
/dist/**
.webpack.json
__build__/**
.webpack.json

#doc
/doc

# IDE #
.idea/
*.swp
!/typings/custom.d.ts

# Build Artifacts #
release
76 changes: 76 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
var path = require('path');

module.exports = function(karma) {
'use strict';

karma.set({
basePath: __dirname,

frameworks: ['jasmine'],

files: [
{ pattern: 'tests.js', watched: false }
],

exclude: [],

preprocessors: {
'tests.js': ['coverage', 'webpack', 'sourcemap']
},

reporters: ['mocha', 'coverage'],

coverageReporter: {
dir: 'coverage/',
subdir: '.',
reporters: [
{ type: 'text-summary' },
{ type: 'json' },
{ type: 'html' }
]
},

browsers: ['Chrome'],

port: 9018,
runnerPort: 9101,
colors: true,
logLevel: karma.LOG_INFO,
autoWatch: true,
singleRun: false,

webpack: {
devtool: 'inline-source-map',
resolve: {
root: __dirname,
extensions: ['', '.ts', '.js']
},
module: {
loaders: [
{
test: /\.ts?$/,
exclude: /(node_modules)/,
loader: 'awesome-typescript-loader'
}
],
postLoaders: [
{
test: /\.(js|ts)$/, loader: 'istanbul-instrumenter-loader',
include: path.resolve(__dirname, 'lib'),
exclude: [
/\.(e2e|spec)\.ts$/,
/node_modules/
]
}
]
},
ts: {
configFileName: './spec/tsconfig.json'
}
},

webpackServer: {
noInfo: true
}
});
};
14 changes: 13 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"postinstall": "npm run typings -- install",
"build": "webpack --progress --profile --colors --display-error-details --display-cached",
"server:dev": "webpack-dev-server --inline --progress --profile --colors --watch --display-error-details --display-cached --content-base src/",
"start": "npm run server:dev"
"start": "npm run server:dev",
"test": "karma start --single-run",
"test:watch": "karma start"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -68,7 +70,17 @@
"html-webpack-plugin": "^2.15.0",
"http-server": "^0.9.0",
"imports-loader": "^0.6.5",
"istanbul-instrumenter-loader": "^0.2.0",
"jasmine-core": "^2.4.1",
"json-loader": "^0.5.4",
"karma": "^0.13.22",
"karma-chrome-launcher": "^0.2.3",
"karma-coverage": "^0.5.5",
"karma-jasmine": "^0.3.8",
"karma-mocha-reporter": "^2.0.0",
"karma-sourcemap-loader": "^0.3.7",
"karma-typescript-preprocessor": "0.0.21",
"karma-webpack": "^1.7.0",
"promise-loader": "^1.0.0",
"raw-loader": "0.5.1",
"source-map-loader": "^0.1.5",
Expand Down
20 changes: 20 additions & 0 deletions spec/fixtures/books.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Book } from '../../src/models/book';


export const TestBook: Book = {
id: '123',
volumeInfo: {
title: 'This is a test book',
subtitle: 'Not a real book',
authors: [ 'Mike Ryan' ],
publisher: 'ngrx',
publishDate: '06082016',
description: 'This book is used for mocks in tests',
averageRating: 3,
ratingsCount: 10,
imageLinks: {
thumbnail: '/path/to/an/image.jpg',
smallThumbnail: '/path/to/a/smaller/image.png'
}
}
};
68 changes: 68 additions & 0 deletions spec/reducers/book.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import 'rxjs/add/operator/let';
import { of } from 'rxjs/observable/of';
import { BookActions } from '../../src/actions/book';
import booksReducer, * as fromBooks from '../../src/reducers/books';
import { TestBook } from '../fixtures/books';

describe('Books', function() {
const bookActions = new BookActions();

describe('Reducer', function() {
it('should have an empty initial state', function() {
const initialState = booksReducer(undefined, { type: 'test-action' });

expect(initialState.ids).toEqual([]);
expect(initialState.entities).toEqual({});
});

it('should add a book to the entities table and its ID to the IDs list when loaded', function() {
const action = bookActions.loadBook(TestBook);
const state = booksReducer(undefined, action);

expect(state.ids).toEqual([ TestBook.id ]);
expect(state.entities[TestBook.id]).toBe(TestBook);
});
});

describe('Selectors', function() {
describe('getBookEntities', function() {
it('should get the entities table out of the books state', function() {
const state = booksReducer(undefined, { type: 'test-action' });

of(state).let(fromBooks.getBookEntities()).subscribe(entities => {
expect(entities).toBe(state.entities);
});
});
});

describe('getBook', function() {
it('should get a selected book out of the books state', function() {
const state: fromBooks.BooksState = {
entities: {
[TestBook.id]: TestBook
},
ids: [ TestBook.id ]
};

of(state).let(fromBooks.getBook(TestBook.id)).subscribe(book => {
expect(book).toBe(TestBook);
});
});
});

describe('getBooks', function() {
it('should return all of the books in an array for a given list of ids', function() {
const state: fromBooks.BooksState = {
entities: {
[TestBook.id]: TestBook
},
ids: [ TestBook.id ]
};

of(state).let(fromBooks.getBooks([ TestBook.id ])).subscribe(books => {
expect(books).toEqual([ TestBook ]);
});
});
});
});
});
20 changes: 20 additions & 0 deletions tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require('core-js');
require('ts-helpers');
require('zone.js/dist/zone.js');
require('zone.js/dist/long-stack-trace-zone.js');
require('zone.js/dist/jasmine-patch.js');
require('zone.js/dist/async-test.js');
require('zone.js/dist/fake-async-test.js');

Error.stackTraceLimit = Infinity;

require('reflect-metadata');

const testContext = require.context('./spec', true, /\.spec\.ts/);
testContext.keys().forEach(testContext);

const testing = require('@angular/core/testing');
const browser = require('@angular/platform-browser-dynamic/testing');
testing.setBaseTestProviders(
browser.TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
browser.TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
],
"filesGlob": [
"./src/**/*.ts",
"./test/**/*.ts",
"./spec/**/*.ts",
"!./node_modules/**/*.ts",
"src/custom-typings.d.ts",
"typings/index.d.ts"
Expand Down
3 changes: 2 additions & 1 deletion typings.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"dependencies": { },
"dependencies": {},
"devDependencies": {},
"globalDependencies": {
"core-js": "registry:dt/core-js#0.0.0+20160317120654",
"hammerjs": "github:DefinitelyTyped/DefinitelyTyped/hammerjs/hammerjs.d.ts#74a4dfc1bc2dfadec47b8aae953b28546cb9c6b7",
"jasmine": "registry:dt/jasmine#2.2.0+20160505161446",
"node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#8cf8164641be73e8f1e652c2a5b967c7210b6729",
"webpack": "github:DefinitelyTyped/DefinitelyTyped/webpack/webpack.d.ts#95c02169ba8fa58ac1092422efbd2e3174a206f4"
}
Expand Down

0 comments on commit a583221

Please sign in to comment.