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

Fix/utils unit tests #25

Merged
merged 8 commits into from
Jan 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions .flowconfig

This file was deleted.

18 changes: 0 additions & 18 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,3 @@ Contributing guidelines

* We use [react-a11y](https://github.com/reactjs/react-a11y) which throws warnings in the console if any accessibility issues are encountered.
* Look at the [Facebook documentation](https://facebook.github.io/react/docs/accessibility.html) for more information about accessibility and React.

### Type Annotation Using [Flow](https://flow.org/en/)

* We use Flow as a static type checker for all files in /utils.

Example:

```javascript
// Type annotate variables:
const hello: String = "Hello, world!";

// And methods:
logout(callback: (success: boolean) => void) {
...
}
```

* Use `npm run flow` to check for Flow type warnings/errors
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"chai": "^3.5.0",
"eslint": "^3.0.0",
"eslint-config-airbnb": "^15.0.1",
"eslint-plugin-flowtype": "^2.34.1",
"eslint-plugin-import": "^2.3.0",
"eslint-plugin-jsx-a11y": "^4.0.0",
"eslint-plugin-react": "^7.1.0",
Expand All @@ -26,7 +25,6 @@
"jasmine-node": "^1.14.5",
"jasmine-es6": "^0.4.0",
"coveralls": "^2.13.1",
"flow-bin": "^0.48.0",
"mocha": "^3.0.2",
"istanbul": "^0.4.5",
"morgan": "^1.7.0",
Expand Down Expand Up @@ -82,8 +80,7 @@
"test:server": "./node_modules/mocha/bin/mocha test/server.test.js; ./node_modules/mocha/bin/mocha test/server-test-spec --recursive",
"test:selenium": "NODE_ENV=test UI_URL=http://localhost:3000 ./node_modules/jasmine/bin/jasmine.js test/integration-test.js",
"lint": "./node_modules/eslint/bin/eslint.js **/*.js --ignore-pattern /test/*.js",
"lint-report-xml": "./node_modules/eslint/bin/eslint.js . -f checkstyle -o ./coverage/eslint-report-checkstyle.xml; exit 0",
"flow": "flow"
"lint-report-xml": "./node_modules/eslint/bin/eslint.js . -f checkstyle -o ./coverage/eslint-report-checkstyle.xml; exit 0"
},
"eslintConfig": {
"extends": "./node_modules/react-scripts/config/eslint.js",
Expand Down
16 changes: 7 additions & 9 deletions src/utils/apiInfo.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @flow

import config from '../config/api-urls';

const { AUTH_URL, REROUTE_URL } = config;
Expand All @@ -13,7 +11,7 @@ const apiInfo = {
* Gets version/lastUpdate info from the UI.
* @param {Function} callback Called with returned data.
*/
getUiInfo(callback: (success: boolean, data: {}) => void) {
getUiInfo(callback) {
fetch(`${AUTH_URL}/api/info`, {
method: 'GET',
headers: {
Expand All @@ -23,8 +21,8 @@ const apiInfo = {
}).then((response) => {
if (response.status === 200) {
return response.json().then((json) => {
const version: string = json.version;
const lastUpdate: string = json.lastUpdate;
const version = json.version;
const lastUpdate = json.lastUpdate;
callback(true, { version, lastUpdate });
});
}
Expand All @@ -37,7 +35,7 @@ const apiInfo = {
* Gets version/lastUpdate info from the API.
* @param {Function} callback Called with returned data.
*/
getApiInfo(callback: (success: boolean, data: {}) => void) {
getApiInfo(callback) {
fetch(`${REROUTE_URL}`, {
method: 'POST',
headers: {
Expand All @@ -51,9 +49,9 @@ const apiInfo = {
}).then((response) => {
if (response.status === 200) {
return response.json().then((json) => {
const version: string = json.version;
const lastApiUpdate: string = json.builtAtString;
const lastDataUpdate: string = json.lastDataUpdate;
const version = json.version;
const lastApiUpdate = json.builtAtString;
const lastDataUpdate = json.lastDataUpdate;
callback(true, { version, lastApiUpdate, lastDataUpdate });
});
}
Expand Down
6 changes: 1 addition & 5 deletions src/utils/apiSearch.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @flow

import config from '../config/api-urls';

const { REROUTE_URL, API_VERSION } = config;
Expand All @@ -13,9 +11,7 @@ const apiSearch = {
* Searches API for match
* @param {Function} callback Called with returned data.
*/
search(query: string, callback: (success: boolean, data: {}, response?: {}) => void) {
// fetch(`${REROUTE_URL}/${API_VERSION}/${SEARCH_ENDPOINT}${query}`, {
// method: 'GET',
search(query, callback) {
fetch(`${REROUTE_URL}`, {
method: 'POST',
headers: {
Expand Down
24 changes: 10 additions & 14 deletions src/utils/auth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @flow

import config from '../config/api-urls';

const { AUTH_URL } = config;
Expand All @@ -15,7 +13,7 @@ const auth = {
* @param {string} password The password of the user
* @param {Function} callback Called after a user was logged in on the remote server
*/
login(username: string, basicAuth: string, callback: (success: boolean, data: {}) => void) {
login(username, basicAuth, callback) {
// Do not need to check if user is already logged in, this is done in
// routes.js before this method is called

Expand All @@ -31,12 +29,10 @@ const auth = {
if (response.status === 200) {
return response.json().then((json) => {
// const token: string = json.jToken;
const loginName: string = json.username;
const accessToken: string = json.accessToken;
const showConfetti: string = json.showConfetti;
const role: string = json.role;
// const role: string = json.role;
// sessionStorage.setItem('token', token);
const loginName = json.username;
const accessToken = json.accessToken;
const showConfetti = json.showConfetti;
const role = json.role;
// Send auth request to save token username pair
callback(true, { username: loginName, accessToken, showConfetti, role });
});
Expand All @@ -47,7 +43,7 @@ const auth = {
return callback(false, { message: 'Server error: request timed out.' });
});
},
checkToken(accessToken: string, callback: (success: boolean, data: ?{}) => void) {
checkToken(accessToken, callback) {
fetch(`${AUTH_URL}/auth/checkToken`, {
method: 'POST',
headers: {
Expand All @@ -57,9 +53,9 @@ const auth = {
}).then((response) => {
if (response.status === 200) {
return response.json().then((json) => {
const newAccessToken: string = json.accessToken;
const username: string = json.username;
const role: string = json.role;
const newAccessToken = json.accessToken;
const username = json.username;
const role = json.role;
// Send auth request to save token username pair
callback(true, { username, newAccessToken, role });
});
Expand All @@ -73,7 +69,7 @@ const auth = {
/**
* Logs the current user out
*/
logout(accessToken: string, callback: (success: boolean) => void) {
logout(accessToken, callback) {
// const token: string = sessionStorage.token;
fetch(`${AUTH_URL}/auth/logout`, {
method: 'POST',
Expand Down
67 changes: 1 addition & 66 deletions src/utils/helperMethods.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,8 @@
// @flow

export function countStatus(history: Array<{}>, status: number) {
if (history === []) {
return 0;
}
return history.filter(h => h.HTTPCode === status).length;
}

export function countStatusBetween(history: Array<{}>, status: object) {
if (history === []) {
return 0;
}
return history.filter(h => h.HTTPCode >= status.min && h.HTTPCode <= status.max).length;
}

export function formatResultsTable(results: Array<{}>) {
const formattedResults: Array<{}> = [];
results.forEach((i) => {
const record: Object = i;
if (record.source === 'VAT' || record.source === 'Legal Unit') {
record.name = record.businessName;
}
formattedResults.push(record);
});
return formattedResults;
}

export function getValueByKey(object: {}, toGet: string) {
return (toGet in object) ? object[toGet] : '';
}

export function getChildValues(json: {}, compareString: string) {
const arr: Array<{}> = [];
Object.keys(json).forEach((k) => {
if (json[k] === compareString) {
const obj: {} = {};
obj[compareString] = k;
arr.push(obj);
}
});
return arr;
}

export function getLegalStatusDescription(status: string) {
switch (status) {
case '1':
return 'Company';
case '2':
return 'Sole Proprietor';
case '3':
return 'Partnership';
case '4':
return 'Public Corporation';
case '5':
return 'Central Government';
case '6':
return 'Local Authority';
case '7':
return 'Non-Profit Body';
default:
return 'Not Allocated';
}
}

export function maxSize(...args) {
return args.reduce((a, b) => (b.length > a ? b.length : a), 0);
}

export function formatData(business: {}) {
export function formatData(business) {
const largestRef = maxSize(business.vatRefs, business.payeRefs, [business.companyNo]);
const formattedData = [];
for (let i = 0; i <= largestRef - 1; i += 1) {
Expand Down
4 changes: 1 addition & 3 deletions src/utils/validation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @flow

import config from '../config/validation';

const { UBRN } = config;
Expand All @@ -11,7 +9,7 @@ const { UBRN } = config;
*
* @return {string} Validation state string for bootstrap
*/
export function validateUBRNSearch(query: string) {
export function validateUBRNSearch(query) {
if ((query.length >= UBRN.min && query.length <= UBRN.max) && !isNaN(query)) return 'success';
return 'error';
}
23 changes: 23 additions & 0 deletions test/utils-spec/convertBands-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { employmentBands, legalStatusBands, tradingStatusBands, turnoverBands } from '../../src/utils/convertBands';

describe("Convert bands test suite", () => {
it("converts an Employment Band to a number", () => {
const conversion = employmentBands['A'];
expect(conversion).toBe('0');
});

it("converts a Legal Status Band to a string", () => {
const conversion = legalStatusBands['8'];
expect(conversion).toBe('Charity');
});

it("converts a Trading Status Band to a string", () => {
const conversion = tradingStatusBands['A'];
expect(conversion).toBe('Active');
});

it("converts a Turnover Band to a string", () => {
const conversion = turnoverBands['A'];
expect(conversion).toBe('0-99');
});
});
5 changes: 0 additions & 5 deletions test/utils-spec/example-test.js

This file was deleted.

10 changes: 10 additions & 0 deletions test/utils-spec/helperMethods-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { maxSize } from '../../src/utils/helperMethods';

describe("Helper Methods test suite", () => {
it("gets the maxSize of given arrays", () => {
const arr1 = [1, 2, 3];
const arr2 = [1];
const largest = maxSize(arr1, arr2);
expect(largest).toBe(arr1.length);
});
});
14 changes: 14 additions & 0 deletions test/utils-spec/siccode-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import industryCodeDescription from '../../src/utils/siccode';

describe("Sic Code conversion test suite", () => {
it("gets a Sic Code description from a valid Sic code", () => {
const description = 'Growing of cereals (except rice), leguminous crops and oil seeds';
const conversion = industryCodeDescription['01110'];
expect(conversion).toBe(description);
});

it("returns undefined for a Sic code that doesn't exist", () => {
const conversion = industryCodeDescription['1234567890'];
expect(conversion).toBe(undefined);
});
});