Skip to content

Instantly share code, notes, and snippets.

@eldyvoon
Created June 29, 2019 07:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eldyvoon/0f18ad5a74edf62a3ca438698f3563ad to your computer and use it in GitHub Desktop.
Save eldyvoon/0f18ad5a74edf62a3ca438698f3563ad to your computer and use it in GitHub Desktop.
Users.split.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";
afterEach(() => {
axios.get.mockClear();
});
function mockCall() {
axios.get.mockResolvedValueOnce({
data: {
results: [
{
name: {
first: "ali"
}
},
{
name: {
first: "abu"
}
}
]
}
});
}
test('show loader when it"s fetching data', () => {
mockCall();
const { getByText } = render(<Users />);
expect(getByText(/loading.../i)).toBeInTheDocument();
});
test("render users' name on rows", async () => {
mockCall();
const { getAllByTestId } = render(<Users />);
//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).toHaveBeenCalledTimes(1);
expect(axios.get).toHaveBeenCalledWith(url);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment