From 7bd3dff988e523b6869e9c9df828161a82ff3fb1 Mon Sep 17 00:00:00 2001 From: Alexandre Hajjar Date: Wed, 2 Jun 2021 13:10:42 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Cypress=20stubbing=20simplified?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * filter on hostnames * do not call cy.wait on responses --- ...QwMzA0OTQ0OD9zdGF0dXRfZGlmZnVzaW9uPU8=.json | 296 ------------------ .../ZmllbGRzPWRlcGFydGVtZW50LHJlZ2lvbg==.json | 12 - .../integration/mon-entreprise/gérer.js | 50 ++- 3 files changed, 19 insertions(+), 339 deletions(-) delete mode 100644 mon-entreprise/cypress/fixtures/gérer/aHR0cHM6Ly9lbnRyZXByaXNlLmRhdGEuZ291di5mci9hcGkvc2lyZW5lL3YzL3VuaXRlc19sZWdhbGVzLzQwMzA0OTQ0OD9zdGF0dXRfZGlmZnVzaW9uPU8=.json delete mode 100644 mon-entreprise/cypress/fixtures/gérer/aHR0cHM6Ly9nZW8uYXBpLmdvdXYuZnIvY29tbXVuZXMvNzUxMjA/ZmllbGRzPWRlcGFydGVtZW50LHJlZ2lvbg==.json diff --git a/mon-entreprise/cypress/fixtures/gérer/aHR0cHM6Ly9lbnRyZXByaXNlLmRhdGEuZ291di5mci9hcGkvc2lyZW5lL3YzL3VuaXRlc19sZWdhbGVzLzQwMzA0OTQ0OD9zdGF0dXRfZGlmZnVzaW9uPU8=.json b/mon-entreprise/cypress/fixtures/gérer/aHR0cHM6Ly9lbnRyZXByaXNlLmRhdGEuZ291di5mci9hcGkvc2lyZW5lL3YzL3VuaXRlc19sZWdhbGVzLzQwMzA0OTQ0OD9zdGF0dXRfZGlmZnVzaW9uPU8=.json deleted file mode 100644 index 3ab24960e..000000000 --- a/mon-entreprise/cypress/fixtures/gérer/aHR0cHM6Ly9lbnRyZXByaXNlLmRhdGEuZ291di5mci9hcGkvc2lyZW5lL3YzL3VuaXRlc19sZWdhbGVzLzQwMzA0OTQ0OD9zdGF0dXRfZGlmZnVzaW9uPU8=.json +++ /dev/null @@ -1,296 +0,0 @@ -{ - "unite_legale": { - "id": 163930767, - "siren": "403049448", - "statut_diffusion": "O", - "unite_purgee": null, - "date_creation": "1995-11-29", - "sigle": null, - "sexe": null, - "prenom_1": null, - "prenom_2": null, - "prenom_3": null, - "prenom_4": null, - "prenom_usuel": null, - "pseudonyme": null, - "identifiant_association": null, - "tranche_effectifs": "NN", - "annee_effectifs": null, - "date_dernier_traitement": "2020-01-21T19:35:08", - "nombre_periodes": "5", - "categorie_entreprise": null, - "annee_categorie_entreprise": null, - "date_fin": null, - "date_debut": "2019-09-02", - "etat_administratif": "A", - "nom": null, - "nom_usage": null, - "denomination": "MISTRAL MENOZZI", - "denomination_usuelle_1": null, - "denomination_usuelle_2": null, - "denomination_usuelle_3": null, - "categorie_juridique": "6540", - "activite_principale": "68.20A", - "nomenclature_activite_principale": "NAFRev2", - "nic_siege": "00034", - "economie_sociale_solidaire": "N", - "caractere_employeur": "N", - "created_at": "2021-06-02T02:36:00.235+02:00", - "updated_at": "2021-06-02T02:36:00.235+02:00", - "etablissement_siege": { - "id": 465543867, - "siren": "403049448", - "nic": "00034", - "siret": "40304944800034", - "statut_diffusion": "O", - "date_creation": "2019-09-02", - "tranche_effectifs": null, - "annee_effectifs": null, - "activite_principale_registre_metiers": null, - "date_dernier_traitement": "2020-01-21T19:35:08", - "etablissement_siege": "true", - "nombre_periodes": "1", - "complement_adresse": null, - "numero_voie": "551", - "indice_repetition": null, - "type_voie": "RTE", - "libelle_voie": "DE GADAGNE", - "code_postal": "84450", - "libelle_commune": "SAINT-SATURNIN-LES-AVIGNON", - "libelle_commune_etranger": null, - "distribution_speciale": null, - "code_commune": "84119", - "code_cedex": null, - "libelle_cedex": null, - "code_pays_etranger": null, - "libelle_pays_etranger": null, - "complement_adresse_2": null, - "numero_voie_2": null, - "indice_repetition_2": null, - "type_voie_2": null, - "libelle_voie_2": null, - "code_postal_2": null, - "libelle_commune_2": null, - "libelle_commune_etranger_2": null, - "distribution_speciale_2": null, - "code_commune_2": null, - "code_cedex_2": null, - "libelle_cedex_2": null, - "code_pays_etranger_2": null, - "libelle_pays_etranger_2": null, - "date_debut": "2019-09-02", - "etat_administratif": "A", - "enseigne_1": null, - "enseigne_2": null, - "enseigne_3": null, - "denomination_usuelle": null, - "activite_principale": "68.20A", - "nomenclature_activite_principale": "NAFRev2", - "caractere_employeur": "N", - "longitude": "4.938683", - "latitude": "43.953835", - "geo_score": "0.85", - "geo_type": "housenumber", - "geo_adresse": "551 Route de Gadagne 84450 Saint-Saturnin-lès-Avignon", - "geo_id": "84119_0298_00551", - "geo_ligne": "G", - "geo_l4": "551 ROUTE DE GADAGNE", - "geo_l5": null, - "unite_legale_id": 163930767, - "created_at": "2021-06-02T04:50:13.598+02:00", - "updated_at": "2021-06-02T04:50:13.598+02:00" - }, - "numero_tva_intra": "FR94403049448", - "etablissements": [ - { - "id": 465543867, - "siren": "403049448", - "nic": "00034", - "siret": "40304944800034", - "statut_diffusion": "O", - "date_creation": "2019-09-02", - "tranche_effectifs": null, - "annee_effectifs": null, - "activite_principale_registre_metiers": null, - "date_dernier_traitement": "2020-01-21T19:35:08", - "etablissement_siege": "true", - "nombre_periodes": "1", - "complement_adresse": null, - "numero_voie": "551", - "indice_repetition": null, - "type_voie": "RTE", - "libelle_voie": "DE GADAGNE", - "code_postal": "84450", - "libelle_commune": "SAINT-SATURNIN-LES-AVIGNON", - "libelle_commune_etranger": null, - "distribution_speciale": null, - "code_commune": "84119", - "code_cedex": null, - "libelle_cedex": null, - "code_pays_etranger": null, - "libelle_pays_etranger": null, - "complement_adresse_2": null, - "numero_voie_2": null, - "indice_repetition_2": null, - "type_voie_2": null, - "libelle_voie_2": null, - "code_postal_2": null, - "libelle_commune_2": null, - "libelle_commune_etranger_2": null, - "distribution_speciale_2": null, - "code_commune_2": null, - "code_cedex_2": null, - "libelle_cedex_2": null, - "code_pays_etranger_2": null, - "libelle_pays_etranger_2": null, - "date_debut": "2019-09-02", - "etat_administratif": "A", - "enseigne_1": null, - "enseigne_2": null, - "enseigne_3": null, - "denomination_usuelle": null, - "activite_principale": "68.20A", - "nomenclature_activite_principale": "NAFRev2", - "caractere_employeur": "N", - "longitude": "4.938683", - "latitude": "43.953835", - "geo_score": "0.85", - "geo_type": "housenumber", - "geo_adresse": "551 Route de Gadagne 84450 Saint-Saturnin-lès-Avignon", - "geo_id": "84119_0298_00551", - "geo_ligne": "G", - "geo_l4": "551 ROUTE DE GADAGNE", - "geo_l5": null, - "unite_legale_id": 163930767, - "created_at": "2021-06-02T04:50:13.598+02:00", - "updated_at": "2021-06-02T04:50:13.598+02:00" - }, - { - "id": 465543866, - "siren": "403049448", - "nic": "00026", - "siret": "40304944800026", - "statut_diffusion": "O", - "date_creation": "2007-02-01", - "tranche_effectifs": null, - "annee_effectifs": null, - "activite_principale_registre_metiers": null, - "date_dernier_traitement": "2020-01-21T19:35:08", - "etablissement_siege": "false", - "nombre_periodes": "3", - "complement_adresse": null, - "numero_voie": "1950", - "indice_repetition": null, - "type_voie": "CHE", - "libelle_voie": "DE SAINT GENS", - "code_postal": "84200", - "libelle_commune": "CARPENTRAS", - "libelle_commune_etranger": null, - "distribution_speciale": null, - "code_commune": "84031", - "code_cedex": null, - "libelle_cedex": null, - "code_pays_etranger": null, - "libelle_pays_etranger": null, - "complement_adresse_2": null, - "numero_voie_2": null, - "indice_repetition_2": null, - "type_voie_2": null, - "libelle_voie_2": null, - "code_postal_2": null, - "libelle_commune_2": null, - "libelle_commune_etranger_2": null, - "distribution_speciale_2": null, - "code_commune_2": null, - "code_cedex_2": null, - "libelle_cedex_2": null, - "code_pays_etranger_2": null, - "libelle_pays_etranger_2": null, - "date_debut": "2019-09-02", - "etat_administratif": "F", - "enseigne_1": null, - "enseigne_2": null, - "enseigne_3": null, - "denomination_usuelle": null, - "activite_principale": "68.20A", - "nomenclature_activite_principale": "NAFRev2", - "caractere_employeur": "N", - "longitude": "5.046323", - "latitude": "44.032154", - "geo_score": "0.87", - "geo_type": "housenumber", - "geo_adresse": "1950 Chemin de Saint-gens 84200 Carpentras", - "geo_id": "84031_1805_01950", - "geo_ligne": "G", - "geo_l4": "1950 CHEMIN DE SAINT GENS", - "geo_l5": null, - "unite_legale_id": 163930767, - "created_at": "2021-06-02T04:50:13.598+02:00", - "updated_at": "2021-06-02T04:50:13.598+02:00" - }, - { - "id": 465543865, - "siren": "403049448", - "nic": "00018", - "siret": "40304944800018", - "statut_diffusion": "O", - "date_creation": "1995-11-29", - "tranche_effectifs": "NN", - "annee_effectifs": null, - "activite_principale_registre_metiers": null, - "date_dernier_traitement": "2007-02-15T11:44:24", - "etablissement_siege": "false", - "nombre_periodes": "3", - "complement_adresse": null, - "numero_voie": "145", - "indice_repetition": null, - "type_voie": "AV", - "libelle_voie": "DWIGHT EISENHOWER", - "code_postal": "84200", - "libelle_commune": "CARPENTRAS", - "libelle_commune_etranger": null, - "distribution_speciale": null, - "code_commune": "84031", - "code_cedex": null, - "libelle_cedex": null, - "code_pays_etranger": null, - "libelle_pays_etranger": null, - "complement_adresse_2": null, - "numero_voie_2": null, - "indice_repetition_2": null, - "type_voie_2": null, - "libelle_voie_2": null, - "code_postal_2": null, - "libelle_commune_2": null, - "libelle_commune_etranger_2": null, - "distribution_speciale_2": null, - "code_commune_2": null, - "code_cedex_2": null, - "libelle_cedex_2": null, - "code_pays_etranger_2": null, - "libelle_pays_etranger_2": null, - "date_debut": "2007-02-01", - "etat_administratif": "F", - "enseigne_1": null, - "enseigne_2": null, - "enseigne_3": null, - "denomination_usuelle": null, - "activite_principale": "70.2A", - "nomenclature_activite_principale": "NAFRev1", - "caractere_employeur": "N", - "longitude": "5.045559", - "latitude": "44.043377", - "geo_score": "0.73", - "geo_type": "street", - "geo_adresse": "Avenue Dwight Eisenhower 84200 Carpentras", - "geo_id": "84031_0589", - "geo_ligne": "G", - "geo_l4": "AVENUE DWIGHT EISENHOWER", - "geo_l5": null, - "unite_legale_id": 163930767, - "created_at": "2021-06-02T04:50:13.598+02:00", - "updated_at": "2021-06-02T04:50:13.598+02:00" - } - ] - } -} \ No newline at end of file diff --git a/mon-entreprise/cypress/fixtures/gérer/aHR0cHM6Ly9nZW8uYXBpLmdvdXYuZnIvY29tbXVuZXMvNzUxMjA/ZmllbGRzPWRlcGFydGVtZW50LHJlZ2lvbg==.json b/mon-entreprise/cypress/fixtures/gérer/aHR0cHM6Ly9nZW8uYXBpLmdvdXYuZnIvY29tbXVuZXMvNzUxMjA/ZmllbGRzPWRlcGFydGVtZW50LHJlZ2lvbg==.json deleted file mode 100644 index fe941ed7f..000000000 --- a/mon-entreprise/cypress/fixtures/gérer/aHR0cHM6Ly9nZW8uYXBpLmdvdXYuZnIvY29tbXVuZXMvNzUxMjA/ZmllbGRzPWRlcGFydGVtZW50LHJlZ2lvbg==.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "nom": "Paris 20e Arrondissement", - "code": "75120", - "departement": { - "code": "75", - "nom": "Paris" - }, - "region": { - "code": "11", - "nom": "Île-de-France" - } -} \ No newline at end of file diff --git a/mon-entreprise/cypress/integration/mon-entreprise/gérer.js b/mon-entreprise/cypress/integration/mon-entreprise/gérer.js index d72673612..74ee277cb 100644 --- a/mon-entreprise/cypress/integration/mon-entreprise/gérer.js +++ b/mon-entreprise/cypress/integration/mon-entreprise/gérer.js @@ -4,16 +4,13 @@ const FIXTURES_FOLDER = 'cypress/fixtures' const GERER_FIXTURES_FOLDER = `${FIXTURES_FOLDER}/gérer` const writeFixtures = Cypress.env('record_http') !== undefined const stubFixtures = !writeFixtures -const setInterceptResponses = (requestsMatches, responses, ...urlsMatch) => { - // TODO: just capture everything by default? +const setInterceptResponses = (responses, hostnames) => { if (writeFixtures) { - urlsMatch.forEach((urlMatch) => { - cy.intercept(urlMatch, (req) => { - requestsMatches.push(urlMatch) - req.on('after:response', (res) => { - responses[res.url] = res.body - }) - }).as(urlMatch) + cy.intercept('*', (req) => { + if (!hostnames.includes(new URL(req.url).hostname)) return + req.on('after:response', (res) => { + responses[res.url] = res.body + }) }) } else if (stubFixtures) { const urlOfFilepath = (filename) => { @@ -32,40 +29,31 @@ const setInterceptResponses = (requestsMatches, responses, ...urlsMatch) => { }) } } -const waitResponses = (requestsMatches, responses) => { +const waitResponses = (responses) => { if (writeFixtures) { - if (!requestsMatches.length) return - // TODO: we pobably don't need to `cy.wait`. - // Caveat: for a given urlMatch, when any of the matching responses is - // received (the first one), the `cy.wait` will resolve. - // see https://docs.cypress.io/api/commands/intercept#Waiting-on-a-request - cy.wait(requestsMatches.map((urlMatch) => `@${urlMatch}`)).then(() => { - Object.keys(responses).map((url) => { - cy.writeFile( - `${GERER_FIXTURES_FOLDER}/${btoa(url)}.json`, - JSON.stringify(responses[url], null, 2) - ) - }) + // No need to `cy.wait`, we anyway don't care about not-yet-received + // responses, as we don't care about responses not yet utilized in the + // test itself. Caveat: fixtures folder is undeterministic when recording. + Object.keys(responses).map((url) => { + if (responses[url] === undefined) return + cy.writeFile( + `${GERER_FIXTURES_FOLDER}/${btoa(url)}.json`, + JSON.stringify(responses[url], null, 2) + ) }) } } describe('Manage page test', function () { - let requestsMatches = [] let responses = {} - const urlsMatch = [ - 'https://entreprise.data.gouv.fr/api/sirene/v1/full_text/**', - 'https://entreprise.data.gouv.fr/api/sirene/v3/unites_legales/**', - 'https://geo.api.gouv.fr/communes/**', - ] + const hostnamesToRecord = ['entreprise.data.gouv.fr', 'geo.api.gouv.fr'] beforeEach(() => { - requestsMatches = [] responses = {} - setInterceptResponses(requestsMatches, responses, ...urlsMatch) + setInterceptResponses(responses, hostnamesToRecord) cy.visit(fr ? encodeURI('/gérer') : '/manage') }) afterEach(() => { - waitResponses(requestsMatches, responses) + waitResponses(responses) }) it('should not crash', function () { cy.contains(fr ? 'Gérer mon activité' : 'Manage my business')