Skip to content

Commit

Permalink
Feat use display name for survey units (#112)
Browse files Browse the repository at this point in the history
* feat: use display name for terminated survey-units

* feat: use display name for review and survey unit listing

* feat: use display name for close page

* feat: change column name in export file

* chore: exclude tests from sonar analysis

* fix: coverage configuration

---------

Co-authored-by: Simon Demaziere <[email protected]>
  • Loading branch information
nicolasTurban and SimonDmz authored Dec 5, 2024
1 parent 786f46d commit b5bbd3e
Show file tree
Hide file tree
Showing 31 changed files with 3,486 additions and 3,128 deletions.
602 changes: 313 additions & 289 deletions cypress/fixtures/getSurveyUnitsSurveyOnSomething.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion cypress/fixtures/getSurveyUnitsVqs.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[
{
"id" : "1032",
"displayName" : "1032",
"ssech" : "hgSkR29",
"location" : "95160",
"city" : "MONTMORENCY",
Expand All @@ -13,6 +14,7 @@
},
{
"id" : "4819",
"displayName" : "4819",
"ssech" : "dlcB55jdf",
"location" : "90000",
"city" : "BELFORT",
Expand All @@ -24,4 +26,3 @@
}
}
]

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sonor",
"version": "0.5.35",
"version": "0.6.0",
"private": true,
"dependencies": {
"@tanstack/react-query": "4.0.5",
Expand Down
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ sonar.organization=inseefr

# exclude tests files for code duplication evaluation
sonar.sources = src
sonar.exclusions = src/tests/**, src/components/**/__snapshots__/* , //src/tests/**
sonar.exclusions = src/tests/**, src/components/**/__snapshots__/* , src/components/**/*.test.jsx, components/**/*.test.jsx, //src/tests/**
sonar.javascript.lcov.reportPaths = coverage/lcov.info
125 changes: 81 additions & 44 deletions src/components/Close/Close.test.jsx
Original file line number Diff line number Diff line change
@@ -1,48 +1,69 @@
// Link.react.test.js
import React from 'react';
import { act } from 'react-dom/test-utils';
import React from "react";
import { act } from "react-dom/test-utils";
import {
render, screen, fireEvent, cleanup, waitForElement, wait,
} from '@testing-library/react';
import { Router, Route, Switch } from 'react-router-dom';
import { createMemoryHistory } from 'history';
import DataFormatter from '../../utils/DataFormatter';
import Close from './Close';
import mocks from '../../tests/mocks';
render,
screen,
fireEvent,
cleanup,
waitForElement,
wait,
} from "@testing-library/react";
import { Router, Route, Switch } from "react-router-dom";
import { createMemoryHistory } from "history";
import DataFormatter from "../../utils/DataFormatter";
import Close from "./Close";
import mocks from "../../tests/mocks";

const history = createMemoryHistory();

