From bdff4b8a4337b068a43b9e1add79b8ba98da57c4 Mon Sep 17 00:00:00 2001 From: Ivan Scoles Date: Mon, 13 Aug 2018 17:25:00 -0300 Subject: [PATCH 1/2] Integration with api services from users. --- src/app/actions/usersActions.tsx | 88 +++++++++++++++++++------------- src/app/api/ApiService.test.ts | 82 +++++++++++++++-------------- src/app/api/ApiService.ts | 15 ++++-- 3 files changed, 106 insertions(+), 79 deletions(-) diff --git a/src/app/actions/usersActions.tsx b/src/app/actions/usersActions.tsx index 5cf997c..bd39363 100644 --- a/src/app/actions/usersActions.tsx +++ b/src/app/actions/usersActions.tsx @@ -1,3 +1,4 @@ +import { apiService } from 'rootApp/api/ApiService'; import { omit } from 'rootApp/utils/functions'; import { USERS as actionTypes } from 'rootApp/actions/actionTypes'; import { @@ -61,59 +62,76 @@ export function selectUser(user: any) { }; } -export function deleteUser(user: any) { +export function deleteUser(data: any) { return (dispatch: any) => { dispatch(loadingUsersBegin()); - return deleteUsers(getUserId(user)) - .then(() => { - dispatch(loadingUsersComplete()); - dispatch(deleteUsersSuccess(user)); - return user; - }) - .catch((error: any) => handleErrors(error, dispatch)); + + return apiService.delete({ + entity: 'users', + id: getUserId(data) + }).then((response : any) => { + dispatch(loadingUsersComplete()); + dispatch(deleteUsersSuccess(data)); + + return data; + }).catch((error: any) => handleErrors(error, dispatch)); }; } -export function updateUser(user: any) { +export function updateUser(data: any) { return (dispatch: any) => { dispatch(loadingUsersBegin()); - return updateUsers(getUserId(user), omit(user, DEFAULT_USER_VALID_ID_PATHS)) - .then(({ data } : any) => { - dispatch(loadingUsersComplete()); - dispatch(updateUsersSuccess(data)); - return data; - }) - .catch((error) => handleErrors(error, dispatch)); + + return apiService.update({ + entity: 'users', + id: getUserId(data), + data: omit(data, DEFAULT_USER_VALID_ID_PATHS), + }).then((response : any) => { + dispatch(loadingUsersComplete()); + dispatch(updateUsersSuccess(response)); + + return response; + }); }; } -export function createUser(userData: any) { +export function createUser(data: any) { return (dispatch: any) => { dispatch(loadingUsersBegin()); - return createUsers(omit(userData, DEFAULT_USER_VALID_ID_PATHS)) - .then(({ data } : any) => { - dispatch(loadingUsersComplete()); - dispatch(createUsersSuccess(data)); - return data; - }) - .catch((error) => handleErrors(error, dispatch)); + + return apiService.create({ + entity: 'users', + data + }).then((response : any) => { + dispatch(loadingUsersComplete()); + dispatch(createUsersSuccess(response)); + + return data; + }).catch((error: any) => handleErrors(error, dispatch)); }; } export function getUsers(queryParams = DEFAULT_PAGINATION_QUERY) { return (dispatch: any) => { dispatch(loadingUsersBegin()); - return fetchUsers(queryParams) - .then(({ data } : any) => { - const usersPayload = { - ...omit(data, ['docs']), - users: data.docs - }; - dispatch(loadingUsersComplete()); - dispatch(getUsersSuccess(usersPayload)); - return usersPayload; - }) - .catch((error) => handleErrors(error, dispatch)); + + return apiService.getAll({ + entity: 'users', + params: { + page: 1, + limit: 100 + } + }).then((response : any) => { + const usersPayload = { + ...omit(response, ['docs']), + users: response.docs + }; + + dispatch(loadingUsersComplete()); + dispatch(getUsersSuccess(usersPayload)); + + return usersPayload; + }).catch((error: any) => handleErrors(error, dispatch)); }; } diff --git a/src/app/api/ApiService.test.ts b/src/app/api/ApiService.test.ts index f3b90d7..63e7634 100644 --- a/src/app/api/ApiService.test.ts +++ b/src/app/api/ApiService.test.ts @@ -1,64 +1,74 @@ import { apiService } from './ApiService'; import settings from '../../config/settings'; -import { AxiosInstance } from 'axios'; describe('ApiService', () => { + describe('create', () => { - beforeEach(async () => { + it('should call POST verb from axios api', async () => { await apiService.create({ - entity: 'user', + entity: 'users', data: { name: 'John', lastname: 'Doe' } } as any); - }); - it('should call POST verb from axios api', () => { - expect(apiService.http.post).toHaveBeenCalledWith('/user', { - data: { - name: 'John', - lastname: 'Doe' - } + expect(apiService.http.post).toHaveBeenCalledWith('/users', { + name: 'John', + lastname: 'Doe' } as any); }); }); describe('update', () => { - beforeEach(async () => { + it('should call PUT verb from axios api', async () => { await apiService.update({ - entity: 'user', + entity: 'users', id: 'user-id-1', data: { name: 'John', lastname: 'UpdateDoe' } } as any); - }); - it('should call PUT verb from axios api', () => { - expect(apiService.http.put).toHaveBeenCalledWith('/user', { - data: { - name: 'John', - lastname: 'UpdateDoe' - }, - params: { - id: 'user-id-1' - } + expect(apiService.http.put).toHaveBeenCalledWith('/users/user-id-1', { + name: 'John', + lastname: 'UpdateDoe' }); }); }); + + describe('get actions', () => { + it('should call GET verb from axios api', async () => { - describe('read', () => { - beforeEach(async () => { - await apiService.read({ - entity: 'user', + await apiService.get({ + entity: 'users', id: 'user-id-1' } as any); + + const getSpy = jest.spyOn(apiService.http, 'get'); + + expect(getSpy).toHaveBeenCalledWith('/users', { + params: { + id: 'user-id-1' + } + } as any); }); - it('should call GET verb from axios api', () => { - expect(apiService.http.get).toHaveBeenCalledWith('/user', { + it('should call GET verb from axios api', async () => { + await apiService.getAll({ + entity: 'users', + params: { + page: 1, + limit: 100 + } + } as any); + + const getSpy = jest.spyOn(apiService.http, 'get'); + + apiService.http.get = getSpy as any; + + expect(getSpy).toHaveBeenCalledWith('/users', { params: { id: 'user-id-1' } @@ -67,24 +77,18 @@ describe('ApiService', () => { }); describe('delete', () => { - beforeEach(async () => { + it('should call DELETE verb from axios api', async () => { await apiService.delete({ - entity: 'user', + entity: 'users', id: 'user-id-1' }); - }); - - it('should call DELETE verb from axios api', () => { - expect(apiService.http.delete).toHaveBeenCalledWith('/user', { - params: { - id: 'user-id-1' - } - }); + + expect(apiService.http.delete).toHaveBeenCalledWith('/users/user-id-1'); }); }); describe('http', () => { - it('should have a baseURL property with the expected value regarding the environment', () => { + it('should have a baseURL property with the expected value regarding the environment', async () => { const http = apiService.http as any; expect(http.baseURL).toEqual(settings.SERVICE.BASE_URL); }); diff --git a/src/app/api/ApiService.ts b/src/app/api/ApiService.ts index d98e8c7..9d33aad 100644 --- a/src/app/api/ApiService.ts +++ b/src/app/api/ApiService.ts @@ -8,22 +8,27 @@ class ApiService { create = async (options: { entity: String, data: Object }): Promise => { const { entity, data } = options; - return this.http.post(`/${entity}`, { data }).then(response => response.data); + return this.http.post(`/${entity}`, data).then(response => response.data); } update = async (options: { entity: String, id: String, data: Object }): Promise => { const { entity, id, data } = options; - return this.http.put(`/${entity}`, { params: { id }, data }).then(response => response.data); + return this.http.put(`/${entity}/${id}`, data).then(response => response.data); } - read = async (options: { entity: String, id: String }): Promise => { + getAll = async (options: { entity: String, params: Object }): Promise => { + const { entity, params } = options; + return this.http.get(`/${entity}`, { params }).then((response: any) => response.data); + } + + get = async (options: { entity: String, id: String }): Promise => { const { entity, id } = options; return this.http.get(`/${entity}`, { params: { id } }).then(response => response.data); } - + delete = async (options: { entity: String, id: String }): Promise => { const { entity, id } = options; - return this.http.delete(`/${entity}`, { params: { id } }).then(response => response.data); + return this.http.delete(`/${entity}/${id}`).then(response => response.data); } } From 50fd717ffd2e642aa733a725eae5ac985fd7cd83 Mon Sep 17 00:00:00 2001 From: Ivan Scoles Date: Mon, 13 Aug 2018 17:35:27 -0300 Subject: [PATCH 2/2] Removed unused import from userServices. --- src/app/actions/usersActions.tsx | 6 ------ src/app/services/userService.tsx | 18 ------------------ 2 files changed, 24 deletions(-) delete mode 100644 src/app/services/userService.tsx diff --git a/src/app/actions/usersActions.tsx b/src/app/actions/usersActions.tsx index bd39363..a8a9524 100644 --- a/src/app/actions/usersActions.tsx +++ b/src/app/actions/usersActions.tsx @@ -1,12 +1,6 @@ import { apiService } from 'rootApp/api/ApiService'; import { omit } from 'rootApp/utils/functions'; import { USERS as actionTypes } from 'rootApp/actions/actionTypes'; -import { - createUsers, - deleteUsers, - fetchUsers, - updateUsers -} from 'rootApp/services/userService'; import { DEFAULT_USER_VALID_ID_PATHS, DEFAULT_PAGINATION_QUERY diff --git a/src/app/services/userService.tsx b/src/app/services/userService.tsx deleted file mode 100644 index 6555db0..0000000 --- a/src/app/services/userService.tsx +++ /dev/null @@ -1,18 +0,0 @@ -import axios from 'axios'; -import { DEFAULT_API_USERS_ENDPOINT } from 'rootApp/constants/defaults'; - -export function fetchUsers(params = {}) { - return axios.get(DEFAULT_API_USERS_ENDPOINT, { params }); -} - -export function createUsers(data = {}) { - return axios.post(DEFAULT_API_USERS_ENDPOINT, { ...data }); -} - -export function deleteUsers(userId: any) { - return axios.delete(`${DEFAULT_API_USERS_ENDPOINT}/${userId}`); -} - -export function updateUsers(userId: any, data = {}) { - return axios.put(`${DEFAULT_API_USERS_ENDPOINT}/${userId}`, { ...data }); -}