Skip to content

Commit

Permalink
adding tests for error message
Browse files Browse the repository at this point in the history
  • Loading branch information
igatanasov committed Nov 13, 2024
1 parent fa8f7bd commit ab92f4b
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 3 deletions.
4 changes: 1 addition & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@
"request": "launch",
"skipFiles": ["<node_internals>/**"],
"type": "node",
"env": {
// "N8N_PORT": "5679",
},
"envFile": "${workspaceFolder}/.env",
"outputCapture": "std",
"killBehavior": "polite"
},
Expand Down
105 changes: 105 additions & 0 deletions packages/editor-ui/src/composables/usePushConnection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import type { PushMessage, PushPayload } from '@n8n/api-types';
import { usePushConnection } from '@/composables/usePushConnection';
import { usePushConnectionStore } from '@/stores/pushConnection.store';
import { useOrchestrationStore } from '@/stores/orchestration.store';
import { useUIStore } from '@/stores/ui.store';
import { useWorkflowsStore } from '@/stores/workflows.store';
import { useToast } from '@/composables/useToast';
import { WorkflowOperationError } from 'n8n-workflow';

Check failure on line 11 in packages/editor-ui/src/composables/usePushConnection.test.ts

View workflow job for this annotation

GitHub Actions / Lint / Lint

All imports in the declaration are only used as types. Use `import type`

vi.mock('vue-router', () => {
return {
Expand All @@ -16,20 +20,37 @@ vi.mock('vue-router', () => {
};
});

vi.mock('@/composables/useToast', () => {
const showMessage = vi.fn();
const showError = vi.fn();
return {
useToast: () => {
return {
showMessage,
showError,
};
},
};
});

vi.useFakeTimers();

describe('usePushConnection()', () => {
let router: ReturnType<typeof useRouter>;
let pushStore: ReturnType<typeof usePushConnectionStore>;
let orchestrationStore: ReturnType<typeof useOrchestrationStore>;
let pushConnection: ReturnType<typeof usePushConnection>;
let uiStore: ReturnType<typeof useUIStore>;
let workflowsStore: ReturnType<typeof useWorkflowsStore>;

beforeEach(() => {
setActivePinia(createPinia());

router = vi.mocked(useRouter)();
pushStore = usePushConnectionStore();
orchestrationStore = useOrchestrationStore();
uiStore = useUIStore();
workflowsStore = useWorkflowsStore();
pushConnection = usePushConnection({ router });
});

Expand Down Expand Up @@ -106,5 +127,89 @@ describe('usePushConnection()', () => {
expect(result).toBeTruthy();
});
});

describe('executionFinished', () => {
it('should handle executionFinished event correctly', async () => {
const event: PushMessage = {
type: 'executionFinished',
data: {
executionId: '1',
data: {
data: {
resultData: {
runData: {},
},
},
finished: true,
mode: 'manual',
startedAt: new Date(),
stoppedAt: new Date(),
status: 'success',
},
},
};

workflowsStore.activeExecutionId = '1';
uiStore.isActionActive['workflowRunning'] = true;

const result = await pushConnection.pushMessageReceived(event);

expect(result).toBeTruthy();
expect(workflowsStore.workflowExecutionData).toBeDefined();
expect(uiStore.isActionActive['workflowRunning']).toBeTruthy();

expect(useToast().showMessage).toHaveBeenCalledWith({
title: 'Workflow executed successfully',
type: 'success',
});
});

it('should handle isManualExecutionCancelled correctly', async () => {
const event: PushMessage = {
type: 'executionFinished',
data: {
executionId: '1',
data: {
data: {
startData: {},
resultData: {
runData: {
'Last Node': [],
},
lastNodeExecuted: 'Last Node',
error: {
message:
'Your trial has ended. <a href="https://app.n8n.cloud/account/change-plan">Upgrade now</a> to keep automating',
name: 'NodeApiError',
node: 'Last Node',
} as unknown as WorkflowOperationError,
},
},
startedAt: new Date(),
mode: 'manual',
status: 'running',
},
},
};

workflowsStore.activeExecutionId = '1';
uiStore.isActionActive['workflowRunning'] = true;

const result = await pushConnection.pushMessageReceived(event);

expect(useToast().showMessage).toHaveBeenCalledWith({
message:
'Your trial has ended. <a href="https://app.n8n.cloud/account/change-plan">Upgrade now</a> to keep automating',
title: 'Problem in node ‘Last Node‘',
type: 'error',
duration: 0,
dangerouslyUseHTMLString: true,
});

expect(result).toBeTruthy();
expect(workflowsStore.workflowExecutionData).toBeDefined();
expect(uiStore.isActionActive['workflowRunning']).toBeTruthy();
});
});
});
});

0 comments on commit ab92f4b

Please sign in to comment.