-
Notifications
You must be signed in to change notification settings - Fork 471
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
144 additions
and
125 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 |
---|---|---|
|
@@ -18,14 +18,11 @@ jobs: | |
|
||
- uses: ./.github/workflows/yarn | ||
|
||
- name: Run tests | ||
run: 'yarn test:ci' | ||
|
||
- name: Comment in failing tests | ||
uses: mattallty/[email protected] | ||
if: failure() | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
- name: Annotations and coverage report | ||
uses: ArtiomTr/jest-coverage-report-action@v2 | ||
with: | ||
test-command: 'echo unit tests already executed' | ||
coverage-comment: false | ||
skip-step: install | ||
annotations: failed-tests | ||
package-manager: yarn | ||
test-script: yarn test:ci | ||
github-token: ${{ secrets.GITHUB_TOKEN }} |
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
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
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,34 @@ | ||
import { render, waitFor, screen } from '@/tests/test-utils' | ||
import madProps from '../mad-props' | ||
|
||
describe('madProps', () => { | ||
it('should map a set of props to hooks', async () => { | ||
const Component = ({ foo, bar }: { foo: string; bar: string }) => <>{foo + bar}</> | ||
|
||
const MappedComponent = madProps(Component, { | ||
foo: () => 'foo', | ||
bar: () => 'bar', | ||
}) | ||
|
||
render(<MappedComponent />) | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText('foobar')).toBeInTheDocument() | ||
}) | ||
}) | ||
|
||
it('should accept additional props', async () => { | ||
const Component = ({ foo, bar, baz }: { foo: string; bar: string; baz: string }) => <>{foo + bar + baz}</> | ||
|
||
const MappedComponent = madProps(Component, { | ||
foo: () => 'foo', | ||
bar: () => 'bar', | ||
}) | ||
|
||
render(<MappedComponent baz="baz" />) | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText('foobarbaz')).toBeInTheDocument() | ||
}) | ||
}) | ||
}) |
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,40 @@ | ||
import type { ComponentType, FC } from 'react' | ||
import React, { memo } from 'react' | ||
|
||
type HookMap<P> = { | ||
[K in keyof P]?: () => P[K] | ||
} | ||
|
||
/** | ||
* Injects props into a component using hooks. | ||
* This allows to keep the original component pure and testable. | ||
* | ||
* @param Component The component to wrap | ||
* @param hookMap A map of hooks to use, keys are the props to inject, values are the hooks | ||
* @returns A new component with the props injected | ||
*/ | ||
const madProps = <P extends Record<string, unknown>, H extends keyof P>( | ||
Component: ComponentType<P>, | ||
hookMap: HookMap<Pick<P, H>>, | ||
): FC<Omit<P, H>> => { | ||
const MadComponent = (externalProps: Omit<P, H>) => { | ||
let newProps: P = { ...externalProps } as P | ||
|
||
for (const key in hookMap) { | ||
const hook = hookMap[key] | ||
if (hook !== undefined) { | ||
newProps[key as H] = hook() | ||
} | ||
} | ||
|
||
return <Component {...newProps} /> | ||
} | ||
|
||
MadComponent.displayName = Component.displayName || Component.name | ||
|
||
// Wrapping MadComponent with React.memo and casting to FC<Omit<P, H>> | ||
// The casting is only needed because of memo, the component itself satisfies the type | ||
return memo(MadComponent) as unknown as FC<Omit<P, H>> | ||
} | ||
|
||
export default madProps |
Oops, something went wrong.