Skip to content

Commit

Permalink
chore: update timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
JB1905 committed Apr 4, 2021
1 parent cebf4ab commit cf5db7e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
36 changes: 32 additions & 4 deletions packages/timeouts/__tests__/Timeouts.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ describe('SetTimeout', () => {
const TIMEOUT = 4000;

it('should render content after timeout', async () => {
const onEnabled = jest.fn();
const onDisabled = jest.fn();
const onTimeout = jest.fn();

const { container, getByText } = render(
<SetTimeout enabled timeout={TIMEOUT} onTimeout={onTimeout}>
<SetTimeout
enabled
timeout={TIMEOUT}
onEnabled={onEnabled}
onTimeout={onTimeout}
>
<p>Hello World!</p>
</SetTimeout>
);
Expand All @@ -19,6 +26,8 @@ describe('SetTimeout', () => {
() => {
expect(getByText('Hello World!')).toBeDefined();

expect(onEnabled).toHaveBeenCalled();
expect(onDisabled).not.toHaveBeenCalled();
expect(onTimeout).toHaveBeenCalledTimes(1);
},
{
Expand All @@ -30,6 +39,8 @@ describe('SetTimeout', () => {
});

it('should not render content after timeout when timer is not enabled', async () => {
const onEnabled = jest.fn();
const onDisabled = jest.fn();
const onTimeout = jest.fn();

const { container } = render(
Expand All @@ -38,10 +49,27 @@ describe('SetTimeout', () => {
</SetTimeout>
);

await waitFor(() => expect(onTimeout).toHaveBeenCalledTimes(0), {
timeout: TIMEOUT,
});
await waitFor(
() => {
expect(onEnabled).not.toHaveBeenCalled();
expect(onDisabled).toHaveBeenCalled();
expect(onTimeout).toHaveBeenCalledTimes(0);
},
{
timeout: TIMEOUT,
}
);

expect(container).toMatchSnapshot();
});
});

describe('SetInterval', () => {
it.skip('should', () => {
// const { container } = render(
// <SetInterval>
// <p>Hello World!</p>
// </SetInterval>
// );
});
});
4 changes: 2 additions & 2 deletions packages/timeouts/src/SetTimeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export const SetTimeout = ({
const [done, setDone] = useState(false);

useEffect(() => {
onEnabled?.();

if (enabled) {
onEnabled?.();

const timer = setTimeout(() => {
setDone(true);

Expand Down

0 comments on commit cf5db7e

Please # to comment.