more Util tests

This commit is contained in:
Devin Howard 2016-10-24 10:51:26 +08:00
parent 1809293031
commit ad5200a62b
3 changed files with 93 additions and 14 deletions

View file

@ -88,10 +88,13 @@ const Util = {
} }
return coords return coords
}, },
getPastelColor: function () { getPastelColor: function ({ rseed, gseed, bseed }) {
var r = (Math.round(Math.random() * 127) + 127).toString(16) if (rseed === undefined) rseed = Math.random()
var g = (Math.round(Math.random() * 127) + 127).toString(16) if (gseed === undefined) gseed = Math.random()
var b = (Math.round(Math.random() * 127) + 127).toString(16) if (bseed === undefined) bseed = Math.random()
var r = (Math.round(rseed * 127) + 127).toString(16)
var g = (Math.round(gseed * 127) + 127).toString(16)
var b = (Math.round(bseed * 127) + 127).toString(16)
return Util.colorLuminance('#' + r + g + b, -0.4) return Util.colorLuminance('#' + r + g + b, -0.4)
}, },
// darkens a hex value by 'lum' percentage // darkens a hex value by 'lum' percentage
@ -113,13 +116,6 @@ const Util = {
return rgb return rgb
}, },
generateOptionsList: function (data) {
var newlist = ''
for (var i = 0; i < data.length; i++) {
newlist = newlist + '<option value="' + data[i]['id'] + '">' + data[i]['1'][1] + '</option>'
}
return newlist
},
checkURLisImage: function (url) { checkURLisImage: function (url) {
// when the page reloads the following regular expression will be screwed up // when the page reloads the following regular expression will be screwed up
// please replace it with this one before you save: /*backslashhere*.(jpeg|jpg|gif|png)$/ // please replace it with this one before you save: /*backslashhere*.(jpeg|jpg|gif|png)$/

View file

@ -23,10 +23,10 @@ describe('Metamaps.Util.js', function () {
}) })
}) })
describe('nowDateFormatted', function() { describe('nowDateFormatted', function() {
// TODO need `Date` it.skip('TODO need `Date`')
}) })
describe('decodeEntities', function() { describe('decodeEntities', function() {
// TODO need `document` it.skip('TODO need `document`')
}) })
describe('getDistance', function() { describe('getDistance', function() {
it('(0,0) -> (0,0) = 0', function() { it('(0,0) -> (0,0) = 0', function() {
@ -42,4 +42,87 @@ describe('Metamaps.Util.js', function () {
.to.equal('8.6023') .to.equal('8.6023')
}) })
}) })
describe('coordsToPixels', function() {
it('returns 0,0 for null canvas', function() {
expect(Util.coordsToPixels(null).x).to.equal(0)
expect(Util.coordsToPixels(null).y).to.equal(0)
})
it.skip('TODO need initialized mGraph to test further')
})
describe('pixelsToCoords', function() {
it('returns 0,0 for null canvas', function() {
expect(Util.pixelsToCoords(null).x).to.equal(0)
expect(Util.pixelsToCoords(null).y).to.equal(0)
})
it.skip('TODO need initialized mGraph to test further')
})
describe('getPastelColor', function() {
it('1 => fefefe', function() {
expect(Util.getPastelColor({ rseed: 1, gseed: 1, bseed: 1 }))
.to.equal(Util.colorLuminance('#fefefe', -0.4))
})
it('0 => 7f7f7f', function() {
expect(Util.getPastelColor({ rseed: 0, gseed: 0, bseed: 0 }))
.to.equal(Util.colorLuminance('#7f7f7f', -0.4))
})
})
describe('colorLuminance', function() {
describe('-0.4 lum', function() {
it('white => ?', function() {
expect(Util.colorLuminance('#ffffff', -0.4)).to.equal('#999999')
})
it('black => ?', function() {
expect(Util.colorLuminance('#000000', -0.4)).to.equal('#000000')
})
it('7f7f7f => ?', function() {
expect(Util.colorLuminance('#7f7f7f', -0.4)).to.equal('#4c4c4c')
})
})
describe('other lum values', function() {
it('-1', function() {
expect(Util.colorLuminance('#7f7f7f', -1)).to.equal('#000000')
})
it('-0.5', function() {
expect(Util.colorLuminance('#7f7f7f', -0.5)).to.equal('#404040')
})
it('0', function() {
expect(Util.colorLuminance('#7f7f7f', 0)).to.equal('#7f7f7f')
})
it('0.5', function() {
expect(Util.colorLuminance('#7f7f7f', 0.5)).to.equal('#bfbfbf')
})
it('1', function() {
expect(Util.colorLuminance('#7f7f7f', 1)).to.equal('#fefefe')
})
})
})
describe('openLink', function() {
it.skip('TODO need `window`')
})
describe('mdToHTML', function() {
it('filters xss', function() {
const md = '<script>alert("xss");</script>'
const html = '<!-- raw HTML omitted -->'
expect(Util.mdToHTML(md).trim()).to.equal(html)
})
it('bold and italics', function() {
const md = '**Bold** *Italics*'
const html = '<p><strong>Bold</strong> <em>Italics</em></p>'
expect(Util.mdToHTML(md).trim()).to.equal(html)
})
it('links and images', function() {
const md = '[Link](https://metamaps.cc) ![Image](https://example.org/image.png)'
const html = '<p><a href="https://metamaps.cc">Link</a> <img src="https://example.org/image.png" alt="Image" /></p>'
expect(Util.mdToHTML(md).trim()).to.equal(html)
})
})
describe('logCanvasAttributes', function() {
it.skip('TODO need a canvas')
})
describe('resizeCanvas', function() {
it.skip('TODO need a canvas')
})
}) })

View file

@ -5,7 +5,7 @@
"scripts": { "scripts": {
"build": "webpack", "build": "webpack",
"build:watch": "webpack --watch", "build:watch": "webpack --watch",
"test": "mocha --compilers js:babel-core/register frontend/test || (echo 'Run `npm install` to setup testing' && false)" "test": "mocha --compilers js:babel-core/register frontend/test"
}, },
"repository": { "repository": {
"type": "git", "type": "git",