Skip to content

Commit

Permalink
[test: config] fix linter warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
JBallin committed Nov 14, 2018
1 parent 9bb8a5a commit b182741
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 55 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"description": "Useful scripts to save time",
"scripts": {
"test": "eslint . && mocha"
"test": "eslint . && NODE_ENV=test mocha"
},
"repository": {
"type": "git",
Expand Down
100 changes: 46 additions & 54 deletions test/config.test.js
Original file line number Diff line number Diff line change
@@ -1,119 +1,111 @@
process.env.NODE_ENV = 'test'

const { assert } = require('chai');
const fs = require('fs');
const {
getConfig,
setConfig,
configAction,
stringify,
configPath,
fetchConfig,
configMessages
configMessages,
} = require('../config');

const fetchConfigJSON = () => fetchConfig().configJSON;

const setConfigErr = 'INVALID: setConfig takes two arguments: "keys" and "value"';
const invalidConfigActionErr = 'INVALID: non-existent action given to ballin_config';
const currentConfigJSON = fetchConfigJSON();

const setTest = (keys, value) => {
setConfig(keys, value);
assert.deepEqual(value, getConfig(keys, value))
}

assert.deepEqual(value, getConfig(keys));
};

describe('config', () => {

let savedConfig;
before('fetchConfigJSON should return a String', () => {
assert.typeOf(fetchConfigJSON(), 'String');
})
});
after('tests shouldn\'t alter config', () => {
assert.equal(currentConfigJSON, fetchConfigJSON());
})
});
beforeEach('Save config', () => {
savedConfig = fetchConfigJSON();
assert.typeOf(savedConfig, 'String');
})
});
afterEach('Reset config', () => {
fs.writeFileSync(configPath, savedConfig, 'utf8');
})
});

describe('getConfig', () => {
it('("theme") should return an Object', () => {
assert.isObject(getConfig('theme'))
})
assert.isObject(getConfig('theme'));
});
it('("theme.dark") should return an Array', () => {
assert.isArray(getConfig('theme.dark'))
})
assert.isArray(getConfig('theme.dark'));
});
it('("gu.id") should return a String', () => {
assert.isString(getConfig('gu.id'))
})
assert.isString(getConfig('gu.id'));
});
it('("up.cleanup") should return a Boolean', () => {
assert.isBoolean(getConfig('up.cleanup'))
})
assert.isBoolean(getConfig('up.cleanup'));
});
it('() should return a String', () => {
assert.isString(getConfig())
})
assert.isString(getConfig());
});
it('should return an error if given keys that don\'t exist', () => {
assert.equal(getConfig('wrong'), '"wrong" doesn\'t exist in config')
})
assert.equal(getConfig('wrong'), '"wrong" doesn\'t exist in config');
});
});


describe('setConfig', () => {
let savedConfig = '';
const initialConfig = fetchConfigJSON();

after('setConfig tests shouldn\'t alter config', () => {
assert.equal(fetchConfigJSON(), initialConfig)
})
assert.equal(fetchConfigJSON(), initialConfig);
});

it('should set theme.dark', () => {
setTest('theme.dark', ['rainbow', 'rainbow-lite'])
})
setTest('theme.dark', ['rainbow', 'rainbow-lite']);
});
it('should set up.cleanup', () => {
setTest('up.cleanup', 'test');
})
});
it('should set gu.id', () => {
setTest('gu.id', '123');
})
});
it('should give error if given no arguments', () => {
assert.equal(setConfig(), setConfigErr)
})
assert.equal(setConfig(), configMessages.setArgsErr);
});
it('should give error if given 3 arguments', () => {
assert.equal(setConfig('a','b','c'), setConfigErr)
})
assert.equal(setConfig('a', 'b', 'c'), configMessages.setArgsErr);
});
it('should return the keys/value it set', () => {
const keys = 'theme.light';
const val = 'new theme';
assert.equal(setConfig(keys, val), `"${keys}" set to: "${val}"`)
})
})
assert.equal(setConfig(keys, val), `"${keys}" set to: "${val}"`);
});
});

describe('configAction', () => {
it('() should return a String', () => {
assert.typeOf(configAction('get'), 'string');
})
});
it('("get") should return a String', () => {
assert.typeOf(configAction('get'), 'string');
})
});
it('("set") should return a setConfig error', () => {
assert.equal(configAction('set'), setConfigErr)
})
assert.equal(configAction('set'), configMessages.setArgsErr);
});
it('("get", "gu.id") should return a Number', () => {
assert.typeOf(configAction('get', 'gu.id'), 'string')
})
assert.typeOf(configAction('get', 'gu.id'), 'string');
});
it('("get", "theme.light") should return an Array', () => {
assert.isArray(configAction('get', 'theme.light'))
})
assert.isArray(configAction('get', 'theme.light'));
});
it('("wrong") should return an invalid error', () => {
assert.equal(configAction('wrong'), invalidConfigActionErr);
})
assert.equal(configAction('wrong'), configMessages.actionErr);
});
it('("set", "gu.id", "123") should set gu.id to "123"', () => {
setTest('gu.id', '123', configAction);
})
})

})
});
});
});

0 comments on commit b182741

Please sign in to comment.