diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts new file mode 100644 index 0000000..50dae15 --- /dev/null +++ b/__tests__/utils.test.ts @@ -0,0 +1,20 @@ +import {getLanguages} from '../src/utils'; +import axios from 'axios'; +jest.mock('axios'); + +const mockedAxios = axios as jest.Mocked; + +describe('getLanguages function', () => { + it('should return a list of languages', async () =>{ + const languages = [{"code":"et-EE","name":"Estonian (Estonia)"},{"code":"fo","name":"Faroese"}]; + const response = {"data":languages}; + mockedAxios.get.mockImplementationOnce(() => Promise.resolve(response)); + await expect(getLanguages('http://api.test.com')).resolves.toEqual(languages); + }); + it('should return an error if something goes wrong', async () => { + const Error = 'network error'; + mockedAxios.get.mockRejectedValue(Error); + const returnValue = await getLanguages('http://api.test.com'); + expect(returnValue).toEqual(Error); + }); +}); \ No newline at end of file