-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce synchronous env detection
- Loading branch information
Showing
10 changed files
with
194 additions
and
118 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 was deleted.
Oops, something went wrong.
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,24 @@ | ||
import { render as preactRender } from '@testing-library/preact' | ||
import { h } from 'preact' | ||
|
||
describe('WithEnvironment', () => { | ||
describe('when running within Preact', () => { | ||
beforeEach(() => { | ||
jest.doMock('react-dom', () => require('preact/compat')) | ||
jest.doMock('react', () => require('preact/compat')) | ||
}) | ||
|
||
afterEach(() => { | ||
jest.resetModules() | ||
}) | ||
|
||
it('should detect env as "preact"', () => { | ||
const { WithEnvironment } = require('../src/components/with-environment') | ||
const PrintEnv = (props: any) => h('div', null, props?.env?.name) | ||
|
||
const { container } = preactRender(h(WithEnvironment, null, h(PrintEnv, null))) | ||
|
||
expect(container.innerHTML).toContain('preact') | ||
}) | ||
}) | ||
}) |
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, { FunctionComponent } from 'react' | ||
import { render } from '@testing-library/react' | ||
|
||
import { WithEnvironment } from '../src/components/with-environment' | ||
|
||
describe('WithEnvironment', () => { | ||
it('enhances provided element with `env` prop', () => { | ||
const Mock = jest.fn(() => <div>foo</div>) as FunctionComponent | ||
|
||
render( | ||
<WithEnvironment> | ||
<Mock /> | ||
</WithEnvironment> | ||
) | ||
|
||
expect(Mock).toHaveBeenCalledWith(expect.objectContaining({ env: expect.any(Object) }), expect.anything()) | ||
}) | ||
|
||
it('keeps the original props of the element', () => { | ||
const Echo = ({ message }: { message: string }) => <span>{message}</span> | ||
|
||
const { container } = render( | ||
<WithEnvironment> | ||
<Echo message='hello' /> | ||
</WithEnvironment> | ||
) | ||
|
||
expect(container.innerHTML).toContain('hello') | ||
}) | ||
}) |
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 was deleted.
Oops, something went wrong.
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,53 @@ | ||
import { Component, cloneElement } from 'react' | ||
|
||
import { getEnvironment } from '../get-env' | ||
import { type DetectEnvParams } from '../detect-env' | ||
import { type EnvDetails } from '../env.types' | ||
|
||
export interface WithEnvironmentProps { | ||
// exactly one element must be provided | ||
children: JSX.Element | ||
} | ||
|
||
/** | ||
* Utility component that synchronously detects the current environment (React/Preact etc.) and | ||
* provides it as a prop to the child element. | ||
* | ||
* @example | ||
* ```jsx | ||
* const App = ({ env }: { env: EnvDetails }) => `I'm running in ${env.name}!` | ||
* | ||
* <WithEnvironment> | ||
* <App /> | ||
* </WithEnvironment> | ||
* ``` | ||
*/ | ||
class WithEnvironment extends Component<WithEnvironmentProps> { | ||
constructor(props: WithEnvironmentProps) { | ||
super(props) | ||
} | ||
|
||
detectedEnv: EnvDetails | undefined | ||
|
||
render(...args: any[]) { | ||
if (!this.detectedEnv) { | ||
// unlike React, class components in Preact always receive `props` and `state` in render() | ||
// this is true for both Preact 8.x and 10.x | ||
const hasAnyArguments = args.length > 0 | ||
const detectParams: DetectEnvParams = { | ||
context: { classRenderReceivesAnyArguments: hasAnyArguments }, | ||
} | ||
|
||
this.detectedEnv = getEnvironment(detectParams) | ||
} | ||
|
||
// passes the `env` down as a prop | ||
return cloneElement(this.props.children, { env: this.detectedEnv }) | ||
} | ||
|
||
shouldComponentUpdate() { | ||
return false | ||
} | ||
} | ||
|
||
export { WithEnvironment } |
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
Oops, something went wrong.