Skip to content

Commit

Permalink
Initialize tests (#49)
Browse files Browse the repository at this point in the history
* Created isPromise.test.ts

* Used @testing-library/react

* Fixed jest config
  • Loading branch information
AlexShukel authored Jun 13, 2022
1 parent 1b7d67c commit 1ed8520
Show file tree
Hide file tree
Showing 7 changed files with 582 additions and 164 deletions.
7 changes: 7 additions & 0 deletions jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"globals": {
"ts-jest": {
"tsconfig": "tsconfig.test.json"
}
}
}
652 changes: 491 additions & 161 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
},
"homepage": "https://github.com/AlexShukel/reactive-popups#readme",
"devDependencies": {
"@babel/preset-react": "^7.17.12",
"@babel/preset-typescript": "^7.17.12",
"@testing-library/react": "^13.3.0",
"@types/jest": "^27.4.0",
"@types/react": "^18.0.9",
"@typescript-eslint/eslint-plugin": "^4.33.0",
Expand All @@ -43,7 +46,7 @@
"eslint-plugin-react-hooks": "^4.4.0",
"eslint-plugin-simple-import-sort": "^7.0.0",
"np": "^7.6.1",
"react": "^18.0.0",
"react": "^18.1.0",
"typescript": "^4.5.4"
},
"dependencies": {
Expand Down
5 changes: 4 additions & 1 deletion src/utils/isPromise.ts
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';
51 changes: 51 additions & 0 deletions test/components/PopupsRenderer.test.tsx
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);
});
});
24 changes: 24 additions & 0 deletions test/utils/isPromise.test.ts
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();
});
});
2 changes: 1 addition & 1 deletion tsconfig.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"noImplicitThis": false,
"noImplicitAny": false,
"strictNullChecks": false,
"suppressImplicitAnyIndexErrors": false,
"suppressImplicitAnyIndexErrors": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"strict": false,
Expand Down

0 comments on commit 1ed8520

Please sign in to comment.