2022-07-27 06:59:11 +00:00
|
|
|
import axios from 'axios';
|
|
|
|
import { actions } from '../actions';
|
|
|
|
import * as types from '../../../mutation-types';
|
|
|
|
const articleList = [
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
category_id: 1,
|
|
|
|
title: 'Documents are required to complete KYC',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
const commit = jest.fn();
|
2022-08-18 06:15:08 +00:00
|
|
|
const dispatch = jest.fn();
|
2022-07-27 06:59:11 +00:00
|
|
|
global.axios = axios;
|
|
|
|
jest.mock('axios');
|
|
|
|
|
|
|
|
describe('#actions', () => {
|
|
|
|
describe('#index', () => {
|
|
|
|
it('sends correct actions if API is success', async () => {
|
2022-08-02 11:44:10 +00:00
|
|
|
axios.get.mockResolvedValue({
|
|
|
|
data: {
|
|
|
|
payload: articleList,
|
|
|
|
meta: {
|
|
|
|
current_page: '1',
|
|
|
|
articles_count: 5,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
await actions.index(
|
|
|
|
{ commit },
|
|
|
|
{ pageNumber: 1, portalSlug: 'test', locale: 'en' }
|
|
|
|
);
|
2022-07-27 06:59:11 +00:00
|
|
|
expect(commit.mock.calls).toEqual([
|
|
|
|
[types.default.SET_UI_FLAG, { isFetching: true }],
|
2022-08-02 11:44:10 +00:00
|
|
|
[types.default.CLEAR_ARTICLES],
|
2022-07-27 06:59:11 +00:00
|
|
|
[
|
|
|
|
types.default.ADD_MANY_ARTICLES,
|
|
|
|
[
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
category_id: 1,
|
|
|
|
title: 'Documents are required to complete KYC',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
2022-08-02 11:44:10 +00:00
|
|
|
[
|
|
|
|
types.default.SET_ARTICLES_META,
|
|
|
|
{ current_page: '1', articles_count: 5 },
|
|
|
|
],
|
2022-07-27 06:59:11 +00:00
|
|
|
[types.default.ADD_MANY_ARTICLES_ID, [1]],
|
|
|
|
[types.default.SET_UI_FLAG, { isFetching: false }],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
it('sends correct actions if API is error', async () => {
|
|
|
|
axios.get.mockRejectedValue({ message: 'Incorrect header' });
|
2022-08-02 11:44:10 +00:00
|
|
|
await expect(
|
|
|
|
actions.index(
|
|
|
|
{ commit },
|
|
|
|
{ pageNumber: 1, portalSlug: 'test', locale: 'en' }
|
|
|
|
)
|
|
|
|
).rejects.toThrow(Error);
|
2022-07-27 06:59:11 +00:00
|
|
|
expect(commit.mock.calls).toEqual([
|
|
|
|
[types.default.SET_UI_FLAG, { isFetching: true }],
|
|
|
|
[types.default.SET_UI_FLAG, { isFetching: false }],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#create', () => {
|
|
|
|
it('sends correct actions if API is success', async () => {
|
2022-08-18 06:15:08 +00:00
|
|
|
axios.post.mockResolvedValue({ data: { payload: articleList[0] } });
|
|
|
|
await actions.create({ commit, dispatch }, articleList[0]);
|
2022-07-27 06:59:11 +00:00
|
|
|
expect(commit.mock.calls).toEqual([
|
|
|
|
[types.default.SET_UI_FLAG, { isCreating: true }],
|
|
|
|
[types.default.ADD_ARTICLE, articleList[0]],
|
|
|
|
[types.default.ADD_ARTICLE_ID, 1],
|
2022-08-18 06:15:08 +00:00
|
|
|
[types.default.ADD_ARTICLE_FLAG, 1],
|
2022-07-27 06:59:11 +00:00
|
|
|
[types.default.SET_UI_FLAG, { isCreating: false }],
|
|
|
|
]);
|
|
|
|
});
|
2022-08-18 06:15:08 +00:00
|
|
|
|
2022-07-27 06:59:11 +00:00
|
|
|
it('sends correct actions if API is error', async () => {
|
|
|
|
axios.post.mockRejectedValue({ message: 'Incorrect header' });
|
|
|
|
await expect(actions.create({ commit }, articleList[0])).rejects.toThrow(
|
|
|
|
Error
|
|
|
|
);
|
|
|
|
expect(commit.mock.calls).toEqual([
|
|
|
|
[types.default.SET_UI_FLAG, { isCreating: true }],
|
|
|
|
[types.default.SET_UI_FLAG, { isCreating: false }],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#update', () => {
|
|
|
|
it('sends correct actions if API is success', async () => {
|
2022-08-16 12:25:34 +00:00
|
|
|
axios.patch.mockResolvedValue({ data: { payload: articleList[0] } });
|
|
|
|
await actions.update(
|
|
|
|
{ commit },
|
|
|
|
{
|
|
|
|
portalSlug: 'room-rental',
|
|
|
|
articleId: 1,
|
|
|
|
title: 'Documents are required to complete KYC',
|
|
|
|
}
|
|
|
|
);
|
2022-07-27 06:59:11 +00:00
|
|
|
expect(commit.mock.calls).toEqual([
|
|
|
|
[
|
2022-08-18 06:15:08 +00:00
|
|
|
types.default.UPDATE_ARTICLE_FLAG,
|
2022-07-27 06:59:11 +00:00
|
|
|
{ uiFlags: { isUpdating: true }, articleId: 1 },
|
|
|
|
],
|
|
|
|
[types.default.UPDATE_ARTICLE, articleList[0]],
|
|
|
|
[
|
2022-08-18 06:15:08 +00:00
|
|
|
types.default.UPDATE_ARTICLE_FLAG,
|
2022-07-27 06:59:11 +00:00
|
|
|
{ uiFlags: { isUpdating: false }, articleId: 1 },
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
it('sends correct actions if API is error', async () => {
|
|
|
|
axios.patch.mockRejectedValue({ message: 'Incorrect header' });
|
2022-08-16 12:25:34 +00:00
|
|
|
await expect(
|
|
|
|
actions.update(
|
|
|
|
{ commit },
|
|
|
|
{
|
|
|
|
portalSlug: 'room-rental',
|
|
|
|
articleId: 1,
|
|
|
|
title: 'Documents are required to complete KYC',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
).rejects.toThrow(Error);
|
|
|
|
|
2022-07-27 06:59:11 +00:00
|
|
|
expect(commit.mock.calls).toEqual([
|
|
|
|
[
|
2022-08-18 06:15:08 +00:00
|
|
|
types.default.UPDATE_ARTICLE_FLAG,
|
2022-07-27 06:59:11 +00:00
|
|
|
{ uiFlags: { isUpdating: true }, articleId: 1 },
|
|
|
|
],
|
|
|
|
[
|
2022-08-18 06:15:08 +00:00
|
|
|
types.default.UPDATE_ARTICLE_FLAG,
|
2022-07-27 06:59:11 +00:00
|
|
|
{ uiFlags: { isUpdating: false }, articleId: 1 },
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#delete', () => {
|
|
|
|
it('sends correct actions if API is success', async () => {
|
|
|
|
axios.delete.mockResolvedValue({ data: articleList[0] });
|
2022-09-01 05:25:59 +00:00
|
|
|
await actions.delete(
|
|
|
|
{ commit },
|
|
|
|
{ portalSlug: 'test', articleId: articleList[0].id }
|
|
|
|
);
|
|
|
|
|
2022-07-27 06:59:11 +00:00
|
|
|
expect(commit.mock.calls).toEqual([
|
|
|
|
[
|
2022-08-18 06:15:08 +00:00
|
|
|
types.default.UPDATE_ARTICLE_FLAG,
|
2022-07-27 06:59:11 +00:00
|
|
|
{ uiFlags: { isDeleting: true }, articleId: 1 },
|
|
|
|
],
|
|
|
|
[types.default.REMOVE_ARTICLE, articleList[0].id],
|
|
|
|
[types.default.REMOVE_ARTICLE_ID, articleList[0].id],
|
|
|
|
[
|
2022-08-18 06:15:08 +00:00
|
|
|
types.default.UPDATE_ARTICLE_FLAG,
|
2022-07-27 06:59:11 +00:00
|
|
|
{ uiFlags: { isDeleting: false }, articleId: 1 },
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
it('sends correct actions if API is error', async () => {
|
|
|
|
axios.delete.mockRejectedValue({ message: 'Incorrect header' });
|
|
|
|
await expect(
|
2022-09-01 05:25:59 +00:00
|
|
|
actions.delete(
|
|
|
|
{ commit },
|
|
|
|
{ portalSlug: 'test', articleId: articleList[0].id }
|
|
|
|
)
|
2022-07-27 06:59:11 +00:00
|
|
|
).rejects.toThrow(Error);
|
|
|
|
expect(commit.mock.calls).toEqual([
|
|
|
|
[
|
2022-08-18 06:15:08 +00:00
|
|
|
types.default.UPDATE_ARTICLE_FLAG,
|
2022-07-27 06:59:11 +00:00
|
|
|
{ uiFlags: { isDeleting: true }, articleId: 1 },
|
|
|
|
],
|
|
|
|
[
|
2022-08-18 06:15:08 +00:00
|
|
|
types.default.UPDATE_ARTICLE_FLAG,
|
2022-07-27 06:59:11 +00:00
|
|
|
{ uiFlags: { isDeleting: false }, articleId: 1 },
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|