Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#82] Refatorado a store do productList #83

Merged
merged 1 commit into from
Jan 10, 2021
Merged
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
4 changes: 2 additions & 2 deletions src/routes/new-item/__tests__/useNewItem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('NewItem - useNewItem', () => {

expect(checkForm).toHaveBeenCalled();
expect(dispatch).toHaveBeenCalledWith(
productListActions.createProductItemAsync(
productListActions.createItem(
initialProps.formParams,
listId,
),
Expand Down Expand Up @@ -95,7 +95,7 @@ describe('NewItem - useNewItem', () => {

expect(checkForm).toHaveBeenCalled();
expect(dispatch).toHaveBeenCalledWith(
productListActions.updateProductItemAsync(
productListActions.updateItem(
newInitialProps.formParams,
listId,
),
Expand Down
18 changes: 3 additions & 15 deletions src/routes/new-item/useNewItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useCallback, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';

import { ProductNavigatorParamsList } from '@navigator/product-navigator';
import productListSelectors from '@store/product-list/selectors';
import { productListSelectors } from '@store/product-list';
import { ProductItem } from '@store/product-list/types';

interface Props {
Expand All @@ -29,22 +29,10 @@ const useNewItem = (props: Props) => {

if (isValid) {
if (formParams.id) {
// #TODO Após refatorar o productListActions, deve manter o mesmo nome e remover esse if
if ('updateItem' in action) {
dispatch(action.updateItem(formParams));
} else {
dispatch(action.updateProductItemAsync(formParams, listId));
}

dispatch(action.updateItem(formParams, listId));
return;
}

if ('createItem' in action) {
dispatch(action.createItem(formParams));
} else {
dispatch(action.createProductItemAsync(formParams, listId));
}

dispatch(action.createItem(formParams, listId));
return;
}
}, [formParams]);
Expand Down
4 changes: 2 additions & 2 deletions src/routes/product-list/new-list/__tests__/useNewList.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('NewList - useNewList', () => {

expect(checkForm).toHaveBeenCalled();
expect(dispatch).toHaveBeenCalledWith(
productListActions.createProductListAsync(initialProps.listParams),
productListActions.createList(initialProps.listParams),
);
expect(showAd).toHaveBeenCalled();
});
Expand All @@ -56,7 +56,7 @@ describe('NewList - useNewList', () => {

expect(checkForm).toHaveBeenCalled();
expect(dispatch).toHaveBeenCalledWith(
productListActions.updateProductListAsync(newInitialProps.listParams),
productListActions.updateList(newInitialProps.listParams),
);
expect(showAd).not.toHaveBeenCalled();
});
Expand Down
4 changes: 2 additions & 2 deletions src/routes/product-list/new-list/useNewList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ const useNewList = (props: Props) => {
const canSubmit = await checkForm();
if (canSubmit) {
if (listParams.id) {
dispatch(productListActions.updateProductListAsync(listParams));
dispatch(productListActions.updateList(listParams));
return;
}
dispatch(productListActions.createProductListAsync(listParams));
dispatch(productListActions.createList(listParams));
showAd();
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('ProductItems - useProductItems', () => {
renderHook(useProductItems);

expect(dispatch).toHaveBeenCalledWith(
productListActions.getProductItemsAsync('123456'),
productListActions.requestItems('123456'),
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('ProductItems - Components - ItemCard - useItemCard', () => {
const itemId = '654321';
const listId = '123456';
expect(dispatch).toHaveBeenCalledWith(
productListActions.deleteProductItemAsync(itemId, listId),
productListActions.deleteItem(itemId, listId),
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const useItemCard = (props: Props) => {

const handleDeleteItem = useCallback(() => {
const itemId = productItem.id;
dispatch(productListActions.deleteProductItemAsync(itemId, listId));
dispatch(productListActions.deleteItem(itemId, listId));
}, [productItem]);

return {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/product-list/product-items/useProductItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const useProductItems = () => {
const [ordenedList, setOrdenedList] = useState(productItems);

const fetchProductItems = useCallback(() => {
dispatch(productListActions.getProductItemsAsync(listId));
dispatch(productListActions.requestItems(listId));
}, []);

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('ProductList - useProductLists', () => {
renderHook(useProductLists);

expect(dispatch).toHaveBeenCalledWith(
productListActions.getProductListsAsync(),
productListActions.requestLists(),
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('ProductList - useListCard', () => {
});

expect(dispatch).toHaveBeenCalledWith(
productListActions.deleteProductListAsync(mockProductList.id),
productListActions.deleteList(mockProductList.id),
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const useListCard = (props: Props) => {

const _handleDeleteItem = useCallback(() => {
const listId = productList.id;
dispatch(productListActions.deleteProductListAsync(listId));
dispatch(productListActions.deleteList(listId));
}, [productList]);

const handleListLongPress = useCallback(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/product-list/product-lists/useProductLists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const useProductLists = () => {
}, []);

const fetchProductLists = useCallback(() => {
dispatch(productListActions.getProductListsAsync());
dispatch(productListActions.requestLists());
}, []);

useEffect(() => {
Expand Down
230 changes: 93 additions & 137 deletions src/store/product-list/__tests__/actions.test.ts
Original file line number Diff line number Diff line change
@@ -1,144 +1,100 @@
import { productListActions } from '../';
import { ProductItemBuilderMock } from '../__mocks__/productItemBuilder.mock';
import { ProductListBuilderMock } from '../__mocks__/productListBuilder.mock';
import actions from '../actions';
import { ProductListTypes } from '../types';

describe('ProductList Actions', () => {
test('deve retornar setLoading corretamente.', () => {
const result = productListActions.setLoading(true);

expect(result).toEqual({
type: ProductListTypes.SET_LOADING,
payload: {
isLoading: true,
},
});
});

test('deve retornar setError corretamente.', () => {
const mockError = new Error('teste');
const result = productListActions.setError(mockError.message);

expect(result).toEqual({
type: ProductListTypes.SET_ERROR,
payload: {
error: 'teste',
},
});
});

test('deve retornar getProductListsAsync corretamente.', () => {
const result = productListActions.getProductListsAsync();

expect(result).toEqual({
type: ProductListTypes.GET_PRODUCT_LISTS_ASYNC,
});
});

test('deve retornar createProductListAsync corretamente.', () => {
const mockData = new ProductListBuilderMock().withName('Lista').build();

const result = productListActions.createProductListAsync(mockData);

expect(result).toEqual({
type: ProductListTypes.CREATE_PRODUCT_LIST_ASYNC,
payload: {
productList: mockData,
},
});
});

test('deve retornar updateProductListAsync corretamente.', () => {
const mockData = new ProductListBuilderMock().withName('Lista').build();

const result = productListActions.updateProductListAsync(mockData);

expect(result).toEqual({
type: ProductListTypes.UPDATE_PRODUCT_LIST_ASYNC,
payload: {
productList: mockData,
},
});
});

test('deve retornar deleteProductListAsync corretamente.', () => {
const result = productListActions.deleteProductListAsync('12345');

expect(result).toEqual({
type: ProductListTypes.DELETE_PRODUCT_LIST_ASYNC,
payload: {
listId: '12345',
},
});
});

test('deve retornar setProductLists corretamente.', () => {
const mockData = [new ProductListBuilderMock().withName('Lista').build()];

const result = productListActions.setProductLists(mockData);

expect(result).toEqual({
type: ProductListTypes.SET_PRODUCT_LIST,
payload: {
productLists: mockData,
},
});
});

test('deve retornar getProductItemsAsync corretamente.', () => {
const listId = '12345';
const result = productListActions.getProductItemsAsync(listId);

expect(result).toEqual({
type: ProductListTypes.GET_PRODUCT_ITEMS_ASYNC,
payload: {
listId,
},
});
});

test('deve retornar createProductItemAsync corretamente.', () => {
const mockData = new ProductItemBuilderMock().withName('Lista').build();
const listId = '12345';
const result = productListActions.createProductItemAsync(mockData, listId);

expect(result).toEqual({
type: ProductListTypes.CREATE_PRODUCT_ITEM_ASYNC,
payload: {
productItem: mockData,
listId,
},
});
});

test('deve retornar deleteProductItemAsync corretamente.', () => {
const itemId = '54321';
const listId = '12345';
const result = productListActions.deleteProductItemAsync(itemId, listId);

expect(result).toEqual({
type: ProductListTypes.DELETE_PRODUCT_ITEM_ASYNC,
payload: {
itemId,
listId,
},
});
});

test('deve retornar updateProductItemAsync corretamente.', () => {
const productItem = new ProductItemBuilderMock().withName('Lista').build();
const listId = '12345';
const result = productListActions.updateProductItemAsync(
productItem,
listId,
);

expect(result).toEqual({
type: ProductListTypes.UPDATE_PRODUCT_ITEM_ASYNC,
payload: {
listId,
productItem,
},
});
const mockList = new ProductListBuilderMock().withName('Lista').build();
const mockItem = new ProductItemBuilderMock().withName('Lista').build();

//#region Actions

const setError = [
'setError',
actions.setError('error'),
{ type: ProductListTypes.SET_ERROR, payload: { error: 'error' } },
];

const requestLists = [
'requestLists',
actions.requestLists(),
{ type: ProductListTypes.REQUEST_LISTS },
];

const createList = [
'createList',
actions.createList(mockList),
{ type: ProductListTypes.CREATE_LIST, payload: { productList: mockList } },
];

const updateList = [
'updateList',
actions.updateList(mockList),
{ type: ProductListTypes.UPDATE_LIST, payload: { productList: mockList } },
];

const deleteList = [
'deleteList',
actions.deleteList('12345'),
{ type: ProductListTypes.DELETE_LIST, payload: { listId: '12345' } },
];

const setProductLists = [
'setProductLists',
actions.setProductLists([mockList]),
{
type: ProductListTypes.SET_PRODUCT_LISTS,
payload: { productLists: [mockList] },
},
];

const requestItems = [
'requestItems',
actions.requestItems('12345'),
{ type: ProductListTypes.REQUEST_ITEMS, payload: { listId: '12345' } },
];

const createItem = [
'createItem',
actions.createItem(mockItem, '12345'),
{
type: ProductListTypes.CREATE_ITEM,
payload: { productItem: mockItem, listId: '12345' },
},
];

const deleteItem = [
'deleteItem',
actions.deleteItem('111', '12345'),
{
type: ProductListTypes.DELETE_ITEM,
payload: { itemId: '111', listId: '12345' },
},
];

const updateItem = [
'updateItem',
actions.updateItem(mockItem, '12345'),
{
type: ProductListTypes.UPDATE_ITEM,
payload: { productItem: mockItem, listId: '12345' },
},
];

//#endregion

test.each([
setError,
requestLists,
createList,
updateList,
deleteList,
setProductLists,
requestItems,
createItem,
deleteItem,
updateItem,
])('deve retornar corretamente a action %s', (describe, action, expected) => {
expect(action).toEqual(expected);
});
});
Loading