-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Created isPromise.test.ts * Used @testing-library/react * Fixed jest config
- Loading branch information
1 parent
1b7d67c
commit 1ed8520
Showing
7 changed files
with
582 additions
and
164 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,7 @@ | ||
{ | ||
"globals": { | ||
"ts-jest": { | ||
"tsconfig": "tsconfig.test.json" | ||
} | ||
} | ||
} |
Large diffs are not rendered by default.
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
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 |
---|---|---|
@@ -1,2 +1,5 @@ | ||
export const isPromise = (p: void | Promise<void>): p is Promise<void> => | ||
typeof p === 'object' && typeof p.then === 'function'; | ||
typeof p === 'object' && | ||
typeof p.then === 'function' && | ||
typeof p.catch === 'function' && | ||
typeof p.finally === 'function'; |
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,51 @@ | ||
import React from 'react'; | ||
import { fireEvent, render } from '@testing-library/react'; | ||
|
||
import { createPopupGroup, PopupGroup } from '../../src/components/PopupGroup'; | ||
import { PopupsContextProvider } from '../../src/components/PopupsContextProvider'; | ||
import { PopupsRenderer } from '../../src/components/PopupsRenderer'; | ||
import { usePopupsFactory } from '../../src/hooks/usePopupsFactory'; | ||
|
||
const PopupComponent = () => { | ||
return <div>This is simple popup</div>; | ||
}; | ||
|
||
const App = ({ group }: { group: PopupGroup }) => { | ||
const create = usePopupsFactory(PopupComponent, {}, group); | ||
|
||
return <button onClick={create}>open</button>; | ||
}; | ||
|
||
describe('PopupsRenderer', () => { | ||
it('should render one popup', async () => { | ||
const group = createPopupGroup(); | ||
|
||
const { getByRole, getByText } = render( | ||
<PopupsContextProvider> | ||
<PopupsRenderer group={group} /> | ||
<App group={group} /> | ||
</PopupsContextProvider> | ||
); | ||
|
||
fireEvent.click(getByRole('button')); | ||
|
||
expect(getByText('This is simple popup')).toBeDefined(); | ||
}); | ||
|
||
it('should render multiple popups', () => { | ||
const group = createPopupGroup(); | ||
|
||
const { getByRole, getAllByText } = render( | ||
<PopupsContextProvider> | ||
<PopupsRenderer group={group} /> | ||
<App group={group} /> | ||
</PopupsContextProvider> | ||
); | ||
|
||
fireEvent.click(getByRole('button')); | ||
fireEvent.click(getByRole('button')); | ||
fireEvent.click(getByRole('button')); | ||
|
||
expect(getAllByText('This is simple popup').length).toBe(3); | ||
}); | ||
}); |
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 { isPromise } from '../../src/utils/isPromise'; | ||
|
||
describe('isPromise utility', () => { | ||
it('should identify promise', () => { | ||
const promise = new Promise<undefined>((res) => setTimeout(res, 2000)); | ||
expect(isPromise(promise)).toBeTruthy(); | ||
}); | ||
|
||
it('should return false when value is not promise', () => { | ||
const a1 = { | ||
then: () => { | ||
// | ||
}, | ||
catch: () => { | ||
// | ||
}, | ||
finally: 42, | ||
}; | ||
expect(isPromise(a1 as unknown as Promise<void>)).toBeFalsy(); | ||
|
||
const a2 = []; | ||
expect(isPromise(a2 as unknown as Promise<void>)).toBeFalsy(); | ||
}); | ||
}); |
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