Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

55 add unit tests for room creation #120

Merged
merged 14 commits into from
Aug 29, 2022
28 changes: 25 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@
"sanitize-html": "^2.3.2",
"ua-parser-js": "^0.7.24"
},
"devDependencies-comments": {
"tchap-added" : {
"@wojtekmaj/enzyme-adapter-react-17": "^0.6.7",
"enzyme": "^3.11.0",
"enzyme-to-json": "^3.6.2"
}
},
"devDependencies": {
"@babel/core": "^7.12.10",
"@babel/eslint-parser": "^7.12.10",
Expand Down Expand Up @@ -102,6 +109,7 @@
"@types/ua-parser-js": "^0.7.36",
"@typescript-eslint/eslint-plugin": "^5.6.0",
"@typescript-eslint/parser": "^5.6.0",
"@wojtekmaj/enzyme-adapter-react-17": "^0.6.7",
"allchange": "^1.0.6",
"autoprefixer": "^9.8.6",
"babel-jest": "^28.0.0",
Expand All @@ -111,6 +119,8 @@
"cpx": "^1.5.0",
"css-loader": "^3.6.0",
"dotenv": "^10.0.0",
"enzyme": "^3.11.0",
"enzyme-to-json": "^3.6.2",
"eslint": "8.9.0",
"eslint-config-google": "^0.14.0",
"eslint-plugin-deprecate": "^0.7.0",
Expand Down Expand Up @@ -172,15 +182,28 @@
"resolutions": {
"@types/react": "17.0.14"
},
"jest-comments": {
"README" : "For the tests to work, you need matrix-react-sdk to be git-cloned and yarn linked into this project.",
"snapshotSerializers" : "used for jest snapshot",
"testEnvironment" :"switch to jsdom like in matrix-react-sdk",
"testMatch" :"execute only tests in unit-tests directory",
"setupFilesAfterEnv" :"duplicate enzyme configuration in our own setup file '<rootDir>/test/setupTests.js'",
"moduleNameMapper" : "use mapper from element-web, helps at mocking {module, ressources} directly with regexp",
"transformIgnorePatterns" : "make regexp inline {matrix-js-sdk|matrix-react-sdk} else it does not work"
},
"jest": {
"snapshotSerializers": [
"enzyme-to-json/serializer"
],
"testEnvironment": "jsdom",
"testEnvironmentOptions": {
"url": "http://localhost/"
},
"testMatch": [
"<rootDir>/test/**/*-test.[tj]s?(x)"
"<rootDir>/test/unit-tests/**/*-test.[tj]s?(x)"
],
"setupFilesAfterEnv": [
"<rootDir>/test/setupTests.js",
"<rootDir>/node_modules/matrix-react-sdk/test/setupTests.js"
],
"moduleNameMapper": {
Expand All @@ -203,8 +226,7 @@
"RecorderWorklet": "<rootDir>/node_modules/matrix-react-sdk/__mocks__/empty.js"
},
"transformIgnorePatterns": [
"/node_modules/(?!matrix-js-sdk).+$",
"/node_modules/(?!matrix-react-sdk).+$"
"\/node_modules\/(?!matrix-js-sdk|matrix-react-sdk).+$"
],
"coverageReporters": [
"text-summary",
Expand Down
4 changes: 2 additions & 2 deletions src/components/views/dialogs/TchapCreateRoomDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import BaseDialog from "matrix-react-sdk/src/components/views/dialogs/BaseDialog
import TchapUtils from '../../../util/TchapUtils';
import TchapRoomTypeSelector from "./../elements/TchapRoomTypeSelector";
import { TchapRoomType } from "../../../@types/tchap";
import roomCreateOptions from "../../../lib/createTchapRoom";
import TchapCreateRoom from "../../../lib/createTchapRoom";

// We leave the same props as Element's version, to avoid unknown props warnings.
interface IProps {
Expand Down Expand Up @@ -123,7 +123,7 @@ export default class TchapCreateRoomDialog extends React.Component<IProps, IStat
// first. Queue a `setState` callback and wait for it to resolve.
await new Promise<void>(resolve => this.setState({}, resolve));
if (this.state.nameIsValid) {
this.props.onFinished(true, roomCreateOptions(
this.props.onFinished(true, TchapCreateRoom.roomCreateOptions(
this.state.name,
this.state.tchapRoomType,
this.state.isFederated));
Expand Down
78 changes: 41 additions & 37 deletions src/lib/createTchapRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,53 @@ export const DEFAULT_FEDERATE_VALUE = true;
* @param federate is the room federated
* @returns rooms options
*/
export default function roomCreateOptions(
name: string, tchapRoomType: TchapRoomType, federate: boolean = DEFAULT_FEDERATE_VALUE): IOpts {
const opts: IOpts = {};
const createRoomOpts: ITchapCreateRoomOpts = {};
opts.createOpts = createRoomOpts;
export default class TchapCreateRoom {
static roomCreateOptions(
name: string,
tchapRoomType: TchapRoomType,
federate: boolean = DEFAULT_FEDERATE_VALUE): IOpts {
const opts: IOpts = {};
const createRoomOpts: ITchapCreateRoomOpts = {};
opts.createOpts = createRoomOpts;

//tchap common options
createRoomOpts.name = name;
opts.guestAccess = false; //guest access are not authorized in tchap
//tchap common options
createRoomOpts.name = name;
opts.guestAccess = false; //guest access are not authorized in tchap

createRoomOpts.creation_content = { 'm.federate': federate };
createRoomOpts.creation_content = { 'm.federate': federate };

switch (tchapRoomType) {
case TchapRoomType.Forum: {
switch (tchapRoomType) {
case TchapRoomType.Forum: {
//"Forum" only for tchap members and not encrypted
createRoomOpts.accessRule = TchapRoomAccessRule.Restricted;
createRoomOpts.visibility = Visibility.Public;
createRoomOpts.preset = Preset.PublicChat;
opts.joinRule = JoinRule.Public;
opts.encryption = false;
opts.historyVisibility = HistoryVisibility.Shared;
break;
}
case TchapRoomType.Private: {
createRoomOpts.accessRule = TchapRoomAccessRule.Restricted;
createRoomOpts.visibility = Visibility.Public;
createRoomOpts.preset = Preset.PublicChat;
opts.joinRule = JoinRule.Public;
opts.encryption = false;
opts.historyVisibility = HistoryVisibility.Shared;
break;
}
case TchapRoomType.Private: {
//"Salon", only for tchap member and encrypted
createRoomOpts.accessRule = TchapRoomAccessRule.Restricted;
createRoomOpts.visibility = Visibility.Private;
createRoomOpts.preset = Preset.PrivateChat;
opts.joinRule = JoinRule.Invite;
opts.encryption = true;
opts.historyVisibility = HistoryVisibility.Invited;
break;
}
case TchapRoomType.External: {
createRoomOpts.accessRule = TchapRoomAccessRule.Restricted;
createRoomOpts.visibility = Visibility.Private;
createRoomOpts.preset = Preset.PrivateChat;
opts.joinRule = JoinRule.Invite;
opts.encryption = true;
opts.historyVisibility = HistoryVisibility.Invited;
break;
}
case TchapRoomType.External: {
//open to external and encrypted,
createRoomOpts.accessRule = TchapRoomAccessRule.Unrestricted;
createRoomOpts.visibility = Visibility.Private;
createRoomOpts.preset = Preset.PrivateChat;
opts.joinRule = JoinRule.Invite;
opts.encryption = true;
opts.historyVisibility = HistoryVisibility.Invited;
break;
createRoomOpts.accessRule = TchapRoomAccessRule.Unrestricted;
createRoomOpts.visibility = Visibility.Private;
createRoomOpts.preset = Preset.PrivateChat;
opts.joinRule = JoinRule.Invite;
opts.encryption = true;
opts.historyVisibility = HistoryVisibility.Invited;
break;
}
}
return opts;
}
return opts;
}
5 changes: 5 additions & 0 deletions test/setupTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

//is duplicated from matrix-react-sdk/test/setupTests.js in order to work
import Adapter from "@wojtekmaj/enzyme-adapter-react-17";
import { configure } from "enzyme";
configure({ adapter: new Adapter() });
Loading