Skip to content

Commit

Permalink
Upgrade Testing Library
Browse files Browse the repository at this point in the history
  • Loading branch information
tassoevan committed May 14, 2024
1 parent 0a02b60 commit 601b4d2
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 156 deletions.
21 changes: 15 additions & 6 deletions apps/meteor/client/portals/ModalPortal.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ReactElement, ReactNode } from 'react';
import React, { memo, useEffect, useState } from 'react';
import type { ReactNode } from 'react';
import { memo, useEffect, useState } from 'react';
import { createPortal } from 'react-dom';

import { createAnchor } from '../lib/utils/createAnchor';
Expand All @@ -9,10 +9,19 @@ type ModalPortalProps = {
children?: ReactNode;
};

const ModalPortal = ({ children }: ModalPortalProps): ReactElement => {
const [modalRoot] = useState(() => createAnchor('modal-root'));
useEffect(() => (): void => deleteAnchor(modalRoot), [modalRoot]);
return <>{createPortal(children, modalRoot)}</>;
const ModalPortal = ({ children }: ModalPortalProps) => {
const [modalRoot, setModalRoot] = useState<HTMLElement>();

useEffect(() => {
const modalRoot = createAnchor('modal-root');
setModalRoot(modalRoot);

return () => deleteAnchor(modalRoot);
}, []);

if (!modalRoot) return null;

return createPortal(children, modalRoot);
};

export default memo(ModalPortal);
70 changes: 40 additions & 30 deletions apps/meteor/client/providers/ModalProvider/ModalProvider.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// import type { IMessage } from '@rocket.chat/core-typings';
import { Emitter } from '@rocket.chat/emitter';
import { useSetModal } from '@rocket.chat/ui-contexts';
import { render, screen } from '@testing-library/react';
import { expect } from 'chai';
import type { ReactNode } from 'react';
import React, { Suspense, createContext, useContext, useEffect } from 'react';
import React, { act, Suspense, createContext, useContext, useEffect } from 'react';

import GenericModal from '../../components/GenericModal';
import { imperativeModal } from '../../lib/imperativeModal';
Expand All @@ -19,13 +17,15 @@ const TestModal = ({ emitterEvent, modalFunc }: { emitterEvent: string; modalFun
const setModal = useSetModal();
const { title } = useContext(TestContext);

useEffect(() => {
emitter.on(emitterEvent, () => {
setModal(modalFunc || <GenericModal title={title} onClose={() => undefined} />);
});
}, [emitterEvent, setModal, title, modalFunc]);
useEffect(
() =>
emitter.on(emitterEvent, () => {
setModal(modalFunc || <GenericModal title={title} onClose={() => undefined} />);
}),
[emitterEvent, setModal, title, modalFunc],
);

return <></>;
return null;
};

describe('Modal Provider', () => {
Expand All @@ -37,8 +37,10 @@ describe('Modal Provider', () => {
</ModalProviderWithRegion>
</Suspense>,
);
emitter.emit('open');
expect(await screen.findByText('default')).to.exist;

act(() => emitter.emit('open'));

expect(await screen.findByText('default')).toBeInTheDocument();
});

it('should render a modal that is passed as a function', async () => {
Expand All @@ -49,8 +51,10 @@ describe('Modal Provider', () => {
</ModalProviderWithRegion>
</Suspense>,
);
emitter.emit('open');
expect(await screen.findByText('function modal')).to.exist;

act(() => emitter.emit('open'));

expect(await screen.findByText('function modal')).toBeInTheDocument();
});

it('should render a modal through imperative modal', () => {
Expand All @@ -68,11 +72,11 @@ describe('Modal Provider', () => {
props: { title: 'imperativeModal' },
});

expect(await screen.findByText('imperativeModal')).to.exist;
expect(await screen.findByText('imperativeModal')).toBeInTheDocument();

close();

expect(screen.queryByText('imperativeModal')).to.not.exist;
expect(screen.queryByText('imperativeModal')).not.toBeInTheDocument();
};
});

Expand All @@ -85,31 +89,37 @@ describe('Modal Provider', () => {
</Suspense>,
);

imperativeModal.open({
component: GenericModal,
props: { title: 'imperativeModal' },
});
act(() =>
imperativeModal.open({
component: GenericModal,
props: { title: 'imperativeModal' },
}),
);

expect(screen.queryByText('imperativeModal')).to.not.exist;
expect(screen.queryByText('imperativeModal')).not.toBeInTheDocument();
});

it('should render a modal in another region', () => {
it('should render a modal in another region', async () => {
render(
<TestContext.Provider value={{ title: 'modal1' }}>
<ModalProviderWithRegion>
<TestModal emitterEvent='openModal1' />
</ModalProviderWithRegion>
<TestContext.Provider value={{ title: 'modal2' }}>
<Suspense fallback={null}>
<ModalProviderWithRegion>
<TestModal emitterEvent='openModal2' />
<TestModal emitterEvent='openModal1' />
</ModalProviderWithRegion>
</Suspense>
<TestContext.Provider value={{ title: 'modal2' }}>
<Suspense fallback={null}>
<ModalProviderWithRegion>
<TestModal emitterEvent='openModal2' />
</ModalProviderWithRegion>
</Suspense>
</TestContext.Provider>
</TestContext.Provider>,
);

emitter.emit('openModal1');
expect(screen.getByText('modal1')).to.exist;
emitter.emit('openModal2');
expect(screen.getByText('modal2')).to.exist;
await act(async () => emitter.emit('openModal1'));
await expect(screen.findByText('modal1')).resolves.toBeInTheDocument();
await act(async () => emitter.emit('openModal2'));
await expect(screen.findByText('modal2')).resolves.toBeInTheDocument();
});
});
3 changes: 1 addition & 2 deletions apps/meteor/client/views/modal/ModalRegion.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { useModal, useCurrentModal } from '@rocket.chat/ui-contexts';
import type { ReactElement } from 'react';
import React, { lazy, useCallback } from 'react';

import ModalBackdrop from '../../components/ModalBackdrop';
import ModalPortal from '../../portals/ModalPortal';

const FocusScope = lazy(() => import('react-aria').then((module) => ({ default: module.FocusScope })));

const ModalRegion = (): ReactElement | null => {
const ModalRegion = () => {
const currentModal = useCurrentModal();
const { setModal } = useModal();
const handleDismiss = useCallback(() => setModal(null), [setModal]);
Expand Down
7 changes: 3 additions & 4 deletions apps/meteor/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Config } from 'jest';

const config: Config = {
export default {
projects: [
{
displayName: 'client',
Expand All @@ -24,6 +24,7 @@ const config: Config = {
'^@tanstack/(.+)': '<rootDir>/node_modules/@tanstack/$1',
'^meteor/(.*)': '<rootDir>/.meteorMocks/index.ts',
},
setupFilesAfterEnv: ['@testing-library/jest-dom'],
},
{
displayName: 'server',
Expand Down Expand Up @@ -51,6 +52,4 @@ const config: Config = {
},
],
collectCoverage: true,
};

export default config;
} satisfies Config;
2 changes: 1 addition & 1 deletion apps/meteor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
"@swc/core": "^1.3.95",
"@swc/jest": "^0.2.29",
"@tanstack/react-query-devtools": "^4.19.1",
"@testing-library/react": "~12.1.5",
"@testing-library/react": "~14.3.1",
"@testing-library/react-hooks": "^8.0.1",
"@testing-library/user-event": "~13.5.0",
"@types/adm-zip": "^0.5.3",
Expand Down
4 changes: 2 additions & 2 deletions ee/packages/pdf-worker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"devDependencies": {
"@storybook/addon-essentials": "~6.5.16",
"@storybook/react": "~6.5.16",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "~13.4.0",
"@testing-library/jest-dom": "~6.4.5",
"@testing-library/react": "~14.3.1",
"@types/emojione": "^2.2.8",
"@types/jest": "~29.5.7",
"@types/react-dom": "~18.3.0",
Expand Down
3 changes: 0 additions & 3 deletions ee/packages/pdf-worker/src/worker.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import fs from 'fs';

import { PdfWorker } from './index';
import {
bigConversationData,
Expand Down Expand Up @@ -51,7 +49,6 @@ describe('PdfWorker', () => {
const stream = await pdfWorker.renderToStream({ data: dataWithASingleMessageAndAnImage });
const buffer = await streamToBuffer(stream);

fs.writeFileSync('test.pdf', buffer);
expect(buffer).toBeTruthy();
});

Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"@changesets/cli": "^2.26.2",
"@types/chart.js": "^2.9.39",
"@types/js-yaml": "^4.0.8",
"turbo": "^1.12.4"
"turbo": "latest"
},
"workspaces": [
"apps/*",
Expand Down Expand Up @@ -63,8 +63,7 @@
"mongodb@^4.17.1": "patch:mongodb@npm:4.17.1#.yarn/patches/mongodb-npm-4.17.1-a2fe811ff1.patch",
"@rocket.chat/forked-matrix-sdk-crypto-nodejs": "0.1.0-beta.13",
"[email protected]": "patch:typia@npm:5.3.3#.yarn/patches/typia-npm-5.3.3-21d3e18463.patch",
"typia@~5.3.3": "patch:typia@npm%3A5.3.3#./.yarn/patches/typia-npm-5.3.3-21d3e18463.patch",
"@types/react": "18.2.79"
"typia@~5.3.3": "patch:typia@npm%3A5.3.3#./.yarn/patches/typia-npm-5.3.3-21d3e18463.patch"
},
"dependencies": {
"node-gyp": "^9.4.1"
Expand Down
2 changes: 1 addition & 1 deletion packages/fuselage-ui-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"@storybook/source-loader": "~6.5.16",
"@storybook/theming": "~6.5.16",
"@tanstack/react-query": "^4.16.1",
"@testing-library/react": "^14.2.2",
"@testing-library/react": "~14.3.1",
"@testing-library/react-hooks": "^8.0.1",
"@types/babel__core": "^7.20.3",
"@types/babel__preset-env": "^7.9.4",
Expand Down
4 changes: 2 additions & 2 deletions packages/gazzodown/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"@storybook/testing-library": "~0.0.13",
"@swc/core": "^1.3.95",
"@swc/jest": "^0.2.29",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "~12.1.5",
"@testing-library/jest-dom": "~6.4.5",
"@testing-library/react": "~14.3.1",
"@types/jest": "~29.5.7",
"@types/katex": "~0.16.5",
"@types/react": "~18.3.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/ui-client/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ export default {
},
collectCoverage: true,
collectCoverageFrom: ['src/**/*.{ts,tsx}'],
setupFilesAfterEnv: ['@testing-library/jest-dom/extend-expect'],
setupFilesAfterEnv: ['@testing-library/jest-dom'],
};
4 changes: 2 additions & 2 deletions packages/ui-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"@storybook/react": "~6.5.16",
"@storybook/testing-library": "~0.0.13",
"@swc/jest": "^0.2.29",
"@testing-library/jest-dom": "~5.16.5",
"@testing-library/react": "^12.1.5",
"@testing-library/jest-dom": "~6.4.5",
"@testing-library/react": "~14.3.1",
"@testing-library/react-hooks": "^8.0.1",
"@types/babel__core": "~7.20.3",
"@types/jest": "~29.5.7",
Expand Down
2 changes: 1 addition & 1 deletion packages/web-ui-registration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@storybook/react": "~6.5.16",
"@storybook/testing-library": "^0.2.2",
"@tanstack/react-query": "^4.16.1",
"@testing-library/react": "^13.3.0",
"@testing-library/react": "~14.3.1",
"@types/jest": "~29.5.7",
"@types/react": "~18.3.2",
"babel-loader": "~8.3.0",
Expand Down
Loading

0 comments on commit 601b4d2

Please sign in to comment.