Skip to content

Commit

Permalink
100% coverage (fixes #144)
Browse files Browse the repository at this point in the history
  • Loading branch information
mstriemer committed Mar 24, 2016
1 parent fec597f commit 9065b8e
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 72 deletions.
133 changes: 69 additions & 64 deletions src/config/index.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,73 @@
import path from 'path';
const config = new Map();
const NODE_ENV = process.env.NODE_ENV;

// Default to production unless overridden.
config.set('env', NODE_ENV || 'production');

config.set('basePath', path.resolve(__dirname, '../'));

// This is the host / port for running as production.
config.set('serverHost', process.env.SERVER_HOST || '127.0.0.1');
config.set('serverPort', process.env.SERVER_PORT || 4000);

// This is the host / port for the development instance.
config.set('devServerHost', process.env.SERVER_HOST || '127.0.0.1');
config.set('devServerPort', process.env.SERVER_PORT || 3000);

// This is the host / port for the webpack dev server.
config.set('webpackServerHost', process.env.WEBPACK_SERVER_HOST || '127.0.0.1');
config.set('webpackServerPort', process.env.WEBPACK_SERVER_PORT || 3001);

config.set('apiHost',
process.env.API_HOST ||
NODE_ENV === 'development' ?
'https://addons-dev.allizom.org' : 'https://addons.mozilla.org');

config.set('apiPath', process.env.API_PATH || '/api/v3');
config.set('apiBase', config.get('apiHost') + config.get('apiPath'));

const CSP = {
directives: {
connectSrc: ["'self'", config.get('apiHost')],
defaultSrc: ["'self'"],
scriptSrc: ["'self'"],
styleSrc: ["'self'"],
reportUri: '/__cspreport__',
},

// Set to true if you only want browsers to report errors, not block them
reportOnly: false,

// Set to true if you want to blindly set all headers: Content-Security-Policy,
// X-WebKit-CSP, and X-Content-Security-Policy.
setAllHeaders: false,

// Set to true if you want to disable CSP on Android where it can be buggy.
disableAndroid: false,
};
const WEBPACK_HOST =
`http://${config.get('webpackServerHost')}:${config.get('webpackServerPort')}`;

if (config.get('env') === 'development') {
CSP.directives.scriptSrc.push(WEBPACK_HOST);
CSP.directives.styleSrc.push('blob:');
CSP.directives.connectSrc.push(WEBPACK_HOST);
CSP.reportOnly = true;
}

config.set('CSP', CSP);

const APP_NAMES = ['search', 'disco'];
if (APP_NAMES.indexOf(process.env.APP_NAME) < 0) {
config.set('currentApp', 'core');
} else {
config.set('currentApp', process.env.APP_NAME);
}

/* istanbul ignore next */
// Looks like you can't ignore a file but you can ignore a function, we don't want coverage here.
(function defineConfig() {
const NODE_ENV = process.env.NODE_ENV;

// Default to production unless overridden.
config.set('env', NODE_ENV || 'production');

config.set('basePath', path.resolve(__dirname, '../'));

// This is the host / port for running as production.
config.set('serverHost', process.env.SERVER_HOST || '127.0.0.1');
config.set('serverPort', process.env.SERVER_PORT || 4000);

// This is the host / port for the development instance.
config.set('devServerHost', process.env.SERVER_HOST || '127.0.0.1');
config.set('devServerPort', process.env.SERVER_PORT || 3000);

// This is the host / port for the webpack dev server.
config.set('webpackServerHost', process.env.WEBPACK_SERVER_HOST || '127.0.0.1');
config.set('webpackServerPort', process.env.WEBPACK_SERVER_PORT || 3001);

config.set('apiHost',
process.env.API_HOST ||
NODE_ENV === 'development' ?
'https://addons-dev.allizom.org' : 'https://addons.mozilla.org');

config.set('apiPath', process.env.API_PATH || '/api/v3');
config.set('apiBase', config.get('apiHost') + config.get('apiPath'));

const CSP = {
directives: {
connectSrc: ["'self'", config.get('apiHost')],
defaultSrc: ["'self'"],
scriptSrc: ["'self'"],
styleSrc: ["'self'"],
reportUri: '/__cspreport__',
},

// Set to true if you only want browsers to report errors, not block them
reportOnly: false,

// Set to true if you want to blindly set all headers: Content-Security-Policy,
// X-WebKit-CSP, and X-Content-Security-Policy.
setAllHeaders: false,

// Set to true if you want to disable CSP on Android where it can be buggy.
disableAndroid: false,
};
const WEBPACK_HOST =
`http://${config.get('webpackServerHost')}:${config.get('webpackServerPort')}`;

if (config.get('env') === 'development') {
CSP.directives.scriptSrc.push(WEBPACK_HOST);
CSP.directives.styleSrc.push('blob:');
CSP.directives.connectSrc.push(WEBPACK_HOST);
CSP.reportOnly = true;
}

config.set('CSP', CSP);

const APP_NAMES = ['search', 'disco'];
if (APP_NAMES.indexOf(process.env.APP_NAME) < 0) {
config.set('currentApp', 'core');
} else {
config.set('currentApp', process.env.APP_NAME);
}
}());

export default config;
8 changes: 2 additions & 6 deletions src/core/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,9 @@ function makeQueryString(opts) {
return Object.keys(opts).map((k) => `${k}=${opts[k]}`).join('&');
}

function callApi(endpoint, schema, params = {}) {
function callApi(endpoint, schema, params) {
const queryString = makeQueryString(params);
let fullUrl = `${API_BASE}/${endpoint}/`;
if (queryString) {
fullUrl += `?${queryString}`;
}
return fetch(fullUrl)
return fetch(`${API_BASE}/${endpoint}/?${queryString}`)
.then((response) => response.json())
.then((response) => normalize(response, schema));
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/containers/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Link } from 'react-router';
export default class App extends React.Component {
render() {
return (
<ul>
<ul ref="appList">
<li><Link to="/search">Search</Link></li>
<li><Link to="/disco">Discovery Pane</Link></li>
</ul>
Expand Down
2 changes: 1 addition & 1 deletion src/disco/components/hello-world.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { gettext as _ } from 'core/utils';
export default class AppView extends React.Component {
render() {
return (
<div id="app-view">
<div id="app-view" ref="container">
<h1>{_('HELLO DISCO WORLD')}</h1>
</div>
);
Expand Down
14 changes: 14 additions & 0 deletions tests/core/containers/TestApp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react';
import { renderIntoDocument } from 'react-addons-test-utils';
import App from 'core/containers/App';

describe('core <App />', () => {
it('has a list of apps', () => {
const root = renderIntoDocument(<App />);
const list = root.refs.appList;
assert.equal(list.tagName, 'UL');
assert.equal(list.childNodes.length, 2);
assert.equal(list.childNodes[0].textContent, 'Search');
assert.equal(list.childNodes[1].textContent, 'Discovery Pane');
});
});
10 changes: 10 additions & 0 deletions tests/disco/components/test-hello-world.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react';
import { renderIntoDocument } from 'react-addons-test-utils';
import AppView from 'disco/components/hello-world';

describe('disco <AppView />', () => {
it('is just a hello world', () => {
const root = renderIntoDocument(<AppView />);
assert.equal(root.refs.container.textContent, 'HELLO DISCO WORLD');
});
});
9 changes: 9 additions & 0 deletions tests/search/components/TestSearchResults.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ describe('<SearchResults />', () => {
assert.include(searchResultsMsg.textContent, 'No results were found');
});

it('renders a loading message when loading', () => {
const root = renderResults({
query: 'test',
loading: true,
});
const searchResultsMsg = root.refs.message;
assert.equal(searchResultsMsg.textContent, 'Searching...');
});

it('renders search results when supplied', () => {
const root = renderResults({
query: 'test',
Expand Down
20 changes: 20 additions & 0 deletions tests/search/test_store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import createStore from 'search/store';

describe('search createStore', () => {
it('sets the reducers', () => {
const store = createStore();
assert.deepEqual(
Object.keys(store.getState()).sort(),
['addons', 'reduxAsyncConnect', 'search']);
});

it('creates an empty store', () => {
const store = createStore();
assert.deepEqual(store.getState().addons, {});
});

it('creates a store with an initial state', () => {
const store = createStore({addons: {foo: {slug: 'foo'}}});
assert.deepEqual(store.getState().addons, {foo: {slug: 'foo'}});
});
});

0 comments on commit 9065b8e

Please sign in to comment.