2023-10-04 13:07:26 +03:00
|
|
|
import assert from 'assert/strict';
|
|
|
|
import {Recommendation} from '../src';
|
|
|
|
|
|
|
|
describe('Recommendation', function () {
|
|
|
|
describe('validate', function () {
|
|
|
|
it('Throws for an empty title', function () {
|
|
|
|
assert.throws(() => {
|
|
|
|
Recommendation.validate({
|
|
|
|
title: '',
|
2023-10-09 17:19:44 +03:00
|
|
|
description: null,
|
2023-10-04 13:07:26 +03:00
|
|
|
excerpt: null,
|
|
|
|
featuredImage: null,
|
|
|
|
favicon: null,
|
|
|
|
url: 'https://example.com',
|
|
|
|
oneClickSubscribe: false
|
|
|
|
});
|
|
|
|
}, {
|
|
|
|
name: 'ValidationError',
|
|
|
|
message: 'Title must not be empty'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Throws for a long title', function () {
|
|
|
|
assert.throws(() => {
|
|
|
|
Recommendation.validate({
|
|
|
|
title: 'a'.repeat(2001),
|
2023-10-09 17:19:44 +03:00
|
|
|
description: null,
|
2023-10-04 13:07:26 +03:00
|
|
|
excerpt: null,
|
|
|
|
featuredImage: null,
|
|
|
|
favicon: null,
|
|
|
|
url: 'https://example.com',
|
|
|
|
oneClickSubscribe: false
|
|
|
|
});
|
|
|
|
}, {
|
|
|
|
name: 'ValidationError',
|
|
|
|
message: 'Title must be less than 2000 characters'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-10-09 17:19:44 +03:00
|
|
|
it('Throws for a long description', function () {
|
2023-10-04 13:07:26 +03:00
|
|
|
assert.throws(() => {
|
|
|
|
Recommendation.validate({
|
|
|
|
title: 'Test',
|
2023-10-09 17:19:44 +03:00
|
|
|
description: 'a'.repeat(2001),
|
2023-10-04 13:07:26 +03:00
|
|
|
excerpt: null,
|
|
|
|
featuredImage: null,
|
|
|
|
favicon: null,
|
|
|
|
url: 'https://example.com',
|
|
|
|
oneClickSubscribe: false
|
|
|
|
});
|
|
|
|
}, {
|
|
|
|
name: 'ValidationError',
|
2023-10-09 17:19:44 +03:00
|
|
|
message: 'Description must be less than 2000 characters'
|
2023-10-04 13:07:26 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Throws for a long excerpt', function () {
|
|
|
|
assert.throws(() => {
|
|
|
|
Recommendation.validate({
|
|
|
|
title: 'Test',
|
2023-10-09 17:19:44 +03:00
|
|
|
description: null,
|
2023-10-04 13:07:26 +03:00
|
|
|
excerpt: 'a'.repeat(2001),
|
|
|
|
featuredImage: null,
|
|
|
|
favicon: null,
|
|
|
|
url: 'https://example.com',
|
|
|
|
oneClickSubscribe: false
|
|
|
|
});
|
|
|
|
}, {
|
|
|
|
name: 'ValidationError',
|
|
|
|
message: 'Excerpt must be less than 2000 characters'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('clean', function () {
|
|
|
|
it('sets createdAt ms to 0', function () {
|
|
|
|
const recommendation = Recommendation.create({
|
|
|
|
title: 'Test',
|
2023-10-09 17:19:44 +03:00
|
|
|
description: null,
|
2023-10-04 13:07:26 +03:00
|
|
|
excerpt: null,
|
|
|
|
featuredImage: null,
|
|
|
|
favicon: null,
|
|
|
|
url: 'https://example.com',
|
|
|
|
oneClickSubscribe: false,
|
|
|
|
createdAt: new Date('2021-01-01T00:00:05Z')
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(recommendation.createdAt.getMilliseconds(), 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets updatedAt ms to 0', function () {
|
|
|
|
const recommendation = Recommendation.create({
|
|
|
|
title: 'Test',
|
2023-10-09 17:19:44 +03:00
|
|
|
description: null,
|
2023-10-04 13:07:26 +03:00
|
|
|
excerpt: null,
|
|
|
|
featuredImage: null,
|
|
|
|
favicon: null,
|
|
|
|
url: 'https://example.com',
|
|
|
|
oneClickSubscribe: false,
|
|
|
|
updatedAt: new Date('2021-01-01T00:00:05Z')
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(recommendation.updatedAt!.getMilliseconds(), 0);
|
|
|
|
});
|
|
|
|
|
2023-10-09 17:19:44 +03:00
|
|
|
it('sets empty description to null', function () {
|
2023-10-04 13:07:26 +03:00
|
|
|
const recommendation = Recommendation.create({
|
|
|
|
title: 'Test',
|
2023-10-09 17:19:44 +03:00
|
|
|
description: '',
|
2023-10-04 13:07:26 +03:00
|
|
|
excerpt: null,
|
|
|
|
featuredImage: null,
|
|
|
|
favicon: null,
|
|
|
|
url: 'https://example.com',
|
|
|
|
oneClickSubscribe: false,
|
|
|
|
updatedAt: new Date('2021-01-01T00:00:05Z')
|
|
|
|
});
|
|
|
|
|
2023-10-09 17:19:44 +03:00
|
|
|
assert.equal(recommendation.description, null);
|
2023-10-04 13:07:26 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('removes search and hash params', function () {
|
|
|
|
const recommendation = Recommendation.create({
|
|
|
|
title: 'Test',
|
2023-10-09 17:19:44 +03:00
|
|
|
description: '',
|
2023-10-04 13:07:26 +03:00
|
|
|
excerpt: null,
|
|
|
|
featuredImage: null,
|
|
|
|
favicon: null,
|
|
|
|
url: 'https://example.com/?query=1#hash',
|
|
|
|
oneClickSubscribe: false,
|
|
|
|
updatedAt: new Date('2021-01-01T00:00:05Z')
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(recommendation.url.toString(), 'https://example.com/');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('plain', function () {
|
|
|
|
it('does not return instance of self', function () {
|
|
|
|
const recommendation = Recommendation.create({
|
|
|
|
title: 'Test',
|
2023-10-09 17:19:44 +03:00
|
|
|
description: null,
|
2023-10-04 13:07:26 +03:00
|
|
|
excerpt: null,
|
|
|
|
featuredImage: null,
|
|
|
|
favicon: null,
|
|
|
|
url: 'https://example.com',
|
|
|
|
oneClickSubscribe: false,
|
|
|
|
createdAt: new Date('2021-01-01T00:00:05Z')
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(recommendation.plain instanceof Recommendation, false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('edit', function () {
|
|
|
|
it('can edit known properties', function () {
|
|
|
|
const recommendation = Recommendation.create({
|
|
|
|
title: 'Test',
|
2023-10-09 17:19:44 +03:00
|
|
|
description: null,
|
2023-10-04 13:07:26 +03:00
|
|
|
excerpt: null,
|
|
|
|
featuredImage: null,
|
|
|
|
favicon: null,
|
|
|
|
url: 'https://example.com',
|
|
|
|
oneClickSubscribe: false,
|
|
|
|
createdAt: new Date('2021-01-01T00:00:05Z'),
|
|
|
|
updatedAt: null
|
|
|
|
});
|
|
|
|
|
|
|
|
recommendation.edit({
|
|
|
|
title: 'Updated'
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(recommendation.title, 'Updated');
|
|
|
|
assert.notEqual(recommendation.updatedAt, null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can not edit unknown properties', function () {
|
|
|
|
const recommendation = Recommendation.create({
|
|
|
|
title: 'Test',
|
2023-10-09 17:19:44 +03:00
|
|
|
description: null,
|
2023-10-04 13:07:26 +03:00
|
|
|
excerpt: null,
|
|
|
|
featuredImage: null,
|
|
|
|
favicon: null,
|
|
|
|
url: 'https://example.com',
|
|
|
|
oneClickSubscribe: false,
|
|
|
|
createdAt: new Date('2021-01-01T00:00:05Z'),
|
|
|
|
updatedAt: null
|
|
|
|
});
|
|
|
|
|
|
|
|
recommendation.edit({
|
|
|
|
bla: true
|
|
|
|
} as any);
|
|
|
|
|
|
|
|
assert.notEqual(recommendation.updatedAt, null);
|
|
|
|
assert.equal((recommendation as any).bla, undefined);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('delete', function () {
|
|
|
|
it('can delete', function () {
|
|
|
|
const recommendation = Recommendation.create({
|
|
|
|
title: 'Test',
|
2023-10-09 17:19:44 +03:00
|
|
|
description: null,
|
2023-10-04 13:07:26 +03:00
|
|
|
excerpt: null,
|
|
|
|
featuredImage: null,
|
|
|
|
favicon: null,
|
|
|
|
url: 'https://example.com',
|
|
|
|
oneClickSubscribe: false,
|
|
|
|
createdAt: new Date('2021-01-01T00:00:05Z'),
|
|
|
|
updatedAt: null
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(recommendation.deleted, false);
|
|
|
|
recommendation.delete();
|
|
|
|
assert.equal(recommendation.deleted, true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|