import * as jsdom from 'jsdom'
import * as xjs from '../../index'
import {ValueFunction} from '../../src/class/Element.class'
import test from './test'
let x: xjs.Element = new xjs.Element(jsdom.JSDOM.fragment('').querySelector('span') !)
export default Promise.all([
test(x.outerHTML(), '')
// set an attribute to a string
.then(() => test(x.attr('attr1', 'val1').outerHTML(), ''))
.then(() => test(x.attr('attr1') ! , 'val1'))
// add a boolean attribute
.then(() => test(x.attr('attr2', '').outerHTML() , ''))
.then(() => test(x.attr('attr2') ! , ''))
// remove an attribute
.then(() => test(x.attr('attr2', null).outerHTML() , ''))
.then(() => test(`${x.attr('attr2')}` , 'null'))
// set an attribute to the string `'null'`
.then(() => test(x.attr('attr2', 'null').outerHTML(), ''))
.then(() => test(x.attr('attr2') ! , 'null'))
// set an attribute using a function
.then(() => test(x.attr('attr3', function () { return this.attr('attr1') }).outerHTML(), ''))
.then(() => test((() => {
let valueFn: ValueFunction = function (this: xjs.Element) { return this.attr('attr2') }
return x.attr('attr3', valueFn).outerHTML()
})(), ''))
// call `attr()` with an object
.then(() => test(x.attr({ attr1: 'string', attr2: 42, attr3: true }).outerHTML(), ''))
.then(() => test(x.attr({ attr1: null }).outerHTML() , ''))
// call `attr()` with no args, `null`, or an empty object `{}`
.then(() => test(x.attr( ).outerHTML() , ''))
.then(() => test(x.attr(null).outerHTML() , ''))
.then(() => test(x.attr({} ).outerHTML() , ''))
// fail to call `attr()` with `''`
.then(() => test((() => {
try {
return x.attr('') !
} catch (e) {
return e.name
}
})(), 'RangeError'))
// fail to call `attr()` with `NaN`
.then(() => test((() => {
try {
return x.attr('foo', NaN).attr('foo') !
} catch (e) {
return e.name
}
})(), 'NaNError'))
])