Permalink
Browse files

feat(test): Added tests for plugin

  • Loading branch information...
RWOverdijk committed Jan 15, 2016
1 parent 421e14e commit 08beea51a8ea6ddbcfb496a0a75693928b1c8f07
Showing with 315 additions and 0 deletions.
  1. +25 −0 build/tasks/server.js
  2. +72 −0 karma.conf.js
  3. +99 −0 test/config.spec.js
  4. +34 −0 test/endpoint.spec.js
  5. +33 −0 test/index.spec.js
  6. +10 −0 test/resource/inject-test.js
  7. +38 −0 test/rest.spec.js
  8. +4 −0 test/setup.js
@@ -0,0 +1,25 @@
var app = require('express')();
var bodyParser = require('body-parser');
var cors = require('cors');
var server;

app.use(bodyParser.json());
app.use(cors());

app.all('*', function(req, res) {
res.send({
path: req.path,
query: req.query,
body: req.body,
method: req.method
});
});

module.exports = {
start: function(done) {
server = app.listen(1927, done);
},
stop: function(cb) {
server.close(cb);
}
};
@@ -0,0 +1,72 @@
module.exports = function (config) {
var configuration = {

// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',

// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks : ['jspm', 'jasmine'],
jspm : {
// Edit this to your needs
loadFiles: ['src/**/*.js', 'test/setup.js', 'test/**/*.js'],
paths : {
'*': '*.js'
}

},
'babelPreprocessor': {
options: {
sourceMap: 'inline',
modules : 'system',
moduleIds: false,
optional : [
"es7.decorators",
"es7.classProperties"
]
}
},

// list of files / patterns to load in the browser.
files: [],

// list of files to exclude
exclude: [],

// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {},

// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],

// web server port
port: 9876,

// enable / disable colors in the output (reporters and logs)
colors: true,

// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,

// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,

// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome', 'Firefox', 'ChromeCanary'],

// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false
};

if (process.env.TRAVIS) {
configuration.browsers = ['Firefox'];
}

config.set(configuration);
};
@@ -0,0 +1,99 @@
import {HttpClient} from 'aurelia-fetch-client';
import {Config, Rest} from '../src/index';
import {Container} from 'aurelia-dependency-injection';

describe('Config', function() {
describe('.registerEndpoint()', function() {
it('Should properly register an endpoint when providing a config callback.', function() {
var config = new Config;
var returned = config.registerEndpoint('github', function(configure) {
configure.withBaseUrl(baseUrls.github);
});

expect(config.endpoints.github.client.defaults).toEqual({});
expect(config.endpoints.github.client.baseUrl).toEqual(baseUrls.github);
expect(returned).toBe(config);
});

it('Should properly register an endpoint when providing an endpoint string.', function() {
var config = new Config;
var returned = config.registerEndpoint('api', baseUrls.api);

expect(config.endpoints.api.client.defaults).toEqual({});
expect(config.endpoints.api.client.baseUrl).toEqual(baseUrls.api);
expect(returned).toBe(config);
});

it('Should properly register an endpoint with no arguments.', function() {
var config = new Config;
var returned = config.registerEndpoint('boring');

expect(config.endpoints.boring.client.defaults).toEqual(null);
expect(config.endpoints.boring.client.baseUrl).toEqual('');
expect(returned).toBe(config);
});

it('Should properly register an endpoint when providing an endpoint string and defaults.', function() {
var config = new Config;
var returned = config.registerEndpoint('api', baseUrls.api, {headers: {'x-scope': 'Tests'}});

expect(config.endpoints.api.client.baseUrl).toEqual(baseUrls.api);
expect(config.endpoints.api.client.defaults).toEqual({headers: {'x-scope': 'Tests'}});
expect(returned).toBe(config);
});
});

describe('.getEndpoint()', function() {
it('Should return the registered endpoint, or null.', function() {
var config = new Config;

config.registerEndpoint('api', baseUrls.api);

var endpoint = config.getEndpoint('api');
var nullEndpoint = config.getEndpoint('no');
var defaultNullEndpoint = config.getEndpoint();

expect(endpoint instanceof Rest).toBe(true);
expect(endpoint.client instanceof HttpClient).toBe(true);
expect(nullEndpoint instanceof Rest).toBe(false);
expect(nullEndpoint).toBe(null);
expect(defaultNullEndpoint instanceof Rest).toBe(false);
expect(defaultNullEndpoint).toBe(null);

config.setDefaultEndpoint('api');

var defaultEndpoint = config.getEndpoint();

expect(defaultEndpoint instanceof Rest).toBe(true);
expect(defaultEndpoint.client instanceof HttpClient).toBe(true);
});
});

describe('.endpointExists()', function() {
it('Should return if given name is a registered endpoint.', function() {
var config = new Config;

config.registerEndpoint('api', baseUrls.api);

expect(config.endpointExists('api')).toBe(true);
expect(config.endpointExists('cake')).toBe(false);
expect(config.endpointExists()).toBe(false);
});
});

describe('.setDefaultEndpoint()', function() {
it('Should set the default endpoint.', function() {
var config = new Config;

config.registerEndpoint('api', baseUrls.api);
expect(config.getEndpoint()).toBe(null);
config.setDefaultEndpoint('api');
expect(config.getEndpoint() instanceof Rest).toBe(true);
});
});
});

