diff options
author | Grégoire Aubert <gregoire.aubert@sonarsource.com> | 2019-06-24 12:26:27 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2019-06-28 08:45:51 +0200 |
commit | ea0447b8ee7fd647ff127900ca7c489209e877a7 (patch) | |
tree | 59b349b2c7d30d5d0092d5e66c7a89be91ffb405 /server/sonar-web/src/main | |
parent | 40643b17b0d4a2cfa402a6690e3f91fe7d7dc308 (diff) | |
download | sonarqube-ea0447b8ee7fd647ff127900ca7c489209e877a7.tar.gz sonarqube-ea0447b8ee7fd647ff127900ca7c489209e877a7.zip |
Add unit test for alm-integration api call
Diffstat (limited to 'server/sonar-web/src/main')
-rw-r--r-- | server/sonar-web/src/main/js/api/__tests__/alm-integration-test.ts | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/server/sonar-web/src/main/js/api/__tests__/alm-integration-test.ts b/server/sonar-web/src/main/js/api/__tests__/alm-integration-test.ts new file mode 100644 index 00000000000..cde81f9f022 --- /dev/null +++ b/server/sonar-web/src/main/js/api/__tests__/alm-integration-test.ts @@ -0,0 +1,71 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +import { getJSON } from '../../helpers/request'; +import { getAlmOrganization } from '../alm-integration'; + +jest.useFakeTimers(); +jest.mock('../../helpers/request', () => ({ + ...jest.requireActual('../../helpers/request'), + getJSON: jest.fn() +})); +jest.mock('../../app/utils/throwGlobalError', () => ({ + default: jest.fn().mockImplementation(r => Promise.reject(r)) +})); + +beforeEach(() => { + jest.clearAllTimers(); + jest.clearAllMocks(); +}); + +describe('getAlmOrganization', () => { + it('should return the organization', () => { + const response = { almOrganization: { key: 'foo', name: 'Foo' } }; + (getJSON as jest.Mock).mockResolvedValue(response); + return expect(getAlmOrganization({ installationId: 'foo' })).resolves.toEqual(response); + }); + + it('should reject with an error', () => { + const error = { response: { status: 401 } }; + (getJSON as jest.Mock).mockRejectedValue(error); + return expect(getAlmOrganization({ installationId: 'foo' })).rejects.toEqual(error); + }); + + it('should try until getting the organization', async () => { + (getJSON as jest.Mock).mockRejectedValue({ response: { status: 404 } }); + const spy = jest.fn(); + getAlmOrganization({ installationId: 'foo' }).then(spy); + for (let i = 1; i < 5; i++) { + expect(getJSON).toBeCalledTimes(i); + expect(spy).not.toBeCalled(); + await new Promise(setImmediate); + jest.runAllTimers(); + } + expect(getJSON).toBeCalledTimes(5); + expect(spy).not.toBeCalled(); + + const response = { almOrganization: { key: 'foo', name: 'Foo' } }; + (getJSON as jest.Mock).mockResolvedValue(response); + await new Promise(setImmediate); + jest.runAllTimers(); + expect(getJSON).toBeCalledTimes(6); + await new Promise(setImmediate); + expect(spy).toBeCalledWith(response); + }); +}); |