@sync-in/server
Version:
The secure, open-source platform for file storage, sharing, collaboration, and sync
162 lines (161 loc) • 6.9 kB
JavaScript
/*
* Copyright (C) 2012-2025 Johan Legrand <johan.legrand@sync-in.com>
* This file is part of Sync-in | The open source file sync and share solution
* See the LICENSE file for licensing details
*/ "use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
const _testing = require("@nestjs/testing");
const _cacheservice = require("../../infrastructure/cache/services/cache.service");
const _contextmanagerservice = require("../../infrastructure/context/services/context-manager.service");
const _constants = require("../../infrastructure/database/constants");
const _filesqueriesservice = require("../files/services/files-queries.service");
const _linksqueriesservice = require("../links/services/links-queries.service");
const _notificationsmanagerservice = require("../notifications/services/notifications-manager.service");
const _sharesmanagerservice = require("../shares/services/shares-manager.service");
const _sharesqueriesservice = require("../shares/services/shares-queries.service");
const _spacesmanagerservice = require("../spaces/services/spaces-manager.service");
const _spacesqueriesservice = require("../spaces/services/spaces-queries.service");
const _usersqueriesservice = require("../users/services/users-queries.service");
const _commentscontroller = require("./comments.controller");
const _commentsmanagerservice = require("./services/comments-manager.service");
const _commentsqueriesservice = require("./services/comments-queries.service");
describe(_commentscontroller.CommentsController.name, ()=>{
let commentsController;
let commentsManager;
const user = {
id: 'user-1'
};
const space = {
id: 'space-1'
};
const commentsSample = [
{
id: 'c1'
},
{
id: 'c2'
}
];
const commentSample = {
id: 'c1',
text: 'hello'
};
const commentsManagerMock = {
getComments: jest.fn(),
createComment: jest.fn(),
updateComment: jest.fn(),
deleteComment: jest.fn(),
getRecents: jest.fn()
};
beforeAll(async ()=>{
const module = await _testing.Test.createTestingModule({
controllers: [
_commentscontroller.CommentsController
],
providers: [
{
provide: _notificationsmanagerservice.NotificationsManager,
useValue: {}
},
{
provide: _constants.DB_TOKEN_PROVIDER,
useValue: {}
},
{
provide: _cacheservice.Cache,
useValue: {}
},
_contextmanagerservice.ContextManager,
{
provide: _commentsmanagerservice.CommentsManager,
useValue: commentsManagerMock
},
_commentsqueriesservice.CommentsQueries,
_spacesmanagerservice.SpacesManager,
_spacesqueriesservice.SpacesQueries,
_filesqueriesservice.FilesQueries,
_sharesmanagerservice.SharesManager,
_sharesqueriesservice.SharesQueries,
_usersqueriesservice.UsersQueries,
_linksqueriesservice.LinksQueries
]
}).compile();
commentsController = module.get(_commentscontroller.CommentsController);
commentsManager = module.get(_commentsmanagerservice.CommentsManager);
});
beforeEach(()=>{
jest.clearAllMocks();
});
it('should be defined', ()=>{
expect(commentsController).toBeDefined();
});
it('getFromSpace calls CommentsManager.getComments and returns the list', async ()=>{
commentsManager.getComments.mockResolvedValueOnce(commentsSample);
const res = await commentsController.getFromSpace(user, space);
expect(commentsManager.getComments).toHaveBeenCalledTimes(1);
expect(commentsManager.getComments).toHaveBeenCalledWith(user, space);
expect(res).toEqual(commentsSample);
});
it('createFromSpace calls CommentsManager.createComment and returns the created comment', async ()=>{
const dto = {
text: 'new comment'
};
commentsManager.createComment.mockResolvedValueOnce(commentSample);
const res = await commentsController.createFromSpace(user, space, dto);
expect(commentsManager.createComment).toHaveBeenCalledTimes(1);
expect(commentsManager.createComment).toHaveBeenCalledWith(user, space, dto);
expect(res).toEqual(commentSample);
});
it('updateFromSpace calls CommentsManager.updateComment and returns the updated comment', async ()=>{
const dto = {
id: 'c1',
text: 'updated'
};
const updated = {
id: 'c1',
text: 'updated'
};
commentsManager.updateComment.mockResolvedValueOnce(updated);
const res = await commentsController.updateFromSpace(user, space, dto);
expect(commentsManager.updateComment).toHaveBeenCalledTimes(1);
expect(commentsManager.updateComment).toHaveBeenCalledWith(user, space, dto);
expect(res).toEqual(updated);
});
it('deleteFromSpace calls CommentsManager.deleteComment', async ()=>{
const dto = {
id: 'c1'
};
commentsManager.deleteComment.mockResolvedValueOnce(undefined);
await expect(commentsController.deleteFromSpace(user, space, dto)).resolves.toBeUndefined();
expect(commentsManager.deleteComment).toHaveBeenCalledTimes(1);
expect(commentsManager.deleteComment).toHaveBeenCalledWith(user, space, dto);
});
it('getRecents calls CommentsManager.getRecents with the provided limit', async ()=>{
const recents = [
{
id: 'r1'
}
];
commentsManager.getRecents.mockResolvedValueOnce(recents);
const res = await commentsController.getRecents(user, 5);
expect(commentsManager.getRecents).toHaveBeenCalledTimes(1);
expect(commentsManager.getRecents).toHaveBeenCalledWith(user, 5);
expect(res).toEqual(recents);
});
it('getRecents uses the default limit (10) when not provided', async ()=>{
const recents = [
{
id: 'r2'
}
];
commentsManager.getRecents.mockResolvedValueOnce(recents);
// Call with undefined to trigger the parameter default value
const res = await commentsController.getRecents(user, undefined);
expect(commentsManager.getRecents).toHaveBeenCalledTimes(1);
expect(commentsManager.getRecents).toHaveBeenCalledWith(user, 10);
expect(res).toEqual(recents);
});
});
//# sourceMappingURL=comments.controller.spec.js.map