Skip to content
Permalink
Browse files

Fix tests

  • Loading branch information...
rafaelgssa committed Sep 28, 2019
1 parent 704638b commit 16872a3e804f4dff9105a8448e02fa5825df1dda
Showing with 121 additions and 52 deletions.
  1. +1 −1 karma.conf.js
  2. +64 −0 package-lock.json
  3. +4 −3 package.json
  4. +1 −1 src/settings.js
  5. +11 −0 test-helpers/fake-fetch.js
  6. +2 −2 test/BrowserStorage.js
  7. +1 −1 test/ItemParser.js
  8. +10 −12 test/Oauth.js
  9. +18 −20 test/Search.js
  10. +9 −12 test/content/Scrobble.js
@@ -25,7 +25,7 @@ module.exports = config => {
singleRun: true,
webpack: webpackConfig({
development: true,
test: true
test: true
})
};

Some generated files are not rendered by default. Learn more.

@@ -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",
@@ -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`,
@@ -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;
@@ -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();
});

@@ -78,4 +78,4 @@ describe(`ItemParser`, () => {
done();
}, 500);
});
});
});
@@ -1,26 +1,24 @@
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';
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,15 +33,15 @@ 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"}`});
});

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();
};

0 comments on commit 16872a3

Please sign in to comment.
You can’t perform that action at this time.