From 44e9d3919d3ef51fd2ec4b2fae08cd635d7302ad Mon Sep 17 00:00:00 2001 From: kialam Date: Tue, 13 Nov 2018 09:46:43 -0500 Subject: [PATCH] Update unit tests. --- __tests__/App.test.jsx | 7 +++-- __tests__/api.test.js | 48 ++++--------------------------- __tests__/pages/Organizations.jsx | 6 ++++ src/api.js | 9 ++---- 4 files changed, 20 insertions(+), 50 deletions(-) diff --git a/__tests__/App.test.jsx b/__tests__/App.test.jsx index 01e216659d..58c8d57056 100644 --- a/__tests__/App.test.jsx +++ b/__tests__/App.test.jsx @@ -2,6 +2,8 @@ import React from 'react'; import { shallow, mount } from 'enzyme'; import App from '../src/App'; import api from '../src/api'; +import * as constant from '../src/endpoints'; + import Dashboard from '../src/pages/Dashboard'; import Login from '../src/pages/Login'; import { asyncFlush } from '../jest.setup'; @@ -64,12 +66,13 @@ describe('', () => { }); test('api.logout called from logout button', async () => { - api.logout = jest.fn().mockImplementation(() => Promise.resolve({})); + api.BaseGet = jest.fn().mockImplementation(() => Promise.resolve({})); const appWrapper = mount(); const logoutButton = appWrapper.find('LogoutButton'); logoutButton.props().onDevLogout(); appWrapper.setState({ activeGroup: 'foo', activeItem: 'bar' }); - expect(api.logout).toHaveBeenCalledTimes(1); + expect(api.BaseGet).toHaveBeenCalledTimes(1); + expect(api.BaseGet).toHaveBeenCalledWith(constant.API_LOGOUT); await asyncFlush(); expect(appWrapper.state().activeItem).toBe(DEFAULT_ACTIVE_ITEM); expect(appWrapper.state().activeGroup).toBe(DEFAULT_ACTIVE_GROUP); diff --git a/__tests__/api.test.js b/__tests__/api.test.js index 5d14b20503..f4aa913153 100644 --- a/__tests__/api.test.js +++ b/__tests__/api.test.js @@ -1,14 +1,7 @@ import mockAxios from 'axios'; import APIClient from '../src/api'; - -const API_ROOT = '/api/'; -const API_LOGIN = `${API_ROOT}login/`; -const API_LOGOUT = `${API_ROOT}logout/`; -const API_V2 = `${API_ROOT}v2/`; -const API_CONFIG = `${API_V2}config/`; -const API_PROJECTS = `${API_V2}projects/`; -const API_ORGANIZATIONS = `${API_V2}organizations/`; +import * as constant from '../src/endpoints'; const CSRF_COOKIE_NAME = 'csrftoken'; const CSRF_HEADER_NAME = 'X-CSRFToken'; @@ -52,9 +45,9 @@ describe('APIClient (api.js)', () => { APIClient.setCookie = jest.fn(); APIClient.login(un, pw, next).then(() => { expect(mockAxios.get).toHaveBeenCalledTimes(1); - expect(mockAxios.get).toHaveBeenCalledWith(API_LOGIN, { headers }); + expect(mockAxios.get).toHaveBeenCalledWith(constant.API_LOGIN, { headers }); expect(mockAxios.post).toHaveBeenCalledTimes(1); - expect(mockAxios.post).toHaveBeenCalledWith(API_LOGIN, data, { headers }); + expect(mockAxios.post).toHaveBeenCalledWith(constant.API_LOGIN, data, { headers }); done(); }); }); @@ -67,7 +60,7 @@ describe('APIClient (api.js)', () => { const data = `username=${encodeURIComponent(un)}&password=${encodeURIComponent(pw)}&next=${encodeURIComponent(next)}`; APIClient.login(un, pw, next).then(() => { expect(mockAxios.post).toHaveBeenCalledTimes(1); - expect(mockAxios.post).toHaveBeenCalledWith(API_LOGIN, data, { headers }); + expect(mockAxios.post).toHaveBeenCalledWith(constant.API_LOGIN, data, { headers }); done(); }); }); @@ -76,42 +69,13 @@ describe('APIClient (api.js)', () => { const un = 'foo'; const pw = 'bar'; const headers = { 'Content-Type': LOGIN_CONTENT_TYPE }; - const data = `username=${un}&password=${pw}&next=${encodeURIComponent(API_CONFIG)}`; + const data = `username=${un}&password=${pw}&next=${encodeURIComponent(constant.API_CONFIG)}`; APIClient.setCookie = jest.fn(); APIClient.login(un, pw).then(() => { expect(mockAxios.post).toHaveBeenCalledTimes(1); - expect(mockAxios.post).toHaveBeenCalledWith(API_LOGIN, data, { headers }); + expect(mockAxios.post).toHaveBeenCalledWith(constant.API_LOGIN, data, { headers }); done(); }); }); - test('logout calls get to logout route', () => { - APIClient.logout(); - expect(mockAxios.get).toHaveBeenCalledTimes(1); - expect(mockAxios.get).toHaveBeenCalledWith(API_LOGOUT); - }); - - test('getConfig calls get to config route', () => { - APIClient.getConfig(); - expect(mockAxios.get).toHaveBeenCalledTimes(1); - expect(mockAxios.get).toHaveBeenCalledWith(API_CONFIG); - }); - - test('getProjects calls get to projects route', () => { - APIClient.getProjects(); - expect(mockAxios.get).toHaveBeenCalledTimes(1); - expect(mockAxios.get).toHaveBeenCalledWith(API_PROJECTS); - }); - - test('getOrganigzations calls get to organizations route', () => { - APIClient.getOrganizations(); - expect(mockAxios.get).toHaveBeenCalledTimes(1); - expect(mockAxios.get).toHaveBeenCalledWith(API_ORGANIZATIONS); - }); - - test('getRoot calls get to root route', () => { - APIClient.getRoot(); - expect(mockAxios.get).toHaveBeenCalledTimes(1); - expect(mockAxios.get).toHaveBeenCalledWith(API_ROOT); - }); }); diff --git a/__tests__/pages/Organizations.jsx b/__tests__/pages/Organizations.jsx index f41b830091..87efbf95f1 100644 --- a/__tests__/pages/Organizations.jsx +++ b/__tests__/pages/Organizations.jsx @@ -1,5 +1,6 @@ import React from 'react'; import { mount } from 'enzyme'; +import { API_ORGANIZATIONS } from '../../src/endpoints'; import Organizations from '../../src/pages/Organizations'; describe('', () => { @@ -51,4 +52,9 @@ describe('', () => { expect(galleryItems.length).toBe(3); expect(orgCards.length).toBe(3); }); + + test('API Organization endpoint is valid', () => { + expect(API_ORGANIZATIONS).toBeDefined(); + }); + }); diff --git a/src/api.js b/src/api.js index 0671d6ea65..0b0513cc85 100644 --- a/src/api.js +++ b/src/api.js @@ -40,15 +40,12 @@ class APIClient { const data = `username=${un}&password=${pw}&next=${next}`; const headers = { 'Content-Type': LOGIN_CONTENT_TYPE }; - try { - await this.http.get(constant.API_LOGIN, { headers }); - await this.http.post(constant.API_LOGIN, data, { headers }); - } catch (err) { - alert(`There was a problem logging in: ${err}`); - } + await this.http.get(constant.API_LOGIN, { headers }); + await this.http.post(constant.API_LOGIN, data, { headers }); } BaseGet = (endpoint) => this.http.get(endpoint); + } export default new APIClient();