-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathbooks.tests.ts
176 lines (142 loc) · 4.39 KB
/
books.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { describe, it, expect } from '@jest/globals';
import { BooksPropertiesI } from '../src/entities/Books/BooksEntity';
import * as Books from '../src/entities/Books/BooksModels';
export const TestBooks = () =>
describe("Step 3 - Authors' CRUD", () => {
let book: BooksPropertiesI;
const bookProps: Omit<BooksPropertiesI, 'id'> = {
title: "Harry Potter and the Philosopher's Stone",
summary: 'A real famous book',
rate: 8,
price: 25,
pages: 233,
};
describe('0 - Create Book', () => {
it('Create Book', async () => {
book = await Books.createBook(bookProps);
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('1 - Get Books', () => {
it('Get Books', async () => {
const books = await Books.getBooks();
expect(books).toHaveLength(1);
const test = () => {
expect(books[0]).toHaveProperty('id');
// Check title
expect(books[0]).toHaveProperty('title');
expect(bookProps.title).toBe(books[0].title);
// Check summary
expect(books[0]).toHaveProperty('summary');
expect(bookProps.summary).toBe(books[0].summary);
// Check price
expect(books[0]).toHaveProperty('price');
expect(bookProps.price).toBe(books[0].price);
// Check rate
expect(books[0]).toHaveProperty('rate');
expect(bookProps.rate).toBe(books[0].rate);
// Check pages
expect(books[0]).toHaveProperty('pages');
expect(bookProps.pages).toBe(books[0].pages);
};
test();
});
});
describe('2 - Get Book', () => {
it('Get invalid book', async () => {
const notFound = await Books.getBook('not found');
if (notFound != null) {
fail();
}
});
it('Get Book', async () => {
const foundBook = await Books.getBook(book.id);
if (!foundBook) {
fail();
}
const test = () => {
expect(foundBook).toHaveProperty('id');
// Check title
expect(foundBook).toHaveProperty('title');
expect(bookProps.title).toBe(foundBook.title);
// Check summary
expect(foundBook).toHaveProperty('summary');
expect(bookProps.summary).toBe(foundBook.summary);
// Check price
expect(foundBook).toHaveProperty('price');
expect(bookProps.price).toBe(foundBook.price);
// Check rate
expect(foundBook).toHaveProperty('rate');
expect(bookProps.rate).toBe(foundBook.rate);
// Check pages
expect(foundBook).toHaveProperty('pages');
expect(bookProps.pages).toBe(foundBook.pages);
};
test();
});
});
describe('3 - Update Book', () => {
it('Update invalid book', async () => {
const notFound = await Books.updateBook('not found', {
rate: 10,
});
if (notFound != null) {
fail();
}
});
it('Update Book', async () => {
const updatedBook = await Books.updateBook(book.id, {
rate: 10,
});
if (!updatedBook) {
fail();
}
bookProps.rate = 10;
const test = () => {
expect(updatedBook).toHaveProperty('id');
// Check title
expect(updatedBook).toHaveProperty('title');
expect(bookProps.title).toBe(updatedBook.title);
// Check summary
expect(updatedBook).toHaveProperty('summary');
expect(bookProps.summary).toBe(updatedBook.summary);
// Check price
expect(updatedBook).toHaveProperty('price');
expect(bookProps.price).toBe(updatedBook.price);
// Check rate
expect(updatedBook).toHaveProperty('rate');
expect(bookProps.rate).toBe(updatedBook.rate);
// Check pages
expect(updatedBook).toHaveProperty('pages');
expect(bookProps.pages).toBe(updatedBook.pages);
};
test();
book = updatedBook;
});
});
describe('4 - Delete Book', () => {
it('Delete Book', async () => {
await Books.deleteBook(book.id);
const books = await Books.getBooks();
expect(books).toHaveLength(0);
});
});
});