Skip to content

Commit

Permalink
coverage ok
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexGrouard committed Feb 21, 2023
1 parent 34ca357 commit f78b3ca
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 126 deletions.
252 changes: 165 additions & 87 deletions src/__tests__/Bills.js
Original file line number Diff line number Diff line change
@@ -1,102 +1,180 @@
/**
* @jest-environment jsdom
*/
import "@testing-library/jest-dom"
import userEvent from '@testing-library/user-event'
import {screen, waitFor} from "@testing-library/dom"
import BillsUI from "../views/BillsUI.js"
import { bills } from "../fixtures/bills.js"
import { ROUTES_PATH} from "../constants/routes.js";
import {localStorageMock} from "../__mocks__/localStorage.js";

import "@testing-library/jest-dom";
import userEvent from "@testing-library/user-event";
import { screen, waitFor } from "@testing-library/dom";
import BillsUI from "../views/BillsUI.js";
import { bills } from "../fixtures/bills.js";
import { ROUTES_PATH } from "../constants/routes.js";
import { localStorageMock } from "../__mocks__/localStorage.js";
import mockStore from "../__mocks__/store.js";
import router from "../app/Router.js";
import Bills from "../containers/Bills.js";

jest.mock("../app/store", () => mockStore);

describe("Given I am connected as an employee", () => {
describe("When I am on Bills Page", () => {
test("Then bill icon in vertical layout should be highlighted", async () => {

Object.defineProperty(window, 'localStorage', { value: localStorageMock })
window.localStorage.setItem('user', JSON.stringify({
type: 'Employee'
}))
const root = document.createElement("div")
root.setAttribute("id", "root")
document.body.append(root)
router()
window.onNavigate(ROUTES_PATH.Bills)
await waitFor(() => screen.getByTestId('icon-window'))
const windowIcon = screen.getByTestId('icon-window')
expect(windowIcon).toHaveClass("active-icon")

})
Object.defineProperty(window, "localStorage", {
value: localStorageMock,
});
window.localStorage.setItem(
"user",
JSON.stringify({
type: "Employee",
})
);
const root = document.createElement("div");
root.setAttribute("id", "root");
document.body.append(root);
router();
window.onNavigate(ROUTES_PATH.Bills);
await waitFor(() => screen.getByTestId("icon-window"));
const windowIcon = screen.getByTestId("icon-window");
expect(windowIcon).toHaveClass("active-icon");
});
test("Then bills should be ordered from earliest to latest", () => {
document.body.innerHTML = BillsUI({ data: bills })
const dates = screen.getAllByText(/^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$/i).map(a => a.innerHTML)
const antiChrono = (a, b) => ((a < b) ? 1 : -1)
const datesSorted = [...dates].sort(antiChrono)
expect(dates).toEqual(datesSorted)
})
// test d'intégration GET

test("fetches bills from mock API GET", async () => {
document.body.innerHTML = BillsUI({ data: bills });
const dates = screen
.getAllByText(
/^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$/i
)
.map((a) => a.innerHTML);
const antiChrono = (a, b) => (a < b ? 1 : -1);
const datesSorted = [...dates].sort(antiChrono);
expect(dates).toEqual(datesSorted);
});
});
});

