-
-
Notifications
You must be signed in to change notification settings - Fork 377
/
cat.service.spec.ts
109 lines (104 loc) · 3.29 KB
/
cat.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { Test, TestingModule } from '@nestjs/testing';
import { CatService } from './cat.service';
import { CatDTO } from './dto/cats.dto';
const testCat1 = 'Test Cat 1';
const testBreed1 = 'Test Breed 1';
describe('CatService', () => {
let service: CatService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [CatService],
}).compile();
service = module.get<CatService>(CatService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
describe('getAll', () => {
it('should return the array of cats', () => {
const cats = service.getAll();
expect(cats.length).toBe(3);
expect(cats[0]).toEqual({
id: 1,
name: 'Ventus',
breed: 'Russian Blue',
age: 3,
});
expect(cats[1].name).toBe('Terra');
expect(cats[2].age).toBe(5);
});
});
describe('getById', () => {
it('should return a found cat', () => {
const cat = service.getById(2);
expect(cat).toBeTruthy();
expect(cat.name).toBe('Terra');
expect(cat.id).toBe(2);
});
it('should throw and error for a bad id', () => {
try {
service.getById(15874);
} catch (err) {
expect(err.message.message).toBe('No cat found with id 15874.');
expect(err.message.error).toBe('Bad Request');
}
});
});
describe('addCat', () => {
it('should add the cat', () => {
const catDTO: CatDTO = {
name: testCat1,
breed: testBreed1,
age: 8,
};
const newCat = service.addCat(catDTO);
expect(newCat).toEqual({ id: 4, ...catDTO });
});
});
describe('deleteCat', () => {
it('should return true if the id is valid', () => {
expect(service.deleteCat(2));
});
it('should return false if the id is invalid', () => {
expect(!service.deleteCat(5874533));
});
});
// a bit of an in depth test to make sure the cats are staying in memory
describe('getAll, addCat, getAll, deleteCat, getAll, findCat', () => {
/**
* Due to how javascript assign objects values by reference and not by value
* it is imperative to run each test right after the function runs.
* Also a good idea to get the length immediately as that will not change but the array will
*/
it('should get all cats, create new cat, get all cats again, delete a cat, get all cats and find a specified cat', () => {
const firstCatSet = service.getAll();
const firstCatSetLength = firstCatSet.length;
expect(firstCatSet.length).toBe(3);
const newCat = service.addCat({
name: testCat1,
breed: testBreed1,
age: 4,
});
expect(newCat).toEqual({
id: 4,
name: testCat1,
breed: testBreed1,
age: 4,
});
const secondCatSet = service.getAll();
expect(secondCatSet.length).toBe(firstCatSetLength + 1);
expect(secondCatSet).toContainEqual(newCat);
const deleted = service.deleteCat(2);
expect(deleted);
const thirdCatSet = service.getAll();
expect(thirdCatSet.length).toBe(firstCatSetLength);
const foundCat = service.getById(3);
expect(foundCat).toEqual({
id: 3,
name: 'Aqua',
breed: 'Maine Coon',
age: 5,
});
});
});
});