Skip to content

Commit

Permalink
test(snippet): write tests case for updating and deleting a snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
tericcabrel committed Nov 1, 2023
1 parent d52c4bc commit a49bee6
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 12 deletions.
173 changes: 161 additions & 12 deletions packages/domain/__tests__/services/snippets/snippet.service.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { Snippet } from '@sharingan/database';
import SharinganError, { errors } from '@sharingan/utils';
import SharinganError, { errors, generateRandomId } from '@sharingan/utils';

import { roleService, snippetService } from '../../../index';
import {
createTestSnippet,
createTestSnippetDto,
createUserWithRootFolder,
deleteTestFoldersById,
deleteTestSnippetDto,
deleteTestSnippetsById,
deleteTestUsersById,
updateTestSnippetDto,
} from '../../setup/test-utils';

describe('Test Snippet service', () => {
Expand Down Expand Up @@ -140,29 +142,176 @@ describe('Test Snippet service', () => {
await deleteTestUsersById([user1.id, user2.id]);
});

it('should find all snippets of a folder', async () => {
it('should retrieve a snippet by its ID', async () => {
// GIVEN
const [user1, rootFolder1] = await createUserWithRootFolder();

const [snippet] = await Promise.all([
createTestSnippet({ folderId: rootFolder1.id, userId: user1.id, visibility: 'public' }),
]);

// WHEN
const snippetFound = await snippetService.findById(snippet.id);

// THEN
expect(snippetFound).toMatchObject({
folderId: rootFolder1.id,
id: snippet.id,
userId: user1.id,
visibility: 'public',
});

await deleteTestSnippetsById([snippet.id]);
await deleteTestFoldersById([rootFolder1.id]);
await deleteTestUsersById([user1.id]);
});

it('should found no snippet given the ID provided', async () => {
// GIVEN
const snippetId = generateRandomId();

// WHEN
// THEN
await expect(async () => {
await snippetService.findById(snippetId);
}).rejects.toThrow(new SharinganError(errors.SNIPPET_NOT_FOUND(snippetId), 'SNIPPET_NOT_FOUND'));
});

it('should delete an existing snippet belonging to a user', async () => {
// GIVEN
const [user, rootFolder] = await createUserWithRootFolder();

const [snippet1, snippet2] = await Promise.all([
createTestSnippet({ folderId: rootFolder.id, userId: user.id, visibility: 'public' }),
createTestSnippet({ folderId: rootFolder.id, userId: user.id, visibility: 'private' }),
]);

// WHEN
const deleteSnippetDto = deleteTestSnippetDto({ snippetId: snippet1.id, userId: snippet1.userId });

await snippetService.delete(deleteSnippetDto);

// THEN
const folderSnippets = await snippetService.findByFolder(rootFolder.id);

expect(folderSnippets).toHaveLength(1);

await deleteTestSnippetsById([snippet2.id]);
await deleteTestFoldersById([rootFolder.id]);
await deleteTestUsersById([user.id]);
});

it('should not delete an existing snippet because it belongs to other user', async () => {
// GIVEN
const [user1, rootFolder1] = await createUserWithRootFolder();
const [user2, rootFolder2] = await createUserWithRootFolder();

const existingSnippets = await Promise.all([
const [snippet1, snippet2, snippet3] = await Promise.all([
createTestSnippet({ folderId: rootFolder1.id, userId: user1.id, visibility: 'public' }),
createTestSnippet({ folderId: rootFolder1.id, userId: user1.id, visibility: 'private' }),
createTestSnippet({ folderId: rootFolder2.id, userId: user2.id, visibility: 'public' }),
createTestSnippet({ folderId: rootFolder1.id, userId: user1.id, visibility: 'private' }),
createTestSnippet({ folderId: rootFolder2.id, userId: user2.id, visibility: 'public' }),
createTestSnippet({ folderId: rootFolder1.id, userId: user1.id, visibility: 'private' }),
createTestSnippet({ folderId: rootFolder1.id, userId: user1.id, visibility: 'private' }),
createTestSnippet({ folderId: rootFolder2.id, userId: user2.id, visibility: 'private' }),
createTestSnippet({ folderId: rootFolder1.id, userId: user1.id, visibility: 'private' }),
]);

// WHEN
const folderSnippets = await snippetService.findByFolder(rootFolder1.id);
const deleteSnippetDto = deleteTestSnippetDto({ snippetId: snippet1.id, userId: user2.id });

// THEN
await expect(folderSnippets).toHaveLength(6);
await expect(async () => {
await snippetService.delete(deleteSnippetDto);
}).rejects.toThrow(
new SharinganError(errors.CANT_EDIT_SNIPPET(deleteSnippetDto.creatorId, snippet1.id), 'CANT_EDIT_SNIPPET'),
);

await deleteTestSnippetsById(existingSnippets.map((snippet) => snippet.id));
const user1FolderSnippets = await snippetService.findByFolder(rootFolder1.id);
const user2FolderSnippets = await snippetService.findByFolder(rootFolder2.id);

expect(user1FolderSnippets).toHaveLength(2);
expect(user2FolderSnippets).toHaveLength(1);

await deleteTestSnippetsById([snippet2.id, snippet3.id]);
await deleteTestFoldersById([rootFolder1.id, rootFolder2.id]);
await deleteTestUsersById([user1.id, user2.id]);
});

it('should update an existing snippet in the specified folder', async () => {
// GIVEN
const [user, rootFolder] = await createUserWithRootFolder();
const [snippet] = await Promise.all([
createTestSnippet({ folderId: rootFolder.id, userId: user.id, visibility: 'public' }),
]);

const updateSnippetDto = updateTestSnippetDto({ snippetId: snippet.id, userId: user.id });

// WHEN
const updatedSnippet = await snippetService.update(updateSnippetDto);

// THEN
const snippetToUpdate = updateSnippetDto.toSnippet(snippet);

expect(updatedSnippet).toMatchObject<Snippet>({
content: snippetToUpdate.content,
contentHtml: snippetToUpdate.contentHtml,
createdAt: expect.any(Date),
description: snippetToUpdate.description,
folderId: rootFolder.id,
id: snippet.id,
language: snippetToUpdate.language,
lineHighlight: snippetToUpdate.lineHighlight,
name: snippetToUpdate.name,
size: snippetToUpdate.size,
theme: snippetToUpdate.theme,
updatedAt: expect.any(Date),
userId: user.id,
visibility: snippetToUpdate.visibility,
});

await deleteTestSnippetsById([updatedSnippet.id]);
await deleteTestFoldersById([rootFolder.id]);
await deleteTestUsersById([user.id]);
});

it('should not update an existing snippet in the specified folder because another snippet with the updated name already exists in the folder', async () => {
// GIVEN
const [user, rootFolder] = await createUserWithRootFolder();
const [snippet] = await Promise.all([
createTestSnippet({ folderId: rootFolder.id, name: 'snippet-one.java', userId: user.id }),
createTestSnippet({ folderId: rootFolder.id, name: 'snippet-two.java', userId: user.id, visibility: 'private' }),
]);

const updateSnippetDto = updateTestSnippetDto({ name: 'snippet-two.java', snippetId: snippet.id, userId: user.id });

// WHEN
// THEN
await expect(async () => {
await snippetService.update(updateSnippetDto);
}).rejects.toThrow(
new SharinganError(errors.SNIPPET_ALREADY_EXIST(updateSnippetDto.name), 'SNIPPET_ALREADY_EXIST'),
);

await deleteTestSnippetsById([snippet.id]);
await deleteTestFoldersById([rootFolder.id]);
await deleteTestUsersById([user.id]);
});

it('should not update an existing snippet in the specified folder because because it belongs to other user', async () => {
// GIVEN
const [user1, rootFolder1] = await createUserWithRootFolder();
const [user2, rootFolder2] = await createUserWithRootFolder();
const [snippet] = await Promise.all([
createTestSnippet({ folderId: rootFolder1.id, name: 'snippet-one.java', userId: user1.id }),
]);

const updateSnippetDto = updateTestSnippetDto({ snippetId: snippet.id, userId: user2.id });

// WHEN
// THEN
await expect(async () => {
await snippetService.update(updateSnippetDto);
}).rejects.toThrow(
new SharinganError(errors.CANT_EDIT_SNIPPET(updateSnippetDto.creatorId, snippet.id), 'CANT_EDIT_SNIPPET'),
);

await deleteTestSnippetsById([snippet.id]);
await deleteTestFoldersById([rootFolder1.id, rootFolder2.id]);
await deleteTestUsersById([user1.id, user2.id]);
});
Expand Down
35 changes: 35 additions & 0 deletions packages/domain/__tests__/setup/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import CreateFolderDto from '../../src/folders/dtos/create-folder-dto';
import CreateUserRootFolderDto from '../../src/folders/dtos/create-user-root-folder-dto';
import CreateSessionDto from '../../src/sessions/dtos/create-session-dto';
import CreateSnippetDto from '../../src/snippets/dtos/create-snippet-dto';
import DeleteSnippetDto from '../../src/snippets/dtos/delete-snippet-dto';
import UpdateSnippetDto from '../../src/snippets/dtos/update-snippet-dto';
import UpdateUserDto from '../../src/users/dtos/update-user-dto';

type CreateManyTestFoldersArgs = {
Expand Down Expand Up @@ -218,3 +220,36 @@ export const createTestSession = async (args: { userId: string }): Promise<Sessi
export const deleteTestUserSessions = async (userId: string): Promise<void> => {
await dbClient.session.deleteMany({ where: { userId } });
};

export const deleteTestSnippetDto = (args: { snippetId: string; userId: string }): DeleteSnippetDto => {
return new DeleteSnippetDto({
creatorId: args.userId,
snippetId: args.snippetId,
});
};

export const updateTestSnippetDto = (
args: { name?: string; snippetId?: string; userId?: string; visibility?: SnippetVisibility } | undefined,
): UpdateSnippetDto => {
const languages = ['java', 'js', 'ts', 'c', 'c++', 'python', 'go', 'php', 'csharp'];
const extensions = ['java', 'js', 'ts', 'c', 'cpp', 'py', 'go', 'php', 'cs'];
const themes = ['one-dark-pro', 'dracula', 'dark-plus', 'monokai', 'github-dark', 'github-light'];

const languageIndex = randNumber({ max: languages.length - 1, min: 0 });
const themeIndex = randNumber({ max: themes.length - 1, min: 0 });

const snippetContent = randWord({ length: randNumber({ max: 30, min: 5 }) }).join('\n');

return new UpdateSnippetDto({
content: snippetContent,
contentHighlighted: `${snippetContent} highlighted`,
creatorId: args?.userId ?? generateTestId(),
description: randWord({ length: randNumber({ max: 20, min: 10 }) }).join(' '),
language: languages[languageIndex],
lineHighlight: null,
name: args?.name ?? `${randWord()}.${extensions[languageIndex]}`,
snippetId: args?.snippetId ?? generateTestId(),
theme: themes[themeIndex],
visibility: args?.visibility ?? 'public',
});
};

0 comments on commit a49bee6

Please sign in to comment.