-
Notifications
You must be signed in to change notification settings - Fork 68
/
books-authors.tests.ts
141 lines (113 loc) · 3.86 KB
/
books-authors.tests.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { describe, expect, it } from '@jest/globals';
import * as Authors from '../src/entities/Authors/AuthorsModels';
import * as Books from '../src/entities/Books/BooksModels';
import { BooksPropertiesI } from '../src/entities/Books/BooksEntity';
import { AuthorsPropertiesI, Country } from '../src/entities/Authors/AuthorsEntity';
import { detachBookFromAuthor, getAuthorBooks, linkBookToAuthor } from '../src/entities/Books/BooksRelation';
export const TestBooksAuthorsRelation = () => {
describe('Step 4 - Books / Authors relation', () => {
let books: BooksPropertiesI[] = [];
let author: AuthorsPropertiesI;
const authorProps: Omit<AuthorsPropertiesI, 'id'> = {
firstname: 'John',
lastname: 'Doe',
pseudonym: 'John Doe',
age: 30,
country: Country.FRA,
};
const bookProps: Omit<BooksPropertiesI, 'id'> = {
title: "Books' title",
summary: "Books' description",
rate: 10,
price: 20,
pages: 200,
};
describe('Initialize database', () => {
it('Create author', async () => {
author = await Authors.createAuthor(authorProps);
for (let i = 0; i !== 3; i += 1) {
// eslint-disable-next-line no-await-in-loop
books.push(await Books.createBook(bookProps));
}
});
});
describe('0 - Create Relation', () => {
it('Create Relation', async () => {
const linkedAuthors = await linkBookToAuthor(books[0].id, author.id);
if (!linkedAuthors) {
fail();
}
const test = () => {
expect(author).toHaveProperty('id');
// Check firstname
expect(linkedAuthors).toHaveProperty('firstname');
expect(authorProps.firstname).toBe(linkedAuthors.firstname);
// Check lastname
expect(linkedAuthors).toHaveProperty('lastname');
expect(authorProps.lastname).toBe(linkedAuthors.lastname);
// Check age
expect(linkedAuthors).toHaveProperty('age');
expect(authorProps.age).toBe(linkedAuthors.age);
// Check pseudonym
expect(linkedAuthors).toHaveProperty('pseudonym');
expect(authorProps.pseudonym).toBe(linkedAuthors.pseudonym);
// Check country
expect(linkedAuthors).toHaveProperty('country');
expect(authorProps.country).toBe(linkedAuthors.country);
// Check books
expect(linkedAuthors).toHaveProperty('books');
expect(linkedAuthors.books).toHaveLength(1);
};
test();
});
});
describe('1 - Get Authors relations', () => {
it('Get authors', async () => {
await linkBookToAuthor(books[1].id, author.id);
await linkBookToAuthor(books[2].id, author.id);
const authorBooks = await getAuthorBooks(author.id);
if (!authorBooks) {
fail();
}
expect(authorBooks.books).toHaveLength(3);
authorBooks.books.forEach((book) => {
const test = () => {
expect(book).toHaveProperty('id');
// Check title
expect(book).toHaveProperty('title');
expect(bookProps.title).toBe(book.title);
// Check summary
expect(book).toHaveProperty('summary');
expect(bookProps.summary).toBe(book.summary);
// Check price
expect(book).toHaveProperty('price');
expect(bookProps.price).toBe(book.price);
// Check rate
expect(book).toHaveProperty('rate');
expect(bookProps.rate).toBe(book.rate);
// Check pages
expect(book).toHaveProperty('pages');
expect(bookProps.pages).toBe(book.pages);
};
test();
});
});
});
describe('2 - Detach relation', () => {
it('Detach relation', async () => {
await detachBookFromAuthor(books[0].id, author.id);
const authorBooks = await getAuthorBooks(author.id);
if (!authorBooks) {
fail();
}
expect(authorBooks.books).toHaveLength(2);
});
});
describe('Clean database', () => {
it('Clean database', async () => {
await Authors.deleteAuthor(author.id);
await Promise.all(books.map(async (book) => Books.deleteBook(book.id)));
});
});
});
};