-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat#31 implement: dispatched action logs for debugging
- Loading branch information
Showing
10 changed files
with
160 additions
and
9 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export const repeat = (str: string, times: number) => | ||
new Array(times + 1).join(str); | ||
|
||
export const pad = (num: number, maxLength: number) => | ||
repeat('0', maxLength - num.toString().length) + num; | ||
|
||
export default (time: Date) => | ||
`${pad(time.getHours(), 2)}:${pad(time.getMinutes(), 2)}:${pad( | ||
time.getSeconds(), | ||
2 | ||
)}.${pad(time.getMilliseconds(), 3)}`; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import React, { createContext, useReducer, Dispatch, useContext } from 'react'; | ||
import { AnyAction } from 'redux'; | ||
import { useActionListener } from '../../../../src'; | ||
import middleware from '../middlewares/debugMiddleware'; | ||
|
||
interface CounterState { | ||
cnt: number; | ||
} | ||
interface CounterActions { | ||
increase: () => void; | ||
substract: () => void; | ||
} | ||
type CounterReducer = (state: CounterState, action: AnyAction) => CounterState; | ||
interface CounterContextValues extends CounterState, CounterActions {} | ||
|
||
const counterReducer = ( | ||
state: CounterState, | ||
action: AnyAction | ||
): CounterState => { | ||
switch (action.type) { | ||
case 'INCREASE': | ||
return { | ||
...state, | ||
cnt: state.cnt + action.payload, | ||
}; | ||
case 'SUB': | ||
return { | ||
...state, | ||
cnt: state.cnt - action.payload, | ||
}; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
function increaseAction(dispatch: Dispatch<AnyAction>) { | ||
const action = { | ||
type: 'INCREASE', | ||
payload: 1, | ||
}; | ||
|
||
middleware(action); | ||
dispatch(action); | ||
} | ||
function subAction(dispatch: Dispatch<AnyAction>) { | ||
const action = { | ||
type: 'SUB', | ||
payload: 1, | ||
}; | ||
middleware(action); | ||
dispatch(action); | ||
} | ||
const initialValues: CounterState & CounterActions = { | ||
cnt: 0, | ||
increase: () => {}, | ||
substract: () => {}, | ||
}; | ||
export const CounterContext = createContext<CounterContextValues>( | ||
initialValues | ||
); | ||
export function CounterProvider({ children }: { children: any }) { | ||
const [state, dispatch] = useReducer<CounterReducer>( | ||
counterReducer, | ||
initialValues | ||
); | ||
const actions: CounterActions = { | ||
increase: () => increaseAction(dispatch), | ||
substract: () => subAction(dispatch), | ||
}; | ||
return ( | ||
<CounterContext.Provider value={{ ...state, ...actions }}> | ||
{children} | ||
</CounterContext.Provider> | ||
); | ||
} | ||
|
||
export function Counter() { | ||
const { cnt, increase, substract } = useContext(CounterContext); | ||
return ( | ||
<> | ||
<button type="button" onClick={() => increase()} data-testid="cnt"> | ||
{cnt} | ||
</button> | ||
<button type="button" onClick={() => substract()} data-testid="sub"> | ||
sub | ||
</button> | ||
</> | ||
); | ||
} |
2 changes: 1 addition & 1 deletion
2
test/react/context/components/MiddlewareBeforeDispatchCounter.tsx
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React from 'react'; | ||
import { render, screen, fireEvent, cleanup } from '@testing-library/react'; | ||
import debugMiddleware from './middlewares/debugMiddleware'; | ||
import { | ||
CounterProvider as DebugCounterProvider, | ||
Counter as DebugCounter, | ||
} from './components/DebugCouter'; | ||
|
||
beforeAll(() => {}); | ||
afterEach(() => { | ||
cleanup(); | ||
debugMiddleware.cleanup(); | ||
jest.clearAllMocks(); | ||
}); | ||
afterAll(() => {}); | ||
|
||
test('Should logs are called when isDebugContext = true', () => { | ||
const spy = jest.spyOn(console, 'log'); | ||
|
||
render( | ||
<DebugCounterProvider> | ||
<DebugCounter /> | ||
</DebugCounterProvider> | ||
); | ||
const button = screen.getByTestId('cnt'); | ||
debugMiddleware.addListener('INCREASE', (action) => {}); | ||
fireEvent.click(button); | ||
|
||
expect(spy).toBeCalledTimes(2); | ||
}); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { createMiddleware } from '../../../../src'; | ||
|
||
// gloabl middleware | ||
const middleware = createMiddleware({ isContext: true, isDebugContext: true }); | ||
export default middleware; |
2 changes: 1 addition & 1 deletion
2
test/react/context/middleware.ts → test/react/context/middlewares/middleware.ts
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