feat: Ability for the logged in user to create a new account (#985)

Co-authored-by: Divyesh <dkothari@box8.in>
Co-authored-by: Pranav Raj S <pranav@thoughtwoot.com>
This commit is contained in:
Divyesh Kothari
2020-07-26 12:54:50 +05:30
committed by GitHub
parent 858b72a404
commit 89ed0b425b
13 changed files with 219 additions and 28 deletions

View File

@@ -48,6 +48,18 @@ export const actions = {
throw new Error(error);
}
},
create: async ({ commit }, accountInfo) => {
commit(types.default.SET_ACCOUNT_UI_FLAG, { isCreating: true });
try {
const response = await AccountAPI.createAccount(accountInfo);
const account_id = response.data.data.account_id;
commit(types.default.SET_ACCOUNT_UI_FLAG, { isCreating: false });
return account_id;
} catch (error) {
commit(types.default.SET_ACCOUNT_UI_FLAG, { isCreating: false });
throw error;
}
},
};
export const mutations = {

View File

@@ -8,6 +8,10 @@ const accountData = {
locale: 'en',
};
const newAccountInfo = {
accountName: 'Company two',
};
const commit = jest.fn();
global.axios = axios;
jest.mock('axios');
@@ -53,4 +57,27 @@ describe('#actions', () => {
]);
});
});
describe('#create', () => {
it('sends correct actions if API is success', async () => {
axios.post.mockResolvedValue({
data: { data: { id: 1, name: 'John' } },
});
await actions.create({ commit, getters }, newAccountInfo);
expect(commit.mock.calls).toEqual([
[types.default.SET_ACCOUNT_UI_FLAG, { isCreating: true }],
[types.default.SET_ACCOUNT_UI_FLAG, { isCreating: false }],
]);
});
it('sends correct actions if API is error', async () => {
axios.patch.mockRejectedValue({ message: 'Incorrect header' });
await expect(
actions.create({ commit, getters }, newAccountInfo)
).rejects.toThrow(Error);
expect(commit.mock.calls).toEqual([
[types.default.SET_ACCOUNT_UI_FLAG, { isCreating: true }],
[types.default.SET_ACCOUNT_UI_FLAG, { isCreating: false }],
]);
});
});
});