mattermost-mobile/app/products/playbooks/client/rest.test.ts
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

174 lines
7.3 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import TestHelper from '@test/test_helper';
import {buildQueryString} from '@utils/helpers';
let client: any;
beforeEach(() => {
client = TestHelper.createClient();
client.doFetch = jest.fn();
});
describe('fetchPlaybookRuns', () => {
test('should fetch playbook runs with params and groupLabel', async () => {
const params = {team_id: 'team1', page: 1, per_page: 10};
const groupLabel: RequestGroupLabel = 'Cold Start';
const queryParams = buildQueryString(params);
const expectedUrl = `/plugins/playbooks/api/v0/runs${queryParams}`;
const expectedOptions = {method: 'get', groupLabel};
const mockResponse = {items: [], total_count: 0, page_count: 0, has_more: false};
jest.mocked(client.doFetch).mockResolvedValue(mockResponse);
const result = await client.fetchPlaybookRuns(params, groupLabel);
expect(client.doFetch).toHaveBeenCalledWith(expectedUrl, expectedOptions);
expect(result).toEqual(mockResponse);
});
test('should fetch playbook runs with params only', async () => {
const params = {team_id: 'team1', page: 0, per_page: 10};
const queryParams = buildQueryString(params);
const expectedUrl = `/plugins/playbooks/api/v0/runs${queryParams}`;
const expectedOptions = {method: 'get', groupLabel: undefined};
const mockResponse = {items: [], total_count: 0, page_count: 0, has_more: false};
jest.mocked(client.doFetch).mockResolvedValue(mockResponse);
const result = await client.fetchPlaybookRuns(params);
expect(client.doFetch).toHaveBeenCalledWith(expectedUrl, expectedOptions);
expect(result).toEqual(mockResponse);
});
test('should return default response when doFetch returns null', async () => {
const params = {team_id: 'team1', page: 0, per_page: 10};
const expectedResponse = {items: [], total_count: 0, page_count: 0, has_more: false};
jest.mocked(client.doFetch).mockResolvedValue(null);
const result = await client.fetchPlaybookRuns(params);
expect(result).toEqual(expectedResponse);
});
test('should return default response when doFetch throws error', async () => {
const params = {team_id: 'team1', page: 0, per_page: 10};
const expectedResponse = {items: [], total_count: 0, page_count: 0, has_more: false};
jest.mocked(client.doFetch).mockRejectedValue(new Error('Network error'));
const result = await client.fetchPlaybookRuns(params);
expect(result).toEqual(expectedResponse);
});
});
describe('setChecklistItemState', () => {
test('should set checklist item state successfully', async () => {
const playbookRunID = 'run123';
const checklistNum = 1;
const itemNum = 2;
const newState: ChecklistItemState = 'closed';
const expectedUrl = `/plugins/playbooks/api/v0/runs/${playbookRunID}/checklists/${checklistNum}/item/${itemNum}/state`;
const expectedOptions = {method: 'put', body: {new_state: newState}};
const mockResponse = {success: true};
jest.mocked(client.doFetch).mockResolvedValue(mockResponse);
const result = await client.setChecklistItemState(playbookRunID, checklistNum, itemNum, newState);
expect(client.doFetch).toHaveBeenCalledWith(expectedUrl, expectedOptions);
expect(result).toEqual(mockResponse);
});
test('should handle error when setting checklist item state', async () => {
const playbookRunID = 'run123';
const checklistNum = 1;
const itemNum = 2;
const newState = 'in_progress' as ChecklistItemState;
const expectedError = {error: new Error('Network error')};
jest.mocked(client.doFetch).mockRejectedValue(new Error('Network error'));
const result = await client.setChecklistItemState(playbookRunID, checklistNum, itemNum, newState);
expect(result).toEqual(expectedError);
});
test('should set checklist item state with empty state', async () => {
const playbookRunID = 'run123';
const checklistNum = 0;
const itemNum = 0;
const newState: ChecklistItemState = '';
const expectedUrl = `/plugins/playbooks/api/v0/runs/${playbookRunID}/checklists/${checklistNum}/item/${itemNum}/state`;
const expectedOptions = {method: 'put', body: {new_state: newState}};
const mockResponse = {success: true};
jest.mocked(client.doFetch).mockResolvedValue(mockResponse);
const result = await client.setChecklistItemState(playbookRunID, checklistNum, itemNum, newState);
expect(client.doFetch).toHaveBeenCalledWith(expectedUrl, expectedOptions);
expect(result).toEqual(mockResponse);
});
test('should set checklist item state with skipped state', async () => {
const playbookRunID = 'run123';
const checklistNum = 2;
const itemNum = 3;
const newState: ChecklistItemState = 'skipped';
const expectedUrl = `/plugins/playbooks/api/v0/runs/${playbookRunID}/checklists/${checklistNum}/item/${itemNum}/state`;
const expectedOptions = {method: 'put', body: {new_state: newState}};
const mockResponse = {success: true};
jest.mocked(client.doFetch).mockResolvedValue(mockResponse);
const result = await client.setChecklistItemState(playbookRunID, checklistNum, itemNum, newState);
expect(client.doFetch).toHaveBeenCalledWith(expectedUrl, expectedOptions);
expect(result).toEqual(mockResponse);
});
});
describe('runChecklistItemSlashCommand', () => {
test('should run checklist item slash command successfully', async () => {
const playbookRunId = 'run123';
const checklistNumber = 1;
const itemNumber = 2;
const expectedUrl = `/plugins/playbooks/api/v0/runs/${playbookRunId}/checklists/${checklistNumber}/item/${itemNumber}/run`;
const expectedOptions = {method: 'post'};
jest.mocked(client.doFetch).mockResolvedValue(undefined);
await client.runChecklistItemSlashCommand(playbookRunId, checklistNumber, itemNumber);
expect(client.doFetch).toHaveBeenCalledWith(expectedUrl, expectedOptions);
});
test('should run checklist item slash command with zero indices', async () => {
const playbookRunId = 'run123';
const checklistNumber = 0;
const itemNumber = 0;
const expectedUrl = `/plugins/playbooks/api/v0/runs/${playbookRunId}/checklists/${checklistNumber}/item/${itemNumber}/run`;
const expectedOptions = {method: 'post'};
jest.mocked(client.doFetch).mockResolvedValue(undefined);
await client.runChecklistItemSlashCommand(playbookRunId, checklistNumber, itemNumber);
expect(client.doFetch).toHaveBeenCalledWith(expectedUrl, expectedOptions);
});
test('should handle error when running checklist item slash command', async () => {
const playbookRunId = 'run123';
const checklistNumber = 1;
const itemNumber = 2;
jest.mocked(client.doFetch).mockRejectedValue(new Error('Network error'));
await expect(client.runChecklistItemSlashCommand(playbookRunId, checklistNumber, itemNumber)).rejects.toThrow('Network error');
});
});