Object.defineProperty(window, 'localStorage', { value: localStorageMock })
window.localStorage.setItem('user', JSON.stringify({
type: 'Employee'
}))
const root = document.createElement("div")
root.setAttribute("id", "root")
document.body.append(root)
router()
window.onNavigate(ROUTES_PATH.Bills)
await waitFor(() => screen.getByTestId('data-table'))
//expect(screen.getByTestId("data-table")).toBeTruthy()
})
})
describe("When an error occurs on API", () => {
beforeEach(() => {
jest.spyOn(mockStore, "bills")
Object.defineProperty(
window,
'localStorage',
{ value: localStorageMock }
)
window.localStorage.setItem('user', JSON.stringify({
type: 'employee'
}))
const root = document.createElement("div")
root.setAttribute("id", "root")
document.body.appendChild(root)
router()
})
test("fetches bills from an API and fails with 404 message error", async () => {
describe("Given I am connected as an employee", () => {
describe("When I am on Bills Page", () => {
describe("When I click new bill button", () => {
test("A form should open", () => {
Object.defineProperty(window, "localStorage", {
value: localStorageMock,
});
window.localStorage.setItem(
"user",
JSON.stringify({
type: "Employee",
})
);
const root = document.createElement("div");
root.setAttribute("id", "root");
document.body.append(root);
router();
window.onNavigate(ROUTES_PATH.Bills);
const button = screen.getByTestId("btn-new-bill");
userEvent.click(button);
const modal = screen.getByTestId("form-new-bill");
expect(modal).toBeTruthy();
});
});
});
});

mockStore.bills.mockImplementationOnce(() => {
return {
list : () => {
return Promise.reject(new Error("Erreur 404"))
}
}})
window.onNavigate(ROUTES_PATH.Bills)
await new Promise(process.nextTick);
const message = await screen.getByText(/Erreur 404/)
expect(message).toBeTruthy()
})
describe("Given I am connected as an employee", () => {
describe("When I am on Bills Page", () => {
describe("When I click on the eye icon", () => {
test("A modal should open", () => {
Object.defineProperty(window, "localStorage", {
value: localStorageMock,
});
window.localStorage.setItem(
"user",
JSON.stringify({
type: "Employee",
})
);
const root = document.createElement("div");
root.setAttribute("id", "root");
document.body.append(root);
router();
window.onNavigate(ROUTES_PATH.Bills);

const handleClickIconEye = jest.fn(Bills.handleClickIconEye);
const eyes = screen.getAllByTestId("icon-eye");
const eye = eyes[0];

test("fetches messages from an API and fails with 500 message error", async () => {
eye.addEventListener("click", handleClickIconEye);
userEvent.click(eye);
expect(handleClickIconEye).toHaveBeenCalled();

mockStore.bills.mockImplementationOnce(() => {
return {
list : () => {
return Promise.reject(new Error("Erreur 500"))
}
}})
/* const modal = screen.getByRole("document");
expect(modal).toBeTruthy(); */
});
});
});
});
describe("Given I am connected as an employee", () => {
describe("When I am on Bills Page", () => {
test("fetches bills from mock API GET", async () => {
Object.defineProperty(window, "localStorage", {
value: localStorageMock,
});
window.localStorage.setItem(
"user",
JSON.stringify({
type: "Employee",
})
);
const root = document.createElement("div");
root.setAttribute("id", "root");
document.body.append(root);
router();
window.onNavigate(ROUTES_PATH.Bills);
await waitFor(() => screen.getByTestId("tbody"));
expect(screen.getByTestId("tbody")).toBeDefined();
});
describe("When an error occurs on API", () => {
beforeEach(() => {
jest.spyOn(mockStore, "bills");
Object.defineProperty(window, "localStorage", {
value: localStorageMock,
});
window.localStorage.setItem(
"user",
JSON.stringify({
type: "Employee",
email: "a@a",
})
);
const root = document.createElement("div");
root.setAttribute("id", "root");
document.body.appendChild(root);
router();
});
test("fetches bills from an API and fails with 404 message error", async () => {
mockStore.bills.mockImplementationOnce(() => {
return {
list: () => {
return Promise.reject(new Error("Erreur 404"));
},
};
});
window.onNavigate(ROUTES_PATH.Bills);
await new Promise(process.nextTick);
const message = await screen.getByText(/Erreur 404/);
expect(message).toBeTruthy();
});

window.onNavigate(ROUTES_PATH.Dashboard)
await new Promise(process.nextTick);
const message = await screen.getByText(/Erreur 500/)
expect(message).toBeTruthy()
})
})
})
test("fetches messages from an API and fails with 500 message error", async () => {
mockStore.bills.mockImplementationOnce(() => {
return {
list: () => {
return Promise.reject(new Error("Erreur 500"));
},
};
});

window.onNavigate(ROUTES_PATH.Bills);
await new Promise(process.nextTick);
const message = await screen.getByText(/Erreur 500/);
expect(message).toBeTruthy();
});
});
});
});
16 changes: 12 additions & 4 deletions src/__tests__/NewBill.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ describe("Given I am connected as an employee", () => {
document.body.innerHTML = html
const expenseList = screen.getByLabelText('Type de dépense').length
expect(expenseList).toEqual(6)
}) */
test("Then I should be able to choose input", () => {
})
test("Then the mail icon in vertical layout should be highlighted", () => {
const html = NewBillUI()
document.body.innerHTML = html
const mailIcon = screen.getByTestId('icon-mail')
expect(mailIcon).toHaveClass("active-icon")
})*/
test("Then I should be able to input data", () => {

const html = NewBillUI()
document.body.innerHTML = html
Expand All @@ -27,9 +33,11 @@ describe("Given I am connected as an employee", () => {
expect(screen.getByTestId('pct')).toBeTruthy()
expect(screen.getByTestId('commentary')).toBeTruthy()
})

it.todo('Should emulate a user using the form')
it.todo('Should fill the form with mock data from a JSON file (fake API call)')
it.todo('Should test if the newbill icon is highlighted')
it.todo('Should authorise the upload a file in PNG,JPEG or JPG')
it.todo('Should permit the upload if the form is correct')
it.todo('Should make a POST test')
})
})

Expand Down
Loading

0 comments on commit f78b3ca

Please sign in to comment.