2022-09-09 20:20:38 +02:00
|
|
|
import chai from 'chai'
|
|
|
|
import { Point } from '../src/index.mjs'
|
2022-08-25 11:47:54 +02:00
|
|
|
|
|
|
|
const expect = chai.expect
|
2018-08-11 19:17:39 +02:00
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
const newAttr = () => new Point(0, 0).attributes
|
2021-11-21 17:18:10 +01:00
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
const a = newAttr()
|
2021-11-21 17:18:10 +01:00
|
|
|
|
2022-08-27 09:27:07 +02:00
|
|
|
describe('Attributes', () => {
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should set an attribute', () => {
|
2022-08-27 09:27:07 +02:00
|
|
|
a.set('hold', 'me')
|
2022-09-09 20:20:38 +02:00
|
|
|
expect(a.get('hold')).to.equal('me')
|
|
|
|
})
|
2021-11-21 17:18:10 +01:00
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should remove an attribute', () => {
|
2022-08-27 09:27:07 +02:00
|
|
|
a.remove('hold')
|
2022-09-09 20:20:38 +02:00
|
|
|
expect(a.get('hold')).to.equal(false)
|
|
|
|
})
|
2018-08-11 19:17:39 +02:00
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should only set an unset attribute', () => {
|
2022-08-27 09:27:07 +02:00
|
|
|
a.setIfUnset('hold', 'me')
|
2022-09-09 20:20:38 +02:00
|
|
|
expect(a.get('hold')).to.equal('me')
|
2022-08-27 09:27:07 +02:00
|
|
|
a.setIfUnset('hold', 'thatDudeOverThere')
|
2022-09-09 20:20:38 +02:00
|
|
|
expect(a.get('hold')).to.equal('me')
|
|
|
|
})
|
2018-08-11 19:17:39 +02:00
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should return false when getting an unset attribute', () => {
|
2022-08-27 09:27:07 +02:00
|
|
|
let a = newAttr()
|
2022-09-09 20:20:38 +02:00
|
|
|
expect(a.get('test')).to.equal(false)
|
|
|
|
})
|
2018-09-06 12:02:43 +02:00
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should render attributes correctly', () => {
|
|
|
|
let a = newAttr().set('class', 'test').add('class', 'render').set('transform', 'scale(1)')
|
|
|
|
expect(a.render()).to.equal(' class="test render" transform="scale(1)"')
|
|
|
|
})
|
2018-12-09 14:17:46 +01:00
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should render attributes with given prefix only', () => {
|
2022-08-27 09:27:07 +02:00
|
|
|
let a = newAttr()
|
2022-09-09 20:20:38 +02:00
|
|
|
.set('class', 'test')
|
|
|
|
.add('class', 'render')
|
|
|
|
.add('data-text', 'foo')
|
|
|
|
.add('data-text', 'bar')
|
|
|
|
.add('data-mode', 'test')
|
|
|
|
.set('transform', 'scale(1)')
|
|
|
|
expect(a.renderIfPrefixIs('data-')).to.equal(' text="foo bar" mode="test"')
|
|
|
|
})
|
2022-07-23 17:00:48 +02:00
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should return attributes as array', () => {
|
|
|
|
let a = newAttr().set('class', 'test').add('class', 'render')
|
|
|
|
expect(JSON.stringify(a.getAsArray('class'))).to.equal(JSON.stringify(['test', 'render']))
|
|
|
|
expect(a.getAsArray('nope')).to.equal(false)
|
|
|
|
})
|
2022-07-23 17:00:48 +02:00
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should render attributes as CSS', () => {
|
|
|
|
let a = newAttr().set('line-height', 1.2).add('border', '1px solid red')
|
|
|
|
expect(a.renderAsCss()).to.equal(' line-height:1.2; border:1px solid red;')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should return attributes as props and filter a prefix', () => {
|
2022-08-27 09:27:07 +02:00
|
|
|
const a = newAttr()
|
2022-09-09 20:20:38 +02:00
|
|
|
.set('line-height', 1.2)
|
|
|
|
.add('border', '1px solid red')
|
|
|
|
.set('data-text', 'This is a test')
|
|
|
|
.set('data-text-class', 'center')
|
2022-08-27 09:27:07 +02:00
|
|
|
const props = a.asPropsIfPrefixIs('data-')
|
2022-09-09 20:20:38 +02:00
|
|
|
expect(props.text).to.equal('This is a test')
|
|
|
|
expect(props['text-class']).to.equal('center')
|
|
|
|
})
|
2022-08-27 09:27:07 +02:00
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should return attributes as props and handle special class case', () => {
|
|
|
|
const a = newAttr().set('class', 'fabric')
|
2022-08-27 09:27:07 +02:00
|
|
|
const props = a.asPropsIfPrefixIs('')
|
2022-09-09 20:20:38 +02:00
|
|
|
expect(props.className).to.equal('fabric')
|
|
|
|
})
|
2022-08-27 09:27:07 +02:00
|
|
|
})
|