Skip to content

Commit

Permalink
refactor(tests): Align Jest tests (#32550)
Browse files Browse the repository at this point in the history
  • Loading branch information
tassoevan authored Aug 11, 2024
1 parent 4ec97ed commit 27bd512
Show file tree
Hide file tree
Showing 212 changed files with 3,903 additions and 4,486 deletions.
6 changes: 6 additions & 0 deletions _templates/package/new/jest.config.ts.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import server from '@rocket.chat/jest-presets/server';
import type { Config } from 'jest';

export default {
preset: server.preset,
} satisfies Config;
8 changes: 4 additions & 4 deletions _templates/package/new/package.json.ejs.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ to: packages/<%= name %>/package.json
"version": "0.0.1",
"private": true,
"devDependencies": {
"@types/jest": "~29.5.3",
"@rocket.chat/jest-presets": "workspace:~",
"@types/jest": "~29.5.12",
"eslint": "~8.45.0",
"jest": "~29.6.1",
"ts-jest": "~29.0.5",
"typescript": "~5.1.6"
"jest": "~29.7.0",
"typescript": "~5.3.3"
},
"scripts": {
"lint": "eslint --ext .js,.jsx,.ts,.tsx .",
Expand Down
4 changes: 2 additions & 2 deletions _templates/package/new/tsconfig.json.ejs.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
to: packages/<%= name %>/tsconfig.json
---
{
"extends": "../../tsconfig.base.client.json",
"extends": "../../tsconfig.base.server.json",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist"
},
"include": ["./src/**/*"]
"include": ["./src/**/*"],
}
42 changes: 0 additions & 42 deletions apps/meteor/.mocharc.client.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { mockAppRoot } from '@rocket.chat/mock-providers';
import { renderHook } from '@testing-library/react-hooks';
import { renderHook, waitFor } from '@testing-library/react';

import { useAuditMenu } from './useAuditMenu';

it('should return an empty array of items if doesn`t have license', async () => {
const { result, waitFor } = renderHook(() => useAuditMenu(), {
const { result } = renderHook(() => useAuditMenu(), {
legacyRoot: true,
wrapper: mockAppRoot()
.withEndpoint('GET', '/v1/licenses.info', () => ({
// @ts-expect-error: just for testing
Expand All @@ -18,13 +19,12 @@ it('should return an empty array of items if doesn`t have license', async () =>
.build(),
});

await waitFor(() => result.all.length > 1);

expect(result.current).toEqual([]);
await waitFor(() => expect(result.current).toEqual([]));
});

it('should return an empty array of items if have license and not have permissions', async () => {
const { result, waitFor } = renderHook(() => useAuditMenu(), {
const { result } = renderHook(() => useAuditMenu(), {
legacyRoot: true,
wrapper: mockAppRoot()
.withEndpoint('GET', '/v1/licenses.info', () => ({
license: {
Expand All @@ -41,13 +41,12 @@ it('should return an empty array of items if have license and not have permissio
.build(),
});

await waitFor(() => result.all.length > 1);

expect(result.current).toEqual([]);
await waitFor(() => expect(result.current).toEqual([]));
});

it('should return auditItems if have license and permissions', async () => {
const { result, waitFor } = renderHook(() => useAuditMenu(), {
const { result } = renderHook(() => useAuditMenu(), {
legacyRoot: true,
wrapper: mockAppRoot()
.withEndpoint('GET', '/v1/licenses.info', () => ({
license: {
Expand All @@ -65,12 +64,12 @@ it('should return auditItems if have license and permissions', async () => {
.build(),
});

await waitFor(() => result.current.length > 0);

expect(result.current[0].items[0]).toEqual(
expect.objectContaining({
id: 'messages',
}),
await waitFor(() =>
expect(result.current[0]?.items[0]).toEqual(
expect.objectContaining({
id: 'messages',
}),
),
);

expect(result.current[0].items[1]).toEqual(
Expand All @@ -81,7 +80,8 @@ it('should return auditItems if have license and permissions', async () => {
});

it('should return auditMessages item if have license and can-audit permission', async () => {
const { result, waitFor } = renderHook(() => useAuditMenu(), {
const { result } = renderHook(() => useAuditMenu(), {
legacyRoot: true,
wrapper: mockAppRoot()
.withEndpoint('GET', '/v1/licenses.info', () => ({
license: {
Expand All @@ -98,17 +98,18 @@ it('should return auditMessages item if have license and can-audit permission',
.build(),
});

await waitFor(() => result.current.length > 0);

expect(result.current[0].items[0]).toEqual(
expect.objectContaining({
id: 'messages',
}),
await waitFor(() =>
expect(result.current[0]?.items[0]).toEqual(
expect.objectContaining({
id: 'messages',
}),
),
);
});

it('should return audiLogs item if have license and can-audit-log permission', async () => {
const { result, waitFor } = renderHook(() => useAuditMenu(), {
const { result } = renderHook(() => useAuditMenu(), {
legacyRoot: true,
wrapper: mockAppRoot()
.withEndpoint('GET', '/v1/licenses.info', () => ({
license: {
Expand All @@ -125,11 +126,11 @@ it('should return audiLogs item if have license and can-audit-log permission', a
.build(),
});

await waitFor(() => result.current.length > 0);

expect(result.current[0].items[0]).toEqual(
expect.objectContaining({
id: 'auditLog',
}),
await waitFor(() =>
expect(result.current[0]?.items[0]).toEqual(
expect.objectContaining({
id: 'auditLog',
}),
),
);
});
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { UIActionButtonContext } from '@rocket.chat/apps-engine/definition/ui';
import { mockAppRoot } from '@rocket.chat/mock-providers';
import { renderHook } from '@testing-library/react-hooks';
import { renderHook, waitFor } from '@testing-library/react';

import { useMarketPlaceMenu } from './useMarketPlaceMenu';

it('should return and empty array if the user does not have `manage-apps` and `access-marketplace` permission', () => {
const { result } = renderHook(() => useMarketPlaceMenu(), {
legacyRoot: true,
wrapper: mockAppRoot()
.withEndpoint('GET', '/apps/actionButtons', () => [])
.build(),
Expand All @@ -16,6 +17,7 @@ it('should return and empty array if the user does not have `manage-apps` and `a

it('should return `explore` and `installed` items if the user has `access-marketplace` permission', () => {
const { result } = renderHook(() => useMarketPlaceMenu(), {
legacyRoot: true,
wrapper: mockAppRoot()
.withEndpoint('GET', '/apps/actionButtons', () => [])
.withPermission('access-marketplace')
Expand All @@ -37,6 +39,7 @@ it('should return `explore` and `installed` items if the user has `access-market

it('should return `explore`, `installed` and `requested` items if the user has `manage-apps` permission', () => {
const { result } = renderHook(() => useMarketPlaceMenu(), {
legacyRoot: true,
wrapper: mockAppRoot()
.withEndpoint('GET', '/apps/actionButtons', () => [])
.withEndpoint('GET', '/apps/app-request/stats', () => ({
Expand Down Expand Up @@ -69,7 +72,8 @@ it('should return `explore`, `installed` and `requested` items if the user has `
});

it('should return one action from the server with no conditions', async () => {
const { result, waitForValueToChange } = renderHook(() => useMarketPlaceMenu(), {
const { result } = renderHook(() => useMarketPlaceMenu(), {
legacyRoot: true,
wrapper: mockAppRoot()
.withEndpoint('GET', '/apps/actionButtons', () => [
{
Expand Down Expand Up @@ -101,18 +105,19 @@ it('should return one action from the server with no conditions', async () => {
}),
);

await waitForValueToChange(() => result.current[0].items[3]);

expect(result.current[0].items[3]).toEqual(
expect.objectContaining({
id: 'APP_ID_ACTION_ID',
}),
await waitFor(() =>
expect(result.current[0]?.items[3]).toEqual(
expect.objectContaining({
id: 'APP_ID_ACTION_ID',
}),
),
);
});

describe('Marketplace menu with role conditions', () => {
it('should return the action if the user has admin role', async () => {
const { result, waitForValueToChange } = renderHook(() => useMarketPlaceMenu(), {
const { result } = renderHook(() => useMarketPlaceMenu(), {
legacyRoot: true,
wrapper: mockAppRoot()
.withEndpoint('GET', '/apps/actionButtons', () => [
{
Expand Down Expand Up @@ -149,17 +154,18 @@ describe('Marketplace menu with role conditions', () => {
}),
);

await waitForValueToChange(() => result.current[0].items[3]);

expect(result.current[0].items[3]).toEqual(
expect.objectContaining({
id: 'APP_ID_ACTION_ID',
}),
await waitFor(() =>
expect(result.current[0]?.items[3]).toEqual(
expect.objectContaining({
id: 'APP_ID_ACTION_ID',
}),
),
);
});

it('should return filter the action if the user doesn`t have admin role', async () => {
const { result } = renderHook(() => useMarketPlaceMenu(), {
legacyRoot: true,
wrapper: mockAppRoot()
.withEndpoint('GET', '/apps/actionButtons', () => [
{
Expand Down Expand Up @@ -206,7 +212,8 @@ describe('Marketplace menu with role conditions', () => {

describe('Marketplace menu with permission conditions', () => {
it('should return the action if the user has manage-apps permission', async () => {
const { result, waitForValueToChange } = renderHook(() => useMarketPlaceMenu(), {
const { result } = renderHook(() => useMarketPlaceMenu(), {
legacyRoot: true,
wrapper: mockAppRoot()
.withEndpoint('GET', '/apps/actionButtons', () => [
{
Expand Down Expand Up @@ -241,17 +248,18 @@ describe('Marketplace menu with permission conditions', () => {
}),
);

await waitForValueToChange(() => result.current[0].items[3]);

expect(result.current[0].items[3]).toEqual(
expect.objectContaining({
id: 'APP_ID_ACTION_ID',
}),
await waitFor(() =>
expect(result.current[0].items[3]).toEqual(
expect.objectContaining({
id: 'APP_ID_ACTION_ID',
}),
),
);
});

it('should return filter the action if the user doesn`t have `any` permission', async () => {
const { result } = renderHook(() => useMarketPlaceMenu(), {
legacyRoot: true,
wrapper: mockAppRoot()
.withEndpoint('GET', '/apps/actionButtons', () => [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { mockAppRoot } from '@rocket.chat/mock-providers';
import { renderHook } from '@testing-library/react-hooks';
import { renderHook, waitFor } from '@testing-library/react';

import { useAdministrationMenu } from './useAdministrationMenu';

it('should return omnichannel item if has `view-livechat-manager` permission ', async () => {
const { result, waitFor } = renderHook(() => useAdministrationMenu(), {
const { result } = renderHook(() => useAdministrationMenu(), {
legacyRoot: true,
wrapper: mockAppRoot()
.withEndpoint('GET', '/v1/licenses.info', () => ({
// @ts-expect-error this is a mock
Expand All @@ -19,17 +20,18 @@ it('should return omnichannel item if has `view-livechat-manager` permission ',
.build(),
});

await waitFor(() => !!result.current.length);

expect(result.current[0].items[0]).toEqual(
expect.objectContaining({
id: 'omnichannel',
}),
await waitFor(() =>
expect(result.current[0]?.items[0]).toEqual(
expect.objectContaining({
id: 'omnichannel',
}),
),
);
});

it('should show administration item if has at least one admin permission', async () => {
const { result, waitFor } = renderHook(() => useAdministrationMenu(), {
const { result } = renderHook(() => useAdministrationMenu(), {
legacyRoot: true,
wrapper: mockAppRoot()
.withEndpoint('GET', '/v1/licenses.info', () => ({
// @ts-expect-error this is a mock
Expand All @@ -44,11 +46,11 @@ it('should show administration item if has at least one admin permission', async
.build(),
});

await waitFor(() => !!result.current.length);

expect(result.current[0].items[0]).toEqual(
expect.objectContaining({
id: 'workspace',
}),
await waitFor(() =>
expect(result.current[0]?.items[0]).toEqual(
expect.objectContaining({
id: 'workspace',
}),
),
);
});
Loading

0 comments on commit 27bd512

Please sign in to comment.