Skip to content
This repository has been archived by the owner on Oct 11, 2022. It is now read-only.

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelgssa committed Sep 28, 2019
1 parent 704638b commit 16872a3
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 52 deletions.
2 changes: 1 addition & 1 deletion karma.conf.js
Expand Up @@ -25,7 +25,7 @@ module.exports = config => {
singleRun: true,
webpack: webpackConfig({
development: true,
test: true
test: true
})
};

Expand Down
64 changes: 64 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion 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`,
Expand Down
11 changes: 11 additions & 0 deletions 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;
4 changes: 2 additions & 2 deletions test/BrowserStorage.js
Expand Up @@ -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();
});

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

Expand Down
2 changes: 1 addition & 1 deletion test/ItemParser.js
Expand Up @@ -78,4 +78,4 @@ describe(`ItemParser`, () => {
done();
}, 500);
});
});
});
22 changes: 10 additions & 12 deletions test/Oauth.js
@@ -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(() => {
Expand All @@ -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});
});
Expand All @@ -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();
Expand All @@ -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();
};
Expand All @@ -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();
};
Expand All @@ -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" }`);
Expand All @@ -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();
};
Expand Down

0 comments on commit 16872a3

Please sign in to comment.