-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdocument.service.spec.ts
95 lines (81 loc) · 3.47 KB
/
document.service.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { Test, TestingModule } from '@nestjs/testing';
import { OsoInstance } from '../oso/oso-instance';
import { ProjectModule } from '../project/project.module';
import { UsersModule } from '../users/users.module';
import { DocumentModule } from './document.module';
import { DocumentService } from './document.service';
import { CreateDocumentDto } from './dto/document.dto';
import { Document } from './entity/document';
import { Comment } from './entity/comment';
describe(DocumentService.name, () => {
let service: DocumentService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [DocumentModule, UsersModule, ProjectModule],
providers: [DocumentService, OsoInstance],
}).compile();
service = module.get<DocumentService>(DocumentService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
it('should be able to find a unique document by id', async () => {
const id = 1;
const document: Document = await service.findOne(id);
expect(document).toBeDefined();
expect(document.id).toBeDefined();
expect(document.id).toEqual(id);
// make sure it doesn't always return the same doc
const id2 = 2;
const document2: Document = await service.findOne(id2);
expect(document2).toBeDefined();
expect(document2.id).toEqual(id2);
expect(document2.id).not.toEqual(document.id);
});
it('should be able to get all documents', async () => {
const documents: Document[] = await service.findAll();
expect(documents).toBeDefined();
expect(documents.length).toEqual(3);
});
it('should be able to create a new document', async () => {
const ownerId = 1;
const projectId = 1;
const data = 'a nice new document';
const createDocument = new CreateDocumentDto();
createDocument.ownerId = ownerId;
createDocument.projectId = projectId;
createDocument.document = data;
const authorize = jest.fn();
const id: number = await service.create(createDocument, authorize);
expect(id).toBeDefined();
const document: Document = await service.findOne(id);
expect(document).toBeDefined();
expect(document.id).toEqual(id);
expect(document.project).toBeDefined();
expect(document.project.id).toEqual(projectId);
expect(document.document).toEqual(data);
});
it('should be able to create and retrieve comments for a specific document', async () => {
const document: Document = await service.findOne(1);
expect(document).toBeDefined();
const commentData = 'A nice new comment';
const commentId = await service.createComment(document.id, commentData);
expect(commentId).toBeDefined();
// ensure comment retrieved is sane
let comments: Comment[] = await service.findCommentsByDocument(document.id);
expect(comments).toBeDefined();
expect(comments.length).toEqual(1);
expect(comments[0].id).toEqual(commentId);
expect(comments[0].documentId).toEqual(document.id);
expect(comments[0].data).toEqual(commentData);
// ensure multiple comments retrieved are sane
const comment2Data = 'Comment 2';
const comment2Id = await service.createComment(document.id, comment2Data);
comments = await service.findCommentsByDocument(document.id);
expect(comments).toBeDefined();
expect(comments.length).toEqual(2);
expect(comments[1].id).toEqual(comment2Id);
expect(comments[1].documentId).toEqual(document.id);
expect(comments[1].data).toEqual(comment2Data);
});
});