Skip to content

Commit 72b7021

Browse files
Enhance weekly financial reports workflow tests and error handling
- Added a mock for `fetchFinancialAppData` in the test suite to validate its integration with the `weeklyFinancialReportsWorkflow`. - Implemented a new test case to ensure that errors from `fetchFinancialAppData` are properly propagated. - Updated the error message for invalid group names to correct a typo in the parameter name. These changes improve the robustness of the workflow tests and ensure accurate error reporting.
1 parent 15e87c7 commit 72b7021

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

workers/main/src/workflows/weeklyFinancialReports/weeklyFinancialReports.workflow.test.ts

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,38 @@ import { weeklyFinancialReportsWorkflow } from './weeklyFinancialReports.workflo
77

88
vi.mock('@temporalio/workflow', () => {
99
const getTargetUnitsMock = vi.fn();
10+
const fetchFinancialAppDataMock = vi.fn();
1011

1112
return {
1213
proxyActivities: () => ({
1314
getTargetUnits: getTargetUnitsMock,
15+
fetchFinancialAppData: fetchFinancialAppDataMock,
1416
}),
1517
__getTargetUnitsMock: () => getTargetUnitsMock,
18+
__getFetchFinancialAppDataMock: () => fetchFinancialAppDataMock,
1619
};
1720
});
1821

1922
describe('weeklyFinancialReportsWorkflow', () => {
2023
type WorkflowModuleWithMock = typeof workflowModule & {
2124
__getTargetUnitsMock: () => ReturnType<typeof vi.fn>;
25+
__getFetchFinancialAppDataMock: () => ReturnType<typeof vi.fn>;
2226
};
2327
const getTargetUnitsMock = (
2428
workflowModule as WorkflowModuleWithMock
2529
).__getTargetUnitsMock();
30+
const fetchFinancialAppDataMock = (
31+
workflowModule as WorkflowModuleWithMock
32+
).__getFetchFinancialAppDataMock();
2633

2734
beforeEach(() => {
2835
getTargetUnitsMock.mockReset();
36+
fetchFinancialAppDataMock.mockReset();
2937
});
3038

31-
it.each([[GroupNameEnum.SD_REPORT], [GroupNameEnum.ED_REPORT]])(
32-
'returns the fileLink from getTargetUnits for group %s',
33-
async (groupName: GroupNameEnum) => {
34-
getTargetUnitsMock.mockResolvedValueOnce({
35-
fileLink: `${groupName}-mocked-link.json`,
36-
});
37-
const result = await weeklyFinancialReportsWorkflow(groupName);
38-
39-
expect(result).toBe(`${groupName}-mocked-link.json`);
40-
},
41-
);
42-
4339
it('throws AppError for invalid group name', async () => {
4440
const allowedValues = Object.values(GroupNameEnum).join('", "');
45-
const expectedMessage = `Invalid groupName paramter: INVALID_GROUP. Allowed values: "${allowedValues}"`;
41+
const expectedMessage = `Invalid groupName parameter: INVALID_GROUP. Allowed values: "${allowedValues}"`;
4642

4743
await expect(
4844
weeklyFinancialReportsWorkflow(
@@ -62,4 +58,26 @@ describe('weeklyFinancialReportsWorkflow', () => {
6258
weeklyFinancialReportsWorkflow(GroupNameEnum.SD_REPORT),
6359
).rejects.toThrow('activity error');
6460
});
61+
62+
it('propagates error from fetchFinancialAppData', async () => {
63+
getTargetUnitsMock.mockResolvedValueOnce({ fileLink: 'file.json' });
64+
fetchFinancialAppDataMock.mockRejectedValueOnce(new Error('fetch error'));
65+
await expect(
66+
weeklyFinancialReportsWorkflow(GroupNameEnum.SD_REPORT),
67+
).rejects.toThrow('fetch error');
68+
});
69+
70+
it('returns fileLink on success', async () => {
71+
getTargetUnitsMock.mockResolvedValueOnce({ fileLink: 'file.json' });
72+
fetchFinancialAppDataMock.mockResolvedValueOnce({
73+
fileLink: 'result.json',
74+
});
75+
const result = await weeklyFinancialReportsWorkflow(
76+
GroupNameEnum.SD_REPORT,
77+
);
78+
79+
expect(result).toBe('result.json');
80+
expect(getTargetUnitsMock).toHaveBeenCalledWith(GroupNameEnum.SD_REPORT);
81+
expect(fetchFinancialAppDataMock).toHaveBeenCalledWith('file.json');
82+
});
6583
});

workers/main/src/workflows/weeklyFinancialReports/weeklyFinancialReports.workflow.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export async function weeklyFinancialReportsWorkflow(
1616
): Promise<string> {
1717
if (!(Object.values(GroupNameEnum) as GroupName[]).includes(groupName)) {
1818
throw new AppError(
19-
`Invalid groupName paramter: ${groupName}. Allowed values: "${Object.values(GroupNameEnum).join('", "')}"`,
19+
`Invalid groupName parameter: ${groupName}. Allowed values: "${Object.values(GroupNameEnum).join('", "')}"`,
2020
'weeklyFinancialReportsWorkflow',
2121
);
2222
}

0 commit comments

Comments
 (0)