From 311381940ddabfb5aa04fcae2c14eea8d7e4d236 Mon Sep 17 00:00:00 2001 From: Scott Bishel Date: Tue, 5 Nov 2024 17:00:26 -0700 Subject: [PATCH] update manage token model to work on load (#29022) Co-authored-by: Mattermost Build --- .../manage_tokens_modal.test.tsx.snap | 270 +++++++++++++++++- .../manage_tokens_modal.test.tsx | 45 ++- .../manage_tokens_modal.tsx | 11 +- 3 files changed, 303 insertions(+), 23 deletions(-) diff --git a/webapp/channels/src/components/admin_console/manage_tokens_modal/__snapshots__/manage_tokens_modal.test.tsx.snap b/webapp/channels/src/components/admin_console/manage_tokens_modal/__snapshots__/manage_tokens_modal.test.tsx.snap index 9ccf398081..6e3d8e48dc 100644 --- a/webapp/channels/src/components/admin_console/manage_tokens_modal/__snapshots__/manage_tokens_modal.test.tsx.snap +++ b/webapp/channels/src/components/admin_console/manage_tokens_modal/__snapshots__/manage_tokens_modal.test.tsx.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`components/admin_console/manage_tokens_modal/manage_tokens_modal.tsx should match snapshot 1`] = ` +exports[`components/admin_console/manage_tokens_modal/manage_tokens_modal.tsx initial call should match snapshot 1`] = ` `; + +exports[`components/admin_console/manage_tokens_modal/manage_tokens_modal.tsx should display list of tokens 1`] = ` + + + + + + + +
+
+ +
+
+ @some-user +
+
+
+
+
+ +
+
+
+
+
+ + description +
+
+ + id1 +
+
+
+ +
+
+
+
+
+ + description +
+
+ + id2 +
+
+
+ +
+
+
+
+ + +`; + +exports[`components/admin_console/manage_tokens_modal/manage_tokens_modal.tsx should replace loading screen on update 1`] = ` + + + + + + + +
+
+ +
+
+ @some-user +
+
+
+
+
+ +
+
+
+ +
+
+
+ + +`; diff --git a/webapp/channels/src/components/admin_console/manage_tokens_modal/manage_tokens_modal.test.tsx b/webapp/channels/src/components/admin_console/manage_tokens_modal/manage_tokens_modal.test.tsx index 008f05ad15..16c3b8e1bc 100644 --- a/webapp/channels/src/components/admin_console/manage_tokens_modal/manage_tokens_modal.test.tsx +++ b/webapp/channels/src/components/admin_console/manage_tokens_modal/manage_tokens_modal.test.tsx @@ -4,6 +4,8 @@ import {shallow} from 'enzyme'; import React from 'react'; +import LoadingScreen from 'components/loading_screen'; + import {TestHelper} from 'utils/test_helper'; import ManageTokensModal from './manage_tokens_modal'; @@ -20,36 +22,51 @@ describe('components/admin_console/manage_tokens_modal/manage_tokens_modal.tsx', onExited: jest.fn(), }; - test('should match snapshot', () => { + test('initial call should match snapshot', () => { const wrapper = shallow( , ); expect(wrapper).toMatchSnapshot(); + expect(baseProps.actions.getUserAccessTokensForUser).toHaveBeenCalledTimes(1); + expect(wrapper.find('.manage-teams__teams').exists()).toBe(true); + expect(wrapper.find(LoadingScreen).exists()).toBe(true); }); - test('should not call getUserAccessTokensForUser on mount', () => { + test('should replace loading screen on update', () => { const wrapper = shallow( , ); - expect(baseProps.actions.getUserAccessTokensForUser).toHaveBeenCalledTimes(0); - expect(wrapper.state('userAccessTokens')).toBeUndefined(); - }); - test('should call getUserAccessTokensForUser on user change', () => { - // create new user as only by then the update method triggers token retrieval const newProps = { ...baseProps, - user: TestHelper.getUserMock({ - id: 'newuser', - }), + userAccessTokens: {}, }; + wrapper.setProps(newProps); + expect(wrapper).toMatchSnapshot(); + expect(wrapper.find('.manage-teams__teams').exists()).toBe(true); + expect(wrapper.find('.manage-row__empty').exists()).toBe(true); + }); + + test('should display list of tokens', () => { const wrapper = shallow( , ); + const newProps = { + ...baseProps, + userAccessTokens: [ + { + id: 'id1', + description: 'description', + }, + { + id: 'id2', + description: 'description', + }, + ], + }; wrapper.setProps(newProps); - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - wrapper.instance().componentDidUpdate(baseProps, newProps); - expect(newProps.actions.getUserAccessTokensForUser).toHaveBeenCalledTimes(2); + expect(wrapper).toMatchSnapshot(); + expect(wrapper.find('.manage-teams__teams').exists()).toBe(true); + expect(wrapper.find('.manage-teams__team').length).toBe(2); }); }); diff --git a/webapp/channels/src/components/admin_console/manage_tokens_modal/manage_tokens_modal.tsx b/webapp/channels/src/components/admin_console/manage_tokens_modal/manage_tokens_modal.tsx index 01c452ffdb..37c0b08236 100644 --- a/webapp/channels/src/components/admin_console/manage_tokens_modal/manage_tokens_modal.tsx +++ b/webapp/channels/src/components/admin_console/manage_tokens_modal/manage_tokens_modal.tsx @@ -22,7 +22,7 @@ export type Props = { /** * The user the roles are being managed for */ - user?: UserProfile; + user: UserProfile; /** * The personal access tokens for a user, object with token ids as keys @@ -53,10 +53,9 @@ export default class ManageTokensModal extends React.PureComponent }; } - public componentDidUpdate(prevProps: Props): void { + public componentDidMount(): void { const userId = this.props.user ? this.props.user.id : null; - const prevUserId = prevProps.user ? prevProps.user.id : null; - if (userId && prevUserId !== userId) { + if (userId) { this.props.actions.getUserAccessTokensForUser(userId, 0, 200); } } @@ -74,10 +73,6 @@ export default class ManageTokensModal extends React.PureComponent private renderContents = (): JSX.Element => { const {user, userAccessTokens} = this.props; - if (!user) { - return ; - } - let name = UserUtils.getFullName(user); if (name) { name += ` (@${user.username})`;