-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathusersFind.queryHandler.spec.ts
168 lines (162 loc) · 5.04 KB
/
usersFind.queryHandler.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
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
import { Test } from '@nestjs/testing';
import { PaginationResponseDTO, PrismaService, UsersFindQuery } from '@seed/back/api/shared';
import { UsersFindQueryHandler } from './usersFind.queryHandler';
import { mockUsers } from '@seed/shared/mock-data';
import { ONE, PAGINATION_DEFAULTS } from '@seed/shared/constants';
import { User, UserRole } from '@prisma/client';
describe(UsersFindQueryHandler.name, () => {
//region VARIABLES
let handler: UsersFindQueryHandler;
const page = 3;
const limit = 50;
const role = UserRole.ADMIN;
const findManyMockResult = mockUsers;
const countMockResult = mockUsers.length;
const findManyMock = jest.fn().mockReturnValue(findManyMockResult);
const countMock = jest.fn().mockReturnValue(countMockResult);
const transactionMock = jest.fn().mockReturnValue([findManyMockResult, countMockResult]);
const prismaServiceMock = jest.fn().mockImplementation(() => ({
user: {
findMany: findManyMock,
count: countMock,
},
$transaction: transactionMock,
}));
//endregion
//region SETUP
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
providers: [
UsersFindQueryHandler,
{
provide: PrismaService,
useClass: prismaServiceMock,
},
],
}).compile();
handler = moduleRef.get(UsersFindQueryHandler);
});
beforeEach(() => {
findManyMock.mockClear();
countMock.mockClear();
transactionMock.mockClear();
});
function getQuery(
pageArg = PAGINATION_DEFAULTS.page,
limitArg = PAGINATION_DEFAULTS.limit,
search?: string,
roleFilter?: UserRole,
): UsersFindQuery {
return new UsersFindQuery(pageArg, limitArg, search, roleFilter);
}
//endregion
it('should call prisma.user.findMany(), prisma.user.count(), prisma.$transaction() with basic params when no search query is provided', async () => {
const result = await handler.execute(getQuery(page, limit));
expect(findManyMock).toBeCalledWith({
skip: (page - ONE) * limit,
take: limit,
});
expect(countMock).toHaveBeenCalledWith(undefined);
expect(transactionMock).toHaveBeenCalledWith([findManyMockResult, countMockResult]);
expect(result).toEqual(new PaginationResponseDTO<User>(findManyMockResult, page, limit, countMockResult));
});
it('should call prisma.user.findMany(), prisma.user.count() with basic params + search query condition for single word + role', async () => {
const query = getQuery(page, limit, 'John', role);
const result = await handler.execute(query);
const where = {
AND: [
{
OR: [
{
userName: {
contains: 'John',
mode: 'insensitive',
},
},
{
firstName: {
contains: 'John',
mode: 'insensitive',
},
},
{
lastName: {
contains: 'John',
mode: 'insensitive',
},
},
],
},
{
role,
},
],
};
expect(findManyMock).toBeCalledWith({
skip: (page - ONE) * limit,
take: limit,
where,
});
expect(countMock).toHaveBeenCalledWith({ where });
expect(transactionMock).toHaveBeenCalledWith([findManyMockResult, countMockResult]);
expect(result).toEqual(new PaginationResponseDTO<User>(findManyMockResult, page, limit, countMockResult));
});
it('should call prisma.user.findMany(), prisma.user.count() with basic params + search query condition for multiple words', async () => {
const query = getQuery(page, limit);
query.search = 'John Wick';
const result = await handler.execute(query);
const where = {
AND: [
{
OR: [
{
userName: {
contains: 'John',
mode: 'insensitive',
},
},
{
firstName: {
contains: 'John',
mode: 'insensitive',
},
},
{
lastName: {
contains: 'John',
mode: 'insensitive',
},
},
{
userName: {
contains: 'Wick',
mode: 'insensitive',
},
},
{
firstName: {
contains: 'Wick',
mode: 'insensitive',
},
},
{
lastName: {
contains: 'Wick',
mode: 'insensitive',
},
},
],
},
{ role: undefined },
],
};
expect(findManyMock).toBeCalledWith({
skip: (page - ONE) * limit,
take: limit,
where,
});
expect(countMock).toHaveBeenCalledWith({ where });
expect(transactionMock).toHaveBeenCalledWith([findManyMockResult, countMockResult]);
expect(result).toEqual(new PaginationResponseDTO<User>(findManyMockResult, page, limit, countMockResult));
});
});