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

Adds unit tests to Catalog actions #875

Merged
merged 6 commits into from
Feb 11, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ class Category extends Component {
id: 3
};

componentDidUpdate(prevProps) {
// If the current page has changed, scroll back up to the top.
if (this.props.currentPage !== prevProps.currentPage) {
window.scrollTo(0, 0);
}
}
supernova-at marked this conversation as resolved.
Show resolved Hide resolved

render() {
const {
id,
Expand Down
154 changes: 154 additions & 0 deletions packages/venia-concept/src/actions/catalog/__tests__/actions.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import actions from '../actions';

const MOCK_PAYLOAD = 'Unit Test Payload';
const ERROR = new Error('Unit Test');

describe('getAllCategories', () => {
const PREFIX = 'CATALOG/GET_ALL_CATEGORIES';

describe('REQUEST', () => {
const EXPECTED_NAME = `${PREFIX}/REQUEST`;

test('it returns the proper action type', () => {
expect(actions.getAllCategories.request.toString()).toBe(
EXPECTED_NAME
);
});

test('it returns a proper action object', () => {
expect(actions.getAllCategories.request(MOCK_PAYLOAD)).toEqual({
type: EXPECTED_NAME,
payload: MOCK_PAYLOAD
});

expect(actions.getAllCategories.request(ERROR)).toEqual({
type: EXPECTED_NAME,
payload: ERROR,
error: true
});
});
});

describe('RECEIVE', () => {
const EXPECTED_NAME = `${PREFIX}/RECEIVE`;

test('it returns the proper action type', () => {
expect(actions.getAllCategories.receive.toString()).toBe(
EXPECTED_NAME
);
});

test('it returns a proper action object', () => {
expect(actions.getAllCategories.receive(MOCK_PAYLOAD)).toEqual({
type: EXPECTED_NAME,
payload: MOCK_PAYLOAD
});

expect(actions.getAllCategories.receive(ERROR)).toEqual({
type: EXPECTED_NAME,
payload: ERROR,
error: true
});
});
});
});

describe('setCurrentPage', () => {
const PREFIX = 'CATALOG/SET_CURRENT_PAGE';

describe('REQUEST', () => {
const EXPECTED_NAME = `${PREFIX}/REQUEST`;

test('it returns the proper action type', () => {
expect(actions.setCurrentPage.request.toString()).toBe(
EXPECTED_NAME
);
});

test('it returns a proper action object', () => {
expect(actions.setCurrentPage.request(MOCK_PAYLOAD)).toEqual({
type: EXPECTED_NAME,
payload: MOCK_PAYLOAD
});

expect(actions.setCurrentPage.request(ERROR)).toEqual({
type: EXPECTED_NAME,
payload: ERROR,
error: true
});
});
});

describe('RECEIVE', () => {
const EXPECTED_NAME = `${PREFIX}/RECEIVE`;

test('it returns the proper action type', () => {
expect(actions.setCurrentPage.receive.toString()).toBe(
EXPECTED_NAME
);
});

test('it returns a proper action object', () => {
expect(actions.setCurrentPage.receive(MOCK_PAYLOAD)).toEqual({
type: EXPECTED_NAME,
payload: MOCK_PAYLOAD
});

expect(actions.setCurrentPage.receive(ERROR)).toEqual({
type: EXPECTED_NAME,
payload: ERROR,
error: true
});
});
});
});

describe('setPrevPageTotal', () => {
const PREFIX = 'CATALOG/SET_PREV_PAGE_TOTAL';

describe('REQUEST', () => {
const EXPECTED_NAME = `${PREFIX}/REQUEST`;

test('it returns the proper action type', () => {
expect(actions.setPrevPageTotal.request.toString()).toBe(
EXPECTED_NAME
);
});

test('it returns a proper action object', () => {
expect(actions.setPrevPageTotal.request(MOCK_PAYLOAD)).toEqual({
type: EXPECTED_NAME,
payload: MOCK_PAYLOAD
});

expect(actions.setPrevPageTotal.request(ERROR)).toEqual({
type: EXPECTED_NAME,
payload: ERROR,
error: true
});
});
});

describe('RECEIVE', () => {
const EXPECTED_NAME = `${PREFIX}/RECEIVE`;

test('it returns the proper action type', () => {
expect(actions.setPrevPageTotal.receive.toString()).toBe(
EXPECTED_NAME
);
});

test('it returns a proper action object', () => {
expect(actions.setPrevPageTotal.receive(MOCK_PAYLOAD)).toEqual({
type: EXPECTED_NAME,
payload: MOCK_PAYLOAD
});

expect(actions.setPrevPageTotal.receive(ERROR)).toEqual({
type: EXPECTED_NAME,
payload: ERROR,
error: true
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { dispatch, getState } from 'src/store';
import actions from '../actions';
import mockData from '../mockData';
import {
getAllCategories,
setCurrentPage,
setPrevPageTotal
} from '../asyncActions';

jest.mock('src/store');

const thunkArgs = [dispatch, getState];

afterEach(() => {
dispatch.mockClear();
});

describe('getAllCategories', () => {
test('it returns a thunk', () => {
expect(getAllCategories()).toBeInstanceOf(Function);
});

test('its thunk returns undefined', async () => {
const result = await getAllCategories()(...thunkArgs);

expect(result).toBeUndefined();
});

test('its thunk dispatches actions', async () => {
await getAllCategories()(...thunkArgs);

expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(
1,
actions.getAllCategories.request()
);
expect(dispatch).toHaveBeenNthCalledWith(
2,
actions.getAllCategories.receive(mockData)
);
});
});

describe('setCurrentPage', () => {
const PAYLOAD = 2;

test('it returns a thunk', () => {
expect(setCurrentPage(PAYLOAD)).toBeInstanceOf(Function);
});

test('its thunk returns undefined', async () => {
const result = await setCurrentPage(PAYLOAD)(...thunkArgs);

expect(result).toBeUndefined();
});

test('its thunk dispatches actions', async () => {
await setCurrentPage(PAYLOAD)(...thunkArgs);

expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenNthCalledWith(
1,
actions.setCurrentPage.receive(PAYLOAD)
);
});
});

describe('setPrevPageTotal', () => {
const PAYLOAD = 10;

test('it returns a thunk', () => {
expect(setPrevPageTotal(PAYLOAD)).toBeInstanceOf(Function);
});

test('its thunk returns undefined', async () => {
const result = await setPrevPageTotal(PAYLOAD)(...thunkArgs);

expect(result).toBeUndefined();
});

test('its thunk dispatches actions', async () => {
await setPrevPageTotal(PAYLOAD)(...thunkArgs);

expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenNthCalledWith(
1,
actions.setPrevPageTotal.receive(PAYLOAD)
);
});
});
1 change: 0 additions & 1 deletion packages/venia-concept/src/actions/catalog/asyncActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export const getAllCategories = () =>
export const setCurrentPage = payload =>
async function thunk(dispatch) {
dispatch(actions.setCurrentPage.receive(payload));
window.scrollTo(0, 0);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scrolling the window is a concern of the UI, not the action handler. See the corresponding change in category.js.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having window.scrollTo() in this particular action was a bit arbitrary—it only covered this one use case—but I don't actually think there's anything wrong with it being part of an async action. Ultimately, async actions are a natural place for side effects to live, and scrolling the window is just a side effect.

};

export const setPrevPageTotal = payload =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ class Pagination extends Component {

const queryPage = Math.max(
1,
// Note: The ~ operator is a bitwise NOT operator.
// Bitwise NOTing any number x yields -(x + 1). For example, ~-5 yields 4.
// Importantly, it truncates any fractional component of x. For example, ~-5.7 also yields 4.
// For positive numbers, applying this operator twice has the same effect as Math.floor.
~~getQueryParameterValue({
location,
queryParameter: 'page'
Expand Down