import React from "react"; import {render, unmountComponentAtNode} from "react-dom"; import {act} from "react-dom/test-utils"; import {TableBody} from "../src/control"; import * as test_data from "./test_users.json" import axios from "axios"; let container = null let table = null let load = null let engineer_count = null let agents_count = null let licences_remaining = null jest.mock("axios", () => { return { get: jest.fn(() => Promise.resolve()) }; }); beforeEach(() => { table = document.createElement("table"); container = document.createElement("tbody"); container.id = "tbody"; table.appendChild(container); load = document.createElement("p"); load.id = "loading" load.innerHTML = "Данные загружаются"; licences_remaining = document.createElement('p') licences_remaining.id = "licences_remaining" engineer_count = document.createElement("p") agents_count = document.createElement("p") engineer_count.className = "info-quantity-value" agents_count.className = "info-quantity-value" document.body.appendChild(table) document.body.appendChild(engineer_count) document.body.appendChild(agents_count) document.body.appendChild(licences_remaining) document.body.appendChild(load) }); afterEach(() => { unmountComponentAtNode(container); container.remove(); table.remove(); engineer_count.remove() agents_count.remove() licences_remaining.remove() container = null; table = null; load = null; engineer_count = null agents_count = null licences_remaining = null }); describe("testing table", (done) => { it("has only main table row without axios request", () => { act(() => { render(, container); }); expect(container.getElementsByTagName("tr").length).toBe(1); }); it("shows valid number of free workplaces", async () => { axios.get.mockImplementation(() => Promise.resolve({data: test_data})); await act(async () => { render(, container) }) let licences = Number(licences_remaining.textContent.replace(/Свободных мест: /, '')) expect(licences).toEqual(1) }); it("Pretext must be deleted on render", () => { act(() => { render(, container) }) expect(document.body).not.toContain(load) }); it("has valid number of table rows with axios request", async () => { axios.get.mockImplementation(() => Promise.resolve({data: test_data})); await act(async () => { render(, container) }) expect(container.getElementsByTagName("tr").length) .toEqual(test_data.users.length + test_data.zendesk_users.length + 1) }); it("show valid number for engineers and light agents", async () => { axios.get.mockImplementation(() => Promise.resolve({data: test_data})); await act(async () => { render(, container) }) expect(Number(engineer_count.textContent)).toEqual(test_data.engineers) expect(Number(agents_count.textContent)).toEqual(test_data.light_agents) }); })