Skip to content

Commit

Permalink
remove fetch-mock
Browse files Browse the repository at this point in the history
  • Loading branch information
Mika- committed Jun 28, 2024
1 parent 0e0c99d commit ecc87f2
Show file tree
Hide file tree
Showing 12 changed files with 869 additions and 990 deletions.
858 changes: 211 additions & 647 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
"@types/chai": "^4.2.12",
"@types/chrome": "^0.0.268",
"@types/mocha": "^10.0.1",
"@types/sinon": "^17.0.3",
"chai": "^5.1.0",
"fetch-mock": "^9.10.4",
"jsdom": "^24.0.0",
"mocha": "^10.0.0",
"node-fetch": "^2.6.5",
"sinon": "^18.0.0",
"sinon-chrome": "^3.0.1",
"web-ext": "^8.2.0"
}
Expand Down
17 changes: 17 additions & 0 deletions test/env.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {expect} from 'chai';
import sinon from 'sinon';
import chrome from 'sinon-chrome';
import JSDOM from 'jsdom';

Expand All @@ -9,3 +10,19 @@ const jsdom = new JSDOM.JSDOM();
global.DOMParser = jsdom.window.DOMParser;
global.FileReader = jsdom.window.FileReader;
global.Headers = jsdom.window.Headers;

global.FormData = class FormData {
append(name, value) {
this[name] = value;
}
get (name) {
return this[name];
}
};

export const mochaHooks = {
afterEach() {
sinon.restore();
chrome.flush();
},
};
4 changes: 2 additions & 2 deletions test/helpers.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import fsPromises from 'fs/promises';
import { promises } from 'node:fs';
import JSDOM from 'jsdom';

export const getTestTorrent = async () => {
const jsdom = new JSDOM.JSDOM();

return new jsdom.window.Blob([await fsPromises.readFile('./test/test.torrent')], {
return new jsdom.window.Blob([await promises.readFile('./test/test.torrent')], {
type: 'application/x-bittorrent',
});
}
Expand Down
49 changes: 28 additions & 21 deletions test/lib/cloudtorrent.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fetchMock from 'fetch-mock';

import sinon from 'sinon';
import {getTestTorrent} from '../helpers.js';
import CloudTorrentApi from '../../src/lib/cloudtorrent.js';

describe('CloudTorrentApi', () => {
/** @type {CloudTorrentApi} */
let instance;

before(() => {
Expand All @@ -14,11 +14,6 @@ describe('CloudTorrentApi', () => {
});
});

afterEach(() => {
chrome.flush();
fetchMock.reset();
});

it('Login', async () => {
await instance.logIn();

Expand Down Expand Up @@ -78,35 +73,46 @@ describe('CloudTorrentApi', () => {
});

it('Add torrent', async () => {
fetchMock.postOnce('https://example.com:1234/api/torrentfile', {
const fetchStub = sinon.stub(global, 'fetch');

fetchStub.resolves({
ok: true,
status: 200,
body: 'OK',
text: () => Promise.resolve('OK'),
});

const torrentFile = await getTestTorrent();

await instance.addTorrent(torrentFile);

expect(fetchMock.calls().length).to.equal(1);
expect(fetchMock.lastOptions().method).to.equal('POST');
expect(fetchMock.lastOptions().body).to.equal(torrentFile);
expect(fetchStub.calledOnce).to.equal(true);
expect(fetchStub.lastCall.args[0]).to.equal('https://example.com:1234/api/torrentfile');
expect(fetchStub.lastCall.args[1].method).to.equal('POST');
expect(fetchStub.lastCall.args[1].body).to.equal(torrentFile);
});

it('Add torrent url', async () => {
fetchMock.postOnce('https://example.com:1234/api/magnet', {
const fetchStub = sinon.stub(global, 'fetch');

fetchStub.resolves({
ok: true,
status: 200,
body: 'OK',
text: () => Promise.resolve('OK'),
});

await instance.addTorrentUrl('https://example.com/test.torrent', {});

expect(fetchMock.calls().length).to.equal(1);
expect(fetchMock.lastOptions().method).to.equal('POST');
expect(fetchMock.lastOptions().body).to.equal('https://example.com/test.torrent');
expect(fetchStub.calledOnce).to.equal(true);
expect(fetchStub.lastCall.args[0]).to.equal('https://example.com:1234/api/magnet');
expect(fetchStub.lastCall.args[1].method).to.equal('POST');
expect(fetchStub.lastCall.args[1].body).to.equal('https://example.com/test.torrent');
});

it('Add torrent url fail', async () => {
fetchMock.postOnce('https://example.com:1234/api/magnet', {
const fetchStub = sinon.stub(global, 'fetch');

fetchStub.resolves({
ok: false,
status: 400,
});

Expand All @@ -116,8 +122,9 @@ describe('CloudTorrentApi', () => {
expect(e).to.be.a('Error');
}

expect(fetchMock.calls().length).to.equal(1);
expect(fetchMock.lastOptions().method).to.equal('POST');
expect(fetchMock.lastOptions().body).to.equal('https://example.com/not_a_torrent_file');
expect(fetchStub.calledOnce).to.equal(true);
expect(fetchStub.lastCall.args[0]).to.equal('https://example.com:1234/api/magnet');
expect(fetchStub.lastCall.args[1].body).to.equal('https://example.com/not_a_torrent_file');
expect(fetchStub.lastCall.args[1].method).to.equal('POST');
});
});
Loading

0 comments on commit ecc87f2

Please sign in to comment.