Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 53 additions & 41 deletions src/app/actions/usersActions.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +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
Expand Down Expand Up @@ -61,59 +56,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));
};
}

Expand Down
82 changes: 43 additions & 39 deletions src/app/api/ApiService.test.ts
Original file line number Diff line number Diff line change
@@ -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'
}
Expand All @@ -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);
});
Expand Down
15 changes: 10 additions & 5 deletions src/app/api/ApiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,27 @@ class ApiService {

create = async (options: { entity: String, data: Object }): Promise<void> => {
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<void> => {
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<void> => {
getAll = async (options: { entity: String, params: Object }): Promise<void> => {
const { entity, params } = options;
return this.http.get(`/${entity}`, { params }).then((response: any) => response.data);
}

get = async (options: { entity: String, id: String }): Promise<void> => {
const { entity, id } = options;
return this.http.get(`/${entity}`, { params: { id } }).then(response => response.data);
}

delete = async (options: { entity: String, id: String }): Promise<void> => {
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);
}
}

Expand Down
18 changes: 0 additions & 18 deletions src/app/services/userService.tsx

This file was deleted.