Welcome to mirror list, hosted at ThFree Co, Russian Federation.

basic.js « browser « test - github.com/webtorrent/webtorrent.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 09889d728982ba8376ad8cfe88fd66f341b422fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
var fs = require('fs')
var test = require('tape')
var WebTorrent = require('../../')

var img = fs.readFileSync(__dirname + '/../../img/logo.png')
img.name = 'logo.png'

function verifyImage (t, err, elem) {
  t.error(err)
  t.ok(typeof elem.src === 'string')
  t.ok(elem.src.indexOf('blob') !== -1)
  t.equal(elem.parentElement.nodeName, 'BODY')
  t.ok(elem.alt, 'file.name')
  elem.remove()
}

test('image append w/ query selector', 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) })

  client.seed(img, function (torrent) {
    torrent.files[0].appendTo('body', function (err, elem) {
      verifyImage(t, err, elem)
      client.destroy(function (err) {
        t.error(err, 'client destroyed')
      })
    })
  })
})

test('image append w/ element', 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) })

  client.seed(img, function (torrent) {
    torrent.files[0].appendTo(document.body, function (err, elem) {
      verifyImage(t, err, elem)
      client.destroy(function (err) {
        t.error(err, 'client destroyed')
      })
    })
  })
})

test('image render w/ query selector', 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 tag = document.createElement('img')
  tag.className = 'tag'
  document.body.appendChild(tag)

  client.seed(img, function (torrent) {
    torrent.files[0].renderTo('img.tag', function (err, elem) {
      verifyImage(t, err, elem)
      client.destroy(function (err) {
        t.error(err, 'client destroyed')
      })
    })
  })
})

test('image render w/ element', 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 tag = document.createElement('img')
  document.body.appendChild(tag)

  client.seed(img, function (torrent) {
    torrent.files[0].renderTo(tag, function (err, elem) {
      verifyImage(t, err, elem)
      client.destroy(function (err) {
        t.error(err, 'client destroyed')
      })
    })
  })
})