-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
belozer
committed
Jan 27, 2017
1 parent
b7bd3ef
commit cdcccb9
Showing
4 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const promisify = require('es6-promisify'); | ||
const stringify = require('./stringify'); | ||
|
||
const writeFile = promisify(fs.writeFile); | ||
|
||
/** | ||
* Save normalized declaration to target format | ||
* | ||
* @param {String} filename Filename for save declaration | ||
* @param {BemCell[]} cells Normalized declaraions | ||
* @param {Object} [opts] Addtional options | ||
* @param {String} [opts.format='v2'] The desired format | ||
* @param {String} [opts.exportType='cjs'] The desired type for export | ||
* @returns {Promise.<void>} | ||
*/ | ||
module.exports = (filename, cells, opts) => { | ||
const defaults = { | ||
format: 'v2', | ||
exportType: 'cjs' | ||
}; | ||
const data = stringify(cells, Object.assign({}, defaults, opts)); | ||
|
||
return writeFile(data); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const test = require('ava'); | ||
const sinon = require('sinon'); | ||
const proxyquire = require('proxyquire'); | ||
|
||
test.beforeEach(t => { | ||
const stringifyStub = sinon.stub(); | ||
const writeFileStub = (a, cb) => setTimeout(cb(), 10); | ||
|
||
t.context = { | ||
stringifyStub: stringifyStub, | ||
writeFileStub: writeFileStub, | ||
save: proxyquire('../lib/save', { | ||
'./stringify': stringifyStub, | ||
fs: { writeFile: writeFileStub } | ||
}) | ||
} | ||
}); | ||
|
||
test('method save should be returns Promise', t => { | ||
const promise = t.context.save(); | ||
|
||
t.truthy(promise.then, 'not a Promise') | ||
t.notThrows(promise); | ||
}); | ||
|
||
test('method save should be save file in cjs by default', t => { | ||
const save = t.context.save; | ||
const stringifyStub = t.context.stringifyStub; | ||
|
||
save('decl-test.js'); | ||
|
||
t.truthy(stringifyStub.calledWith(undefined, { format: 'v2', exportType: 'cjs' })); | ||
}); | ||
|
||
test('method save should be save file in custom format', t => { | ||
const save = t.context.save; | ||
const stringifyStub = t.context.stringifyStub; | ||
|
||
save('decl-test.js', null, { format: 'v5' }); | ||
|
||
t.truthy(stringifyStub.calledWith(null, { format: 'v5', exportType: 'cjs' })); | ||
}); | ||
|
||
test('method save should be save file in custom type', t => { | ||
const save = t.context.save; | ||
const stringifyStub = t.context.stringifyStub; | ||
|
||
save('decl-test.js', null, { exportType: 'txt' }); | ||
|
||
t.truthy(stringifyStub.calledWith(null, { format: 'v2', exportType: 'txt' })); | ||
}); |