Skip to content

Instantly share code, notes, and snippets.

View takethefake's full-sized avatar
🥑

Daniel Schulz takethefake

🥑
View GitHub Profile
import { useEffect, useReducer } from "react";
export interface QueryArgs<T> {
url: string;
options: RequestInit;
deserialize?: (res: Response) => Promise<T>;
}
export interface QueryAction<T> {
type: string;
// src/__mocks_axios.js
const mockAxios = jest.genMockFromModule('axios')
// this is the key to fix the axios.create() undefined error!
mockAxios.create = jest.fn(() => mockAxios)
export default mockAxios
// example test class which uses axios mock
it('will check the matchers and pass', () => {
const user = {
createdAt: new Date(),
id: Math.floor(Math.random() * 20),
name: 'LeBron James',
};
expect(user).toMatchSnapshot({
createdAt: expect.any(Date),
id: expect.any(Number),
it('renders correctly', () => {
const tree = renderer
.create(<Link page="https://prettier.io">Prettier</Link>)
.toJSON();
expect(tree).toMatchInlineSnapshot();
});
// Will be changed after one Jest run into:
it('renders correctly', () => {
import React from 'react';
import Link from '../Link.react';
import renderer from 'react-test-renderer';
it('renders correctly', () => {
const tree = renderer
.create(<Link page="http://www.facebook.com">Facebook</Link>)
.toJSON();
expect(tree).toMatchSnapshot();
});
@takethefake
takethefake / enzyme-test.js
Created October 21, 2018 12:07
Medium: Enzyme example test
import * as React from "react";
import ReactDOM from "react-dom";
import { Input } from "../";
test("it initializes with a defaultValue", () => {
const defaultValue = "DefaultValue";
const wrapper = mount(<Input initialValue={defaultValue} />);
const input = wrapper.find("input");
expect(input.instance().value).toBe(defaultValue);
@takethefake
takethefake / flow-static-check-example.js
Last active October 21, 2018 10:58
Medium: Flow Static Typecheck example
/* @flow */
function foo(x: ?number): string {
if (x) {
return x;
}
return "default string";
}
@takethefake
takethefake / typescript-static-check-example.ts
Last active October 21, 2018 10:59
Medium: Typescript compilation example
function foo(x: number): string {
if (x) {
return x;
}
return "default string";
}
@takethefake
takethefake / jest-test-structure.js
Last active October 21, 2018 10:59
Medium: Abstract test structure in Jest
// posible test
describe("what is the unit under test?", () => {
it("define a test scenario that should be tested", () => {
//write your test here
});
it("define an other test scenario that should be tested", () => {
//write your test here
});
});
@takethefake
takethefake / expect-example-vehicle.js
Last active October 21, 2018 10:59
Medium: Jest Expect example test structure
describe('Vehicle color function',()=>{
it('should return blue for car',()=>{
expect(vehicleColor('car')).toBe('blue');
});
});