const toLocaleDateString = Date.prototype.toLocaleString;
Date.prototype.toLocaleDateString = function() {
return toLocaleDateString.call(this, 'en-EN', { timeZone: 'UTC',year: "numeric", month: "numeric", day: "numeric" });
Date.prototype.toLocaleDateString = function () {
return toLocaleDateString.call(this, "en-EN", {
timeZone: "UTC",
year: "numeric",
month: "numeric",
day: "numeric",
});
};
const OriginalDate = global.Date;
jest
.spyOn(global, 'Date')
.mockImplementation((a) => (a ? new OriginalDate(a) : new OriginalDate('2020-08-20T11:01:58.135Z')));
.spyOn(global, "Date")
.mockImplementation((a) =>
a ? new OriginalDate(a) : new OriginalDate("2020-08-20T11:01:58.135Z")
);
Date.now = jest.fn(() => 1597916474000);

beforeEach(() => {
history.push('/close/vqs2021x00');
history.push("/close/vqs2021x00");
});

afterEach(cleanup);

jest.mock('../../utils/DataFormatter');
jest.mock("../../utils/DataFormatter");

const survey = mocks.surveyVqs;
const resp = mocks.formattedCloseData;

const TestingRouter = ({ ComponentWithRedirection }) => (
<Router history={history}>
<Switch>
<Route path="/close/vqs2021x00" component={() => <ComponentWithRedirection />} />
<Route
path="/close/vqs2021x00"
component={() => <ComponentWithRedirection />}
/>
<Route
path="*"
render={(routeProps) => (
<div>
<div data-testid="Redirect-url">{JSON.stringify(routeProps.history.location.pathname)}</div>
<div data-testid="Redirect-survey">{!routeProps.history.location || !routeProps.history.location.survey || JSON.stringify(routeProps.history.location.survey)}</div>
<div data-testid="Redirect-url">
{JSON.stringify(routeProps.history.location.pathname)}
</div>
<div data-testid="Redirect-survey">
{!routeProps.history.location ||
!routeProps.history.location.survey ||
JSON.stringify(routeProps.history.location.survey)}
</div>
</div>
)}
/>
Expand All @@ -51,83 +72,99 @@ const TestingRouter = ({ ComponentWithRedirection }) => (
);

DataFormatter.mockImplementation(() => ({
getDataForClosePage: (c) => (setTimeout(() => { c(resp); }, 3)),
getDataForClosePage: (c) =>
setTimeout(() => {
c(resp);
}, 3),
}));

const mockDataRetreiver = new DataFormatter();

it('Component is correctly displayed', async () => {
it("Component is correctly displayed", async () => {
const component = render(
<Router history={history}>
<Close location={{ survey }} dataRetreiver={mockDataRetreiver} />
</Router>,
</Router>
);
await waitForElement(() =>
screen.getByTestId("TableHeader_interviewer_name")
);
await waitForElement(() => screen.getByTestId('TableHeader_interviewer_name'));
// Should match snapshot (rows displayed)
expect(component).toMatchSnapshot();
});

it('Sort by interviewer name', async () => {
it("Sort by interviewer name", async () => {
const component = render(
<Router history={history}>
<Close location={{ survey }} dataRetreiver={mockDataRetreiver} />
</Router>,
</Router>
);
await waitForElement(() => screen.getByTestId('TableHeader_interviewer_name'));
screen.getByTestId('TableHeader_interviewer_name').click();
await waitForElement(() =>
screen.getByTestId("TableHeader_interviewer_name")
);
screen.getByTestId("TableHeader_interviewer_name").click();
// Should match snapshot (rows sorted by name)
expect(component).toMatchSnapshot();
});

it('Change page', async () => {
it("Change page", async () => {
const component = render(
<Router history={history}>
<Close location={{ survey }} dataRetreiver={mockDataRetreiver} />
</Router>,
</Router>
);

await waitForElement(() => screen.getByTestId('TableHeader_interviewer_name'));
screen.getByTestId('pagination-nav').lastChild.firstChild.click();
await waitForElement(() =>
screen.getByTestId("TableHeader_interviewer_name")
);
screen.getByTestId("pagination-nav").lastChild.firstChild.click();

// Should match snapshot (rows displayed have changed)
expect(component).toMatchSnapshot();
});

it('Change pagination size', async () => {
it("Change pagination size", async () => {
const component = render(
<Router history={history}>
<Close location={{ survey }} dataRetreiver={mockDataRetreiver} />
</Router>,
</Router>
);
await waitForElement(() =>
screen.getByTestId("TableHeader_interviewer_name")
);
await waitForElement(() => screen.getByTestId('TableHeader_interviewer_name'));
act(() => {
fireEvent.change(component.getByTestId('pagination-size-selector'), { target: { value: '10' } });
fireEvent.change(component.getByTestId("pagination-size-selector"), {
target: { value: "10" },
});
});
// Should match snapshot (all 8 rows are now displayed)
expect(component).toMatchSnapshot();
});

it('Export table', async () => {
it("Export table", async () => {
const component = render(
<Router history={history}>
<Close location={{ survey }} dataRetreiver={mockDataRetreiver} />
</Router>,
</Router>
);
await waitForElement(() =>
screen.getByTestId("TableHeader_interviewer_name")
);
await waitForElement(() => screen.getByTestId('TableHeader_interviewer_name'));
const realRemoveFunc = HTMLAnchorElement.prototype.remove;
const removeElmMock = jest.fn();
HTMLAnchorElement.prototype.remove = removeElmMock;

const fileTitle = 'UE_à_clore_8202020.csv';
const fileContent = 'data:text/csv;charset=utf-8,%EF%BB%BFSurvey;Identifier;Interviewer;Ssech;Department;Town;Provisional%20state%0ASimpsons;1023;Dupont%20Chlo%C3%A9;hgSkR29;95;MONTMORENCY;ANS%0ASimpsons;4811;Boulanger%20Jacques;dlcB55jdf;90;BELFORT;ANS%0ASimpsons;1024;Dupont%20Chlo%C3%A9;hgSkR29;95;MONTMORENCY;ANS%0AVQS;4812;Boulanger%20Jacques;dlcB55jdf;90;BELFORT;ANS%0AVQS;1025;Dupont%20Chlo%C3%A9;hgSkR29;95;MONTMORENCY;ANS%0AVQS;4813;Boulanger%20Jacques;dlcB55jdf;90;BELFORT;ANS%0AVQS;1027;Dupont%20Chlo%C3%A9;hgSkR29;95;MONTMORENCY;ANS%0AVQS;4815;Fabres%20Thierry;dlcB55jdf;90;BELFORT;ANS%0AVQS;1028;Dupont%20Chlo%C3%A9;hgSkR29;95;MONTMORENCY;ANS%0AVQS;4816;Boulanger%20Jacques;dlcB55jdf;90;BELFORT;ANS%0AVQS;1029;Fabres%20Thierry;hgSkR29;95;MONTMORENCY;ANS%0AVQS;4817;Boulanger%20Jacques;dlcB55jdf;90;BELFORT;ANS%0AVQS;1030;Dupont%20Chlo%C3%A9;hgSkR29;95;MONTMORENCY;ANS%0ALC%202020;4818;Boulanger%20Jacques;dlcB55jdf;90;BELFORT;ANS%0ALC%202020;1032;Dupont%20Chlo%C3%A9;hgSkR29;95;MONTMORENCY;ANS%0ALC%202020;4819;Boulanger%20Jacques;dlcB55jdf;90;BELFORT;ANS';
screen.getByTestId('export-button').click();
const downnloadLink = component.baseElement.querySelector('a[download]');
const fileTitle = "UE_à_clore_8202020.csv";
const fileContent =
"data:text/csv;charset=utf-8,%EF%BB%BFSurvey;Identifier;Survey%20unit%20identifier;Interviewer;Ssech;Department;Town;Provisional%20state%0ASimpsons;1023;1023;Dupont%20Chlo%C3%A9;hgSkR29;95;MONTMORENCY;ANS%0ASimpsons;4811;4811;Boulanger%20Jacques;dlcB55jdf;90;BELFORT;ANS%0ASimpsons;1024;1024;Dupont%20Chlo%C3%A9;hgSkR29;95;MONTMORENCY;ANS%0AVQS;4812;4812;Boulanger%20Jacques;dlcB55jdf;90;BELFORT;ANS%0AVQS;1025;1025;Dupont%20Chlo%C3%A9;hgSkR29;95;MONTMORENCY;ANS%0AVQS;4813;4813;Boulanger%20Jacques;dlcB55jdf;90;BELFORT;ANS%0AVQS;1027;1027;Dupont%20Chlo%C3%A9;hgSkR29;95;MONTMORENCY;ANS%0AVQS;4815;4815;Fabres%20Thierry;dlcB55jdf;90;BELFORT;ANS%0AVQS;1028;1028;Dupont%20Chlo%C3%A9;hgSkR29;95;MONTMORENCY;ANS%0AVQS;4816;4816;Boulanger%20Jacques;dlcB55jdf;90;BELFORT;ANS%0AVQS;1029;1029;Fabres%20Thierry;hgSkR29;95;MONTMORENCY;ANS%0AVQS;4817;4817;Boulanger%20Jacques;dlcB55jdf;90;BELFORT;ANS%0AVQS;1030;1030;Dupont%20Chlo%C3%A9;hgSkR29;95;MONTMORENCY;ANS%0ALC%202020;4818;4818;Boulanger%20Jacques;dlcB55jdf;90;BELFORT;ANS%0ALC%202020;1032;1032;Dupont%20Chlo%C3%A9;hgSkR29;95;MONTMORENCY;ANS%0ALC%202020;4819;4819;Boulanger%20Jacques;dlcB55jdf;90;BELFORT;ANS";
screen.getByTestId("export-button").click();
const downnloadLink = component.baseElement.querySelector("a[download]");

// Check that file title is correct
expect(downnloadLink.getAttribute('download')).toEqual(fileTitle);
expect(downnloadLink.getAttribute("download")).toEqual(fileTitle);

// Check that file content is correct
expect(downnloadLink.getAttribute('href')).toEqual(fileContent);
expect(downnloadLink.getAttribute("href")).toEqual(fileContent);

// Should match snapshot (with link attached)
expect(component).toMatchSnapshot();
Expand All @@ -137,4 +174,4 @@ it('Export table', async () => {

HTMLAnchorElement.prototype.remove = realRemoveFunc;
downnloadLink.remove();
});
});
Loading

0 comments on commit b5bbd3e

Please sign in to comment.