-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
34ca357
commit f78b3ca
Showing
3 changed files
with
220 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.