var baseUrls = {
github: 'https://api.github.com',
api : 'http://jsonplaceholder.typicode.com'
};
@@ -0,0 +1,34 @@
import {HttpClient} from 'aurelia-fetch-client';
import {Config, Rest, Endpoint} from '../src/index';
import {Container} from 'aurelia-dependency-injection';
import {InjectTest} from './resource/inject-test';

let container = new Container();
let config = container.get(Config);
let baseUrls = {
github: 'https://api.github.com',
api : 'http://jsonplaceholder.typicode.com'
};

config.registerEndpoint('api', baseUrls.api);
config.registerEndpoint('github', baseUrls.github);

describe('Endpoint', function() {
describe('static .of()', function() {
it('Should return a new instance of self.', function() {
let endpoint = Endpoint.of('foo');

expect(endpoint instanceof Endpoint).toBe(true);
expect(endpoint._key).toBe('foo');
});

it('Should return a new instance of Rest.', function() {
let injectTest = container.get(InjectTest);

expect(injectTest.apiEndpoint instanceof Rest).toBe(true);
expect(injectTest.githubEndpoint instanceof Rest).toBe(true);
});
});
});


@@ -0,0 +1,33 @@
import {configure} from '../src/index';
import {Config} from '../src/index';
import {Container} from 'aurelia-dependency-injection';

describe('index', function() {
describe('configure()', function() {
it('Should call callback with a config function', function(done) {
configure(aurelia(), function(config) {
expect(config).toBe(Config);

done();
});
});

it('Should call callback with a config instance', function(done) {
configure(aurelia(new Container()), function(config) {
expect(config instanceof Config).toBe(true);

done();
});
});
});
});

function aurelia(container) {
return {
container: container || {
get: function returnVal(val) {
return val;
}
}
}
}
@@ -0,0 +1,10 @@
import {inject} from 'aurelia-dependency-injection';
import {Endpoint} from '../../src/index';

@inject(Endpoint.of('api'), Endpoint.of('github'))
export class InjectTest {
constructor(apiEndpoint, githubEndpoint) {
this.apiEndpoint = apiEndpoint;
this.githubEndpoint = githubEndpoint;
}
}
@@ -0,0 +1,38 @@
import {HttpClient} from 'aurelia-fetch-client';
import {Config, Rest, Endpoint} from '../src/index';
import {Container} from 'aurelia-dependency-injection';
import {InjectTest} from './resource/inject-test';

let container = new Container();
let config = container.get(Config);
let baseUrls = {
github: 'https://api.github.com/',
api : 'http://127.0.0.1:1927/'
};

config.registerEndpoint('api', baseUrls.api);
config.registerEndpoint('github', baseUrls.github);

describe('Rest', function() {
describe('.find()', function() {
it('Should find results for multiple endpoints.', function(done) {
let injectTest = container.get(InjectTest);

expect(injectTest.apiEndpoint instanceof Rest).toBe(true);
expect(injectTest.githubEndpoint instanceof Rest).toBe(true);

Promise.all([
injectTest.githubEndpoint.find('repos/spoonx/aurelia-orm/contributors')
.then(x => {
expect(x[0].login).toBe('RWOverdijk');
}),
injectTest.apiEndpoint.find('posts')
.then(y => {
expect(y.path).toBe('/posts');
})
]).then(x => {
done();
});
});
});
});
@@ -0,0 +1,4 @@
import {initialize} from 'aurelia-pal-browser';
import 'fetch';

initialize();

0 comments on commit 08beea5

Please sign in to comment.