2023-05-16 07:02:58 +03:00
|
|
|
import assert from 'assert';
|
2023-05-17 09:42:35 +03:00
|
|
|
import ObjectID from 'bson-objectid';
|
2023-05-16 07:02:58 +03:00
|
|
|
import {CollectionsService} from '../src/index';
|
|
|
|
import {CollectionsRepositoryInMemory} from '../src/CollectionsRepositoryInMemory';
|
|
|
|
|
|
|
|
describe('collections', function () {
|
|
|
|
it('Instantiates a CollectionsService', function () {
|
|
|
|
const repository = new CollectionsRepositoryInMemory();
|
|
|
|
const collectionsService = new CollectionsService({repository});
|
|
|
|
assert.ok(collectionsService, 'CollectionsService should initialize');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can do CRUD operations on a collection', async function () {
|
|
|
|
const repository = new CollectionsRepositoryInMemory();
|
|
|
|
const collectionsService = new CollectionsService({repository});
|
|
|
|
|
2023-05-17 09:42:35 +03:00
|
|
|
const savedCollection = await collectionsService.save({
|
2023-05-16 07:02:58 +03:00
|
|
|
title: 'testing collections',
|
|
|
|
description: 'testing collections description',
|
|
|
|
type: 'manual',
|
|
|
|
filter: null,
|
|
|
|
feature_image: null,
|
|
|
|
deleted: false
|
|
|
|
});
|
|
|
|
|
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
|
|
|
|
|
|
|
it('Can create a collection with predefined ID', async function () {
|
|
|
|
const repository = new CollectionsRepositoryInMemory();
|
|
|
|
const collectionsService = new CollectionsService({repository});
|
|
|
|
|
|
|
|
const id = new ObjectID();
|
|
|
|
const savedCollection = await collectionsService.save({
|
|
|
|
id: id.toHexString()
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(savedCollection.id, id.toHexString(), 'Collection should have same id');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can create a collection with predefined ObjectID instance', async function () {
|
|
|
|
const repository = new CollectionsRepositoryInMemory();
|
|
|
|
const collectionsService = new CollectionsService({repository});
|
|
|
|
|
|
|
|
const id = new ObjectID();
|
|
|
|
const savedCollection = await collectionsService.save({
|
|
|
|
id: id
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(savedCollection.id, id.toHexString(), 'Collection should have same id');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Throws an error when trying to save a collection with an invalid ID', async function () {
|
|
|
|
const repository = new CollectionsRepositoryInMemory();
|
|
|
|
const collectionsService = new CollectionsService({repository});
|
|
|
|
|
|
|
|
try {
|
|
|
|
await collectionsService.save({
|
|
|
|
id: 12345
|
|
|
|
});
|
|
|
|
} catch (error: any) {
|
|
|
|
assert.equal(error.message, 'Invalid ID provided for Collection', 'Error message should match');
|
|
|
|
}
|
|
|
|
});
|
2023-05-16 07:02:58 +03:00
|
|
|
});
|