Created
June 18, 2021 10:14
-
-
Save davidcsejtei/6fbe89dcd7820adc46a3345ce760cc95 to your computer and use it in GitHub Desktop.
GetAllUsers endpoint and test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Request, Response } from 'express'; | |
import getAllUsers from './getAllUsers'; | |
describe('Get all users request', () => { | |
let mockRequest: Partial<Request>; | |
let mockResponse: Partial<Response>; | |
let responseObject = {}; | |
beforeEach(() => { | |
mockRequest = { | |
}; | |
mockResponse = { | |
statusCode: 0, | |
send: jest.fn().mockReturnValue({ | |
json: jest.fn().mockImplementation((JSONdata) => { | |
responseObject = JSONdata; | |
}) | |
}) | |
}; | |
}); | |
test('200 - users', async () => { | |
const expectedStatusCode = 200; | |
const expectedReponse = { | |
users: [ | |
{ | |
name: 'John', | |
age: 30 | |
}, | |
{ | |
name: 'Peter', | |
age: 40 | |
} | |
] | |
}; | |
getAllUsers(mockRequest as Request, mockResponse as Response); | |
expect(mockResponse.statusCode).toBe(expectedStatusCode); | |
expect(responseObject).toEqual(expectedReponse); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Request, Response } from "express"; | |
import { findAllUsers } from "../services/userService"; | |
export default function getAllUsers(request: Request, response: Response) { | |
const users = findAllUsers(); | |
response.statusCode = 200; | |
response.send().json({ users }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment