Skip to content

Commit

Permalink
Feature/styles update tests (#36)
Browse files Browse the repository at this point in the history
* Add unit test for exportCSV method

* Add unit tests for formQuery

* Fix method and add test for formatData

* Add unit tests for other helperMethods code
  • Loading branch information
ONS-Tom authored Mar 22, 2018
1 parent 2daebf2 commit b7a6521
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/utils/formQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ const formQuery = (query) => {
}
};

export { formQuery };
export { encodeSpecialChars, formQuery };
10 changes: 5 additions & 5 deletions src/utils/helperMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ const formatData = (business) => {
return Array.from({ length: largestRef }, (a, b) => {
if (b === 0) {
return {
companyNo: business.companyNo,
vatRefs: business.vatRefs[b],
payeRefs: business.payeRefs[b],
companyNo: (business.companyNo !== undefined) ? business.companyNo : '',
vatRefs: (business.vatRefs[b] !== undefined) ? business.vatRefs[b] : '',
payeRefs: (business.payeRefs[b] !== undefined) ? business.payeRefs[b] : '',
};
}
return {
companyNo: '',
vatRefs: business.vatRefs[b],
payeRefs: business.payeRefs[b],
vatRefs: (business.vatRefs[b] !== undefined) ? business.vatRefs[b] : '',
payeRefs: (business.payeRefs[b] !== undefined) ? business.payeRefs[b] : '',
};
});
};
Expand Down
27 changes: 27 additions & 0 deletions test/utils-spec/export-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { exportCSV } from '../../src/utils/export';
import { returnBusiness } from '../../src/utils/requestUtils';

