Skip to content

Instantly share code, notes, and snippets.

@xiongemi
Last active December 1, 2020 03:02
Show Gist options
  • Select an option

  • Save xiongemi/92bad6001793f478457f670a4315ef67 to your computer and use it in GitHub Desktop.

Select an option

Save xiongemi/92bad6001793f478457f670a4315ef67 to your computer and use it in GitHub Desktop.
unit test for country serivce
import fetchMock from 'jest-fetch-mock';
import { countriesService } from './countries.service';
import { mockCountriesResponse } from './models/countries-response.mock';
describe('Countries Service', () => {
afterEach(() => {
fetchMock.resetMocks();
});
it('should return response if successful', async () => {
fetchMock.mockResponseOnce(JSON.stringify(mockCountriesResponse));
const actual = await countriesService.getCountries('en-CA');
expect(actual).toEqual(mockCountriesResponse);
});
it('should throw response if returns error response', async () => {
const response = new Response(null, {
status: 401,
});
fetchMock.mockReturnValueOnce(Promise.resolve(response));
try {
await countriesService.getCountries('en-CA');
} catch (actual) {
expect(actual).toEqual(response);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment