Skip to content

Instantly share code, notes, and snippets.

@tawhidulIKhan
Last active August 1, 2022 08:53
Show Gist options
  • Save tawhidulIKhan/55bddac79ef5b9872a8b38ce15094862 to your computer and use it in GitHub Desktop.
Save tawhidulIKhan/55bddac79ef5b9872a8b38ce15094862 to your computer and use it in GitHub Desktop.
mock
const fetchUsers = require("./index");
const getError = require("./error");
const getFood = require('./food');
const axios = require("axios");
jest.mock('axios');
test('should ger users data', async () => {
const users = [{ name: 'Bob' }];
const resp = { data: users };
axios.mockResolvedValue(resp);
const result = await fetchUsers();
console.log(result);
// expect(result).toEqual(users);
})
test("fetch user correctly", () => {
const value = 2;
expect(value).toBeGreaterThan(1);
})
test("should display name", () => {
const name = "John doe";
expect(name).not.toMatch(/B/)
})
test("Fruits should have mango", () => {
const fruits = ["Pine Apple", "Orange", "Jack fruit", "Mango"];
expect(fruits).toContain("Mango");
expect(new Set(fruits)).toContain("Orange");
})
test("should throw error", () => {
expect(() => getError()).toThrow();
expect(() => getError()).toThrow(Error);
expect(() => getError()).toThrow("You gave wrong input")
})
test("should display food", async () => {
const food = await getFood();
expect(food).toBe("Rice and meat")
});
//////////
import React from 'react';
import axios from 'axios';
import { render, screen, act } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
// eslint-disable-next-line @typescript-eslint/no-var-requires
import TipsCreate from './TipsCreate';
jest.mock('antd', () => {
const antd = jest.requireActual('antd');
const Select = ({ children, onChange, placeholder }) => {
return (
<select
data-testid={placeholder}
onChange={(e) => onChange(e.target.value)}
>
{children}
</select>
);
};
Select.Option = ({ children, value, ...otherProps }) => {
return (
<option data-testid={value} {...otherProps}>
{children}
</option>
);
};
return {
...antd,
Select,
};
});
jest.mock('axios');
// const mockedAxios = axios as jest.Mocked<typeof axios>;
describe('Testing modal is rendered in create Tips', () => {
beforeAll(() => {
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // Deprecated
removeListener: jest.fn(), // Deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
});
});
test('demo test', async () => {
const response = {
data: [
{
uuid: '1',
name: 'Productivity',
slug: 'productivity',
image: '/storage/categories/productivity.png',
},
{
uuid: '2',
name: 'Daily Life',
slug: 'daily-life',
image: '/storage/categories/daily_life.png',
},
{
uuid: '3',
name: 'Life Hack',
slug: 'life-hack',
image: '/storage/categories/life_hack.png',
},
],
status: 200,
};
const promise = Promise.resolve(response);
axios.get.mockImplementationOnce(() => promise);
const { rerender } = render(<TipsCreate />);
// screen.debug();
await act(async () => {
userEvent.click(
screen.getByRole('button', { name: 'Create Tips Create Tips' })
);
});
expect(
await screen.findByRole('button', { name: 'Discard' })
).toBeInTheDocument();
expect(await screen.findByTestId('Select category')).toBeInTheDocument();
rerender(<TipsCreate />);
screen.debug(null, 20000);
});
});
import React from 'react';
import axios from 'axios';
import { render, screen, act } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
// eslint-disable-next-line @typescript-eslint/no-var-requires
import TipsCreate from './TipsCreate';
import CategoryAPI from '../../api/category/request';
import { ExperimentTwoTone } from '@ant-design/icons';
jest.mock('antd', () => {
const antd = jest.requireActual('antd');
const Select = ({ children, onChange, placeholder }) => {
return (
<div data-testid={placeholder} onChange={(e) => onChange(e.target.value)}>
{children}
</div>
);
};
Select.Option = ({ children, value, ...otherProps }) => {
return (
<div data-testid={value} {...otherProps}>
{children}
</div>
);
};
return {
...antd,
Select,
};
});
// jest.mock('axios');
jest.mock('../../api/category/request');
// const mockedAxios = axios as jest.Mocked<typeof axios>;
describe('Testing modal is rendered in create Tips', () => {
beforeAll(() => {
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // Deprecated
removeListener: jest.fn(), // Deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
});
});
test('demo test', async () => {
const response = {
data: [
{
uuid: '1',
name: 'Productivity',
slug: 'productivity',
image: '/storage/categories/productivity.png',
},
{
uuid: '2',
name: 'Daily Life',
slug: 'daily-life',
image: '/storage/categories/daily_life.png',
},
{
uuid: '3',
name: 'Life Hack',
slug: 'life-hack',
image: '/storage/categories/life_hack.png',
},
],
status: 200,
};
const promise = Promise.resolve(response);
// axios.get.mockImplementationOnce(() => promise);
CategoryAPI.all.mockImplementationOnce(() => promise);
const { rerender } = render(<TipsCreate />);
// screen.debug();
await act(async () => {
userEvent.click(
screen.getByRole('button', { name: 'Create Tips Create Tips' })
);
});
expect(
await screen.findByRole('button', { name: 'Discard' })
).toBeInTheDocument();
expect(await screen.findByTestId('Select category')).toBeInTheDocument();
expect(await screen.findByText('Productivity')).toBeInTheDocument();
screen.debug(null, 10000);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment