diff --git a/karma.conf.js b/karma.conf.js index 7a9cab6..dd916ef 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -25,7 +25,7 @@ module.exports = config => { singleRun: true, webpack: webpackConfig({ development: true, - test: true + test: true }) }; diff --git a/package-lock.json b/package-lock.json index 4c15b7a..1cdabc3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5211,6 +5211,64 @@ "integrity": "sha1-lpGEQOMEGnpBT4xS48V06zw+HgU=", "dev": true }, + "fake-fetch": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/fake-fetch/-/fake-fetch-2.3.0.tgz", + "integrity": "sha512-xKAhk6J9ipci/sS6AAycLK8ix/0+455M6G1aLdzSg+nKKTGOQ9QarVcGRsh2vljTYWmpZ5FK0OLgW2Ul7Lv9dQ==", + "dev": true, + "requires": { + "promise": "^8.0.1", + "sinon": "^4.5.0", + "whatwg-fetch": "^2.0.4" + }, + "dependencies": { + "@sinonjs/formatio": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@sinonjs/formatio/-/formatio-2.0.0.tgz", + "integrity": "sha512-ls6CAMA6/5gG+O/IdsBcblvnd8qcO/l1TYoNeAzp3wcISOxlPXQEus0mLcdwazEkWjaBdaJ3TaxmNgCLWwvWzg==", + "dev": true, + "requires": { + "samsam": "1.3.0" + } + }, + "lolex": { + "version": "2.7.5", + "resolved": "https://registry.npmjs.org/lolex/-/lolex-2.7.5.tgz", + "integrity": "sha512-l9x0+1offnKKIzYVjyXU2SiwhXDLekRzKyhnbyldPHvC7BvLPVpdNUNR2KeMAiCN2D/kLNttZgQD5WjSxuBx3Q==", + "dev": true + }, + "promise": { + "version": "8.0.3", + "resolved": "https://registry.npmjs.org/promise/-/promise-8.0.3.tgz", + "integrity": "sha512-HeRDUL1RJiLhyA0/grn+PTShlBAcLuh/1BJGtrvjwbvRDCTLLMEz9rOGCV+R3vHY4MixIuoMEd9Yq/XvsTPcjw==", + "dev": true, + "requires": { + "asap": "~2.0.6" + } + }, + "sinon": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/sinon/-/sinon-4.5.0.tgz", + "integrity": "sha512-trdx+mB0VBBgoYucy6a9L7/jfQOmvGeaKZT4OOJ+lPAtI8623xyGr8wLiE4eojzBS8G9yXbhx42GHUOVLr4X2w==", + "dev": true, + "requires": { + "@sinonjs/formatio": "^2.0.0", + "diff": "^3.1.0", + "lodash.get": "^4.4.2", + "lolex": "^2.2.0", + "nise": "^1.2.0", + "supports-color": "^5.1.0", + "type-detect": "^4.0.5" + } + }, + "whatwg-fetch": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-2.0.4.tgz", + "integrity": "sha512-dcQ1GWpOD/eEQ97k66aiEVpNnapVj90/+R+SXTPYGHpYBBypfKJEQjLrvMZ7YXbKm21gXd4NcuxUTjiv1YtLng==", + "dev": true + } + } + }, "fancy-log": { "version": "1.3.3", "resolved": "https://registry.npmjs.org/fancy-log/-/fancy-log-1.3.3.tgz", @@ -10551,6 +10609,12 @@ "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" }, + "samsam": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/samsam/-/samsam-1.3.0.tgz", + "integrity": "sha512-1HwIYD/8UlOtFS3QO3w7ey+SdSDFE4HRNLZoZRYVQefrOY3l17epswImeB1ijgJFQJodIaHcwkp3r/myBjFVbg==", + "dev": true + }, "scheduler": { "version": "0.13.6", "resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.13.6.tgz", diff --git a/package.json b/package.json index bc524aa..f578b5d 100644 --- a/package.json +++ b/package.json @@ -30,16 +30,16 @@ }, "devDependencies": { "@babel/core": "^7.6.0", + "@babel/plugin-transform-runtime": "^7.6.0", "@babel/preset-env": "^7.6.0", "@babel/preset-flow": "^7.0.0", "@babel/preset-react": "^7.0.0", - "@babel/plugin-transform-runtime": "^7.6.0", "@babel/runtime": "^7.6.0", "@octokit/rest": "^16.28.9", "babel-eslint": "^10.0.3", "babel-loader": "^8.0.6", - "babel-preset-minify": "^0.5.1", "babel-plugin-istanbul": "^5.2.0", + "babel-preset-minify": "^0.5.1", "chai": "^4.2.0", "clean-webpack-plugin": "^3.0.0", "coveralls": "^3.0.6", @@ -50,6 +50,7 @@ "eslint-plugin-mocha": "^6.1.0", "eslint-plugin-react": "^7.14.3", "eslint-plugin-sinon": "^0.2.0", + "fake-fetch": "^2.3.0", "file-loader": "^4.2.0", "fs-extra": "^8.1.0", "gulp": "^4.0.2", @@ -58,8 +59,8 @@ "karma-chai": "^0.1.0", "karma-chrome-launcher": "^3.1.0", "karma-cli": "^2.0.0", - "karma-coveralls": "^2.1.0", "karma-coverage": "^2.0.1", + "karma-coveralls": "^2.1.0", "karma-firefox-launcher": "^1.2.0", "karma-mocha": "^1.3.0", "karma-webpack": "^4.0.2", diff --git a/src/settings.js b/src/settings.js index 53a5712..f81cb58 100644 --- a/src/settings.js +++ b/src/settings.js @@ -1,5 +1,5 @@ const settings = { - browser: browser.runtime.getURL('/').match(/^moz/) ? 'firefox' : 'chrome', + browser: typeof browser !== 'undefined' && (browser.runtime.getURL('/').match(/^moz/) ? 'firefox' : 'chrome'), authorizeUri: `https://trakt.tv/oauth/authorize`, apiUri: `https://api.trakt.tv`, redirectUri: `https://www.netflix.com/Activate`, diff --git a/test-helpers/fake-fetch.js b/test-helpers/fake-fetch.js new file mode 100644 index 0000000..890264b --- /dev/null +++ b/test-helpers/fake-fetch.js @@ -0,0 +1,11 @@ +import fakeFetch from 'fake-fetch'; + +fakeFetch.withArgs = function (url) { + return { + respondWith: function (data, options) { + return window.fetch.withArgs(url).returns(Promise.resolve(new Response(data, options))); + }, + }; +}; + +export default fakeFetch; \ No newline at end of file diff --git a/test/BrowserStorage.js b/test/BrowserStorage.js index ad6d47a..6902948 100644 --- a/test/BrowserStorage.js +++ b/test/BrowserStorage.js @@ -38,7 +38,7 @@ describe(`BrowserStorage`, () => { await BrowserStorage.sync(); expect(browser.storage.sync.get.callCount).to.equal(1); expect(BrowserStorage.set.callCount).to.equal(2); - BrowserStorage.isSyncAvailable.restore(); + BrowserStorage.isSyncAvailable.restore(); BrowserStorage.set.restore(); }); @@ -48,7 +48,7 @@ describe(`BrowserStorage`, () => { await BrowserStorage.sync(); expect(browser.storage.sync.get.callCount).to.equal(0); expect(BrowserStorage.set.callCount).to.equal(0); - BrowserStorage.isSyncAvailable.restore(); + BrowserStorage.isSyncAvailable.restore(); BrowserStorage.set.restore(); }); diff --git a/test/ItemParser.js b/test/ItemParser.js index 89a96ee..9db7ac6 100644 --- a/test/ItemParser.js +++ b/test/ItemParser.js @@ -78,4 +78,4 @@ describe(`ItemParser`, () => { done(); }, 500); }); -}); +}); \ No newline at end of file diff --git a/test/Oauth.js b/test/Oauth.js index ffb109e..3922d6c 100644 --- a/test/Oauth.js +++ b/test/Oauth.js @@ -1,4 +1,5 @@ import browser from 'sinon-chrome'; +import fakeFetch from '../test-helpers/fake-fetch'; import sinon from 'sinon'; import Settings from '../src/settings.js'; import Oauth from '../src/class/Oauth'; @@ -6,21 +7,18 @@ import Shared from '../src/class/Shared'; Shared.setBackgroundPage(true); -let server = null; - describe(`Oauth`, () => { before(() => { window.browser = browser; }); beforeEach(() => { - server = sinon.fakeServer.create(); - server.autoRespond = true; + fakeFetch.install(); }); afterEach(() => { browser.flush(); - server.restore(); + fakeFetch.restore(); }); after(() => { @@ -35,7 +33,7 @@ describe(`Oauth`, () => { it(`requestToken() resolves with success`, async () => { browser.storage.local.get.withArgs(`data`).resolves({data: {access_token: `12345abcde`}}); browser.storage.local.set.withArgs({data: {access_token: `12345abcde`}}).resolves(); - server.respondWith(`POST`, `${Settings.apiUri}/oauth/token`, [200, {[`Content-Type`]: `application/json`}, `{"access_token": "12345abcde"}`]); + fakeFetch.withArgs(`${Settings.apiUri}/oauth/token`).respondWith(`{"access_token": "12345abcde"}`, { status: 200 }); const result = await Oauth.requestToken(); expect(result).to.deep.equal({error: false, response: `{"access_token": "12345abcde"}`}); }); @@ -43,7 +41,7 @@ describe(`Oauth`, () => { it(`requestToken() resolves with error`, async () => { browser.storage.local.get.withArgs(`data`).resolves({data: {access_token: `12345abcde`}}); browser.storage.local.remove.withArgs(`data`).resolves(); - server.respondWith(`POST`, `${Settings.apiUri}/oauth/token`, [401, {[`Content-Type`]: `application/json`}, `{"error": "invalid_grant"}`]); + fakeFetch.withArgs(`${Settings.apiUri}/oauth/token`).respondWith(`{"error": "invalid_grant"}`, { status: 401 }); const result = await Oauth.requestToken({}); expect(result).to.deep.equal({error: true, response: `{"error": "invalid_grant"}`, status: 401}); }); @@ -67,7 +65,7 @@ describe(`Oauth`, () => { it(`getUserInfo() calls success callback`, done => { browser.storage.local.get.withArgs(`data`).resolves({data: {access_token: `12345abcde`}}); - server.respondWith(`GET`, `${Settings.apiUri}/users/me`, [200, {[`Content-Type`]: `application/json`}, `{"username": "FooBar"}`]); + fakeFetch.withArgs(`${Settings.apiUri}/users/me`).respondWith(`{"username": "FooBar"}`, { status: 200 }); const success = response => { expect(response).to.equal(`{"username": "FooBar"}`); done(); @@ -80,7 +78,7 @@ describe(`Oauth`, () => { it(`getUserInfo() calls error callback`, done => { browser.storage.local.get.withArgs(`data`).resolves({data: {access_token: `12345abcde`}}); - server.respondWith(`GET`, `${Settings.apiUri}/users/me`, [400, {[`Content-Type`]: `application/json`}, `{"error": "Bad Request"}`]); + fakeFetch.withArgs(`${Settings.apiUri}/users/me`).respondWith(`{"error": "Bad Request"}`, { status: 400 }); const success = () => { done.fail(); }; @@ -94,7 +92,7 @@ describe(`Oauth`, () => { it(`getUserInfo() calls error callback if status is 401 and refresh_token is empty`, done => { browser.storage.local.get.withArgs(`data`).resolves({}); - server.respondWith(`GET`, `${Settings.apiUri}/users/me`, [401, {[`Content-Type`]: `application/json`}, `{"error": "invalid_grant"}`]); + fakeFetch.withArgs(`${Settings.apiUri}/users/me`).respondWith(`{"error": "invalid_grant"}`, { status: 401 }); const success = () => { done.fail(); }; @@ -111,7 +109,7 @@ describe(`Oauth`, () => { sinon.stub(Oauth, `requestRefreshToken`).withArgs(`54321edcba`).resolves({ response: `{ "access_token": "12345abcde", "refresh_token": "54321edcba" }` }); - server.respondWith(`GET`, `${Settings.apiUri}/users/me`, [401, {[`Content-Type`]: `application/json`}, `{"error": "invalid_grant"}`]); + fakeFetch.withArgs(`${Settings.apiUri}/users/me`).respondWith(`{"error": "invalid_grant"}`, { status: 401 }); const success = response => { expect(Oauth.requestRefreshToken.callCount).to.equal(1); expect(response).to.equal(`{ "access_token": "12345abcde", "refresh_token": "54321edcba" }`); @@ -131,7 +129,7 @@ describe(`Oauth`, () => { response: `{ "error": "invalid_grant" }`, status: 401 }); - server.respondWith(`GET`, `${Settings.apiUri}/users/me`, [401, {[`Content-Type`]: `application/json`}, `{"error": "invalid_grant"}`]); + fakeFetch.withArgs(`${Settings.apiUri}/users/me`).respondWith(`{"error": "invalid_grant"}`, { status: 401 }); const success = () => { done.fail(); }; diff --git a/test/Search.js b/test/Search.js index c0db3a7..302e0e8 100644 --- a/test/Search.js +++ b/test/Search.js @@ -1,4 +1,5 @@ import browser from 'sinon-chrome'; +import fakeFetch from '../test-helpers/fake-fetch'; import sinon from 'sinon'; import Settings from '../src/settings.js'; import Search from '../src/class/Search'; @@ -38,8 +39,6 @@ const episodeSearch = new Search({item: madMen}); const episodeSearchByTitle = new Search({item: narcos}); const collectionSearch = new Search({item: comedians}); -let server = null; - browser.flush(); delete window.browser; @@ -50,12 +49,11 @@ describe(`Search`, () => { }); beforeEach(() => { - server = sinon.fakeServer.create(); - server.autoRespond = true; + fakeFetch.install(); }); afterEach(() => { - server.restore(); + fakeFetch.restore(); }); after(() => { @@ -74,7 +72,7 @@ describe(`Search`, () => { }); it(`findItem() returns first search result`, done => { - server.respondWith(`GET`, `${Settings.apiUri}/search/${madMen.type}?query=${encodeURIComponent(madMen.title)}`, [200, {[`Content-Type`]: `application/json`}, `[{ "show": { "title": "Mad Men" } }, { "show": { "title": "Mad Women" } }]`]); + fakeFetch.withArgs(`${Settings.apiUri}/search/${madMen.type}?query=${encodeURIComponent(madMen.title)}`).respondWith(`[{ "show": { "title": "Mad Men" } }, { "show": { "title": "Mad Women" } }]`, { status: 200 }); const success = response => { expect(response).to.deep.equal({show: {title: `Mad Men`}}); done(); @@ -86,7 +84,7 @@ describe(`Search`, () => { }); it(`findItem() returns exact match for movies with same title from different years`, done => { - server.respondWith(`GET`, `${Settings.apiUri}/search/${rocky.type}?query=${encodeURIComponent(rocky.title)}`, [200, {[`Content-Type`]: `application/json`}, `[{ "movie": { "title": "Rocky", "year": 2000 } }, { "movie": { "title": "Rocky", "year": 2005 } }]`]); + fakeFetch.withArgs(`${Settings.apiUri}/search/${rocky.type}?query=${encodeURIComponent(rocky.title)}`).respondWith(`[{ "movie": { "title": "Rocky", "year": 2000 } }, { "movie": { "title": "Rocky", "year": 2005 } }]`, { status: 200 }); const success = response => { expect(response).to.deep.equal({movie: {title: `Rocky`, year: 2005}}); done(); @@ -98,7 +96,7 @@ describe(`Search`, () => { }); it(`findItem() returns error callback when data is undefined`, done => { - server.respondWith(`GET`, `${Settings.apiUri}/search/${rocky.type}?query=${encodeURIComponent(rocky.title)}`, [200, {[`Content-Type`]: `application/json`}, `[]`]); + fakeFetch.withArgs(`${Settings.apiUri}/search/${rocky.type}?query=${encodeURIComponent(rocky.title)}`).respondWith(`[]`, { status: 200 }); const success = () => { done.fail(); }; @@ -110,7 +108,7 @@ describe(`Search`, () => { }); it(`findItem() returns error callback`, done => { - server.respondWith(`GET`, `${Settings.apiUri}/search/${rocky.type}?query=${encodeURIComponent(rocky.title)}`, [400, {[`Content-Type`]: `application/json`}, `{ "errors": "Bad Request" }`]); + fakeFetch.withArgs(`${Settings.apiUri}/search/${rocky.type}?query=${encodeURIComponent(rocky.title)}`).respondWith(`{ "errors": "Bad Request" }`, { status: 400 }); const success = () => { done.fail(); }; @@ -140,8 +138,8 @@ describe(`Search`, () => { }); it(`findEpisode() returns first search result`, done => { - server.respondWith(`GET`, `${Settings.apiUri}/search/${madMen.type}?query=${encodeURIComponent(madMen.title)}`, [200, {[`Content-Type`]: `application/json`}, `[{ "show": { "title": "Mad Men", "ids": { "slug": "mad-men" } } }]`]); - server.respondWith(`GET`, `${Settings.apiUri}/shows/mad-men/seasons/${madMen.season}/episodes/${madMen.episode}?extended=images`, [200, {[`Content-Type`]: `application/json`}, `{ "title": "Ladies Room", "season": 1, "number": 2 }`]); + fakeFetch.withArgs(`${Settings.apiUri}/search/${madMen.type}?query=${encodeURIComponent(madMen.title)}`).respondWith(`[{ "show": { "title": "Mad Men", "ids": { "slug": "mad-men" } } }]`, { status: 200 }); + fakeFetch.withArgs(`${Settings.apiUri}/shows/mad-men/seasons/${madMen.season}/episodes/${madMen.episode}?extended=images`).respondWith(`{ "title": "Ladies Room", "season": 1, "number": 2 }`, { status: 200 }); const success = response => { expect(response).to.deep.equal({ title: `Ladies Room`, season: 1, number: 2, @@ -158,8 +156,8 @@ describe(`Search`, () => { }); it(`findEpisode() returns first search result with same title when item is a collection`, done => { - server.respondWith(`GET`, `${Settings.apiUri}/search/${comedians.type}?query=${encodeURIComponent(comedians.title)}`, [200, {[`Content-Type`]: `application/json`}, `[{ "show": { "title": "Comedians in Cars Getting Coffee", "ids": { "slug": "comedians-in-cars-getting-coffee" } } }]`]); - server.respondWith(`GET`, `${Settings.apiUri}/search/episode?query=${encodeURIComponent(comedians.epTitle)}`, [200, {[`Content-Type`]: `application/json`}, `[{ "type": "episode", "episode": { "season": 10, "number": 1, "title": "Zach Galifianakis: From The Third Reich To You" } }]`]); + fakeFetch.withArgs(`${Settings.apiUri}/search/${comedians.type}?query=${encodeURIComponent(comedians.title)}`).respondWith(`[{ "show": { "title": "Comedians in Cars Getting Coffee", "ids": { "slug": "comedians-in-cars-getting-coffee" } } }]`, { status: 200 }); + fakeFetch.withArgs(`${Settings.apiUri}/search/episode?query=${encodeURIComponent(comedians.epTitle)}`).respondWith(`[{ "type": "episode", "episode": { "season": 10, "number": 1, "title": "Zach Galifianakis: From The Third Reich To You" } }]`, { status: 200 }); const success = response => { expect(response).to.deep.equal({ title: `Zach Galifianakis: From The Third Reich To You`, season: 10, number: 1, @@ -176,8 +174,8 @@ describe(`Search`, () => { }); it(`findEpisode() returns first search result with same title`, done => { - server.respondWith(`GET`, `${Settings.apiUri}/search/${narcos.type}?query=${encodeURIComponent(narcos.title)}`, [200, {[`Content-Type`]: `application/json`}, `[{ "show": { "title": "Narcos", "ids": { "slug": "narcos" } } }]`]); - server.respondWith(`GET`, `${Settings.apiUri}/shows/narcos/seasons/${narcos.season}?extended=images`, [200, {[`Content-Type`]: `application/json`}, `[{ "season": 1, "number": 1, "title": "Descenso" }, { "season": 1, "number": 2, "title": "The Sword of Simón Bolívar" }]`]); + fakeFetch.withArgs(`${Settings.apiUri}/search/${narcos.type}?query=${encodeURIComponent(narcos.title)}`).respondWith(`[{ "show": { "title": "Narcos", "ids": { "slug": "narcos" } } }]`, { status: 200 }); + fakeFetch.withArgs(`${Settings.apiUri}/shows/narcos/seasons/${narcos.season}?extended=images`).respondWith(`[{ "season": 1, "number": 1, "title": "Descenso" }, { "season": 1, "number": 2, "title": "The Sword of Simón Bolívar" }]`, { status: 200 }); const success = response => { expect(response).to.deep.equal({ title: `The Sword of Simón Bolívar`, season: 1, number: 2, @@ -194,8 +192,8 @@ describe(`Search`, () => { }); it(`findEpisode() returns error callback when an episode with same title was not found`, done => { - server.respondWith(`GET`, `${Settings.apiUri}/search/${narcos.type}?query=${encodeURIComponent(narcos.title)}`, [200, {[`Content-Type`]: `application/json`}, `[{ "show": { "title": "Narcos", "ids": { "slug": "narcos" } } }]`]); - server.respondWith(`GET`, `${Settings.apiUri}/shows/narcos/seasons/${narcos.season}?extended=images`, [200, {[`Content-Type`]: `application/json`}, `[{ "season": 1, "number": 1, "title": "Descenso" }, { "season": 1, "number": 3, "title": "The Men of Always" }]`]); + fakeFetch.withArgs(`${Settings.apiUri}/search/${narcos.type}?query=${encodeURIComponent(narcos.title)}`).respondWith(`[{ "show": { "title": "Narcos", "ids": { "slug": "narcos" } } }]`, { status: 200 }); + fakeFetch.withArgs(`${Settings.apiUri}/shows/narcos/seasons/${narcos.season}?extended=images`).respondWith(`[{ "season": 1, "number": 1, "title": "Descenso" }, { "season": 1, "number": 3, "title": "The Men of Always" }]`, { status: 200 }); const success = () => { done.fail(); }; @@ -212,7 +210,7 @@ describe(`Search`, () => { }); it(`findEpisode() returns error callback on first request`, done => { - server.respondWith(`GET`, `${Settings.apiUri}/search/${madMen.type}?query=${encodeURIComponent(madMen.title)}`, [400, {[`Content-Type`]: `application/json`}, `{ "errors": "Bad Request" }`]); + fakeFetch.withArgs(`${Settings.apiUri}/search/${madMen.type}?query=${encodeURIComponent(madMen.title)}`).respondWith(`{ "errors": "Bad Request" }`, { status: 400 }); const success = () => { done.fail(); }; @@ -230,8 +228,8 @@ describe(`Search`, () => { }); it(`findEpisode() returns error callback on second request`, done => { - server.respondWith(`GET`, `${Settings.apiUri}/search/${madMen.type}?query=${encodeURIComponent(madMen.title)}`, [200, {[`Content-Type`]: `application/json`}, `[{ "show": { "title": "Mad Men", "ids": { "slug": "mad-men" } } }]`]); - server.respondWith(`GET`, `${Settings.apiUri}/shows/mad-men/seasons/${madMen.season}/episodes/${madMen.episode}?extended=images`, [400, {[`Content-Type`]: `application/json`}, `{ "errors": "Bad Request" }`]); + fakeFetch.withArgs(`${Settings.apiUri}/search/${madMen.type}?query=${encodeURIComponent(madMen.title)}`).respondWith(`[{ "show": { "title": "Mad Men", "ids": { "slug": "mad-men" } } }]`, { status: 200 }); + fakeFetch.withArgs(`${Settings.apiUri}/shows/mad-men/seasons/${madMen.season}/episodes/${madMen.episode}?extended=images`).respondWith(`{ "errors": "Bad Request" }`, { status: 400 }); const success = () => { done.fail(); }; diff --git a/test/content/Scrobble.js b/test/content/Scrobble.js index eff6ceb..928b0b9 100644 --- a/test/content/Scrobble.js +++ b/test/content/Scrobble.js @@ -1,5 +1,5 @@ import browser from 'sinon-chrome'; -import sinon from 'sinon'; +import fakeFetch from '../../test-helpers/fake-fetch'; import Settings from '../../src/settings'; import Scrobble from '../../src/class/content/Scrobble'; import NetflixTestUtils from '../../test-helpers/NetflixTestHelper'; @@ -25,8 +25,6 @@ const scrobbleShow = new Scrobble({ } }); -let server = null; - browser.flush(); delete window.browser; @@ -37,13 +35,12 @@ describe(`Scrobble`, () => { beforeEach(() => { document.body.innerHTML = ``; - server = sinon.fakeServer.create(); - server.autoRespond = true; + fakeFetch.install(); }); afterEach(() => { browser.flush(); - server.restore(); + fakeFetch.restore(); }); after(() => { @@ -93,7 +90,7 @@ describe(`Scrobble`, () => { browser.storage.local.get.withArgs(`data`).resolves({data: {access_token: `12345abcde`}}); browser.storage.local.get.withArgs(`options`).resolves({options: {showNotifications: true}}); browser.i18n.getMessage.withArgs(`scrobbleStarted`).returns(`scrobbleStarted`); - server.respondWith(`POST`, `${Settings.apiUri}/scrobble/start`, [200, {[`Content-Type`]: `application/json`}, ``]); + fakeFetch.withArgs(`${Settings.apiUri}/scrobble/start`).respondWith(``, { status: 200 }); scrobbleShow.success = () => { expect(browser.runtime.sendMessage.callCount).to.equal(1); expect(browser.runtime.sendMessage.args[0]).to.deep.equal([{ @@ -113,7 +110,7 @@ describe(`Scrobble`, () => { browser.storage.local.get.withArgs(`data`).resolves({data: {access_token: `12345abcde`}}); browser.storage.local.get.withArgs(`options`).resolves({options: {showNotifications: true}}); browser.i18n.getMessage.withArgs(`couldNotScrobble`).returns(`couldNotScrobble`); - server.respondWith(`POST`, `${Settings.apiUri}/scrobble/start`, [500, {[`Content-Type`]: `application/json`}, ``]); + fakeFetch.withArgs(`${Settings.apiUri}/scrobble/start`).respondWith(``, { status: 500 }); scrobbleShow.success = () => { done.fail(); }; @@ -132,7 +129,7 @@ describe(`Scrobble`, () => { browser.storage.local.get.withArgs(`data`).resolves({data: {access_token: `12345abcde`}}); browser.storage.local.get.withArgs(`options`).resolves({options: {showNotifications: true}}); browser.i18n.getMessage.withArgs(`scrobblePaused`).returns(`scrobblePaused`); - server.respondWith(`POST`, `${Settings.apiUri}/scrobble/pause`, [200, {[`Content-Type`]: `application/json`}, ``]); + fakeFetch.withArgs(`${Settings.apiUri}/scrobble/pause`).respondWith(``, { status: 200 }); scrobbleShow.success = () => { expect(browser.runtime.sendMessage.callCount).to.equal(1); expect(browser.runtime.sendMessage.args[0]).to.deep.equal([{ @@ -152,7 +149,7 @@ describe(`Scrobble`, () => { browser.storage.local.get.withArgs(`data`).resolves({data: {access_token: `12345abcde`}}); browser.storage.local.get.withArgs(`options`).resolves({options: {showNotifications: true}}); browser.i18n.getMessage.withArgs(`couldNotScrobble`).returns(`couldNotScrobble`); - server.respondWith(`POST`, `${Settings.apiUri}/scrobble/pause`, [500, {[`Content-Type`]: `application/json`}, ``]); + fakeFetch.withArgs(`${Settings.apiUri}/scrobble/pause`).respondWith(``, { status: 500 }); scrobbleShow.success = () => { done.fail(); }; @@ -171,7 +168,7 @@ describe(`Scrobble`, () => { browser.storage.local.get.withArgs(`data`).resolves({data: {access_token: `12345abcde`}}); browser.storage.local.get.withArgs(`options`).resolves({options: {showNotifications: true}}); browser.i18n.getMessage.withArgs(`scrobbleStopped`).returns(`scrobbleStopped`); - server.respondWith(`POST`, `${Settings.apiUri}/scrobble/stop`, [200, {[`Content-Type`]: `application/json`}, ``]); + fakeFetch.withArgs(`${Settings.apiUri}/scrobble/stop`).respondWith(``, { status: 200 }); scrobbleShow.success = () => { expect(browser.runtime.sendMessage.callCount).to.equal(1); expect(browser.runtime.sendMessage.args[0]).to.deep.equal([{ @@ -191,7 +188,7 @@ describe(`Scrobble`, () => { browser.storage.local.get.withArgs(`data`).resolves({data: {access_token: `12345abcde`}}); browser.storage.local.get.withArgs(`options`).resolves({options: {showNotifications: true}}); browser.i18n.getMessage.withArgs(`couldNotScrobble`).returns(`couldNotScrobble`); - server.respondWith(`POST`, `${Settings.apiUri}/scrobble/stop`, [500, {[`Content-Type`]: `application/json`}, ``]); + fakeFetch.withArgs(`${Settings.apiUri}/scrobble/stop`).respondWith(``, { status: 500 }); scrobbleShow.success = () => { done.fail(); };