Created
February 7, 2024 05:42
-
-
Save egorvinogradov/17d17e1096f8a19a27e93851fa2309b3 to your computer and use it in GitHub Desktop.
UserRepository tests
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 { Api } from './Api'; | |
import { UserRepository } from './UserRepository'; | |
jest.mock('./Api', () => { | |
return { | |
Api: jest.fn().mockImplementation(() => { | |
return { | |
getUser: jest.fn((id: number) => { | |
return id === 1 ? '[email protected]' : null; | |
}), | |
}; | |
}) | |
}; | |
}); | |
describe('UserRepository', () => { | |
let api: Api; | |
let userRepository: UserRepository; | |
beforeEach(() => { | |
api = new Api(); | |
userRepository = new UserRepository(api); | |
}); | |
test('getUserEmail returns the correct email for a valid ID', async () => { | |
const email = await userRepository.getUserEmail(1); | |
expect(email).toBe('[email protected]'); | |
expect(api.getUser).toHaveBeenCalledWith(1); | |
}); | |
test('getUserEmail should return null for an invalid ID', async () => { | |
const email = await userRepository.getUserEmail(2); | |
expect(email).toBeNull(); | |
expect(api.getUser).toHaveBeenCalledWith(2); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment