Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

data-attribute DOM API with client.render() to activate #1421

Open
wants to merge 3 commits into
base: master
from
Open
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Next

Tests for client.render()

  • Loading branch information
KayleePop committed May 21, 2018
commit 50678ddcd59475c1c4e049d44d82bcca790a3bda
@@ -96,6 +96,150 @@ if (!(global && global.process && global.process.versions && global.process.vers
})
})
})

test('client.render() without argument', function (t) {
t.plan(11)

var client = new WebTorrent({ dht: false, tracker: false })

client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })

client.seed(img, function (torrent) {
var tag = document.createElement('img')
tag.setAttribute('data-torrent-src', torrent.infoHash)
document.body.appendChild(tag)

var tag2 = document.createElement('img')
tag2.setAttribute('data-torrent-src', torrent.infoHash)
document.body.appendChild(tag2)

client.render(function (err) {
verifyImage(t, err, tag)
verifyImage(t, err, tag2)
client.destroy(function (err) {
t.error(err, 'client destroyed')
})
})
})
})

test('client.render() with element argument', function (t) {
t.plan(7)

var client = new WebTorrent({ dht: false, tracker: false })

client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })

client.seed(img, function (torrent) {
var elemToRender = document.createElement('img')
elemToRender.setAttribute('data-torrent-src', torrent.infoHash)
document.body.appendChild(elemToRender)

var elem2 = document.createElement('img')
elem2.setAttribute('data-torrent-src', torrent.infoHash)
document.body.appendChild(elem2)

client.render({elements: [elemToRender]}, function (err) {
verifyImage(t, err, elemToRender)
t.false(elem2.hasAttribute('src'), 'unspecified element should not be altered')
elem2.remove()
client.destroy(function (err) {
t.error(err, 'client destroyed')
})
})
})
})

test('client.render() with path', function (t) {
t.plan(6)

var client = new WebTorrent({ dht: false, tracker: false })

client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })

var text = Buffer.from('text')
text.name = 'text.txt'

client.seed([text, img], function (torrent) {
var imgElem = document.createElement('img')
imgElem.setAttribute('data-torrent-src', torrent.infoHash)
imgElem.setAttribute('data-torrent-path', 'img.png')
document.body.appendChild(imgElem)

var textElem = document.createElement('iframe')
textElem.setAttribute('data-torrent-src', torrent.infoHash)
textElem.setAttribute('data-torrent-path', 'text.txt')
document.body.appendChild(textElem)

client.render(function (err) {
// error will be returned if either is rendered to the wrong element
verifyImage(t, err, imgElem)
textElem.remove()
client.destroy(function (err) {
t.error(err, 'client destroyed')
})
})
})
})

test('client.render() fallback on render error', function (t) {
t.plan(3)

var client = new WebTorrent({ dht: false, tracker: false })

client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })

client.seed(img, function (torrent) {
// use wrong element type to cause error in renderTo()
var elem = document.createElement('video')
elem.setAttribute('data-torrent-src', torrent.infoHash)
elem.setAttribute('data-torrent-fallback', 'fake url')
document.body.appendChild(elem)

client.render(function (err) {
t.ok(err)
t.equals(elem.getAttribute('src'), 'fake url')
elem.remove()
client.destroy(function (err) {
t.error(err, 'client destroyed')
})
})
})
})

test('client.render() fallback on torrent error', function (t) {
t.plan(3)

var client = new WebTorrent({ dht: false, tracker: false })

client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })

client.seed(img, function (torrent) {
var elem = document.createElement('img')
elem.setAttribute('data-torrent-src', torrent.infoHash)
elem.setAttribute('data-torrent-fallback', 'fake url')
document.body.appendChild(elem)

client.render(function (err) {
t.error(err)
process.nextTick(function () {
torrent.emit('error')
process.nextTick(function () {
t.equals(elem.getAttribute('src'), 'fake url')
elem.remove()
client.destroy(function (err) {
t.error(err, 'client destroyed')
})
})
})
})
})
})
}

test('WebTorrent.WEBRTC_SUPPORT', function (t) {
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.