mattermost-mobile/app/screens/channel/header/header.test.tsx
Daniel Espino García bb7ff622af
Add Playbooks read-only support for mobile devices (#8978)
* Add the channel options to get into playbooks (#8750)

* Add the channel options to get into playbooks

* Use playbook run id instead of playbook id

* i18n

* Fix issues and move playbooks to the products folder

* Address some tests

* Fix test

* Address design issues

* Add requested comment

---------

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>

* Playbooks database (#8802)

* Add lastPlaybookFetchAt to channel table (#8916)

* Add lastPlaybookFetchAt to channel table

* Add missing commit

* Use my_channel table instead

* Fix test

* Address feedback

* First implementation of playbooks API (#8897)

* First implementation of playbooks API

* Add version check

* Address feedback

* Fix test

* Add last fetch at usage and other improvements

* Simplify test

* Add sort_order, update_at and previousReminder columns (#8927)

* Add sort_order, update_at and previousReminder columns

* Remove order from the schema

* Fix tests

* Add tests

* Add websockets for playbooks (#8947)

* Add websocket events for playbooks

* Fix typo

* Add playbook run list (#8761)

* Add the channel options to get into playbooks

* Use playbook run id instead of playbook id

* i18n

* Fix issues and move playbooks to the products folder

* Address some tests

* Fix test

* Add playbook run list

* Add missing texts

* Add back button support and item size to flash list

* Address design issues

* Add requested comment

* Standardize tag and use it in the card

* Fix merge

* Add API related functionality

---------

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>

* Add playbooks run details (#8872)

* Add the channel options to get into playbooks

* Use playbook run id instead of playbook id

* i18n

* Fix issues and move playbooks to the products folder

* Address some tests

* Fix test

* Add playbook run list

* Add missing texts

* Add back button support and item size to flash list

* Address design issues

* Add requested comment

* Standardize tag and use it in the card

* Add playbooks run details

* Fix merge

* Add API related functionality

* Add API related changes

* Order fixes

* Several fixes

* Add error state on playbook run

* i18n-extract

* Fix tests

* Fix test

* Several fixes

* Fixes and add missing UI elements

* i18n-extract

* Fix tests

* Remove files from bad merge

---------

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>

* Add missing tests for playbooks (#8976)

* Add the channel options to get into playbooks

* Use playbook run id instead of playbook id

* i18n

* Fix issues and move playbooks to the products folder

* Address some tests

* Fix test

* Add playbook run list

* Add missing texts

* Add back button support and item size to flash list

* Address design issues

* Add requested comment

* Standardize tag and use it in the card

* Add playbooks run details

* Fix merge

* Add API related functionality

* Add API related changes

* Order fixes

* Several fixes

* Add error state on playbook run

* i18n-extract

* Fix tests

* Fix test

* Several fixes

* Fixes and add missing UI elements

* i18n-extract

* Fix tests

* Remove files from bad merge

* Add tests

* Fix typo

* Add missing strings

* Fix tests and skip some

* Fix test

* Fix typo

---------

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>

* Address feedback

* Address feedback and fix tests

* Address comments and fix tests

* Address feedback

* Address plugin changes and fix bugs

---------

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
2025-07-14 09:21:37 +02:00

207 lines
7.8 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React, {type ComponentProps} from 'react';
import NavigationHeader from '@components/navigation_header';
import {useServerUrl} from '@context/server';
import {fetchPlaybookRunsForChannel} from '@playbooks/actions/remote/runs';
import {goToPlaybookRun, goToPlaybookRuns} from '@playbooks/screens/navigation';
import EphemeralStore from '@store/ephemeral_store';
import {renderWithIntl, waitFor} from '@test/intl-test-helper';
import ChannelHeader from './header';
jest.mock('@components/navigation_header', () => ({
__esModule: true,
default: jest.fn(),
}));
jest.mocked(NavigationHeader).mockImplementation((props) => React.createElement('NavigationHeader', {testID: 'navigation-header', ...props}));
jest.mock('@screens/navigation');
jest.mock('@playbooks/screens/navigation');
jest.mock('@playbooks/actions/remote/runs');
jest.mock('@calls/state', () => ({
getCallsConfig: jest.fn().mockReturnValue({
pluginEnabled: false,
}),
}));
const serverUrl = 'some.server.url';
jest.mock('@context/server');
jest.mocked(useServerUrl).mockReturnValue(serverUrl);
describe('ChannelHeader', () => {
function getBaseProps(): ComponentProps<typeof ChannelHeader> {
return {
channelId: 'channel-id',
channelType: 'O',
displayName: 'Test Channel',
teamId: 'team-id',
hasPlaybookRuns: false,
playbooksActiveRuns: 0,
callsEnabledInChannel: false,
groupCallsAllowed: false,
isBookmarksEnabled: false,
canAddBookmarks: false,
hasBookmarks: false,
shouldRenderBookmarks: false,
isCustomStatusEnabled: false,
isCustomStatusExpired: false,
isOwnDirectMessage: false,
shouldRenderChannelBanner: false,
isPlaybooksEnabled: true,
};
}
beforeEach(() => {
jest.clearAllMocks();
});
it('does not show playbook button when there are no active runs', () => {
const props = getBaseProps();
props.hasPlaybookRuns = false;
props.playbooksActiveRuns = 0;
renderWithIntl(<ChannelHeader {...props}/>);
const navHeader = jest.mocked(NavigationHeader).mock.calls[0][0];
expect(navHeader.rightButtons).toEqual(
expect.arrayContaining([
expect.not.objectContaining({
iconName: 'product-playbooks',
}),
]),
);
});
it('shows playbook button with count when there are active runs', () => {
const props = getBaseProps();
props.playbooksActiveRuns = 3;
props.hasPlaybookRuns = true;
const {getByTestId} = renderWithIntl(<ChannelHeader {...props}/>);
const navHeader = getByTestId('navigation-header');
expect(navHeader.props.rightButtons).toEqual(
expect.arrayContaining([
expect.objectContaining({
iconName: 'product-playbooks',
count: 3,
}),
]),
);
});
it('navigates to single playbook run when there is an active playbook provided', () => {
const props = getBaseProps();
props.playbooksActiveRuns = 1;
props.hasPlaybookRuns = true;
props.activeRunId = 'run-id';
const {getByTestId} = renderWithIntl(<ChannelHeader {...props}/>);
const navHeader = getByTestId('navigation-header');
const playbookButton = (navHeader.props as ComponentProps<typeof NavigationHeader>).rightButtons?.find((button) => button.iconName === 'product-playbooks');
expect(playbookButton).toBeTruthy();
playbookButton?.onPress();
expect(goToPlaybookRun).toHaveBeenCalledWith(expect.anything(), 'run-id');
expect(goToPlaybookRuns).not.toHaveBeenCalled();
});
it('navigates to playbook runs list when there is no active playbook provided', () => {
const props = getBaseProps();
props.activeRunId = undefined;
props.playbooksActiveRuns = 3;
props.hasPlaybookRuns = true;
props.displayName = 'Test Channel';
const {getByTestId} = renderWithIntl(<ChannelHeader {...props}/>);
const navHeader = getByTestId('navigation-header');
const playbookButton = (navHeader.props as ComponentProps<typeof NavigationHeader>).rightButtons?.find((button) => button.iconName === 'product-playbooks');
expect(playbookButton).toBeTruthy();
playbookButton?.onPress();
expect(goToPlaybookRuns).toHaveBeenCalledWith(expect.anything(), 'channel-id', 'Test Channel');
expect(goToPlaybookRun).not.toHaveBeenCalled();
});
it('should set the ephemeral store when we fetch the playbook runs for the channel', async () => {
const ephemeralGetSpy = jest.spyOn(EphemeralStore, 'getChannelPlaybooksSynced');
const ephemeralSetSpy = jest.spyOn(EphemeralStore, 'setChannelPlaybooksSynced');
const props = getBaseProps();
props.isPlaybooksEnabled = true;
ephemeralGetSpy.mockReturnValue(false);
jest.mocked(fetchPlaybookRunsForChannel).mockResolvedValue({
runs: [],
});
renderWithIntl(<ChannelHeader {...props}/>);
await waitFor(() => {
expect(ephemeralGetSpy).toHaveBeenCalledWith(serverUrl, 'channel-id');
expect(fetchPlaybookRunsForChannel).toHaveBeenCalledWith(serverUrl, 'channel-id');
expect(ephemeralSetSpy).toHaveBeenCalledWith(serverUrl, 'channel-id');
});
});
it('should not fetch runs when playbooks are disabled', async () => {
const ephemeralGetSpy = jest.spyOn(EphemeralStore, 'getChannelPlaybooksSynced');
const ephemeralSetSpy = jest.spyOn(EphemeralStore, 'setChannelPlaybooksSynced');
const props = getBaseProps();
props.isPlaybooksEnabled = false;
ephemeralGetSpy.mockReturnValue(false);
renderWithIntl(<ChannelHeader {...props}/>);
await waitFor(() => {
expect(ephemeralGetSpy).not.toHaveBeenCalled();
expect(ephemeralSetSpy).not.toHaveBeenCalled();
expect(fetchPlaybookRunsForChannel).not.toHaveBeenCalled();
});
});
it('should not fetch runs when we already have the runs synced', async () => {
const ephemeralGetSpy = jest.spyOn(EphemeralStore, 'getChannelPlaybooksSynced');
const ephemeralSetSpy = jest.spyOn(EphemeralStore, 'setChannelPlaybooksSynced');
const props = getBaseProps();
props.isPlaybooksEnabled = true;
ephemeralGetSpy.mockReturnValue(true);
renderWithIntl(<ChannelHeader {...props}/>);
await waitFor(() => {
expect(ephemeralGetSpy).toHaveBeenCalledWith(serverUrl, 'channel-id');
expect(ephemeralSetSpy).not.toHaveBeenCalled();
expect(fetchPlaybookRunsForChannel).not.toHaveBeenCalled();
});
});
it('should not set the ephemeral store when there is an error fetching the runs', async () => {
const ephemeralGetSpy = jest.spyOn(EphemeralStore, 'getChannelPlaybooksSynced');
const ephemeralSetSpy = jest.spyOn(EphemeralStore, 'setChannelPlaybooksSynced');
const props = getBaseProps();
props.isPlaybooksEnabled = true;
ephemeralGetSpy.mockReturnValue(false);
jest.mocked(fetchPlaybookRunsForChannel).mockResolvedValue({error: new Error('Error fetching runs')});
renderWithIntl(<ChannelHeader {...props}/>);
await waitFor(() => {
expect(ephemeralGetSpy).toHaveBeenCalledWith(serverUrl, 'channel-id');
expect(ephemeralSetSpy).not.toHaveBeenCalled();
expect(fetchPlaybookRunsForChannel).toHaveBeenCalledWith(serverUrl, 'channel-id');
});
});
});