@@ -16,3 +16,80 @@ describe('GroupService', () => {
1616 expect ( service ) . toBeDefined ( ) ;
1717 } ) ;
1818} ) ;
19+
20+ describe ( 'GroupsService' , ( ) => {
21+ let service : GroupsService ;
22+ let prismaMock : any ;
23+
24+ beforeEach ( ( ) => {
25+ prismaMock = {
26+ userGroup : {
27+ findMany : jest . fn ( ) ,
28+ create : jest . fn ( ) ,
29+ findUnique : jest . fn ( ) ,
30+ update : jest . fn ( ) ,
31+ delete : jest . fn ( ) ,
32+ } ,
33+ } ;
34+ service = new GroupsService ( ) ;
35+ // @ts -ignore
36+ service [ 'prisma' ] = prismaMock ;
37+ } ) ;
38+
39+ it ( 'should find all groups' , async ( ) => {
40+ const mockGroups = [ { id : '1' , name : 'group1' } ] ;
41+ prismaMock . userGroup . findMany . mockResolvedValueOnce ( mockGroups ) ;
42+ const result = await service . findAll ( ) ;
43+ expect ( result ) . toBe ( mockGroups ) ;
44+ expect ( prismaMock . userGroup . findMany ) . toHaveBeenCalledWith ( {
45+ select : {
46+ id : true ,
47+ name : true ,
48+ description : true ,
49+ createdAt : true ,
50+ updatedAt : true ,
51+ } ,
52+ } ) ;
53+ } ) ;
54+
55+ it ( 'should create a group' , async ( ) => {
56+ const mockGroup = { id : '2' , name : 'group2' , description : 'desc' } ;
57+ prismaMock . userGroup . create . mockResolvedValueOnce ( mockGroup ) ;
58+ const result = await service . create ( 'group2' , 'desc' ) ;
59+ expect ( result ) . toBe ( mockGroup ) ;
60+ expect ( prismaMock . userGroup . create ) . toHaveBeenCalledWith ( {
61+ data : { name : 'group2' , description : 'desc' } ,
62+ } ) ;
63+ } ) ;
64+
65+ it ( 'should find a group by id' , async ( ) => {
66+ const mockGroup = { id : '3' , name : 'group3' } ;
67+ prismaMock . userGroup . findUnique . mockResolvedValueOnce ( mockGroup ) ;
68+ const result = await service . findById ( '3' ) ;
69+ expect ( result ) . toBe ( mockGroup ) ;
70+ expect ( prismaMock . userGroup . findUnique ) . toHaveBeenCalledWith ( {
71+ where : { id : '3' } ,
72+ } ) ;
73+ } ) ;
74+
75+ it ( 'should update a group' , async ( ) => {
76+ const mockGroup = { id : '4' , name : 'group4' , description : 'desc4' } ;
77+ prismaMock . userGroup . update . mockResolvedValueOnce ( mockGroup ) ;
78+ const result = await service . update ( '4' , { name : 'group4' , description : 'desc4' } ) ;
79+ expect ( result ) . toBe ( mockGroup ) ;
80+ expect ( prismaMock . userGroup . update ) . toHaveBeenCalledWith ( {
81+ where : { id : '4' } ,
82+ data : { name : 'group4' , description : 'desc4' } ,
83+ } ) ;
84+ } ) ;
85+
86+ it ( 'should delete a group' , async ( ) => {
87+ const mockGroup = { id : '5' , name : 'group5' } ;
88+ prismaMock . userGroup . delete . mockResolvedValueOnce ( mockGroup ) ;
89+ const result = await service . delete ( '5' ) ;
90+ expect ( result ) . toBe ( mockGroup ) ;
91+ expect ( prismaMock . userGroup . delete ) . toHaveBeenCalledWith ( {
92+ where : { id : '5' } ,
93+ } ) ;
94+ } ) ;
95+ } ) ;
0 commit comments