test: options

This commit is contained in:
Evan You
2018-01-03 00:18:18 -05:00
parent f384ac2051
commit 1548c613f7
2 changed files with 63 additions and 2 deletions

View File

@@ -1,5 +1,66 @@
jest.mock('fs')
it('should pass', () => {
const fs = require('fs')
const {
rcPath,
saveOptions,
loadSavedOptions,
savePartialOptions
} = require('../options')
it('save options', () => {
saveOptions({
packageManager: 'npm',
plugins: {},
foo: 'bar'
})
const options = JSON.parse(fs.readFileSync(rcPath, 'utf-8'))
expect(options).toEqual({
packageManager: 'npm',
plugins: {}
})
})
it('load options', () => {
expect(loadSavedOptions()).toEqual({
packageManager: 'npm',
plugins: {}
})
fs.unlinkSync(rcPath)
expect(loadSavedOptions()).toEqual({})
})
it('save partial options', () => {
savePartialOptions({
packageManager: 'yarn'
})
expect(loadSavedOptions()).toEqual({
packageManager: 'yarn'
})
savePartialOptions({
plugins: {
foo: { a: 1, b: 2 }
}
})
expect(loadSavedOptions()).toEqual({
packageManager: 'yarn',
plugins: {
foo: { a: 1, b: 2 }
}
})
savePartialOptions({
plugins: {
foo: { a: 2, c: 3 },
bar: { d: 4 }
}
})
expect(loadSavedOptions()).toEqual({
packageManager: 'yarn',
plugins: {
foo: { a: 2, b: 2, c: 3 },
bar: { d: 4 }
}
})
})

View File

@@ -64,7 +64,7 @@ const isObject = val => val && typeof val === 'object'
function deepMerge (to, from) {
for (const key in from) {
if (isObject(to[key]) && isObject(from[key])) {
to[key] = deepMerge(to[key], from[key])
deepMerge(to[key], from[key])
} else {
to[key] = from[key]
}