-
-
Save eldyvoon/0f18ad5a74edf62a3ca438698f3563ad to your computer and use it in GitHub Desktop.
Users.split.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"; | |
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