Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
"bunyan": "1.8.1",
"camelcase": "3.0.0",
"classnames": "2.2.5",
"common-tags": "0.0.3",
"config": "1.20.1",
"express": "4.13.4",
"extract-text-webpack-plugin": "1.0.1",
Expand Down
22 changes: 17 additions & 5 deletions src/client-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@
* When webpack builds the client-side code it exposes
* the clientConfig config via the definePlugin as CLIENT_CONFIG.
*/
import { oneLine } from 'common-tags';

const clientConfig = new Map();
export class ClientConfig {
constructor(objData) {
// This Object.assign keeps the config data private.
Object.assign(this, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment that this is this way to keep objData private.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a test which should make it obvious if this gets changed. Although I guess someone might rename objData while they're at it and the test would still pass. Maybe the test should check Object.keys(new ClientConfig({})) === ['get', 'has']?

has: (key) => objData.hasOwnProperty(key),

Object.keys(CLIENT_CONFIG).forEach((key) => {
clientConfig.set(key, CLIENT_CONFIG[key]);
});
get: (key) => {
if (this.has(key)) {
return objData[key];
}
throw new Error(oneLine`Key was not found in clientConfig. Check the
key has been added to clientConfigKeys`);
},
});
}
}

module.exports = clientConfig;
export default new ClientConfig(CLIENT_CONFIG);
42 changes: 42 additions & 0 deletions tests/client/test_client_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { ClientConfig } from 'client-config';

describe('client-config module', () => {
let config;

beforeEach(() => {
config = new ClientConfig({
foo: 'test-value-1',
bar: 'test-value-2',
});
});

it('provides no access to the underlying data from outside', () => {
assert.equal(config.objData, undefined);
});

it('has the right methods', () => {
assert.sameMembers(Object.keys(new ClientConfig({})), ['get', 'has']);
});

describe('ClientConfig.get()', () => {
it('returns a key when present', () => {
assert.equal(config.get('foo'), 'test-value-1');
});

it('throws if key is missing', () => {
assert.throws(() => {
config.get('missing-key');
}, Error, /Key was not found in clientConfig/);
});
});

describe('ClientConfig.has()', () => {
it('returns true if key is present', () => {
assert.ok(config.has('foo'));
});

it('returns false if key is missing', () => {
assert.notOk(config.has('whatevs'));
});
});
});