2023-05-16 07:02:58 +03:00
|
|
|
import assert from 'assert';
|
2023-06-01 09:43:20 +03:00
|
|
|
import {CollectionsService, CollectionsRepositoryInMemory, Collection} from '../src/index';
|
2023-06-01 07:16:39 +03:00
|
|
|
import {posts} from './fixtures/posts';
|
2023-05-25 18:34:59 +03:00
|
|
|
|
|
|
|
describe('CollectionsService', function () {
|
2023-05-17 12:11:14 +03:00
|
|
|
let collectionsService: CollectionsService;
|
2023-05-25 18:34:59 +03:00
|
|
|
|
|
|
|
beforeEach(async function () {
|
|
|
|
const collectionsRepository = new CollectionsRepositoryInMemory();
|
2023-05-17 12:11:14 +03:00
|
|
|
|
2023-05-25 18:34:59 +03:00
|
|
|
collectionsService = new CollectionsService({
|
2023-06-01 07:16:39 +03:00
|
|
|
collectionsRepository
|
2023-05-25 18:34:59 +03:00
|
|
|
});
|
2023-05-17 12:11:14 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Instantiates a CollectionsService', function () {
|
2023-05-16 07:02:58 +03:00
|
|
|
assert.ok(collectionsService, 'CollectionsService should initialize');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can do CRUD operations on a collection', async function () {
|
2023-06-01 07:16:39 +03:00
|
|
|
const savedCollection = await collectionsService.createCollection({
|
2023-05-16 07:02:58 +03:00
|
|
|
title: 'testing collections',
|
|
|
|
description: 'testing collections description',
|
|
|
|
type: 'manual',
|
2023-06-01 07:16:39 +03:00
|
|
|
filter: null
|
2023-05-16 07:02:58 +03:00
|
|
|
});
|
|
|
|
|
2023-05-17 09:42:35 +03:00
|
|
|
const createdCollection = await collectionsService.getById(savedCollection.id);
|
2023-05-16 07:02:58 +03:00
|
|
|
|
|
|
|
assert.ok(createdCollection, 'Collection should be saved');
|
2023-05-17 09:42:35 +03:00
|
|
|
assert.ok(savedCollection.id, 'Collection should have an id');
|
2023-05-16 07:02:58 +03:00
|
|
|
assert.equal(createdCollection.title, 'testing collections', 'Collection title should match');
|
|
|
|
|
|
|
|
const allCollections = await collectionsService.getAll();
|
2023-05-17 09:52:27 +03:00
|
|
|
assert.equal(allCollections.data.length, 1, 'There should be one collection');
|
2023-05-16 07:02:58 +03:00
|
|
|
|
2023-05-17 09:42:35 +03:00
|
|
|
await collectionsService.destroy(savedCollection.id);
|
|
|
|
const deletedCollection = await collectionsService.getById(savedCollection.id);
|
2023-05-16 07:02:58 +03:00
|
|
|
|
|
|
|
assert.equal(deletedCollection, null, 'Collection should be deleted');
|
|
|
|
});
|
2023-05-17 09:42:35 +03:00
|
|
|
|
2023-06-01 09:43:20 +03:00
|
|
|
describe('toDTO', function () {
|
|
|
|
it('Can map Collection entity to DTO object', async function () {
|
|
|
|
const collection = await Collection.create({});
|
|
|
|
const dto = collectionsService.toDTO(collection);
|
|
|
|
|
|
|
|
assert.equal(dto.id, collection.id, 'DTO should have the same id as the entity');
|
|
|
|
assert.equal(dto.title, null, 'DTO should return null if nullable property of the entity is unassigned');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-06-01 07:16:39 +03:00
|
|
|
describe('addPostToCollection', function () {
|
|
|
|
it('Can add a Post to a Collection', async function () {
|
|
|
|
const collection = await collectionsService.createCollection({
|
|
|
|
title: 'testing collections',
|
|
|
|
description: 'testing collections description',
|
|
|
|
type: 'manual'
|
|
|
|
});
|
|
|
|
|
|
|
|
const editedCollection = await collectionsService.addPostToCollection(collection.id, posts[0]);
|
|
|
|
|
|
|
|
assert.equal(editedCollection?.posts.length, 1, 'Collection should have one post');
|
|
|
|
assert.equal(editedCollection?.posts[0].id, posts[0].id, 'Collection should have the correct post');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Does not error when trying to add a post to a collection that does not exist', async function () {
|
|
|
|
const editedCollection = await collectionsService.addPostToCollection('fake id', posts[0]);
|
|
|
|
assert(editedCollection === null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-05-17 17:59:03 +03:00
|
|
|
describe('edit', function () {
|
|
|
|
it('Can edit existing collection', async function () {
|
2023-06-01 07:16:39 +03:00
|
|
|
const savedCollection = await collectionsService.createCollection({
|
2023-05-17 17:59:03 +03:00
|
|
|
title: 'testing collections',
|
|
|
|
description: 'testing collections description',
|
2023-06-01 07:16:39 +03:00
|
|
|
type: 'manual'
|
2023-05-17 17:59:03 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
const editedCollection = await collectionsService.edit({
|
|
|
|
id: savedCollection.id,
|
2023-06-01 08:37:51 +03:00
|
|
|
title: 'Edited title',
|
|
|
|
description: 'Edited description',
|
|
|
|
feature_image: '/assets/images/edited.jpg'
|
2023-05-17 17:59:03 +03:00
|
|
|
});
|
|
|
|
|
2023-06-01 08:37:51 +03:00
|
|
|
assert.equal(editedCollection?.title, 'Edited title', 'Collection title should be edited');
|
2023-05-17 17:59:03 +03:00
|
|
|
assert.equal(editedCollection?.description, 'Edited description', 'Collection description should be edited');
|
2023-06-01 08:37:51 +03:00
|
|
|
assert.equal(editedCollection?.feature_image, '/assets/images/edited.jpg', 'Collection feature_image should be edited');
|
|
|
|
assert.equal(editedCollection?.type, 'manual', 'Collection type should not be edited');
|
2023-05-17 17:59:03 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Resolves to null when editing unexistend collection', async function () {
|
|
|
|
const editedCollection = await collectionsService.edit({
|
|
|
|
id: '12345'
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(editedCollection, null, 'Collection should be null');
|
|
|
|
});
|
2023-05-25 18:34:59 +03:00
|
|
|
|
|
|
|
it('Adds a Post to a Collection', async function () {
|
2023-06-01 07:16:39 +03:00
|
|
|
const collection = await collectionsService.createCollection({
|
2023-05-25 18:34:59 +03:00
|
|
|
title: 'testing collections',
|
|
|
|
description: 'testing collections description',
|
2023-06-01 07:16:39 +03:00
|
|
|
type: 'manual'
|
2023-05-25 18:34:59 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
const editedCollection = await collectionsService.edit({
|
|
|
|
id: collection.id,
|
|
|
|
posts: [{
|
|
|
|
id: posts[0].id
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(editedCollection?.posts.length, 1, 'Collection should have one post');
|
2023-06-01 08:37:51 +03:00
|
|
|
assert.equal(editedCollection?.posts[0].id, posts[0].id, 'Collection should have the correct post');
|
|
|
|
assert.equal(editedCollection?.posts[0].sort_order, 0, 'Collection should have the correct post sort order');
|
2023-05-25 18:34:59 +03:00
|
|
|
});
|
2023-06-01 10:57:47 +03:00
|
|
|
|
|
|
|
it('Removes a Post from a Collection', async function () {
|
|
|
|
const collection = await collectionsService.createCollection({
|
|
|
|
title: 'testing collections',
|
|
|
|
description: 'testing collections description',
|
|
|
|
type: 'manual'
|
|
|
|
});
|
|
|
|
|
|
|
|
let editedCollection = await collectionsService.edit({
|
|
|
|
id: collection.id,
|
|
|
|
posts: [{
|
|
|
|
id: posts[0].id
|
|
|
|
}, {
|
|
|
|
id: posts[1].id
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(editedCollection?.posts.length, 2, 'Collection should have two posts');
|
|
|
|
|
|
|
|
editedCollection = await collectionsService.removePostFromCollection(collection.id, posts[0].id);
|
|
|
|
|
|
|
|
assert.equal(editedCollection?.posts.length, 1, 'Collection should have one posts');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Returns null when removing post from non existing collection', async function () {
|
|
|
|
const collection = await collectionsService.removePostFromCollection('i-do-not-exist', posts[0].id);
|
|
|
|
|
|
|
|
assert.equal(collection, null, 'Collection should be null');
|
|
|
|
});
|
2023-05-17 17:59:03 +03:00
|
|
|
});
|
2023-05-16 07:02:58 +03:00
|
|
|
});
|