Skip to content

Commit 47e4bbc

Browse files
authored
[MM-66420] Add missing tests for playbooks (#9258)
* [MM-66420] Add missing tests for playbooks * i18n-extract * Update message keys to more accurate ones * Update status update post to use a more standard error handling and fix linter bugs * Add post-merge tests and address feedback * Fix failing test * Address feedback
1 parent db15499 commit 47e4bbc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+6716
-701
lines changed

app/components/button/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ const Button = ({
9595
const loadingComponent = (
9696
<Loading
9797
color={txtStyleToUse.color}
98+
testID={`${testID}-loader`}
9899
/>
99100
);
100101

app/products/playbooks/actions/local/run.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ afterEach(async () => {
2222

2323
describe('handlePlaybookRuns', () => {
2424
it('should handle not found database', async () => {
25-
const {error} = await handlePlaybookRuns('foo', [], false, false);
25+
const {error} = await handlePlaybookRuns('foo', []);
2626
expect(error).toBeDefined();
2727
expect((error as Error).message).toContain('foo database not found');
2828
});
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
2+
// See LICENSE.txt for license information.
3+
4+
import {forceLogoutIfNecessary} from '@actions/remote/session';
5+
import NetworkManager from '@managers/network_manager';
6+
import TestHelper from '@test/test_helper';
7+
import {logDebug} from '@utils/log';
8+
9+
import {fetchPlaybooks} from './playbooks';
10+
11+
jest.mock('@actions/remote/session', () => ({
12+
forceLogoutIfNecessary: jest.fn(),
13+
}));
14+
15+
const serverUrl = 'baseHandler.test.com';
16+
17+
const mockPlaybooksResponse: FetchPlaybooksReturn = {
18+
total_count: 2,
19+
page_count: 1,
20+
has_more: false,
21+
items: [
22+
TestHelper.fakePlaybook({
23+
id: 'playbook-id-1',
24+
title: 'Test Playbook 1',
25+
description: 'Test Description 1',
26+
}),
27+
TestHelper.fakePlaybook({
28+
id: 'playbook-id-2',
29+
title: 'Test Playbook 2',
30+
description: 'Test Description 2',
31+
}),
32+
],
33+
};
34+
35+
const mockClient = {
36+
fetchPlaybooks: jest.fn(),
37+
};
38+
39+
const throwFunc = () => {
40+
throw Error('error');
41+
};
42+
43+
beforeAll(() => {
44+
// @ts-ignore
45+
NetworkManager.getClient = () => mockClient;
46+
});
47+
48+
beforeEach(() => {
49+
jest.clearAllMocks();
50+
});
51+
52+
describe('fetchPlaybooks', () => {
53+
it('should fetch playbooks successfully', async () => {
54+
mockClient.fetchPlaybooks.mockResolvedValueOnce(mockPlaybooksResponse);
55+
56+
const params: FetchPlaybooksParams = {
57+
team_id: 'team-id-1',
58+
page: 0,
59+
per_page: 10,
60+
};
61+
62+
const result = await fetchPlaybooks(serverUrl, params);
63+
64+
expect(result).toBeDefined();
65+
expect(result.error).toBeUndefined();
66+
expect(result.data).toEqual(mockPlaybooksResponse);
67+
expect(mockClient.fetchPlaybooks).toHaveBeenCalledWith(params);
68+
expect(logDebug).not.toHaveBeenCalled();
69+
expect(forceLogoutIfNecessary).not.toHaveBeenCalled();
70+
});
71+
72+
it('should handle client error', async () => {
73+
jest.spyOn(NetworkManager, 'getClient').mockImplementationOnce(throwFunc);
74+
75+
const params: FetchPlaybooksParams = {
76+
team_id: 'team-id-1',
77+
};
78+
79+
const result = await fetchPlaybooks(serverUrl, params);
80+
81+
expect(result).toBeDefined();
82+
expect(result.error).toBeDefined();
83+
expect(result.data).toBeUndefined();
84+
expect(mockClient.fetchPlaybooks).not.toHaveBeenCalled();
85+
expect(logDebug).toHaveBeenCalled();
86+
expect(forceLogoutIfNecessary).toHaveBeenCalledWith(serverUrl, expect.any(Error));
87+
});
88+
89+
it('should handle API exception', async () => {
90+
const apiError = new Error('API error');
91+
mockClient.fetchPlaybooks.mockRejectedValueOnce(apiError);
92+
93+
const params: FetchPlaybooksParams = {
94+
team_id: 'team-id-1',
95+
page: 1,
96+
per_page: 20,
97+
sort: 'title',
98+
direction: 'asc',
99+
};
100+
101+
const result = await fetchPlaybooks(serverUrl, params);
102+
103+
expect(result).toBeDefined();
104+
expect(result.error).toBeDefined();
105+
expect(result.error).toBe(apiError);
106+
expect(result.data).toBeUndefined();
107+
expect(mockClient.fetchPlaybooks).toHaveBeenCalledWith(params);
108+
expect(logDebug).toHaveBeenCalledWith('error on fetchPlaybooks', expect.any(String));
109+
expect(forceLogoutIfNecessary).toHaveBeenCalledWith(serverUrl, apiError);
110+
});
111+
});
112+

0 commit comments

Comments
 (0)