1+ import { TEST_CURRENT_CONTEXT , TEST_IDIR_USER_1 } from '../data' ;
12import { searchUsersController } from '../../../src/controllers/user' ;
23import * as userService from '../../../src/services/user' ;
34
4- import type { Response } from 'express' ;
5+ import type { Request , Response } from 'express' ;
6+ import type { UserSearchParameters } from '../../../src/types' ;
7+ import { prismaTxMock } from '../../__mocks__/prismaMock' ;
58
69// Mock config library - @see {@link https://stackoverflow.com/a/64819698}
710jest . mock ( 'config' ) ;
@@ -23,45 +26,26 @@ afterEach(() => {
2326 jest . resetAllMocks ( ) ;
2427} ) ;
2528
26- const CURRENT_CONTEXT = { authType : 'BEARER' , tokenPayload : null } ;
27-
28- const TEST_USER_LIST = [
29- {
30- bceidBusinessName : null ,
31- userId : '5e3f0c19-8664-4a43-ac9e-210da336e923' ,
32- idp : 'IDIR' ,
33- sub : 'cd90c6bf44074872a7116f4dd4f3a45b@idir' ,
34- email : 'first.last@gov.bc.ca' ,
35- firstName : 'First' ,
36- fullName : 'Last, First' ,
37- lastName : 'Last' ,
38- active : true ,
39- createdAt : null ,
40- createdBy : null ,
41- updatedAt : null ,
42- updatedBy : null
43- }
44- ] ;
29+ const TEST_USER_LIST = [ TEST_IDIR_USER_1 ] ;
4530
4631describe ( 'searchUsersController' , ( ) => {
47- const next = jest . fn ( ) ;
48-
49- // Mock service calls
5032 const searchUsersSpy = jest . spyOn ( userService , 'searchUsers' ) ;
5133
52- it ( 'should return 200 if all good ' , async ( ) => {
34+ it ( 'should call services and respond with 200 and result ' , async ( ) => {
5335 const req = {
5436 query : { userId : '5e3f0c19-8664-4a43-ac9e-210da336e923' } ,
55- currentContext : CURRENT_CONTEXT
37+ currentContext : TEST_CURRENT_CONTEXT
5638 } ;
5739
5840 searchUsersSpy . mockResolvedValue ( TEST_USER_LIST ) ;
5941
60- // eslint-disable-next-line @typescript-eslint/no-explicit-any
61- await searchUsersController ( req as any , res as unknown as Response ) ;
42+ await searchUsersController (
43+ req as unknown as Request < never , never , never , UserSearchParameters > ,
44+ res as unknown as Response
45+ ) ;
6246
6347 expect ( searchUsersSpy ) . toHaveBeenCalledTimes ( 1 ) ;
64- expect ( searchUsersSpy ) . toHaveBeenCalledWith ( {
48+ expect ( searchUsersSpy ) . toHaveBeenCalledWith ( prismaTxMock , {
6549 userId : [ '5e3f0c19-8664-4a43-ac9e-210da336e923' ]
6650 } ) ;
6751 expect ( res . status ) . toHaveBeenCalledWith ( 200 ) ;
@@ -71,38 +55,19 @@ describe('searchUsersController', () => {
7155 it ( 'adds dashes to user IDs' , async ( ) => {
7256 const req = {
7357 query : { userId : '5e3f0c1986644a43ac9e210da336e923,8b9dedd279d442c6b82f52844a8e2757' } ,
74- currentContext : CURRENT_CONTEXT
58+ currentContext : TEST_CURRENT_CONTEXT
7559 } ;
7660
7761 searchUsersSpy . mockResolvedValue ( TEST_USER_LIST ) ;
7862
79- // eslint-disable-next-line @typescript-eslint/no-explicit-any
80- await searchUsersController ( req as any , res as unknown as Response ) ;
63+ await searchUsersController (
64+ req as unknown as Request < never , never , never , UserSearchParameters > ,
65+ res as unknown as Response
66+ ) ;
8167
8268 expect ( searchUsersSpy ) . toHaveBeenCalledTimes ( 1 ) ;
83- expect ( searchUsersSpy ) . toHaveBeenCalledWith ( {
69+ expect ( searchUsersSpy ) . toHaveBeenCalledWith ( prismaTxMock , {
8470 userId : [ '5e3f0c19-8664-4a43-ac9e-210da336e923' , '8b9dedd2-79d4-42c6-b82f-52844a8e2757' ]
8571 } ) ;
8672 } ) ;
87-
88- it ( 'calls next if the user service fails to list users' , async ( ) => {
89- const req = {
90- query : { userId : '5e3f0c19-8664-4a43-ac9e-210da336e923' } ,
91- currentContext : CURRENT_CONTEXT
92- } ;
93-
94- searchUsersSpy . mockImplementationOnce ( ( ) => {
95- throw new Error ( ) ;
96- } ) ;
97-
98- // eslint-disable-next-line @typescript-eslint/no-explicit-any
99- await searchUsersController ( req as any , res as unknown as Response ) ;
100-
101- expect ( searchUsersSpy ) . toHaveBeenCalledTimes ( 1 ) ;
102- expect ( searchUsersSpy ) . toHaveBeenCalledWith ( {
103- userId : [ '5e3f0c19-8664-4a43-ac9e-210da336e923' ]
104- } ) ;
105- expect ( res . status ) . toHaveBeenCalledTimes ( 0 ) ;
106- expect ( next ) . toHaveBeenCalledTimes ( 1 ) ;
107- } ) ;
10873} ) ;
0 commit comments