-
-
Save eldyvoon/9ec8a5aab6f117ef0888b9ec0a7a3456 to your computer and use it in GitHub Desktop.
Users.spec.js
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
import React from "react"; | |
import { render, waitForElement } from "@testing-library/react"; | |
import "jest-dom/extend-expect"; | |
import axios from "axios"; | |
import Users, { url } from "./Users"; | |
test("show loader when it's fetching data, then render users' name on rows", async () => { | |
axios.get.mockResolvedValueOnce({ | |
data: { | |
results: [ | |
{ | |
name: { | |
first: "ali" | |
} | |
}, | |
{ | |
name: { | |
first: "abu" | |
} | |
} | |
] | |
} | |
}); | |
//show loader | |
const { getAllByTestId, getByText } = render(<Users />); | |
expect(getByText(/loading.../i)).toBeInTheDocument(); | |
//check what's rendered in the row | |
const rowValues = await waitForElement(() => | |
getAllByTestId("row").map(row => row.textContent) | |
); | |
expect(rowValues).toEqual(["ali", "abu"]); | |
expect(axios.get).toHaveBeenCalledWith(url); | |
expect(axios.get).toHaveBeenCalledTimes(1); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment