From b585ca0d580dbbd4a8ae3b8d2c6fdfd90d1d15ee Mon Sep 17 00:00:00 2001 From: Tom Cooling Date: Fri, 15 Dec 2017 11:34:30 +0000 Subject: [PATCH 1/8] Add convertBands unit tests --- test/utils-spec/convertBands-test.js | 23 +++++++++++++++++++++++ test/utils-spec/example-test.js | 5 ----- 2 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 test/utils-spec/convertBands-test.js delete mode 100644 test/utils-spec/example-test.js diff --git a/test/utils-spec/convertBands-test.js b/test/utils-spec/convertBands-test.js new file mode 100644 index 0000000..48a6bab --- /dev/null +++ b/test/utils-spec/convertBands-test.js @@ -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'); + }); +}); \ No newline at end of file diff --git a/test/utils-spec/example-test.js b/test/utils-spec/example-test.js deleted file mode 100644 index 1f99ee1..0000000 --- a/test/utils-spec/example-test.js +++ /dev/null @@ -1,5 +0,0 @@ -describe("A suite", () => { - it("contains spec with an expectation", () => { - expect(1).toBe(1); - }); -}); From b61ea81051e081a5710a372aed0cb76176ee95b6 Mon Sep 17 00:00:00 2001 From: Tom Cooling Date: Fri, 15 Dec 2017 11:47:32 +0000 Subject: [PATCH 2/8] Remove unused helper method (already in convert bands) --- src/utils/helperMethods.js | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/src/utils/helperMethods.js b/src/utils/helperMethods.js index 76aeab4..cc61dad 100644 --- a/src/utils/helperMethods.js +++ b/src/utils/helperMethods.js @@ -42,27 +42,6 @@ export function getChildValues(json: {}, compareString: string) { 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); } From de21f4958038d3c38b00b28cb367b0df038e6c2d Mon Sep 17 00:00:00 2001 From: Tom Cooling Date: Fri, 15 Dec 2017 11:52:43 +0000 Subject: [PATCH 3/8] Remove unused helperMethods --- src/utils/helperMethods.js | 42 -------------------------------------- 1 file changed, 42 deletions(-) diff --git a/src/utils/helperMethods.js b/src/utils/helperMethods.js index cc61dad..9222952 100644 --- a/src/utils/helperMethods.js +++ b/src/utils/helperMethods.js @@ -1,47 +1,5 @@ // @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 maxSize(...args) { return args.reduce((a, b) => (b.length > a ? b.length : a), 0); } From e9e253e898cd3b494c5dd7b591620022681c85ea Mon Sep 17 00:00:00 2001 From: Tom Cooling Date: Fri, 15 Dec 2017 11:56:52 +0000 Subject: [PATCH 4/8] Add helper methods unit test --- test/utils-spec/helperMethods-test.js | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 test/utils-spec/helperMethods-test.js diff --git a/test/utils-spec/helperMethods-test.js b/test/utils-spec/helperMethods-test.js new file mode 100644 index 0000000..c59e1fe --- /dev/null +++ b/test/utils-spec/helperMethods-test.js @@ -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); + }); +}); \ No newline at end of file From 9db21fe008e6fa553a7458814caf01f8aaf836f2 Mon Sep 17 00:00:00 2001 From: Tom Cooling Date: Fri, 15 Dec 2017 12:01:45 +0000 Subject: [PATCH 5/8] Remove flow annotations from /utils - Not needed for such a small codebase, parameter types will be documented in JSDoc style doc strings --- src/utils/apiInfo.js | 16 +++++++--------- src/utils/apiSearch.js | 6 +----- src/utils/auth.js | 24 ++++++++++-------------- src/utils/helperMethods.js | 4 +--- src/utils/validation.js | 4 +--- 5 files changed, 20 insertions(+), 34 deletions(-) diff --git a/src/utils/apiInfo.js b/src/utils/apiInfo.js index 3e2b7fb..83000be 100644 --- a/src/utils/apiInfo.js +++ b/src/utils/apiInfo.js @@ -1,5 +1,3 @@ -// @flow - import config from '../config/api-urls'; const { AUTH_URL, REROUTE_URL } = config; @@ -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: { @@ -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 }); }); } @@ -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: { @@ -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 }); }); } diff --git a/src/utils/apiSearch.js b/src/utils/apiSearch.js index 0d10b6a..e32e6ad 100644 --- a/src/utils/apiSearch.js +++ b/src/utils/apiSearch.js @@ -1,5 +1,3 @@ -// @flow - import config from '../config/api-urls'; const { REROUTE_URL, API_VERSION } = config; @@ -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: { diff --git a/src/utils/auth.js b/src/utils/auth.js index 83ca1ef..c553d2a 100644 --- a/src/utils/auth.js +++ b/src/utils/auth.js @@ -1,5 +1,3 @@ -// @flow - import config from '../config/api-urls'; const { AUTH_URL } = config; @@ -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 @@ -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 }); }); @@ -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: { @@ -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 }); }); @@ -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', diff --git a/src/utils/helperMethods.js b/src/utils/helperMethods.js index 9222952..2645175 100644 --- a/src/utils/helperMethods.js +++ b/src/utils/helperMethods.js @@ -1,10 +1,8 @@ -// @flow - 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) { diff --git a/src/utils/validation.js b/src/utils/validation.js index 2ef5027..d2cc10e 100644 --- a/src/utils/validation.js +++ b/src/utils/validation.js @@ -1,5 +1,3 @@ -// @flow - import config from '../config/validation'; const { UBRN } = config; @@ -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'; } From f3e49e919cde6ab5da661f3655839f196965a180 Mon Sep 17 00:00:00 2001 From: Tom Cooling Date: Fri, 15 Dec 2017 12:04:28 +0000 Subject: [PATCH 6/8] Remove flow related configuration - Remove dependancy from package.json - Remove flow config file, .flowconfig --- .flowconfig | 7 ------- package.json | 5 +---- 2 files changed, 1 insertion(+), 11 deletions(-) delete mode 100644 .flowconfig diff --git a/.flowconfig b/.flowconfig deleted file mode 100644 index 8d5ec7f..0000000 --- a/.flowconfig +++ /dev/null @@ -1,7 +0,0 @@ -[ignore] -.*/node_modules/.* -[include] - -[libs] - -[options] diff --git a/package.json b/package.json index 39d6e8c..f2ea569 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", @@ -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", From 3e72564b891178c8784e78c34fe1563fb6c457aa Mon Sep 17 00:00:00 2001 From: Tom Cooling Date: Fri, 15 Dec 2017 12:05:29 +0000 Subject: [PATCH 7/8] Remove flow details in CONTRIBUTING.md --- CONTRIBUTING.md | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 935a1f7..81d6a0e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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 From 86bc6368e79b6cc037d57412d94868e0e1471269 Mon Sep 17 00:00:00 2001 From: Tom Cooling Date: Fri, 15 Dec 2017 12:10:16 +0000 Subject: [PATCH 8/8] Add unit test for Sic code conversion --- test/utils-spec/siccode-test.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 test/utils-spec/siccode-test.js diff --git a/test/utils-spec/siccode-test.js b/test/utils-spec/siccode-test.js new file mode 100644 index 0000000..70a1790 --- /dev/null +++ b/test/utils-spec/siccode-test.js @@ -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); + }); +}); \ No newline at end of file