reuse duplicated code
This commit is contained in:
parent
51b689d8b4
commit
d684838e4a
1 changed files with 42 additions and 21 deletions
|
@ -4,9 +4,23 @@ import { expect } from 'chai'
|
||||||
import sinon from 'sinon'
|
import sinon from 'sinon'
|
||||||
|
|
||||||
import Util from '../../src/Metamaps/Util'
|
import Util from '../../src/Metamaps/Util'
|
||||||
|
import * as EmojiMart from 'emoji-mart'
|
||||||
|
|
||||||
const sandbox = sinon.sandbox.create()
|
const sandbox = sinon.sandbox.create()
|
||||||
|
|
||||||
|
function mockMGraph({ x, y, sx, sy, px, py, width, height, ox, oy }) {
|
||||||
|
return {
|
||||||
|
canvas: {
|
||||||
|
getSize: () => ({ width, height }),
|
||||||
|
getPos: () => ({ x: px, y: py }),
|
||||||
|
translateOffsetX: ox,
|
||||||
|
translateOffsetY: oy,
|
||||||
|
scaleOffsetX: sx,
|
||||||
|
scaleOffsetY: sy
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
describe('Metamaps.Util.js', function() {
|
describe('Metamaps.Util.js', function() {
|
||||||
describe('splitLine', function() {
|
describe('splitLine', function() {
|
||||||
it('splits on words', function() {
|
it('splits on words', function() {
|
||||||
|
@ -78,16 +92,9 @@ describe('Metamaps.Util.js', function() {
|
||||||
describe('coordsToPixels', function() {
|
describe('coordsToPixels', function() {
|
||||||
function assertCoordsToPixels(expectedX, expectedY,
|
function assertCoordsToPixels(expectedX, expectedY,
|
||||||
{ x, y, sx, sy, px, py, width, height, ox, oy }) {
|
{ x, y, sx, sy, px, py, width, height, ox, oy }) {
|
||||||
const mGraph = {
|
const mGraph = mockMGraph({
|
||||||
canvas: {
|
x, y, sx, sy, px, py, width, height, ox, oy
|
||||||
getSize: () => ({ width, height }),
|
}){
|
||||||
getPos: () => ({ x: px, y: py }),
|
|
||||||
translateOffsetX: ox,
|
|
||||||
translateOffsetY: oy,
|
|
||||||
scaleOffsetX: sx,
|
|
||||||
scaleOffsetY: sy
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const coords = { x, y }
|
const coords = { x, y }
|
||||||
const actual = Util.coordsToPixels(mGraph, coords)
|
const actual = Util.coordsToPixels(mGraph, coords)
|
||||||
expect(actual.x).to.equal(expectedX)
|
expect(actual.x).to.equal(expectedX)
|
||||||
|
@ -124,17 +131,9 @@ describe('Metamaps.Util.js', function() {
|
||||||
describe('pixelsToCoords', function() {
|
describe('pixelsToCoords', function() {
|
||||||
function assertPixelsToCoords(expectedX, expectedY,
|
function assertPixelsToCoords(expectedX, expectedY,
|
||||||
{ x, y, px, py, width, height, ox, oy, sx, sy }) {
|
{ x, y, px, py, width, height, ox, oy, sx, sy }) {
|
||||||
const mGraph = {
|
const mGraph = mockMGraph({
|
||||||
canvas: {
|
x, y, sx, sy, px, py, width, height, ox, oy
|
||||||
getSize: () => ({ width, height }),
|
}){
|
||||||
getPos: () => ({ x: px, y: py }),
|
|
||||||
translateOffsetX: ox,
|
|
||||||
translateOffsetY: oy,
|
|
||||||
scaleOffsetX: sx,
|
|
||||||
scaleOffsetY: sy
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const coords = { x, y }
|
|
||||||
const actual = Util.pixelsToCoords(mGraph, coords)
|
const actual = Util.pixelsToCoords(mGraph, coords)
|
||||||
expect(actual.x).to.equal(expectedX)
|
expect(actual.x).to.equal(expectedX)
|
||||||
expect(actual.y).to.equal(expectedY)
|
expect(actual.y).to.equal(expectedY)
|
||||||
|
@ -304,5 +303,27 @@ describe('Metamaps.Util.js', function() {
|
||||||
.to.equal(true)
|
.to.equal(true)
|
||||||
expect(canvas.scale.calledWith(5 - 3, 6 - 4)).to.equal(true)
|
expect(canvas.scale.calledWith(5 - 3, 6 - 4)).to.equal(true)
|
||||||
})
|
})
|
||||||
|
}),
|
||||||
|
describe('emoji', function() {
|
||||||
|
EmojiMart.emojiIndex = {
|
||||||
|
emojis: {
|
||||||
|
emoji1: { native: '__EMOJI1__', colons: ':emoji1:' },
|
||||||
|
emoji2: { native: '__EMOJI2__', colons: ':emoji2:' }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const withEmojiText = 'test __EMOJI1__ test __EMOJI2__ test';
|
||||||
|
const noEmojiText = 'test :emoji1: test :emoji2: test';
|
||||||
|
const emoticonsText = 'test :) test :( test'
|
||||||
|
const emoticonsReplacedText = 'test __EMOJI1__ test __EMOJI2__ test'
|
||||||
|
it('removeEmoji replaces emoji with text', function() {
|
||||||
|
expect(Util.removeEmoji(withEmojiText)).to.equal(noEmojiText)
|
||||||
|
})
|
||||||
|
it('addEmoji replaces text with emoji', function() {
|
||||||
|
expect(Util.addEmoji(noEmojiText)).to.equal(withEmojiText)
|
||||||
|
})
|
||||||
|
it.skip('addEmoji replaces emoticons with emoji', function() {
|
||||||
|
expect(Util.addEmoji(emoticonsText, { emoticons: true }))
|
||||||
|
.to.equal(emoticonsReplacedText)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Reference in a new issue