-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from jeonghwan-kim/feat/myreact-context
MyReact 컨택스트 개선
- Loading branch information
Showing
5 changed files
with
7,751 additions
and
1,851 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// package.json에서 꺼냄. babel-jest rootMode 설정 동작때문. | ||
module.exports = { | ||
presets: [ | ||
"@babel/preset-env", | ||
[ | ||
"@babel/preset-react", | ||
{ | ||
runtime: "automatic", | ||
}, | ||
], | ||
], | ||
}; |
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,33 @@ | ||
import { render } from "@testing-library/react"; | ||
|
||
import MyReact from "../src/lib/MyReact"; | ||
|
||
let consumerRenderCount = 0; | ||
|
||
describe("useContext", () => { | ||
test("컴포넌트가 한 번만 렌더 된다.", () => { | ||
// 컨택스트를 만든다. | ||
const context = MyReact.createContext({ foo: 0 }); | ||
|
||
// 컨택스트에 값을 제공한다. | ||
const Provider = ({ children }) => { | ||
return <context.Provider value={{ foo: 1 }}>{children}</context.Provider>; | ||
}; | ||
|
||
// 훅으로 컨택스트 값을 사용한다. | ||
const Consumer = () => { | ||
consumerRenderCount = consumerRenderCount + 1; | ||
const value = MyReact.useContext(context); | ||
return <div>{value.foo}</div>; | ||
}; | ||
|
||
const { getByText } = render( | ||
<Provider> | ||
<Consumer /> | ||
</Provider> | ||
); | ||
|
||
expect(getByText(1)).toBeTruthy(); | ||
expect(consumerRenderCount).toBe(1); | ||
}); | ||
}); |
Oops, something went wrong.