diff --git a/index.js b/index.js index 7954fdb..6319c78 100644 --- a/index.js +++ b/index.js @@ -53,7 +53,7 @@ module.exports = { departments: (regionName) => { regionName = lowerCase(regionName); - if (regionName.length == 0) { + if (regionName?.length == 0) { throw new RegionReferenceError(); } @@ -73,7 +73,7 @@ module.exports = { population: (regionName) => { regionName = lowerCase(regionName); - if (regionName.length == 0) { + if (regionName?.length == 0) { throw new RegionReferenceError(); } @@ -93,7 +93,7 @@ module.exports = { superficie: (regionName) => { regionName = lowerCase(regionName); - if (regionName.length == 0) { + if (regionName?.length == 0) { throw new RegionReferenceError(); } @@ -125,7 +125,7 @@ module.exports = { arrondissements: (departmentName) => { departmentName = lowerCase(departmentName); - if (departmentName.length == 0) { + if (departmentName?.length == 0) { throw new DepartmentReferenceError(); } @@ -145,7 +145,7 @@ module.exports = { populationDepartment: (departmentName) => { departmentName = lowerCase(departmentName); - if (departmentName.length == 0) { + if (departmentName?.length == 0) { throw new DepartmentReferenceError(); } @@ -165,7 +165,7 @@ module.exports = { superficieDepartment: (departmentName) => { departmentName = lowerCase(departmentName); - if (departmentName.length == 0) { + if (departmentName?.length == 0) { throw new DepartmentReferenceError(); } diff --git a/libs/utils.js b/libs/utils.js index 56e2c93..f92ccec 100644 --- a/libs/utils.js +++ b/libs/utils.js @@ -1,8 +1,8 @@ const rg = require("../dataset/regions.json"); const dp = require("../dataset/departments.json"); -// const { DepartmentReferenceError } = require("./exceptions"); +const { DepartmentReferenceError, RegionReferenceError} = require("./exceptions"); -const lowerCase = (value) => value.toLowerCase().trim(); +const lowerCase = (value) => value?.toLowerCase().trim(); const findItem = (value) => { const region = rg.find( diff --git a/mock/galsenify.test.mock.js b/mock/galsenify.test.mock.js new file mode 100644 index 0000000..34e7341 --- /dev/null +++ b/mock/galsenify.test.mock.js @@ -0,0 +1,111 @@ +const regionNamesMock = [ + "Dakar", + "Diourbel", + "Fatick", + "Kaffrine", + "Kaolack", + "Kédougou", + "Kolda", + "Louga", + "Matam", + "Saint-Louis", + "Sédhiou", + "Tambacounda", + "Thies", + "Ziguinchor", +]; + +const departmentsMock = [ + "Dakar", + "Pikine", + "Guédiawaye", + "Rufisque", + "Keur Massar", +]; + +const codeMock = [ + "DK", + "DB", + "FK", + "KA", + "KL", + "KE", + "KD", + "LG", + "MT", + "SL", + "SE", + "TC", + "TH", + "ZG", +]; + +const languesNationalesMock = [ + "Wolof", + "Pular", + "Sérère", + "Diola", + "Mandingue", + "Soninké", + "Bambara", +]; + +const allDepartmentMock = [ + "Dakar", + "Guédiawaye", + "Keur Massar", + "Pikine", + "Rufisque", + "Bambey", + "Diourbel", + "Mbacké", + "Fatick", + "Foundiougne", + "Gossas", + "Birkelane", + "Kaffrine", + "Koungheul", + "Malème-Hodar", + "Guinguinéo", + "Kaolack", + "Nioro du Rip", + "Kédougou", + "Salemata", + "Saraya", + "Kolda", + "Vélingara", + "Médina Yoro Foulah", + "Kébémer", + "Linguère", + "Louga", + "Kanel", + "Matam", + "Ranérou-Ferlo", + "Dagana", + "Podor", + "Saint-Louis", + "Bounkiling", + "Goudomp", + "Sédhiou", + "Bakel", + "Goudiry", + "Koumpentoum", + "Tambacounda", + "Mbour", + "Thies", + "Tivaouane", + "Bignona", + "Oussouye", + "Ziguinchor", +]; + +const arrondissementsMock = ["Dagoudane", "Thiaroye"]; + +module.exports = { + regionNamesMock, + departmentsMock, + codeMock, + languesNationalesMock, + allDepartmentMock, + arrondissementsMock, +}; diff --git a/test/departments.test.js b/test/departments.test.js index 3e3aac7..3118e51 100644 --- a/test/departments.test.js +++ b/test/departments.test.js @@ -1,21 +1,23 @@ -const department = require("../index") +const department = require("../index"); +const { allDepartmentMock, arrondissementsMock } = require("../mock/galsenify.test.mock"); + describe("Department testing", () => { test("receive the department", () => { const departmentName = department.allDepartments(); - expect(departmentName.length).toEqual(departmentName.length); - expect(departmentName).toContain("Dakar"); + expect(departmentName.length).toEqual(allDepartmentMock.length); + expect(departmentName).toEqual(allDepartmentMock); }); test("receive the arrondissements of passing department", () => { const arrondissements = department.arrondissements("Pikine"); - expect(arrondissements.length).toBeGreaterThan(0); - expect(arrondissements).toContain("Thiaroye"); + expect(arrondissements.length).toEqual(arrondissementsMock.length); + expect(arrondissements).toEqual(arrondissementsMock); }); test("receive the arrrondissements of passing department should fail", () => { - expect(() => department.arrondissements("Yeumbeul")).toThrow(); - expect(() => department.arrondissements("")).toThrow(); + expect(() => department.arrondissements("Yeumbeul")).toThrow("Oups! Ce n'est pas un Département valide !"); + expect(() => department.arrondissements("")).toThrow("Oups! Ce n'est pas un Département valide !"); }); }); diff --git a/test/galsenify.test.js b/test/galsenify.test.js index 7fd0711..5e3c923 100644 --- a/test/galsenify.test.js +++ b/test/galsenify.test.js @@ -1,26 +1,28 @@ const galsenify = require('../index'); +const {regionNamesMock, departmentsMock, codeMock, languesNationalesMock} = require('../mock/galsenify.test.mock'); describe("galsenify testing", () => { test("receive the regions", () => { const regionNames = galsenify.regions(); - expect(regionNames.length).toEqual(regionNames.length); - expect(regionNames).toContain("Dakar"); + expect(regionNames.length).toEqual(14); + expect(regionNames).toEqual(regionNamesMock); }); test("receive the departments of passing region", () => { const departments = galsenify.departments("Dakar"); - expect(departments.length).toBeGreaterThan(0); - expect(departments).toContain("Pikine"); + expect(departments.length).toEqual(departmentsMock.length); + expect(departments).toEqual(departmentsMock); }); test("receive the departments of passing region should fail", () => { - expect(() => galsenify.departments("Adidjan")).toThrow(); - expect(() => galsenify.departments("")).toThrow(); + expect(() => galsenify.departments("Adidjan")).toThrow("Oups! Ce n'est pas une région valide !"); + expect(() => galsenify.departments("")).toThrow("Oups! Ce n'est pas une région valide !"); }); test("receive the codes", () => { const codes = galsenify.codes(); - expect(codes.length).toBeGreaterThan(0); + expect(codes).toEqual(codeMock); + expect(codes.length).toEqual(codeMock.length); }); test("receive the population of passing region", () => { @@ -29,8 +31,8 @@ describe("galsenify testing", () => { }); test("receive the population of passing region should be fail", () => { - expect(() => galsenify.population("Abidjan")).toThrow(); - expect(() => galsenify.population("")).toThrow(); + expect(() => galsenify.population("Abidjan")).toThrow("Oups! Ce n'est pas une région valide !"); + expect(() => galsenify.population("")).toThrow("Oups! Ce n'est pas une région valide !"); }); test("receive the superficie of passing region", () => { @@ -39,14 +41,14 @@ describe("galsenify testing", () => { }); test("receive the superficie of passing region should be fail", () => { - expect(() => galsenify.superficie("Abidjan")).toThrow(); - expect(() => galsenify.superficie("")).toThrow(); + expect(() => galsenify.superficie("Abidjan")).toThrow("Oups! Ce n'est pas une région valide !"); + expect(() => galsenify.superficie("")).toThrow("Oups! Ce n'est pas une région valide !"); }); test("receive the sn information", () => { const sn = galsenify.sn(); expect(sn.pays).toBe("Sénégal"); expect(sn.capital).toBe("Dakar"); - expect(galsenify.languesNationales()).toContain("Wolof"); + expect(galsenify.languesNationales()).toEqual(languesNationalesMock); }); });