Skip to content

Commit

Permalink
reference editor - lint && unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
wonknu committed Oct 31, 2016
1 parent e1b53fe commit ff987f0
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/cli/cms/data/index.js
Expand Up @@ -20,4 +20,4 @@ export {
regex,
metas,
file
}
}
2 changes: 1 addition & 1 deletion src/cli/cms/reference/index.js
Expand Up @@ -2,4 +2,4 @@ import * as reference from './reference'

export {
reference
}
}
14 changes: 8 additions & 6 deletions src/cli/cms/reference/reference.js
Expand Up @@ -2,27 +2,29 @@ import path from 'path'
import fse from 'fs-extra'

import {
coreUtils,
cmsData,
config
} from '../../'

export function getFiles(name = '') {
var pathToReferences = path.join(config.root, config.reference.url)
var res = {}
const pathToReferences = path.join(config.root, config.reference.url)
let res = {}

if(name !== '') res[name] = cmsData.file.get(name)
else {
Array.prototype.forEach.call(fse.readdirSync(pathToReferences), (el) => {
var pathToReference = path.join(pathToReferences, el)
if(el.indexOf('.json') > -1) res[pathToReference] = cmsData.file.get(pathToReference)
const files = coreUtils.file.getFilesSync(pathToReferences, true, '.json')
Array.prototype.forEach.call(files, (pathFile) => {
var fileName = pathFile.split('/');
res[fileName[fileName.length - 1]] = cmsData.file.get(pathFile)
})
}

return res
}

export function saveFile(url, json) {
fse.writeJson(url, JSON.parse(json), function (err) {
fse.writeJson(path.join(config.root, config.reference.url, url), JSON.parse(json), function (err) {
if(err) console.log('saveFile reference error: ', err)
})
}
4 changes: 0 additions & 4 deletions src/server/public/scripts/modules/EditorReferences.js
Expand Up @@ -24,10 +24,6 @@ export default class EditorFiles {
this.textArea = document.querySelector('.display-json')
this.jsonError = document.querySelector('.json-error')
if(!this.referenceLinks) return
Array.prototype.forEach.call(this.referenceLinks, (referenceLink) => {
var dataHref = referenceLink.getAttribute('data-href').split('/')
referenceLink.textContent = dataHref[dataHref.length - 1]
})
this.rebind()
})
})
Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/get-reference.js
Expand Up @@ -7,4 +7,4 @@ var route = function(req, res){
res.send(JSON.stringify({reference: Manager.instance.getReferences()}))
}

export default route
export default route
2 changes: 1 addition & 1 deletion src/server/views/partials/right-references-list.html
Expand Up @@ -10,7 +10,7 @@
<ul class="list-group">
{{#each reference}}
<li class="list-group-item" data-error="0" data-href='{{@key}}' data-ref-json='{{printJson this}}'>

{{@key}}
</li>
{{/each}}
</ul>
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/reference/test.json
Expand Up @@ -2,4 +2,4 @@
"ref1": "ref1",
"ref2": "ref2",
"ref3": "ref3"
}
}
33 changes: 33 additions & 0 deletions test/reference.js
@@ -0,0 +1,33 @@
var chai = require('chai');
var path = require('path')

var config = require('../src/cli').config
config.set({root: __dirname + '/fixtures'})

var cmsReference = require('../src/cli').cmsReference;

var jsonPath = 'test.json';

describe('cmsReference', function() {
/**
* cmsReference.reference.getFiles
*
*/
it('cmsReference.reference.getFiles()', function() {
var json = cmsReference.reference.getFiles();
chai.expect(json).to.be.an('object');
chai.expect(json[jsonPath].ref1).to.equal('ref1');
});

/**
* cmsReference.reference.saveFile
*
*/
it('cmsReference.reference.saveFile()', function() {
var json = cmsReference.reference.getFiles();
var ref1 = json[jsonPath]['ref1'];
console.log(json[jsonPath]['ref1'])
cmsReference.reference.saveFile(jsonPath, JSON.stringify(json[jsonPath]));
chai.expect(json[jsonPath]['ref1']).to.equal('ref1');
});
});

0 comments on commit ff987f0

Please sign in to comment.