-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
5 changed files
with
278 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,4 +56,4 @@ const formQuery = (query) => { | |
} | ||
}; | ||
|
||
export { formQuery }; | ||
export { encodeSpecialChars, formQuery }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |