From 24053363e5bd252aaf11311e6e33b350a5055a5a Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Tue, 11 Jan 2022 11:27:01 -0300 Subject: [PATCH] chore: migrate encryption to ts and add tests --- app/actions/encryption.js | 35 ------------- app/actions/encryption.ts | 51 +++++++++++++++++++ app/definitions/redux/index.ts | 46 +++++++++-------- app/reducers/encryption.test.ts | 24 +++++++++ app/reducers/{encryption.js => encryption.ts} | 10 +++- 5 files changed, 109 insertions(+), 57 deletions(-) delete mode 100644 app/actions/encryption.js create mode 100644 app/actions/encryption.ts create mode 100644 app/reducers/encryption.test.ts rename app/reducers/{encryption.js => encryption.ts} (59%) diff --git a/app/actions/encryption.js b/app/actions/encryption.js deleted file mode 100644 index 390dfe903..000000000 --- a/app/actions/encryption.js +++ /dev/null @@ -1,35 +0,0 @@ -import * as types from './actionsTypes'; - -export function encryptionInit() { - return { - type: types.ENCRYPTION.INIT - }; -} - -export function encryptionStop() { - return { - type: types.ENCRYPTION.STOP - }; -} - -export function encryptionSet(enabled = false, banner = null) { - return { - type: types.ENCRYPTION.SET, - enabled, - banner - }; -} - -export function encryptionSetBanner(banner) { - return { - type: types.ENCRYPTION.SET_BANNER, - banner - }; -} - -export function encryptionDecodeKey(password) { - return { - type: types.ENCRYPTION.DECODE_KEY, - password - }; -} diff --git a/app/actions/encryption.ts b/app/actions/encryption.ts new file mode 100644 index 000000000..12e729ccf --- /dev/null +++ b/app/actions/encryption.ts @@ -0,0 +1,51 @@ +import { Action } from 'redux'; + +import { ENCRYPTION } from './actionsTypes'; + +interface IEncryptionSetBanner extends Action { + banner: null | any; +} + +interface IEncryptionSet extends Action, IEncryptionSetBanner { + enabled: boolean; +} + +interface IEncryptionDecodeKey extends Action { + password: string; +} + +export type TActionEncryption = IEncryptionSetBanner & IEncryptionSet & IEncryptionDecodeKey; + +export function encryptionInit(): Action { + return { + type: ENCRYPTION.INIT + }; +} + +export function encryptionStop(): Action { + return { + type: ENCRYPTION.STOP + }; +} + +export function encryptionSet(enabled = false, banner: null | any): IEncryptionSet { + return { + type: ENCRYPTION.SET, + enabled, + banner + }; +} + +export function encryptionSetBanner(banner: null | any): IEncryptionSetBanner { + return { + type: ENCRYPTION.SET_BANNER, + banner + }; +} + +export function encryptionDecodeKey(password: string): IEncryptionDecodeKey { + return { + type: ENCRYPTION.DECODE_KEY, + password + }; +} diff --git a/app/definitions/redux/index.ts b/app/definitions/redux/index.ts index 0be072d9c..c103009fc 100644 --- a/app/definitions/redux/index.ts +++ b/app/definitions/redux/index.ts @@ -1,36 +1,42 @@ -import { TActionSelectedUsers } from '../../actions/selectedUsers'; import { TActionActiveUsers } from '../../actions/activeUsers'; import { TActionApp } from '../../actions/app'; import { TActionCreateChannel } from '../../actions/createChannel'; +import { TActionEncryption } from '../../actions/encryption'; +import { TActionSelectedUsers } from '../../actions/selectedUsers'; // REDUCERS import { IActiveUsers } from '../../reducers/activeUsers'; -import { ISelectedUsers } from '../../reducers/selectedUsers'; import { IApp } from '../../reducers/app'; import { IConnect } from '../../reducers/connect'; import { ICreateChannel } from '../../reducers/createChannel'; +import { IEncryption } from '../../reducers/encryption'; +import { ISelectedUsers } from '../../reducers/selectedUsers'; export interface IApplicationState { - settings: any; + activeUsers: IActiveUsers; + app: IApp; + createChannel: ICreateChannel; + createDiscussion: any; + customEmojis: any; + encryption: IEncryption; + enterpriseModules: any; + inquiry: any; + inviteLinks: any; login: any; meteor: IConnect; - server: any; - selectedUsers: ISelectedUsers; - createChannel: ICreateChannel; - app: IApp; - room: any; - rooms: any; - sortPreferences: any; - share: any; - customEmojis: any; - activeUsers: IActiveUsers; - usersTyping: any; - inviteLinks: any; - createDiscussion: any; - inquiry: any; - enterpriseModules: any; - encryption: any; permissions: any; roles: any; + room: any; + rooms: any; + selectedUsers: ISelectedUsers; + server: any; + settings: any; + share: any; + sortPreferences: any; + usersTyping: any; } -export type TApplicationActions = TActionActiveUsers & TActionSelectedUsers & TActionApp & TActionCreateChannel; +export type TApplicationActions = TActionActiveUsers & + TActionSelectedUsers & + TActionApp & + TActionCreateChannel & + TActionEncryption; diff --git a/app/reducers/encryption.test.ts b/app/reducers/encryption.test.ts new file mode 100644 index 000000000..539a2230c --- /dev/null +++ b/app/reducers/encryption.test.ts @@ -0,0 +1,24 @@ +import { encryptionSet, encryptionSetBanner } from '../actions/encryption'; +import { initialState } from './encryption'; +import { mockedStore } from './mockedStore'; + +describe('test reducer', () => { + it('should return initial state', () => { + const { encryption } = mockedStore.getState(); + expect(encryption).toEqual(initialState); + }); + + it('should return correct createDiscussion state after dispatch createDiscussionRequest action', () => { + mockedStore.dispatch(encryptionSet(true, {})); + const { encryption } = mockedStore.getState(); + expect(encryption).toEqual({ enabled: true, banner: {} }); + }); + + it('should return correct createDiscussion state after dispatch createDiscussionSuccess action', () => { + mockedStore.dispatch(encryptionSetBanner('test')); + const { + encryption: { banner } + } = mockedStore.getState(); + expect(banner).toEqual('test'); + }); +}); diff --git a/app/reducers/encryption.js b/app/reducers/encryption.ts similarity index 59% rename from app/reducers/encryption.js rename to app/reducers/encryption.ts index 0145ae2d1..06d11d2fe 100644 --- a/app/reducers/encryption.js +++ b/app/reducers/encryption.ts @@ -1,11 +1,17 @@ +import { TApplicationActions } from '../definitions'; import { ENCRYPTION } from '../actions/actionsTypes'; -const initialState = { +export interface IEncryption { + enabled: boolean; + banner: null | any; +} + +export const initialState: IEncryption = { enabled: false, banner: null }; -export default function encryption(state = initialState, action) { +export default function encryption(state = initialState, action: TApplicationActions): IEncryption { switch (action.type) { case ENCRYPTION.SET: return {