Skip to content

Commit b40f7b1

Browse files
authored
Merge pull request #7 from alexlopezp09/jest-and-typescript-enabled
added jest config file, renamed every js file to ts or tsx
2 parents d1bb94c + bf2dfe5 commit b40f7b1

27 files changed

+653
-17596
lines changed

redux-excersice/.prettierrc.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"trailingComma": "es5",
2+
"trailingComma": "all",
33
"tabWidth": 4,
44
"semi": false,
55
"singleQuote": true

redux-excersice/jest.config.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// jest.config.ts
2+
3+
import type { Config } from '@jest/types'
4+
// Sync object
5+
const config: Config.InitialOptions = {
6+
verbose: true,
7+
transform: {
8+
'^.+\\.tsx?$': 'ts-jest',
9+
},
10+
moduleNameMapper: {
11+
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
12+
'<rootDir>/__mocks__/fileMock.js',
13+
'\\.(scss|sass|css)$': 'identity-obj-proxy',
14+
},
15+
globals: {
16+
window: {},
17+
document: {},
18+
},
19+
}
20+
export default config

redux-excersice/package-lock.json

+345-17,246
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

redux-excersice/package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@
4242
]
4343
},
4444
"devDependencies": {
45+
"@babel/preset-typescript": "7.21.0",
4546
"jest-dom": "^4.0.0",
46-
"prettier": "2.8.4"
47+
"prettier": "2.8.4",
48+
"ts-jest": "29.0.5",
49+
"ts-node": "10.9.1"
4750
}
4851
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { cleanup, render, screen } from '@testing-library/react'
2+
import { DispatchContext, initialState, StateContext } from '../constants'
3+
import { ComponentB } from './ComponentB'
4+
5+
describe('App unit tests', () => {
6+
const dispatch = jest.fn
7+
const renderComponentWithContext = async (
8+
initStateContextValue: typeof initialState,
9+
) => {
10+
console.log(ComponentB)
11+
return render(
12+
<StateContext.Provider value={initStateContextValue}>
13+
<DispatchContext.Provider value={dispatch}>
14+
<ComponentB />
15+
</DispatchContext.Provider>
16+
</StateContext.Provider>,
17+
)
18+
}
19+
20+
afterEach(() => {
21+
cleanup()
22+
jest.clearAllMocks()
23+
})
24+
//case only to ensure a correct instalation
25+
it('renders without crashing', async () => {
26+
const { container } = await renderComponentWithContext({ name: '' })
27+
expect(container).toBeTruthy()
28+
})
29+
30+
// UseContext testing with RTL
31+
it.skip('renders but setting values to the form', () => {
32+
const emptyFormValuesToContext = initialState
33+
renderComponentWithContext(emptyFormValuesToContext)
34+
expect(screen.getByDisplayValue(initialState.name)).toBeDefined()
35+
})
36+
})
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import React, { useReducer } from 'react'
2+
import reducer from '../reducer'
3+
import { StateContext, DispatchContext, initialState } from '../constants'
4+
import { useReadPath } from '../hooks/useReadPath'
5+
import { FormComponent } from './FormComponent'
6+
import './styles.css'
7+
8+
export default function App() {
9+
//lets use useReducer to get a state and dispatch
10+
11+
const { name } = useReadPath()
12+
13+
const initStateWithHref = () => {
14+
console.log(
15+
'initializing useReducer with name constant from useReadPath',
16+
name,
17+
)
18+
return {
19+
name,
20+
}
21+
}
22+
23+
const [state, dispatch] = useReducer(
24+
reducer,
25+
initialState,
26+
initStateWithHref,
27+
)
28+
// onBlur event handler
29+
30+
return (
31+
<StateContext.Provider value={state}>
32+
<DispatchContext.Provider value={dispatch}>
33+
<div className="App">
34+
<h1>useReducer and useContext Demo</h1>
35+
<FormComponent />
36+
</div>
37+
</DispatchContext.Provider>
38+
</StateContext.Provider>
39+
)
40+
}

redux-excersice/src/components/ComponentA.js

-40
This file was deleted.

redux-excersice/src/components/ComponentA.test.js

-9
This file was deleted.

redux-excersice/src/components/ComponentA.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ import React, { useReducer } from 'react'
22
import ComponentB from './ComponentB'
33
import reducer from '../reducer'
44
import { StateContext, DispatchContext, initialState } from '../constants'
5-
import './styles.css'
65
import { useReadPath } from '../hooks/useReadPath'
6+
import './styles.css'
77

8-
export default function ComponentA() {
8+
export default function App() {
99
//lets use useReducer to get a state and dispatch
1010

1111
const { name } = useReadPath()
1212

1313
const initStateWithHref = () => {
1414
console.log(
1515
'initializing useReducer with name constant from useReadPath',
16-
name
16+
name,
1717
)
1818
return {
1919
name,
@@ -23,7 +23,7 @@ export default function ComponentA() {
2323
const [state, dispatch] = useReducer(
2424
reducer,
2525
initialState,
26-
initStateWithHref
26+
initStateWithHref,
2727
)
2828
// onBlur event handler
2929

redux-excersice/src/components/ComponentB.js

-22
This file was deleted.

redux-excersice/src/components/ComponentB.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React, { useContext } from 'react'
2-
import ComponentC from './ComponentC'
32
import { StateContext, DispatchContext } from '../constants'
3+
import { ComponentC } from './ComponentC'
44

5-
export default function ComponentB() {
5+
export const ComponentB = () => {
66
const state = useContext(StateContext)
77
const dispatch = useContext(DispatchContext)
88
const handleOnChangeEvent = (
9-
event: React.ChangeEvent<HTMLInputElement>
9+
event: React.ChangeEvent<HTMLInputElement>,
1010
) => {
1111
dispatch({
1212
type: 'CHANGE_NAME',

redux-excersice/src/components/ComponentC.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React, { useContext } from 'react'
22
import { StateContext, DispatchContext } from '../constants'
3-
4-
export default function ComponentC() {
3+
export const ComponentC = () => {
54
const state = useContext(StateContext)
65
const dispatch = useContext(DispatchContext)
76
const handleReset = () => {

redux-excersice/src/components/CustomInput/index.js

-32
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import React, { ReactNode } from 'react'
2+
import { useCallback } from 'react'
3+
4+
interface CustomInputProps {
5+
inputName: string
6+
onInputChange: (inputName: string, value: string) => void
7+
labelText: string
8+
inputType: string
9+
hasError: boolean
10+
inputValue: string
11+
}
12+
13+
export const CustomInput = ({
14+
inputName,
15+
onInputChange,
16+
labelText,
17+
inputType,
18+
hasError,
19+
inputValue,
20+
}: CustomInputProps) => {
21+
const onFieldChange = useCallback(
22+
(event: React.ChangeEvent<HTMLInputElement>) => {
23+
onInputChange(inputName, event.target.value)
24+
},
25+
[onInputChange, inputName],
26+
)
27+
28+
return (
29+
<>
30+
<label>{labelText}</label>
31+
<input
32+
type={inputType}
33+
name={inputName}
34+
id={inputName}
35+
onChange={onFieldChange}
36+
value={inputValue}
37+
/>
38+
{hasError && <p>{`Invalid value for "${labelText}".`}</p>}
39+
</>
40+
)
41+
}

0 commit comments

Comments
 (0)