Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update unit tests #526

Merged
merged 5 commits into from Apr 14, 2020
Merged
Changes from all commits
Commits
File filter
Filter file types
Jump to
Jump to file
Failed to load files.

Always

Just for now

@@ -12,4 +12,4 @@ install:
script:
- node ./tools/licenses/fetchLicenses.js
- yarn lint
- yarn test.unit
- yarn test
@@ -31,7 +31,7 @@ You can also submit feature requests to the [issue tracker](../../issues). Befor
+ Make your pull request from "develop" in your fork to "develop" in the source
+ Use short, meaningful commit messages
+ Lint your code before committing and making a pull request (`npm run lint`)
+ Write unit tests for new features and make sure all tests are passing (`npm run test.unit`)
+ Write unit tests for new features and make sure all tests are passing (`npm run test`)
+ If your pull request has multiple commits or commits that are not meaningful, consider [squashing](https://git-scm.com/docs/git-rebase#_interactive_mode) them

#### Submitting Patches via Email
@@ -62,7 +62,7 @@ $ yarn test.snapshot

```sh
# Run unit tests
$ yarn test.unit
$ yarn test
```

```sh
@@ -16,5 +16,9 @@ module.exports = {
setupFiles: [
'./test/setup.js'
],
testURL: 'http://localhost'
roots: [
'<rootDir>/test/',
'<rootDir>/app/'
],
testURL: 'http://localhost',
};
@@ -16,7 +16,8 @@
"prebuild.dev": "node ./tools/licenses/fetchLicenses.js",
"prebuild.prod": "node ./tools/licenses/fetchLicenses.js",
"prebuild.watch": "node ./tools/licenses/fetchLicenses.js",
"test.unit": "cross-env BABEL_ENV=test jest",
"test": "cross-env BABEL_ENV=test jest",
"test.watch": "cross-env BABEL_ENV=test jest --watch",
"test.snapshot": "jest --updateSnapshot",
"lint": "eslint --ext .js,.jsx ./app ./src",
"lint.fix": "eslint --ext .js,.jsx ./app ./src --fix",
@@ -44,7 +45,6 @@
"dependencies": {
"@cliqz/adblocker-circumvention": "^1.5.0",
"@cliqz/url-parser": "^1.1.1",
"base64-js": "^1.3.1",
"browser-core": "https://github.com/cliqz-oss/browser-core/releases/download/v7.43.7/browser-core-7.43.7.tgz",
"classnames": "^2.2.5",
"d3": "^5.15.0",
@@ -66,16 +66,14 @@
"rsvp": "^4.8.5",
"spanan": "^2.0.0",
"ua-parser-js": "^0.7.21",
"underscore": "^1.9.2",
"whatwg-fetch": "^3.0.0"
"underscore": "^1.9.2"
},
"devDependencies": {
"@babel/core": "^7.8.3",
"@babel/plugin-proposal-class-properties": "^7.8.3",
"@babel/plugin-proposal-object-rest-spread": "^7.8.3",
"@babel/plugin-transform-modules-commonjs": "^7.8.3",
"@babel/preset-react": "^7.8.3",
"@babel/register": "^7.8.3",
"babel-eslint": "^10.0.3",
"babel-loader": "^8.0.6",
"clean-webpack-plugin": "^3.0.0",
@@ -105,14 +103,13 @@
"redux-mock-store": "^1.5.4",
"sass-loader": "^8.0.2",
"seamless-immutable": "^7.1.3",
"sinon": "^8.1.1",
"sinon-chrome": "^3.0.1",
"svg-url-loader": "^3.0.3",
"underscore-template-loader": "^1.0.0",
"url-loader": "^3.0.0",
"vendor-copy": "^2.0.0",
"webpack": "^4.41.5",
"webpack-cli": "^3.3.10",
"webpack-cli": "^3.3.11",
"webpack-shell-plugin": "^0.5.0"
}
}
@@ -13,22 +13,34 @@

// Set stubs for all chrome.* methods and properties
import chrome from 'sinon-chrome';

// Mock fetch calls
import { enableFetchMocks } from 'jest-fetch-mock';
// Set up Enzyme for React Snapshot testing
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

Enzyme.configure({ adapter: new Adapter() });
enableFetchMocks();

// Fake the translation function to only return the translation key
global.t = function(str) {
return str;
};

// Helper function to fake Fetch response
global.mockFetchResponse = function(responseCode, responseData) {
fetch.mockReturnValue(Promise.resolve(new Response(responseData, {
status: responseCode,
headers: {
'Content-type': 'application/json'
}
})));
};

// Create global stubs
global.chrome = chrome;
chrome.runtime.getManifest.returns({
version: '7.0.0',
version: '8.5.0',
debug: true
});

@@ -1,19 +1,17 @@
/**
* /src/classes/BugDb.js Unit Tests
* BugDb.js Unit Tests
*
* Ghostery Browser Extension
* http://www.ghostery.com/
*
* Copyright 2019 Ghostery, Inc. All rights reserved.
* Copyright 2020 Ghostery, Inc. All rights reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0
*/

import _ from 'underscore';
import sinon from 'sinon';
import 'whatwg-fetch';
import bugDb from '../../src/classes/BugDb';
import conf from '../../src/classes/Conf';
import { prefsGet } from '../../src/utils/common';
@@ -106,17 +104,8 @@ describe('src/classes/BugDb.js', () => {
};

beforeAll(done => {
// Fake the translation function for categories for bugDb.init()
global.t = sinon.stub();
global.t.withArgs([
'site_analytics',
'customer_interaction',
'social_media'
]).returns(true);

// Fake XMLHttpRequest for fetchJson(/databases/bugs.json)
sinon.stub(global, 'fetch');
setFetchStubResponse(200, JSON.stringify(bugs))
global.mockFetchResponse(200, JSON.stringify(bugs))

chrome.storage.local.get.yields({ previousVersion: "8.0.8" });
conf.init().then(() => {
@@ -127,21 +116,6 @@ describe('src/classes/BugDb.js', () => {
}).catch(err => console.log(err));
});

afterAll(() => {
global.fetch.restore();
});

// Helper function to fake XHR requests
function setFetchStubResponse (responseCode, responseData) {
const res = new global.Response(responseData, {
status: responseCode,
headers: {
'Content-type': 'application/json'
}
});
global.fetch.returns(Promise.resolve(res));
}

describe('bugDb.db.[key] should not be empty', () => {
test('bugs', () => expect(_.size(bugDb.db.bugs)).toBeGreaterThan(0));
test('apps', () => expect(_.size(bugDb.db.apps)).toBeGreaterThan(0));
@@ -305,7 +279,7 @@ describe('src/classes/BugDb.js', () => {
conf.bugs = old_bugs;

// Fake the xhr request again
setFetchStubResponse(200, JSON.stringify(bugs))
global.mockFetchResponse(200, JSON.stringify(bugs))

// fake an upgrade so that we read the "newer" bugs from disk instead of localStorage
return bugDb.init(true).then(() => {
@@ -1,21 +1,18 @@
/**
* /src/classes/Conf.js Unit Tests
* Conf.js Unit Tests
*
* Ghostery Browser Extension
* http://www.ghostery.com/
*
* Copyright 2019 Ghostery, Inc. All rights reserved.
* Copyright 2020 Ghostery, Inc. All rights reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0
*/

import sinon from 'sinon';
import chrome from 'sinon-chrome';
import conf from '../../src/classes/Conf';
import globals from '../../src/classes/Globals';
import dispatcher from '../../src/classes/Dispatcher';

describe('pre-initialization tests', () => {
test('pre-initialization global is set', () => {
@@ -34,7 +31,7 @@ describe('pre-initialization tests', () => {
return expect(conf.alert_bubble_timeout).toBeUndefined();
});

test('pre-initialization configuration values are saved temporarally', () => {
test('pre-initialization configuration values are saved temporarily', () => {
conf.alert_bubble_pos = 'tl';
return expect(globals.initProps.alert_bubble_pos).toBe('tl');
});
@@ -78,31 +75,3 @@ describe('post-initialization tests', () => {
return expect(chrome.storage.local.set.calledWith({alert_bubble_timeout: 30})).toBeTruthy();
});
});

describe('dispatcher tests', () => {
let spy = sinon.spy(dispatcher, 'trigger');

beforeEach(() => {
spy.resetHistory();
});

test('spy.resetHistory() works and callCount is reset to 0', () => {
return expect(spy.callCount).toBe(0);
});

test('dispatcher is triggered once for conf value not in SYNC_ARRAY', () => {
conf.paused_blocking = true;
return expect(spy.calledOnce).toBeTruthy();
});

test('dispatcher is triggered twice for conf value in SYNC_ARRAY', () => {
conf.alert_expanded = true;
return expect(spy.calledTwice).toBeTruthy();
});

test('dispatcher is triggered with correct arguments', () => {
conf.alert_expanded = true;
return expect(spy.calledWith(`conf.save.alert_expanded`, true)).toBeTruthy();
});

});
@@ -0,0 +1,42 @@
/**
* Dispatcher.js Unit Tests
*
* Ghostery Browser Extension
* http://www.ghostery.com/
*
* Copyright 2020 Ghostery, Inc. All rights reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0
*/

import dispatcher from '../../src/classes/Dispatcher';
import conf from '../../src/classes/Conf';

describe('dispatcher tests', () => {
let spy = jest.spyOn(dispatcher, 'trigger');

afterEach(() => {
jest.clearAllMocks();
});

test('dispatcher is triggered once for conf value not in SYNC_ARRAY', () => {
conf.paused_blocking = true;
return expect(spy).toHaveBeenCalledTimes(1);
});

test('clearAllMocks() works and callCount is reset to 0', () => {
return expect(spy).toHaveBeenCalledTimes(0);
});

test('dispatcher is triggered twice for conf value in SYNC_ARRAY', () => {
conf.alert_expanded = true;
return expect(spy).toHaveBeenCalledTimes(2);
});

test('dispatcher is triggered with correct arguments', () => {
conf.alert_expanded = true;
return expect(spy).toHaveBeenCalledWith(`conf.save.alert_expanded`, true);
});
});
ProTip! Use n and p to navigate between commits in a pull request.