Skip to content

Instantly share code, notes, and snippets.

@eldyvoon
Created June 29, 2019 07:03
Show Gist options
  • Save eldyvoon/9ec8a5aab6f117ef0888b9ec0a7a3456 to your computer and use it in GitHub Desktop.
Save eldyvoon/9ec8a5aab6f117ef0888b9ec0a7a3456 to your computer and use it in GitHub Desktop.
Users.spec.js
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