n8n/packages/cli/test/unit/services/naming.service.test.ts
Iván Ovejero 69c2254742
refactor(core): Introduce naming service (no-changelog) (#7985)
## Summary
Provide details about your pull request and what it adds, fixes, or
changes. Photos and videos are recommended.
Continue breaking down `GenericHelpers.ts`
...

#### How to test the change:
1. ...


## Issues fixed
Include links to Github issue or Community forum post or **Linear
ticket**:
> Important in order to close automatically and provide context to
reviewers

...


## Review / Merge checklist
- [ ] PR title and summary are descriptive. **Remember, the title
automatically goes into the changelog. Use `(no-changelog)` otherwise.**
([conventions](https://github.com/n8n-io/n8n/blob/master/.github/pull_request_title_conventions.md))
- [ ] [Docs updated](https://github.com/n8n-io/n8n-docs) or follow-up
ticket created.
- [ ] Tests included.
> A bug is not considered fixed, unless a test is added to prevent it
from happening again. A feature is not complete without tests.
  >
> *(internal)* You can use Slack commands to trigger [e2e
tests](https://www.notion.so/n8n/How-to-use-Test-Instances-d65f49dfc51f441ea44367fb6f67eb0a?pvs=4#a39f9e5ba64a48b58a71d81c837e8227)
or [deploy test
instance](https://www.notion.so/n8n/How-to-use-Test-Instances-d65f49dfc51f441ea44367fb6f67eb0a?pvs=4#f6a177d32bde4b57ae2da0b8e454bfce)
or [deploy early access version on
Cloud](https://www.notion.so/n8n/Cloudbot-3dbe779836004972b7057bc989526998?pvs=4#fef2d36ab02247e1a0f65a74f6fb534e).
2023-12-11 12:35:14 +01:00

86 lines
2.9 KiB
TypeScript

import { WorkflowRepository } from '@/databases/repositories/workflow.repository';
import { CredentialsRepository } from '@/databases/repositories/credentials.repository';
import { mockInstance } from '../../shared/mocking';
import { NamingService } from '@/services/naming.service';
import type { WorkflowEntity } from '@/databases/entities/WorkflowEntity';
import type { CredentialsEntity } from '@/databases/entities/CredentialsEntity';
describe('NamingService', () => {
const workflowRepository = mockInstance(WorkflowRepository);
const credentialsRepository = mockInstance(CredentialsRepository);
const namingService = new NamingService(workflowRepository, credentialsRepository);
beforeEach(() => {
jest.restoreAllMocks();
});
describe('getUniqueWorkflowName()', () => {
test('should return requested name if already unique', async () => {
workflowRepository.find.mockResolvedValue([]);
const name = await namingService.getUniqueWorkflowName('foo');
expect(name).toEqual('foo');
});
test('should return requested name suffixed if already existing once', async () => {
workflowRepository.find.mockResolvedValue([{ name: 'foo' }] as WorkflowEntity[]);
const name = await namingService.getUniqueWorkflowName('foo');
expect(name).toEqual('foo 2');
});
test('should return requested name with incremented suffix if already suffixed', async () => {
const existingNames = [{ name: 'foo' }, { name: 'foo 2' }] as WorkflowEntity[];
workflowRepository.find.mockResolvedValue(existingNames);
const name = await namingService.getUniqueWorkflowName('foo');
expect(name).toEqual('foo 3');
existingNames.push({ name: 'foo 3' } as WorkflowEntity);
const _name = await namingService.getUniqueWorkflowName('foo');
expect(_name).toEqual('foo 4');
});
});
describe('getUniqueCredentialName()', () => {
test('should return requested name if already unique', async () => {
credentialsRepository.find.mockResolvedValue([]);
const name = await namingService.getUniqueCredentialName('foo');
expect(name).toEqual('foo');
});
test('should return requested name suffixed if already existing once', async () => {
credentialsRepository.find.mockResolvedValue([{ name: 'foo' }] as CredentialsEntity[]);
const name = await namingService.getUniqueCredentialName('foo');
expect(name).toEqual('foo 2');
});
test('should return requested name with incremented suffix if already suffixed', async () => {
const existingNames = [{ name: 'foo' }, { name: 'foo 2' }] as CredentialsEntity[];
credentialsRepository.find.mockResolvedValue(existingNames);
const name = await namingService.getUniqueCredentialName('foo');
expect(name).toEqual('foo 3');
existingNames.push({ name: 'foo 3' } as CredentialsEntity);
const _name = await namingService.getUniqueCredentialName('foo');
expect(_name).toEqual('foo 4');
});
});
});