diff --git a/tests/fixtures.js b/tests/fixtures.js new file mode 100644 index 0000000..0289f8f --- /dev/null +++ b/tests/fixtures.js @@ -0,0 +1,5 @@ +module.exports = +{ + datasViewEltHTML: '
', + datasHTML : '
Z (numéro atomique)ÉlémentSymboleFamilleAbondance des éléments dans la croûte terrestre (μg/k)
1HydrogèneHNon-métal1400000
2HéliumHeGaz noble8
3LithiumLiMétal alcalin20000
4BérylliumBeMétal alcalino-terreux2800
5BoreBMétalloïde10000
6CarboneCNon-métal200000
7AzoteNNon-métal19000
8OxygèneONon-métal461000000
9FluorFHalogène585000
10NéonNeGaz noble5
11SodiumNaMétal alcalin23600000
12MagnésiumMgMétal alcalino-terreux23300000
13AluminiumAlMétal pauvre82300000
14SiliciumSiMétalloïde282000000
15PhosphorePNon-métal1050000
16SoufreSNon-métal350000
17ChloreClHalogène145000
18ArgonArGaz noble3500
19PotassiumKMétal alcalin20900000
20CalciumCaMétal alcalino-terreux41500000
21ScandiumScMétal de transition22000
22TitaneTiMétal de transition5650000
23VanadiumVMétal de transition120000
24ChromeCrMétal de transition102000
25ManganèseMnMétal de transition950000
26FerFeMétal de transition56300000
27CobaltCoMétal de transition25000
28NickelNiMétal de transition84000
29CuivreCuMétal de transition60000
30ZincZnMétal pauvre70000
31GalliumGaMétal pauvre19000
32GermaniumGeMétalloïde1500
33ArsenicAsMétalloïde1800
34SéléniumSeNon-métal50
35BromeBrHalogène2400
36KryptonKrgaz rare0,1
37RubidiumRbMétal alcalin90000
38StrontiumSrMétal alcalino-terreux370000
39YttriumYMétal de transition33000
40ZirconiumZrMétal de transition165000
41NiobiumNbMétal de transition20000
42MolybdèneMoMétal de transition1200
43TechnétiumTcMétal de transitionTraces
44RuthéniumRuMétal de transition1
45RhodiumRhMétal de transition1
46PalladiumPdMétal de transition15
47ArgentAgMétal de transition75
48CadmiumCdMétal pauvre150
49IndiumInMétal pauvre250
50ÉtainSnMétal pauvre2300
51AntimoineSbMétalloïde200
52TellureTeMétalloïde1
53IodeIHalogène450
54XénonXegaz rare0,03
55CésiumCsMétal alcalin3000
56BaryumBaMétal alcalino-terreux425000
57LanthaneLaLanthanide39000
58CériumCeLanthanide66500
59PraséodymePrLanthanide9200
60NéodymeNdLanthanide41500
61ProméthiumPmLanthanideTraces
62SamariumSmLanthanide7050
63EuropiumEuLanthanide2000
64GadoliniumGdLanthanide6200
65TerbiumTbLanthanide1200
66DysprosiumDyLanthanide5200
67HolmiumHoLanthanide1300
68ErbiumErLanthanide3500
69ThuliumTmLanthanide520
70YtterbiumYbLanthanide3200
71LutéciumLuLanthanide800
72HafniumHfMétal de transition3000
73TantaleTaMétal de transition2000
74TungstèneWMétal de transition1250
75RhéniumReMétal de transition0,7
76OsmiumOsMétal de transition1,5
77IridiumIrMétal de transition1
78PlatinePtMétal de transition5
79OrAuMétal de transition4
80MercureHgMétal pauvre85
81ThalliumTlMétal pauvre850
82PlombPbMétal pauvre14000
83BismuthBiMétal pauvre8,5
84PoloniumPoMétal pauvre200×10−9
85AstateAtMétalloïdeTraces
86RadonRnGaz noble400×10−12
87FranciumFrMétal alcalinTraces
88RadiumRaMétal alcalino-terreux900×10−6
89ActiniumAcActinide550×10−9
90ThoriumThActinide9600
91ProtactiniumPaActinide1,4×10−3
92UraniumUActinide2700
93NeptuniumNpActinideTraces
94PlutoniumPuActinideTraces
95AmériciumAmActinide
96CuriumCmActinide
97BerkéliumBkActinide
98CaliforniumCfActinide
99EinsteiniumEsActinide
100FermiumFmActinide
101MendéléviumMdActinide
102NobéliumNoActinide
103LawrenciumLrActinide
104RutherfordiumRfMétal de transition
105DubniumDbMétal de transition
106SeaborgiumSgMétal de transition
107BohriumBhMétal de transition
108HassiumHsMétal de transition
109MeitnériumMtIndéfinie
110DarmstadtiumDsIndéfinie
111RoentgeniumRgIndéfinie
112CoperniciumCnMétal de transition
113NihoniumNhIndéfinie
114FléroviumFlIndéfinie
115MoscoviumMcIndéfinie
116LivermoriumLvIndéfinie
117TennesseTsIndéfinie
118OganessonOgIndéfinie
', +} \ No newline at end of file diff --git a/tests/freeCSV2HTMLSpec.ts b/tests/freeCSV2HTMLSpec.ts index 9e7c397..00fe89b 100644 --- a/tests/freeCSV2HTMLSpec.ts +++ b/tests/freeCSV2HTMLSpec.ts @@ -1,47 +1,116 @@ import { freeCSV2HTML } from "../src/freeCSV2HTML"; const errors=require("../src/errors.js"); +const fixtures=require("./fixtures.js"); -describe("freeCSV2HTML", function() +describe("freeCSV2HTML", () => { let converter: freeCSV2HTML; - const fixture="
"; - beforeEach(function() + beforeEach( () => { converter=new freeCSV2HTML(); - document.body.insertAdjacentHTML('afterbegin', fixture); + document.body.insertAdjacentHTML('afterbegin', fixtures.datasViewEltHTML); }); - afterEach(function() + afterEach( () => { document.body.removeChild(document.getElementById('fixture')); }); - it("Doit être une instance de freeCSV2HTML", function() + it("Doit avoir créé une instance de freeCSV2HTML", () => { expect(converter).toBeInstanceOf(freeCSV2HTML); }); - describe("Setting freeCSV2HTML", function() + describe("Setting freeCSV2HTML", () => { - it("Doit générer une erreur s'il n'y a pas d'élément dans la page pour l'id fourni pour recevoir les données.", function() + it("Doit générer une erreur s'il n'y a pas d'élément dans la page pour l'id fourni pour afficher les données.", () => { - expect(function() { return converter.datasEltId="dontExistId"; }).toThrowError(errors.elementNotFound+"dontExistId"); + expect( () => { return converter.datasViewElt={ id:"dontExistId" }; }).toThrowError(errors.elementNotFound+"dontExistId"); }); - it("Ne doit pas générer une erreur s'il y a un élément dans la page pour l'id fourni pour recevoir les données.", function() + it("Ne doit pas générer une erreur s'il y a bien un élément dans la page pour l'id fourni pour afficher les données.", () => { - expect(function() { return converter.datasEltId="datas"; }).not.toThrowError(); + expect( () => { return converter.datasViewElt={ id:"datas" }; }).not.toThrowError(); }); - it("Doit générer une erreur s'il n'y a pas d'éléments dans la page pour les ids fournis pour recevoir les sélecteurs.", function() + it("Doit générer une erreur s'il n'y a pas d'éléments dans la page pour chaque id fourni pour afficher un des sélecteurs.", () => { - expect(function() { return converter.datasSelectors=[{ colCSV:2, idElt:"selector2"},{ colCSV:3, idElt:"selector3"}]; }).toThrowError(errors.elementNotFound+"selector3"); + expect( () =>{ return converter.datasSelectors=[{ colCSV:2, id:"selector2"},{ colCSV:3, id:"selector3"}]; }).toThrowError(errors.elementNotFound+"selector3"); }); - it("Ne doit pas générer une erreur s'il y a bien un élément dans la page pour chaque id fourni pour recevoir un sélecteur.", function() + it("Doit générer une erreur si tous les numéros de colonne des sélecteurs ne sont pas des nombres naturels.", () => { - expect(function() { return converter.datasSelectors=[{ colCSV:2, idElt:"selector1"},{ colCSV:3, idElt:"selector2"}]; }).not.toThrowError(); + expect( () => { return converter.datasSelectors=[{ colCSV:2.3, id:"selector1"},{ colCSV:3, id:"selector2"}]; }).toThrowError(errors.needNaturalNumber); + expect( () => { return converter.datasSelectors=[{ colCSV:2, id:"selector1"},{ colCSV:-1, id:"selector2"}]; }).toThrowError(errors.needNaturalNumber); + }); + + it("Ne doit pas générer une erreur si les informations fournies pour les sélecteurs sont correctes.", () => + { + expect( () => { return converter.datasSelectors=[{ colCSV:0, id:"selector1"},{ colCSV:3, id:"selector2"}]; }).not.toThrowError(); + }); + + it("Doit générer une erreur si l'url fournie pour le fichier de données est vide.", () => + { + expect( () => { return converter.datasSourceUrl=" "; }).toThrowError(errors.needUrl); + }); + }); + + describe("Running freeCSV2HTML", () => + { + it("Doit générer une erreur si au moins un des deux paramètres nécessaires au fonctionnement n'est pas fourni.", async () => + { + await expectAsync( converter.run()).toBeRejectedWith(new Error(errors.needDatasElt)); + converter.datasViewElt={ id:"datas" }; + await expectAsync( converter.run()).toBeRejectedWith(new Error(errors.needUrl)); + }); + + it("Doit générer une erreur si la lecture du fichier échoue.", async () => + { + converter.datasViewElt={ id:"datas" }; + converter.datasSourceUrl="http://localhost:9876/datas/fake.csv"; + await expectAsync( converter.parse()).toBeRejectedWith(new Error(errors.parserFail)); + }); + + it("Ne doit pas générer d'erreur si les deux paramètres fournis sont ok.", async () => + { + converter.datasViewElt={ id:"datas" }; + converter.datasSourceUrl="http://localhost:9876/datas/elements-chimiques.csv"; + await expectAsync( converter.run()).toBeResolved(); + }); + + it("Doit enregistrer la liste des erreurs rencontrées en parsant le fichier.", async () => + { + converter.datasViewElt={ id:"datas" }; + converter.datasSourceUrl="http://localhost:9876/datas/elements-chimiques-erreurs.csv"; + await converter.parse(); + expect(converter.parseErrors.length).toBeGreaterThan(0); + }); + + it("Ne doit enregistrer aucune erreur de lecture si le fichier est ok.", async () => + { + converter.datasViewElt={ id:"datas" }; + converter.datasSourceUrl="http://localhost:9876/datas/elements-chimiques.csv"; + await converter.parse(); + expect(converter.parseErrors.length).toEqual(0); + }); + + it("Doit afficher une erreur dans la page si aucune donnée n'a été trouvée dans le fichier.", async () => + { + converter.datasViewElt={ id:"datas" }; + converter.datasSourceUrl="http://localhost:9876/datas/elements-chimiques-vide.csv"; + await converter.run(); + let txtDatasViewsElt=document.getElementById("datas").innerHTML; + expect(txtDatasViewsElt).toEqual(errors.datasNotFound); + }); + + it("Doit afficher un tableau correspondant aux données du fichier csv", async () => + { + converter.datasViewElt={ id:"datas" }; + converter.datasSourceUrl="http://localhost:9876/datas/elements-chimiques.csv"; + await converter.run(); + let txtDatasViewsElt=document.getElementById("datas").innerHTML; + expect(txtDatasViewsElt).toEqual(fixtures.datasHTML); }); }); }); \ No newline at end of file