Added more tests

This commit is contained in:
Yuriy Kulakov 2021-05-16 20:43:13 +03:00
parent 716af21f48
commit 2f15d74a56
3 changed files with 21 additions and 7 deletions

View File

@ -6,7 +6,7 @@ import * as test_data from "./test_users.json"
import axios from "axios";
import MockAdapter from "axios-mock-adapter";
let mock;
let mock = null
let container = null
beforeEach(() => {
mock = new MockAdapter(axios);
@ -23,7 +23,7 @@ afterEach(() => {
container = null;
});
it("has only main table row without axios request", () => {
it("has no rows without axios request", () => {
act(() => {
render(<Table/>, container);
});
@ -67,12 +67,14 @@ it("show valid number for engineers and light agents", async () => {
});
it("called one request on mount", async () => {
let req = jest.spyOn(Table.prototype, "get_users")
let requests = jest.spyOn(Table.prototype, "getUsers")
await act(async () => {
render(<Table/>, container)
})
expect(req).toHaveBeenCalledTimes(1)
expect(requests).toHaveBeenCalledTimes(1)
requests.mockRestore()
})
it("checkbox count equals users from db count", async () => {
await act(async () => {
render(<Table/>, container)
@ -82,3 +84,15 @@ it("checkbox count equals users from db count", async () => {
let users = test_data.users
expect(checkboxes.length).toEqual(users.length)
})
it("requests occur every one minute", async () => {
jest.useFakeTimers()
let requests = jest.spyOn(Table.prototype, "getUsers")
await act(async () => {
render(<Table/>, container)
})
jest.advanceTimersByTime(60000)
expect(requests).toHaveBeenCalledTimes(2)
jest.useRealTimers()
requests.mockRestore()
})

View File

@ -120,7 +120,7 @@ export class Table extends React.Component {
};
}
async get_users() {
async getUsers() {
await axios.get("/api/users").then((response) => {
this.setState({
users: response.data.users,
@ -137,12 +137,12 @@ export class Table extends React.Component {
}
componentDidMount() {
this.get_users().then(() => {})
this.getUsers().then(() => {})
.catch(reason => {
console.log(reason)
});
this.interval = setInterval(() => {
this.get_users().catch(reason => {
this.getUsers().catch(reason => {
console.log(reason)
})
}, 60000);