-
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.
- Loading branch information
Showing
7 changed files
with
92 additions
and
21 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
40 changes: 40 additions & 0 deletions
40
...sponsibility.outsideProvider.callUseBuildComponentCallback.withFallbackComponent.test.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** @jest-environment jsdom */ | ||
/// <reference types="@types/jest" /> | ||
|
||
import { render } from '@testing-library/react'; | ||
import React from 'react'; | ||
|
||
import createChainOfResponsibility from './createChainOfResponsibility'; | ||
|
||
type Props = { children?: never }; | ||
|
||
let consoleErrorMock: jest.SpyInstance; | ||
|
||
beforeEach(() => { | ||
// Currently, there is no way to hide the caught exception thrown by render(). | ||
// We are mocking `console.log` to hide the exception. | ||
consoleErrorMock = jest.spyOn(console, 'error').mockImplementation(() => jest.fn()); | ||
}); | ||
|
||
afterEach(() => { | ||
consoleErrorMock.mockRestore(); | ||
}); | ||
|
||
test('when calling useBuildComponentCallback() outside of its <Provider> with fallbackComponent should render', () => { | ||
// GIVEN: useBuildComponentCallback() from a newly created chain of responsibility. | ||
const { useBuildComponentCallback } = createChainOfResponsibility<undefined, Props>(); | ||
|
||
const Fallback = () => <div>Hello, World!</div>; | ||
|
||
const App = () => { | ||
const Component = useBuildComponentCallback()(undefined, { fallbackComponent: Fallback }); | ||
|
||
return Component && <Component />; | ||
}; | ||
|
||
// WHEN: Render. | ||
const result = render(<App />); | ||
|
||
// THEN: Should render fallbackComponent. | ||
expect(result.container).toHaveProperty('textContent', 'Hello, World!'); | ||
}); |
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
30 changes: 30 additions & 0 deletions
30
...createChainOfResponsibility.outsideProvider.renderProxy.withoutFallbackComponent.test.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** @jest-environment jsdom */ | ||
/// <reference types="@types/jest" /> | ||
|
||
import { render } from '@testing-library/react'; | ||
import React from 'react'; | ||
|
||
import createChainOfResponsibility from './createChainOfResponsibility'; | ||
|
||
type Props = { children?: never }; | ||
|
||
let consoleErrorMock: jest.SpyInstance; | ||
|
||
beforeEach(() => { | ||
// Currently, there is no way to hide the caught exception thrown by render(). | ||
// We are mocking `console.log` to hide the exception. | ||
consoleErrorMock = jest.spyOn(console, 'error').mockImplementation(() => jest.fn()); | ||
}); | ||
|
||
afterEach(() => { | ||
consoleErrorMock.mockRestore(); | ||
}); | ||
|
||
test('when rendering <Proxy> outside of its <Provider> without fallbackComponent should throw', () => { | ||
// GIVEN: A <Proxy> of a newly created chain of responsibility. | ||
const { Proxy } = createChainOfResponsibility<undefined, Props>(); | ||
|
||
// WHEN: Render. | ||
// THEN: It should throw an error saying "This component/hook cannot be used outside of its corresponding <Provider>". | ||
expect(() => render(<Proxy />)).toThrow('This component/hook cannot be used outside of its corresponding <Provider>'); | ||
}); |
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