Skip to content

Instantly share code, notes, and snippets.

@egorvinogradov
Created February 7, 2024 05:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egorvinogradov/17d17e1096f8a19a27e93851fa2309b3 to your computer and use it in GitHub Desktop.
Save egorvinogradov/17d17e1096f8a19a27e93851fa2309b3 to your computer and use it in GitHub Desktop.
UserRepository tests
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 ? 'test@test.com' : 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('test@test.com');
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