0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00

Added 100% test coverage to controller and dto

refs https://github.com/TryGhost/Arch/issues/99

- We aim to keep test coverage close to 100% on all new code. This change adds full coverage to controller layer code
This commit is contained in:
Naz 2023-11-28 14:32:11 +08:00 committed by Fabien "egg" O'Carroll
parent e6865ee6f0
commit 12ad019447
4 changed files with 72 additions and 10 deletions

View file

@ -2,8 +2,8 @@ import {Entity} from '../../common/entity';
type SnippetData = {
readonly name: string;
lexical: string;
mobiledoc: string;
lexical?: string;
mobiledoc?: string;
};
export class Snippet extends Entity<SnippetData> implements SnippetData {

View file

@ -1,8 +1,7 @@
import ObjectID from 'bson-objectid';
import {Snippet} from '../../core/snippets/snippet.entity';
export class BrowseSnippetDTO {
id: ObjectID;
id: string;
name: string;
lexical?: string|null;
mobiledoc?: string|null;
@ -10,7 +9,7 @@ export class BrowseSnippetDTO {
updated_at: Date|null;
constructor(data: Snippet, options: {formats?: 'mobiledoc'|'lexical'}) {
this.id = data.id;
this.id = data.id.toString();
this.name = data.name;
if (options.formats === 'lexical') {

View file

@ -0,0 +1,48 @@
import {Snippet} from '../../core/snippets/snippet.entity';
import ObjectId from 'bson-objectid';
import assert from 'assert/strict';
import {BrowseSnippetDTO} from './browse-snippet.dto';
describe('BrowseSnippetDTO', () => {
it('constructs a BrowseSnippetDTO object from a Snippet object with mobiledoc field', async () => {
const snippet = new Snippet({
id: ObjectId(),
name: 'Test',
mobiledoc: '{"version":"0.3.1","atoms":[],"cards":[],"markups":[],"sections":[[1,"p",[[0,[],0,"Test"]]]]}',
lexical: undefined,
createdAt: new Date(),
updatedAt: null,
createdBy: ObjectId(),
updatedBy: null
});
const browseSnippetDTO = new BrowseSnippetDTO(snippet, {formats: 'mobiledoc'});
assert(browseSnippetDTO, 'BrowseSnippetDTO object is not null');
assert.equal(browseSnippetDTO.id, snippet.id.toString());
assert.equal(browseSnippetDTO.name, snippet.name);
assert.equal(browseSnippetDTO.mobiledoc, snippet.mobiledoc);
assert.equal(browseSnippetDTO.lexical, undefined);
});
it('constructs a BrowseSnippetDTO object from a Snippet object with lexical field', async () => {
const snippet = new Snippet({
id: ObjectId(),
name: 'Test',
mobiledoc: undefined,
lexical: `{"root":{"children":[{"type":"html","version":1,"html":"<p>hey!</p>"}],"direction":null,"format":"","indent":0,"type":"root","version":1}}`,
createdAt: new Date(),
updatedAt: null,
createdBy: ObjectId(),
updatedBy: null
});
const browseSnippetDTO = new BrowseSnippetDTO(snippet, {formats: 'lexical'});
assert(browseSnippetDTO, 'BrowseSnippetDTO object is not null');
assert.equal(browseSnippetDTO.id, snippet.id.toString());
assert.equal(browseSnippetDTO.name, snippet.name);
assert.equal(browseSnippetDTO.mobiledoc, undefined);
assert.equal(browseSnippetDTO.lexical, snippet.lexical);
});
});

View file

@ -1,4 +1,6 @@
import ObjectId from 'bson-objectid';
import {Test} from '@nestjs/testing';
import {Snippet} from '../../core/snippets/snippet.entity';
import {SnippetsController} from './snippets.controller';
import {SnippetsService} from '../../core/snippets/snippets.service';
import {SnippetsRepositoryInMemory} from '../../core/snippets/snippets.repository.inmemory';
@ -27,15 +29,28 @@ describe('SnippetsController', () => {
describe('browse', () => {
it('should return an array of snippets', async () => {
const result = [{
id: '1'
}];
const serviceSnippetResult = [new Snippet({
id: ObjectId(),
name: 'Test',
mobiledoc: '{"version":"0.3.1","atoms":[],"cards":[],"markups":[],"sections":[[1,"p",[[0,[],0,"Test"]]]]}',
lexical: undefined,
createdAt: new Date(),
updatedAt: null,
createdBy: ObjectId(),
updatedBy: null
})];
snippetsService.browse = sinon.stub().returns(result);
snippetsService.browse = sinon.stub().returns(serviceSnippetResult);
const response = await snippetsController.browse();
assert.equal(response.snippets.length, 1);
assert.equal(response.snippets[0], result[0]);
assert.equal(Object.keys(response.snippets[0]).length, 6);
assert.equal(response.snippets[0].id, serviceSnippetResult[0].id.toString());
assert.equal(response.snippets[0].name, serviceSnippetResult[0].name);
assert.equal(response.snippets[0].mobiledoc, serviceSnippetResult[0].mobiledoc);
assert.equal(response.snippets[0].lexical, undefined);
assert.equal(response.snippets[0].created_at, serviceSnippetResult[0].createdAt);
assert.equal(response.snippets[0].updated_at, serviceSnippetResult[0].updatedAt);
});
});
});