Last active
December 1, 2020 03:02
-
-
Save xiongemi/92bad6001793f478457f670a4315ef67 to your computer and use it in GitHub Desktop.
unit test for country serivce
This file contains hidden or 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 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