From 46033ae52eca6e22301bb8ed9566c498d3494711 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Rodr=C3=ADguez=20Baquero?= Date: Sat, 10 Jul 2021 20:27:48 -0500 Subject: fix: modernize code (#2134) * fix: modernize code * standard fix --- test/client-add.js | 127 ++++++++++++++++++++++++++--------------------------- 1 file changed, 62 insertions(+), 65 deletions(-) (limited to 'test/client-add.js') diff --git a/test/client-add.js b/test/client-add.js index 90231fc..84c8fd6 100644 --- a/test/client-add.js +++ b/test/client-add.js @@ -2,123 +2,123 @@ const fixtures = require('webtorrent-fixtures') const test = require('tape') const WebTorrent = require('../') -test('client.add: magnet uri, utf-8 string', function (t) { +test('client.add: magnet uri, utf-8 string', t => { t.plan(6) const client = new WebTorrent({ dht: false, tracker: false, lsd: false }) - client.on('error', function (err) { t.fail(err) }) - client.on('warning', function (err) { t.fail(err) }) + client.on('error', err => { t.fail(err) }) + client.on('warning', err => { t.fail(err) }) const torrent = client.add(fixtures.leaves.magnetURI) t.equal(client.torrents.length, 1) - torrent.on('infoHash', function () { + torrent.on('infoHash', () => { t.equal(torrent.infoHash, fixtures.leaves.parsedTorrent.infoHash) t.equal(torrent.magnetURI, fixtures.leaves.magnetURI) - client.remove(fixtures.leaves.magnetURI, function (err) { t.error(err, 'torrent destroyed') }) + client.remove(fixtures.leaves.magnetURI, err => { t.error(err, 'torrent destroyed') }) t.equal(client.torrents.length, 0) - client.destroy(function (err) { t.error(err, 'client destroyed') }) + client.destroy(err => { t.error(err, 'client destroyed') }) }) }) -test('client.add: torrent file, buffer', function (t) { +test('client.add: torrent file, buffer', t => { t.plan(6) const client = new WebTorrent({ dht: false, tracker: false, lsd: false }) - client.on('error', function (err) { t.fail(err) }) - client.on('warning', function (err) { t.fail(err) }) + client.on('error', err => { t.fail(err) }) + client.on('warning', err => { t.fail(err) }) const torrent = client.add(fixtures.leaves.torrent) t.equal(client.torrents.length, 1) - torrent.on('infoHash', function () { + torrent.on('infoHash', () => { t.equal(torrent.infoHash, fixtures.leaves.parsedTorrent.infoHash) t.equal(torrent.magnetURI, fixtures.leaves.magnetURI) - client.remove(fixtures.leaves.torrent, function (err) { t.error(err, 'torrent destroyed') }) + client.remove(fixtures.leaves.torrent, err => { t.error(err, 'torrent destroyed') }) t.equal(client.torrents.length, 0) - client.destroy(function (err) { t.error(err, 'client destroyed') }) + client.destroy(err => { t.error(err, 'client destroyed') }) }) }) -test('client.add: info hash, hex string', function (t) { +test('client.add: info hash, hex string', t => { t.plan(6) const client = new WebTorrent({ dht: false, tracker: false, lsd: false }) - client.on('error', function (err) { t.fail(err) }) - client.on('warning', function (err) { t.fail(err) }) + client.on('error', err => { t.fail(err) }) + client.on('warning', err => { t.fail(err) }) const torrent = client.add(fixtures.leaves.parsedTorrent.infoHash) t.equal(client.torrents.length, 1) - torrent.on('infoHash', function () { + torrent.on('infoHash', () => { t.equal(torrent.infoHash, fixtures.leaves.parsedTorrent.infoHash) - t.equal(torrent.magnetURI, 'magnet:?xt=urn:btih:' + fixtures.leaves.parsedTorrent.infoHash) + t.equal(torrent.magnetURI, `magnet:?xt=urn:btih:${fixtures.leaves.parsedTorrent.infoHash}`) - client.remove(fixtures.leaves.parsedTorrent.infoHash, function (err) { t.error(err, 'torrent destroyed') }) + client.remove(fixtures.leaves.parsedTorrent.infoHash, err => { t.error(err, 'torrent destroyed') }) t.equal(client.torrents.length, 0) - client.destroy(function (err) { t.error(err, 'client destroyed') }) + client.destroy(err => { t.error(err, 'client destroyed') }) }) }) -test('client.add: info hash, buffer', function (t) { +test('client.add: info hash, buffer', t => { t.plan(6) const client = new WebTorrent({ dht: false, tracker: false, lsd: false }) - client.on('error', function (err) { t.fail(err) }) - client.on('warning', function (err) { t.fail(err) }) + client.on('error', err => { t.fail(err) }) + client.on('warning', err => { t.fail(err) }) const torrent = client.add(fixtures.leaves.parsedTorrent.infoHashBuffer) t.equal(client.torrents.length, 1) - torrent.on('infoHash', function () { + torrent.on('infoHash', () => { t.equal(torrent.infoHash, fixtures.leaves.parsedTorrent.infoHash) - t.ok(torrent.magnetURI.indexOf('magnet:?xt=urn:btih:' + fixtures.leaves.parsedTorrent.infoHash) === 0) + t.ok(torrent.magnetURI.indexOf(`magnet:?xt=urn:btih:${fixtures.leaves.parsedTorrent.infoHash}`) === 0) - client.remove(Buffer.from(fixtures.leaves.parsedTorrent.infoHash, 'hex'), function (err) { t.error(err, 'torrent destroyed') }) + client.remove(Buffer.from(fixtures.leaves.parsedTorrent.infoHash, 'hex'), err => { t.error(err, 'torrent destroyed') }) t.equal(client.torrents.length, 0) - client.destroy(function (err) { t.error(err, 'client destroyed') }) + client.destroy(err => { t.error(err, 'client destroyed') }) }) }) -test('client.add: parsed torrent, from `parse-torrent`', function (t) { +test('client.add: parsed torrent, from `parse-torrent`', t => { t.plan(6) const client = new WebTorrent({ dht: false, tracker: false, lsd: false }) - client.on('error', function (err) { t.fail(err) }) - client.on('warning', function (err) { t.fail(err) }) + client.on('error', err => { t.fail(err) }) + client.on('warning', err => { t.fail(err) }) const torrent = client.add(fixtures.leaves.parsedTorrent) t.equal(client.torrents.length, 1) - torrent.on('infoHash', function () { + torrent.on('infoHash', () => { t.equal(torrent.infoHash, fixtures.leaves.parsedTorrent.infoHash) t.equal(torrent.magnetURI, fixtures.leaves.magnetURI) - client.remove(fixtures.leaves.parsedTorrent, function (err) { t.error(err, 'torrent destroyed') }) + client.remove(fixtures.leaves.parsedTorrent, err => { t.error(err, 'torrent destroyed') }) t.equal(client.torrents.length, 0) - client.destroy(function (err) { t.error(err, 'client destroyed') }) + client.destroy(err => { t.error(err, 'client destroyed') }) }) }) -test('client.add: parsed torrent, with string type announce property', function (t) { +test('client.add: parsed torrent, with string type announce property', t => { t.plan(7) const client = new WebTorrent({ dht: false, tracker: false, lsd: false }) - client.on('error', function (err) { t.fail(err) }) - client.on('warning', function (err) { t.fail(err) }) + client.on('error', err => { t.fail(err) }) + client.on('warning', err => { t.fail(err) }) const parsedTorrent = Object.assign({}, fixtures.leaves.parsedTorrent) parsedTorrent.announce = 'http://tracker.local:80' @@ -126,30 +126,29 @@ test('client.add: parsed torrent, with string type announce property', function const torrent = client.add(parsedTorrent) t.equal(client.torrents.length, 1) - torrent.on('infoHash', function () { + torrent.on('infoHash', () => { t.equal(torrent.infoHash, fixtures.leaves.parsedTorrent.infoHash) - const expectedMagnetURI = fixtures.leaves.magnetURI + - '&tr=' + encodeURIComponent('http://tracker.local:80') + const expectedMagnetURI = `${fixtures.leaves.magnetURI}&tr=${encodeURIComponent('http://tracker.local:80')}` t.equal(torrent.magnetURI, expectedMagnetURI) // `torrent.announce` must always be an array t.deepEqual(torrent.announce, ['http://tracker.local:80']) - client.remove(fixtures.leaves.parsedTorrent, function (err) { t.error(err, 'torrent destroyed') }) + client.remove(fixtures.leaves.parsedTorrent, err => { t.error(err, 'torrent destroyed') }) t.equal(client.torrents.length, 0) - client.destroy(function (err) { t.error(err, 'client destroyed') }) + client.destroy(err => { t.error(err, 'client destroyed') }) }) }) -test('client.add: parsed torrent, with array type announce property', function (t) { +test('client.add: parsed torrent, with array type announce property', t => { t.plan(7) const client = new WebTorrent({ dht: false, tracker: false, lsd: false }) - client.on('error', function (err) { t.fail(err) }) - client.on('warning', function (err) { t.fail(err) }) + client.on('error', err => { t.fail(err) }) + client.on('warning', err => { t.fail(err) }) const parsedTorrent = Object.assign({}, fixtures.leaves.parsedTorrent) parsedTorrent.announce = ['http://tracker.local:80', 'http://tracker.local:81'] @@ -157,72 +156,70 @@ test('client.add: parsed torrent, with array type announce property', function ( const torrent = client.add(parsedTorrent) t.equal(client.torrents.length, 1) - torrent.on('infoHash', function () { + torrent.on('infoHash', () => { t.equal(torrent.infoHash, fixtures.leaves.parsedTorrent.infoHash) - const expectedMagnetURI = fixtures.leaves.magnetURI + - '&tr=' + encodeURIComponent('http://tracker.local:80') + - '&tr=' + encodeURIComponent('http://tracker.local:81') + const expectedMagnetURI = `${fixtures.leaves.magnetURI}&tr=${encodeURIComponent('http://tracker.local:80')}&tr=${encodeURIComponent('http://tracker.local:81')}` t.equal(torrent.magnetURI, expectedMagnetURI) t.deepEqual(torrent.announce, ['http://tracker.local:80', 'http://tracker.local:81']) - client.remove(fixtures.leaves.parsedTorrent, function (err) { t.error(err, 'torrent destroyed') }) + client.remove(fixtures.leaves.parsedTorrent, err => { t.error(err, 'torrent destroyed') }) t.equal(client.torrents.length, 0) - client.destroy(function (err) { t.error(err, 'client destroyed') }) + client.destroy(err => { t.error(err, 'client destroyed') }) }) }) -test('client.add: invalid torrent id: empty string', function (t) { +test('client.add: invalid torrent id: empty string', t => { t.plan(3) const client = new WebTorrent({ dht: false, tracker: false, lsd: false }) - client.on('error', function (err) { + client.on('error', err => { t.ok(err instanceof Error) - t.ok(err.message.indexOf('Invalid torrent identifier') >= 0) + t.ok(err.message.includes('Invalid torrent identifier')) - client.destroy(function (err) { t.error(err, 'client destroyed') }) + client.destroy(err => { t.error(err, 'client destroyed') }) }) - client.on('warning', function (err) { t.fail(err) }) + client.on('warning', err => { t.fail(err) }) client.add('') }) -test('client.add: invalid torrent id: short buffer', function (t) { +test('client.add: invalid torrent id: short buffer', t => { t.plan(3) const client = new WebTorrent({ dht: false, tracker: false, lsd: false }) - client.on('error', function (err) { + client.on('error', err => { t.ok(err instanceof Error) - t.ok(err.message.indexOf('Invalid torrent identifier') >= 0) + t.ok(err.message.includes('Invalid torrent identifier')) - client.destroy(function (err) { t.error(err, 'client destroyed') }) + client.destroy(err => { t.error(err, 'client destroyed') }) }) - client.on('warning', function (err) { t.fail(err) }) + client.on('warning', err => { t.fail(err) }) client.add(Buffer.from('abc')) }) -test('client.add: paused torrent', function (t) { +test('client.add: paused torrent', t => { t.plan(5) const client = new WebTorrent({ dht: false, tracker: false, lsd: false }) - client.on('error', (err) => t.fail(err)) - client.on('warning', (err) => t.fail(err)) + client.on('error', (err) => { t.fail(err) }) + client.on('warning', (err) => { t.fail(err) }) const torrent = client.add(fixtures.leaves.magnetURI, { paused: true }) t.equal(client.torrents.length, 1) - torrent.on('infoHash', function () { + torrent.on('infoHash', () => { t.equal(torrent.paused, true) - client.remove(fixtures.leaves.magnetURI, function (err) { t.error(err, 'torrent destroyed') }) + client.remove(fixtures.leaves.magnetURI, err => { t.error(err, 'torrent destroyed') }) t.equal(client.torrents.length, 0) - client.destroy(function (err) { t.error(err, 'client destroyed') }) + client.destroy(err => { t.error(err, 'client destroyed') }) }) }) -- cgit v1.2.3