describe("export.js test suite", () => {
it("creates a valid CSV string from an array of businesses", () => {
const header = 'UBRN,Business Name,PostCode,Industry Code,Legal Status,Trading Status,Turnover,Employment,Company Reference Number';
const results = Array.from({ length: 10 }, () => returnBusiness());
const csv = exportCSV(header, results);
const splitCsv = csv.split(/\r?\n/);
const csvHeader = splitCsv[0];

// Firstly, check the header in the CSV string is correct
expect(csvHeader).toBe(header);

// Remove the header and the new line at the end
const splitCsvNoHeader = splitCsv.slice(1, splitCsv.length - 1);

// Check that businessName is present in the data
// We need to remove the padded double qoutes around each bit of data
results.forEach((business, i) => {
expect(business.businessName).toBe(splitCsvNoHeader[i].split(',')[1].replace(/['"]+/g, ''));
});

// Do a last check on the length to verify they are the same
expect(results.length).toBe(splitCsvNoHeader.length);
});
});
82 changes: 82 additions & 0 deletions test/utils-spec/formQuery-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import config from '../../src/config/form-query';
import { formQuery, encodeSpecialChars } from '../../src/utils/formQuery';

const { SEPERATOR, LIMIT, ES_CONCAT, END_SEPERATOR } = config;

describe("formQuery.js test suite", () => {
it("creates a valid query given a UBRN", () => {
const Id = '12345678';
const correctQuery = `business/${Id}`;
const query = formQuery({
Id,
});

expect(correctQuery).toBe(query);
});

it("encodes the spaces in businessName", () => {
const BusinessName = 'TESCO STORES';
const correctlyEncoded = 'TESCO%20STORES';
const encoded = encodeSpecialChars(BusinessName);

expect(correctlyEncoded).toBe(encoded);
});

it("creates a valid query given a businessName", () => {
const BusinessName = 'TESCO STORES';
const correctQuery = `search/BusinessName:${encodeSpecialChars(BusinessName)}${END_SEPERATOR}${LIMIT}`;
const query = formQuery({
BusinessName,
});

expect(correctQuery).toBe(query);
});

it("creates a valid query given an exact industryCode", () => {
const industryCode = '10000';
const correctQuery = `search/IndustryCode:${industryCode}${END_SEPERATOR}${LIMIT}`;
const query = formQuery({
IndustryCode: {
single: industryCode,
},
});

expect(correctQuery).toBe(query);
});

it("creates a valid query given an industryCode range", () => {
const min = '10000';
const max = '20000';
const correctQuery = `search/IndustryCode:%5B${min} TO ${max}%5D${END_SEPERATOR}${LIMIT}`;
const query = formQuery({
IndustryCode: {
min,
max,
},
});

expect(correctQuery).toBe(query);
});

it("creates a valid query given an search for refs (CH/VAT/PATE)", () => {
const Ref = '123456AB';
const correctQuery = `search/CompanyNo:${Ref} OR PayeRefs:${Ref} OR VatRefs:${Ref}${END_SEPERATOR}${LIMIT}`;
const query = formQuery({
Ref,
});

expect(correctQuery).toBe(query);
});

it("creates a valid query given multiple search parameters", () => {
const BusinessName = 'ASDA';
const PostCode = 'NP10 8XG';
const correctQuery = `search/BusinessName:${BusinessName} AND PostCode:${PostCode}${END_SEPERATOR}${LIMIT}`;
const query = formQuery({
BusinessName,
PostCode,
});

expect(correctQuery).toBe(query);
});
});
165 changes: 163 additions & 2 deletions test/utils-spec/helperMethods-test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,171 @@
import { maxSize } from '../../src/utils/helperMethods';
import {
maxSize, formatData, everyKeyMatches, handleFormChange,
formSelectJson, anyKeyEmpty
} from '../../src/utils/helperMethods';

describe("Helper Methods test suite", () => {
describe("maxSize", () => {
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);
});
});

describe("formatData", () => {
it("creates the correct data format for react-table (single CH)", () => {
const business = {
companyNo: '123456AB',
vatRefs: [],
payeRefs: [],
};
const correctFormat = [{
companyNo: '123456AB',
vatRefs: '',
payeRefs: '',
}];
const formatted = formatData(business);
expect(JSON.stringify(correctFormat)).toBe(JSON.stringify(formatted));
});

it("creates the correct data format for react-table (no CH)", () => {
const business = {
companyNo: '',
vatRefs: [],
payeRefs: [],
};
const correctFormat = [{
companyNo: '',
vatRefs: '',
payeRefs: '',
}];
const formatted = formatData(business);
expect(JSON.stringify(correctFormat)).toBe(JSON.stringify(formatted));
});

it("creates the correct data format for react-table (multiple VatRefs)", () => {
const business = {
companyNo: '123456AB',
vatRefs: ['123', '456'],
payeRefs: [234],
};
const correctFormat = [{
companyNo: '123456AB',
vatRefs: '123',
payeRefs: 234,
},{
companyNo: '',
vatRefs: '456',
payeRefs: '',
}];
const formatted = formatData(business);
expect(JSON.stringify(correctFormat)).toBe(JSON.stringify(formatted));
});

it("creates the correct data format for react-table (multiple PayeRefs)", () => {
const business = {
companyNo: '123456AB',
vatRefs: ['123'],
payeRefs: [234, 567],
};
const correctFormat = [{
companyNo: '123456AB',
vatRefs: '123',
payeRefs: 234,
},{
companyNo: '',
vatRefs: '',
payeRefs: 567,
}];
const formatted = formatData(business);
expect(JSON.stringify(correctFormat)).toBe(JSON.stringify(formatted));
});
});

describe("everyKeyMatches", () => {
it("returns true if every key matches a value", () => {
const result = everyKeyMatches({ a: '', b: '', c: '' }, '');
expect(result).toBe(true);
});

it("returns false if any key matches does not match the value", () => {
const result = everyKeyMatches({ a: 'abc', b: '', c: '' }, '');
expect(result).toBe(false);
});

it("returns false if any key matches does not match the value (equality test)", () => {
const result = everyKeyMatches({ a: '1', b: '1', c: '1' }, 1);
expect(result).toBe(false);
});
});

describe("handleFormChange", () => {
it("removes empty keys (string)", () => {
const result = handleFormChange({ BusinessName: '', PostCode: 'ABC 123' }, 'BusinessName', '');
const expected = {
PostCode: 'ABC 123',
};
expect(JSON.stringify(result)).toBe(JSON.stringify(expected));
});

it("removes empty keys (object)", () => {
const result = handleFormChange({
IndustryCode: {
min: '',
max: '',
single: '',
}, PostCode: 'ABC 123' }, 'IndustryCode', { min: '', max: '', single: '' });
const expected = {
PostCode: 'ABC 123',
};
expect(JSON.stringify(result)).toBe(JSON.stringify(expected));
});

it("removes empty keys (array)", () => {
const result = handleFormChange({ PayeRefs: [], PostCode: 'ABC 123' }, 'PayeRefs', []);
const expected = {
PostCode: 'ABC 123',
};
expect(JSON.stringify(result)).toBe(JSON.stringify(expected));
});

it("transforms businessName to upper case", () => {
const result = handleFormChange({ BusinessName: 'a' }, 'BusinessName', 'a');
const expected = {
BusinessName: 'A',
};
expect(JSON.stringify(result)).toBe(JSON.stringify(expected));
});

it("transforms businessName to upper case", () => {
const result = handleFormChange({ PostCode: 'a' }, 'PostCode', 'a');
const expected = {
PostCode: 'A',
};
expect(JSON.stringify(result)).toBe(JSON.stringify(expected));
});
});

describe("formSelectJson", () => {
it("returns true if every key matches a value", () => {
const result = formSelectJson({ a: '1', b: '2', c: '3' });
const expected = [
{ label: 'a [1]', value: 'a' },
{ label: 'b [2]', value: 'b' },
{ label: 'c [3]', value: 'c' },
];
expect(JSON.stringify(result)).toBe(JSON.stringify(expected));
});
});

describe("anyKeyEmpty", () => {
it("returns true if any key is empty", () => {
const result = anyKeyEmpty({ a: '1', b: '2', c: '' });
expect(result).toBe(true);
});

it("returns false if no keys are empty", () => {
const result = anyKeyEmpty({ a: '1', b: '2', c: '3' });
expect(result).toBe(false);
});
});

0 comments on commit b7a6521

Please sign in to comment.