Skip to content

Instantly share code, notes, and snippets.

View Dromediansk's full-sized avatar

Miroslav Pillár Dromediansk

View GitHub Profile
import React, { useMemo } from 'react';
const EmployeeList = ({ employees, positionFilter }) => {
const filteredEmployees = useMemo(() => {
return employees.filter(employee => employee.position === positionFilter);
}, [employees, positionFilter]);
return (
<div>
<h2>Employee List</h2>
@Dromediansk
Dromediansk / Carousel.js
Created January 8, 2022 07:30
Image carousel component for nextJS and tailwind
@Dromediansk
Dromediansk / useFetch.test.js
Created June 13, 2020 14:21
falsy.reference test in useFetch
it("should not fetch data if current reference is falsy", async () => {
const { result } = renderHook(() =>
useFetch(stubbedFetchUrl, { current: false }, [])
);
expect(global.fetch).not.toHaveBeenCalled();
expect(result.current).toStrictEqual({
loading: true,
data: [],
error: null,
@Dromediansk
Dromediansk / useFetch.test.js
Created June 13, 2020 14:13
Handling errors in useFetch
it("should catch error", async () => {
jest.spyOn(global, "fetch").mockImplementation(() =>
Promise.resolve({
json: () => Promise.reject("oops, error occured!"),
})
);
const { result, waitForNextUpdate } = renderHook(() =>
useFetch(stubbedFetchUrl, { current: true }, [])
);
@Dromediansk
Dromediansk / useFetch.test.js
Last active June 13, 2020 13:53
Getting data test in useFetch.test.js
it("should return data after fetch", async () => {
// Mock API
jest.spyOn(global, "fetch").mockImplementation(() =>
Promise.resolve({
json: () => Promise.resolve(stubbedCountries),
})
);
// Execute
const { result, waitForNextUpdate } = renderHook(() =>
@Dromediansk
Dromediansk / useFetch.test.js
Last active June 13, 2020 13:54
Setting up testing file useFetch.test.js
import { renderHook } from "@testing-library/react-hooks";
import { useFetch } from "./customHooks";
const stubbedCountries = [
{ name: "Slovakia", capital: "Bratislava" },
{ name: "Germany", capital: "Berlin" },
];
const stubbedFetchUrl = "api/countriesUrl-mocked";
afterEach(() => {
@Dromediansk
Dromediansk / countries.js
Created June 11, 2020 05:14
Fetching data error
it("should catch error", async () => {
// Mock API
jest.spyOn(global, "fetch").mockImplementation(() =>
Promise.resolve({
json: () =>
Promise.resolve({
status: 500,
}),
})
);
@Dromediansk
Dromediansk / countries.js
Created June 11, 2020 05:10
Testing success of fetching data
import { getAllCountries } from "./countries";
const stubbedCountries = [
{
numericCode: 1,
name: "Slovakia",
capital: "Bratislava",
region: "Europe",
population: 500,
flag: "Slovakia flag",
@Dromediansk
Dromediansk / countries.js
Created June 11, 2020 05:06
fetch data service
const getAllCountries = async () => {
try {
const response = await fetch(`https://restcountries.eu/rest/v2/all`);
const responseCountries = await response.json();
return responseCountries;
} catch (err) {
console.log(err);
}
};
@Dromediansk
Dromediansk / CountriesContainer.js
Created June 11, 2020 04:23
Filtering countries test
it("should filter countries by name when input value is changed", () => {
useFetch.mockReturnValue({
loading: false,
data: stubbedCountries,
error: null,
});
const { getByTestId } = render(<CountriesContainer />);
act(() => {
fireEvent.change(getByTestId("filter-input-name"), {