Skip to content

Instantly share code, notes, and snippets.

@mendesrl
Created April 14, 2023 19:11
Show Gist options
  • Save mendesrl/26c18c2b3cfe78a4dfd0208ee6eb3845 to your computer and use it in GitHub Desktop.
Save mendesrl/26c18c2b3cfe78a4dfd0208ee6eb3845 to your computer and use it in GitHub Desktop.
http.spec.js
import { ApiService } from '../';
jest.mock('../', () => ({
ApiService: {
get: jest.fn(),
post: jest.fn(),
put: jest.fn(),
delete: jest.fn(),
postMkt: jest.fn(),
customRequest: jest.fn(),
},
}));
describe('Test http services', () => {
it('Should call GET function', () => {
ApiService.get('/local');
const mockFunction = jest.spyOn(ApiService, 'get');
expect(mockFunction).toHaveBeenCalled();
});
it('Should call POST function', () => {
ApiService.post('/local');
const mockFunction = jest.spyOn(ApiService, 'post');
expect(mockFunction).toHaveBeenCalled();
});
it('Should call PUT function', () => {
ApiService.put('/local');
const mockFunction = jest.spyOn(ApiService, 'put');
expect(mockFunction).toHaveBeenCalled();
});
it('Should call DELETE function', () => {
ApiService.delete('/local');
const mockFunction = jest.spyOn(ApiService, 'delete');
expect(mockFunction).toHaveBeenCalled();
});
it('Should call POSTMKT function', () => {
ApiService.postMkt('/local');
const mockFunction = jest.spyOn(ApiService, 'postMkt');
expect(mockFunction).toHaveBeenCalled();
});
it('Should call custom request function', () => {
ApiService.customRequest('/local');
const mockFunction = jest.spyOn(ApiService, 'customRequest');
expect(mockFunction).toHaveBeenCalled();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment