Skip to content

Instantly share code, notes, and snippets.

it("should count increase 1 when invoke increment func", async function () {
jest.isolateModules(() => {
let currentCount = 0;
mockContext(currentCount);
const Container = require("../../component/Counter/CounterContainer").default;
const MockComponent = Container(() => <></>);
let testRenderer = TestRenderer.create(<MockComponent/>);
testRenderer.toTree().rendered.props.increment();
let mockReducer;
let mockState;
function mockContext(count) {
jest.doMock("../../context/CounterContext", () => {
mockReducer = (action) => {
mockState = counterReducer(mockState, action);
};
mockState = {
import React from "react";
import CounterContainer from "./CounterContainer";
export const Counter = CounterContainer(props => {
return(
<div className="wrapper">
<div className="btn-group">
<button onClick={props.increment}>increment</button>
<button onClick={props.decrement}>decrement</button>
</div>
import React, {useContext} from "react";
import {CounterContext} from "../../context/CounterContext";
export default Component => props => {
const {counterState, counterDispatch} = useContext(CounterContext);
const increment = () => {
counterDispatch({type:"increment"});
}
export const counterReducer = (state, action) => {
switch (action.type) {
case 'increment':
return {
...state,
count: state.count + 1
};
case 'decrement':
return {
...state,
import React, {useContext, useReducer} from "react";
import {Counter} from "./component/Counter/Counter";
import "./App.css";
import {CounterContext} from "./context/CounterContext";
import {counterReducer} from "./reducer/counterReducer";
function App() {
const defaultState = useContext(CounterContext);
const [counterState, counterDispatch] = useReducer(counterReducer, defaultState);
return (
import React from 'react';
export const CounterContext = React.createContext({
count: 0
});
(function () {
var currentWindow,
// create an iframe and append to body to load a clean window object
iframe = document.createElement('iframe');
iframe.style.display = 'none';
document.body.appendChild(iframe);
// get the current list of properties on window
currentWindow = Object.getOwnPropertyNames(window);
// filter the list against the properties that exist in the clean window
keys = currentWindow.filter(function(prop) {
console.log(a);
sayHi();
var sayHi = function() {
var name = 'Eric';
console.log('Hi ' + name);
}
var a = 'I"m a';
console.log(a);
sayHi();
function sayHi() {
var name = 'Eric';
console.log('Hi ' + name);
}
var a = 'I"m a';