chore: Add missing frontend specs (#2329)

* complete dshboard api specs

* code cleanup

* add conversation mixin spec

* add isadmin mixin spec

* add agent details component spec

* add notification badge spec

* spec for thumbnail exist in agent details

* fix the deprecation warnings

* add agent details spec

* add account selector specs

* code cleanup

* refactor contact spec

* review fixes

* review fixes

* add shared spec helper

* update api spec helper

* review fixes
This commit is contained in:
Muhsin Keloth 2021-05-25 14:00:21 +05:30 committed by GitHub
parent 652d988993
commit cfdf4a12c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 1009 additions and 71 deletions

View file

@ -0,0 +1,26 @@
import accountAPI from '../account';
import ApiClient from '../ApiClient';
import describeWithAPIMock from './apiSpecHelper';
describe('#accountAPI', () => {
it('creates correct instance', () => {
expect(accountAPI).toBeInstanceOf(ApiClient);
expect(accountAPI).toHaveProperty('get');
expect(accountAPI).toHaveProperty('show');
expect(accountAPI).toHaveProperty('create');
expect(accountAPI).toHaveProperty('update');
expect(accountAPI).toHaveProperty('delete');
expect(accountAPI).toHaveProperty('createAccount');
});
describeWithAPIMock('API calls', context => {
it('#createAccount', () => {
accountAPI.createAccount({
name: 'Chatwoot',
});
expect(context.axiosMock.post).toHaveBeenCalledWith('/api/v1/accounts', {
name: 'Chatwoot',
});
});
});
});

View file

@ -0,0 +1,26 @@
function apiSpecHelper() {
beforeEach(() => {
this.originalAxios = window.axios;
this.axiosMock = {
post: jest.fn(() => Promise.resolve()),
get: jest.fn(() => Promise.resolve()),
patch: jest.fn(() => Promise.resolve()),
};
window.axios = this.axiosMock;
});
afterEach(() => {
window.axios = this.originalAxios;
});
}
// https://stackoverflow.com/a/59344023/3901856
const sharedWrapper = describe('sharedWrapper', () => {});
export default function describeWithAPIMock(skillName, testFn) {
return describe(skillName, function configureContext() {
function Context() {}
Context.prototype = sharedWrapper.ctx;
this.ctx = new Context();
apiSpecHelper.call(this);
testFn.call(this, this);
});
}

View file

@ -1,5 +1,6 @@
import fbChannel from '../../channel/fbChannel';
import ApiClient from '../../ApiClient';
import describeWithAPIMock from '../apiSpecHelper';
describe('#FBChannel', () => {
it('creates correct instance', () => {
@ -10,4 +11,29 @@ describe('#FBChannel', () => {
expect(fbChannel).toHaveProperty('update');
expect(fbChannel).toHaveProperty('delete');
});
describeWithAPIMock('API calls', context => {
it('#create', () => {
fbChannel.create({ omniauthToken: 'ASFM131CSF@#@$', appId: 'chatwoot' });
expect(context.axiosMock.post).toHaveBeenCalledWith(
'/api/v1/callbacks/register_facebook_page',
{
omniauthToken: 'ASFM131CSF@#@$',
appId: 'chatwoot',
}
);
});
it('#reauthorize', () => {
fbChannel.reauthorizeFacebookPage({
omniauthToken: 'ASFM131CSF@#@$',
inboxId: 1,
});
expect(context.axiosMock.post).toHaveBeenCalledWith(
'/api/v1/callbacks/reauthorize_page',
{
omniauth_token: 'ASFM131CSF@#@$',
inbox_id: 1,
}
);
});
});
});

View file

@ -0,0 +1,13 @@
import twilioChannel from '../../channel/twilioChannel';
import ApiClient from '../../ApiClient';
describe('#twilioChannel', () => {
it('creates correct instance', () => {
expect(twilioChannel).toBeInstanceOf(ApiClient);
expect(twilioChannel).toHaveProperty('get');
expect(twilioChannel).toHaveProperty('show');
expect(twilioChannel).toHaveProperty('create');
expect(twilioChannel).toHaveProperty('update');
expect(twilioChannel).toHaveProperty('delete');
});
});

View file

@ -1,9 +1,14 @@
import TwitterClient from '../../channel/twitterClient';
import twitterClient from '../../channel/twitterClient';
import ApiClient from '../../ApiClient';
describe('#TwitterClient', () => {
it('creates correct instance', () => {
expect(TwitterClient).toBeInstanceOf(ApiClient);
expect(TwitterClient).toHaveProperty('generateAuthorization');
expect(twitterClient).toBeInstanceOf(ApiClient);
expect(twitterClient).toHaveProperty('get');
expect(twitterClient).toHaveProperty('show');
expect(twitterClient).toHaveProperty('create');
expect(twitterClient).toHaveProperty('update');
expect(twitterClient).toHaveProperty('delete');
expect(twitterClient).toHaveProperty('generateAuthorization');
});
});

View file

@ -0,0 +1,13 @@
import webChannelClient from '../../channel/webChannel';
import ApiClient from '../../ApiClient';
describe('#webChannelClient', () => {
it('creates correct instance', () => {
expect(webChannelClient).toBeInstanceOf(ApiClient);
expect(webChannelClient).toHaveProperty('get');
expect(webChannelClient).toHaveProperty('show');
expect(webChannelClient).toHaveProperty('create');
expect(webChannelClient).toHaveProperty('update');
expect(webChannelClient).toHaveProperty('delete');
});
});

View file

@ -1,14 +1,44 @@
import contacts from '../contacts';
import contactAPI from '../contacts';
import ApiClient from '../ApiClient';
import describeWithAPIMock from './apiSpecHelper';
describe('#ContactsAPI', () => {
it('creates correct instance', () => {
expect(contacts).toBeInstanceOf(ApiClient);
expect(contacts).toHaveProperty('get');
expect(contacts).toHaveProperty('show');
expect(contacts).toHaveProperty('create');
expect(contacts).toHaveProperty('update');
expect(contacts).toHaveProperty('delete');
expect(contacts).toHaveProperty('getConversations');
expect(contactAPI).toBeInstanceOf(ApiClient);
expect(contactAPI).toHaveProperty('get');
expect(contactAPI).toHaveProperty('show');
expect(contactAPI).toHaveProperty('create');
expect(contactAPI).toHaveProperty('update');
expect(contactAPI).toHaveProperty('delete');
expect(contactAPI).toHaveProperty('getConversations');
});
describeWithAPIMock('API calls', context => {
it('#get', () => {
contactAPI.get(1, 'name');
expect(context.axiosMock.get).toHaveBeenCalledWith(
'/api/v1/contacts?page=1&sort=name'
);
});
it('#getConversations', () => {
contactAPI.getConversations(1);
expect(context.axiosMock.get).toHaveBeenCalledWith(
'/api/v1/contacts/1/conversations'
);
});
it('#getContactableInboxes', () => {
contactAPI.getContactableInboxes(1);
expect(context.axiosMock.get).toHaveBeenCalledWith(
'/api/v1/contacts/1/contactable_inboxes'
);
});
it('#search', () => {
contactAPI.search('leads', 1, 'date');
expect(context.axiosMock.get).toHaveBeenCalledWith(
'/api/v1/contacts/search?q=leads&page=1&sort=date'
);
});
});
});

View file

@ -1,15 +1,36 @@
import conversations from '../conversations';
import conversationsAPI from '../conversations';
import ApiClient from '../ApiClient';
import describeWithAPIMock from './apiSpecHelper';
describe('#ConversationApi', () => {
it('creates correct instance', () => {
expect(conversations).toBeInstanceOf(ApiClient);
expect(conversations).toHaveProperty('get');
expect(conversations).toHaveProperty('show');
expect(conversations).toHaveProperty('create');
expect(conversations).toHaveProperty('update');
expect(conversations).toHaveProperty('delete');
expect(conversations).toHaveProperty('getLabels');
expect(conversations).toHaveProperty('updateLabels');
expect(conversationsAPI).toBeInstanceOf(ApiClient);
expect(conversationsAPI).toHaveProperty('get');
expect(conversationsAPI).toHaveProperty('show');
expect(conversationsAPI).toHaveProperty('create');
expect(conversationsAPI).toHaveProperty('update');
expect(conversationsAPI).toHaveProperty('delete');
expect(conversationsAPI).toHaveProperty('getLabels');
expect(conversationsAPI).toHaveProperty('updateLabels');
});
describeWithAPIMock('API calls', context => {
it('#getLabels', () => {
conversationsAPI.getLabels(1);
expect(context.axiosMock.get).toHaveBeenCalledWith(
'/api/v1/conversations/1/labels'
);
});
it('#updateLabels', () => {
const labels = ['support-query'];
conversationsAPI.updateLabels(1, labels);
expect(context.axiosMock.post).toHaveBeenCalledWith(
'/api/v1/conversations/1/labels',
{
labels,
}
);
});
});
});

View file

@ -0,0 +1,13 @@
import endPoints from '../endPoints';
describe('#endPoints', () => {
it('it should return register url details if register page passed ', () => {
expect(endPoints('register')).toEqual({ url: 'api/v1/accounts.json' });
});
it('it should inbox url details if getInbox page passed', () => {
expect(endPoints('getInbox')).toEqual({
url: 'api/v1/conversations.json',
params: { inbox_id: null },
});
});
});

View file

@ -1,5 +1,6 @@
import conversationAPI from '../../inbox/conversation';
import ApiClient from '../../ApiClient';
import describeWithAPIMock from '../apiSpecHelper';
describe('#ConversationAPI', () => {
it('creates correct instance', () => {
@ -20,27 +21,143 @@ describe('#ConversationAPI', () => {
expect(conversationAPI).toHaveProperty('sendEmailTranscript');
});
describe('API calls', () => {
let originalAxios = null;
let axiosMock = null;
beforeEach(() => {
originalAxios = window.axios;
axiosMock = { post: jest.fn(() => Promise.resolve()) };
window.axios = axiosMock;
describeWithAPIMock('API calls', context => {
it('#get conversations', () => {
conversationAPI.get({
inboxId: 1,
status: 'open',
assigneeType: 'me',
page: 1,
labels: [],
teamId: 1,
});
expect(context.axiosMock.get).toHaveBeenCalledWith(
'/api/v1/conversations',
{
params: {
inbox_id: 1,
team_id: 1,
status: 'open',
assignee_type: 'me',
page: 1,
labels: [],
},
}
);
});
afterEach(() => {
window.axios = originalAxios;
it('#search', () => {
conversationAPI.search({
q: 'leads',
page: 1,
});
expect(context.axiosMock.get).toHaveBeenCalledWith(
'/api/v1/conversations/search',
{
params: {
q: 'leads',
page: 1,
},
}
);
});
it('#toggleStatus', () => {
conversationAPI.toggleStatus({ conversationId: 12, status: 'online' });
expect(context.axiosMock.post).toHaveBeenCalledWith(
`/api/v1/conversations/12/toggle_status`,
{
status: 'online',
}
);
});
it('#assignAgent', () => {
conversationAPI.assignAgent({ conversationId: 12, agentId: 34 });
expect(context.axiosMock.post).toHaveBeenCalledWith(
`/api/v1/conversations/12/assignments?assignee_id=34`,
{}
);
});
it('#assignTeam', () => {
conversationAPI.assignTeam({ conversationId: 12, teamId: 1 });
expect(context.axiosMock.post).toHaveBeenCalledWith(
`/api/v1/conversations/12/assignments`,
{
team_id: 1,
}
);
});
it('#markMessageRead', () => {
conversationAPI.markMessageRead({ id: 12 });
expect(context.axiosMock.post).toHaveBeenCalledWith(
`/api/v1/conversations/12/update_last_seen`
);
});
it('#toggleTyping', () => {
conversationAPI.toggleTyping({
conversationId: 12,
status: 'typing_on',
});
expect(context.axiosMock.post).toHaveBeenCalledWith(
`/api/v1/conversations/12/toggle_typing_status`,
{
typing_status: 'typing_on',
}
);
});
it('#mute', () => {
conversationAPI.mute(45);
expect(context.axiosMock.post).toHaveBeenCalledWith(
'/api/v1/conversations/45/mute'
);
});
it('#unmute', () => {
conversationAPI.unmute(45);
expect(axiosMock.post).toHaveBeenCalledWith(
expect(context.axiosMock.post).toHaveBeenCalledWith(
'/api/v1/conversations/45/unmute'
);
});
it('#meta', () => {
conversationAPI.meta({
inboxId: 1,
status: 'open',
assigneeType: 'me',
labels: [],
teamId: 1,
});
expect(context.axiosMock.get).toHaveBeenCalledWith(
'/api/v1/conversations/meta',
{
params: {
inbox_id: 1,
team_id: 1,
status: 'open',
assignee_type: 'me',
labels: [],
},
}
);
});
it('#sendEmailTranscript', () => {
conversationAPI.sendEmailTranscript({
conversationId: 45,
email: 'john@acme.inc',
});
expect(context.axiosMock.post).toHaveBeenCalledWith(
'/api/v1/conversations/45/transcript',
{
email: 'john@acme.inc',
}
);
});
});
});

View file

@ -0,0 +1,32 @@
import messageAPI from '../../inbox/message';
import ApiClient from '../../ApiClient';
import describeWithAPIMock from '../apiSpecHelper';
describe('#ConversationAPI', () => {
it('creates correct instance', () => {
expect(messageAPI).toBeInstanceOf(ApiClient);
expect(messageAPI).toHaveProperty('get');
expect(messageAPI).toHaveProperty('show');
expect(messageAPI).toHaveProperty('create');
expect(messageAPI).toHaveProperty('update');
expect(messageAPI).toHaveProperty('delete');
expect(messageAPI).toHaveProperty('getPreviousMessages');
});
describeWithAPIMock('API calls', context => {
it('#getPreviousMessages', () => {
messageAPI.getPreviousMessages({
conversationId: 12,
before: 4573,
});
expect(context.axiosMock.get).toHaveBeenCalledWith(
`/api/v1/conversations/12/messages`,
{
params: {
before: 4573,
},
}
);
});
});
});

View file

@ -1,13 +1,31 @@
import inboxes from '../inboxes';
import inboxesAPI from '../inboxes';
import ApiClient from '../ApiClient';
import describeWithAPIMock from './apiSpecHelper';
describe('#InboxesAPI', () => {
it('creates correct instance', () => {
expect(inboxes).toBeInstanceOf(ApiClient);
expect(inboxes).toHaveProperty('get');
expect(inboxes).toHaveProperty('show');
expect(inboxes).toHaveProperty('create');
expect(inboxes).toHaveProperty('update');
expect(inboxes).toHaveProperty('delete');
expect(inboxesAPI).toBeInstanceOf(ApiClient);
expect(inboxesAPI).toHaveProperty('get');
expect(inboxesAPI).toHaveProperty('show');
expect(inboxesAPI).toHaveProperty('create');
expect(inboxesAPI).toHaveProperty('update');
expect(inboxesAPI).toHaveProperty('delete');
expect(inboxesAPI).toHaveProperty('getAssignableAgents');
expect(inboxesAPI).toHaveProperty('getCampaigns');
});
describeWithAPIMock('API calls', context => {
it('#getAssignableAgents', () => {
inboxesAPI.getAssignableAgents(1);
expect(context.axiosMock.get).toHaveBeenCalledWith(
'/api/v1/inboxes/1/assignable_agents'
);
});
it('#getCampaigns', () => {
inboxesAPI.getCampaigns(2);
expect(context.axiosMock.get).toHaveBeenCalledWith(
'/api/v1/inboxes/2/campaigns'
);
});
});
});

View file

@ -1,13 +1,54 @@
import notifications from '../notifications';
import notificationsAPI from '../notifications';
import ApiClient from '../ApiClient';
import describeWithAPIMock from './apiSpecHelper';
describe('#NotificationAPI', () => {
it('creates correct instance', () => {
expect(notifications).toBeInstanceOf(ApiClient);
expect(notifications).toHaveProperty('get');
expect(notifications).toHaveProperty('getNotifications');
expect(notifications).toHaveProperty('getUnreadCount');
expect(notifications).toHaveProperty('read');
expect(notifications).toHaveProperty('readAll');
expect(notificationsAPI).toBeInstanceOf(ApiClient);
expect(notificationsAPI).toHaveProperty('get');
expect(notificationsAPI).toHaveProperty('getNotifications');
expect(notificationsAPI).toHaveProperty('getUnreadCount');
expect(notificationsAPI).toHaveProperty('read');
expect(notificationsAPI).toHaveProperty('readAll');
});
describeWithAPIMock('API calls', context => {
it('#get', () => {
notificationsAPI.get(1);
expect(context.axiosMock.get).toHaveBeenCalledWith(
'/api/v1/notifications?page=1'
);
});
it('#getNotifications', () => {
notificationsAPI.getNotifications(1);
expect(context.axiosMock.get).toHaveBeenCalledWith(
'/api/v1/notifications/1/notifications'
);
});
it('#getUnreadCount', () => {
notificationsAPI.getUnreadCount();
expect(context.axiosMock.get).toHaveBeenCalledWith(
'/api/v1/notifications/unread_count'
);
});
it('#read', () => {
notificationsAPI.read(48670, 'Conversation');
expect(context.axiosMock.post).toHaveBeenCalledWith(
'/api/v1/notifications/read_all',
{
primary_actor_id: 'Conversation',
primary_actor_type: 48670,
}
);
});
it('#readAll', () => {
notificationsAPI.readAll();
expect(context.axiosMock.post).toHaveBeenCalledWith(
'/api/v1/notifications/read_all'
);
});
});
});

View file

@ -1,17 +1,63 @@
import reports from '../reports';
import reportsAPI from '../reports';
import ApiClient from '../ApiClient';
import describeWithAPIMock from './apiSpecHelper';
describe('#Reports API', () => {
it('creates correct instance', () => {
expect(reports).toBeInstanceOf(ApiClient);
expect(reports.apiVersion).toBe('/api/v2');
expect(reports).toHaveProperty('get');
expect(reports).toHaveProperty('show');
expect(reports).toHaveProperty('create');
expect(reports).toHaveProperty('update');
expect(reports).toHaveProperty('delete');
expect(reports).toHaveProperty('getAccountReports');
expect(reports).toHaveProperty('getAccountSummary');
expect(reports).toHaveProperty('getAgentReports');
expect(reportsAPI).toBeInstanceOf(ApiClient);
expect(reportsAPI.apiVersion).toBe('/api/v2');
expect(reportsAPI).toHaveProperty('get');
expect(reportsAPI).toHaveProperty('show');
expect(reportsAPI).toHaveProperty('create');
expect(reportsAPI).toHaveProperty('update');
expect(reportsAPI).toHaveProperty('delete');
expect(reportsAPI).toHaveProperty('getAccountReports');
expect(reportsAPI).toHaveProperty('getAccountSummary');
expect(reportsAPI).toHaveProperty('getAgentReports');
});
describeWithAPIMock('API calls', context => {
it('#getAccountReports', () => {
reportsAPI.getAccountReports(
'conversations_count',
1621103400,
1621621800
);
expect(context.axiosMock.get).toHaveBeenCalledWith(
'/api/v2/reports/account',
{
params: {
metric: 'conversations_count',
since: 1621103400,
until: 1621621800,
},
}
);
});
it('#getAccountSummary', () => {
reportsAPI.getAccountSummary(1621103400, 1621621800);
expect(context.axiosMock.get).toHaveBeenCalledWith(
'/api/v2/reports/account_summary',
{
params: {
since: 1621103400,
until: 1621621800,
},
}
);
});
it('#getAgentReports', () => {
reportsAPI.getAgentReports(1621103400, 1621621800);
expect(context.axiosMock.get).toHaveBeenCalledWith(
'/api/v2/reports/agents',
{
params: {
since: 1621103400,
until: 1621621800,
},
}
);
});
});
});

View file

@ -1,16 +1,49 @@
import teams from '../teams';
import teamsAPI from '../teams';
import ApiClient from '../ApiClient';
import describeWithAPIMock from './apiSpecHelper';
describe('#TeamsAPI', () => {
it('creates correct instance', () => {
expect(teams).toBeInstanceOf(ApiClient);
expect(teams).toHaveProperty('get');
expect(teams).toHaveProperty('show');
expect(teams).toHaveProperty('create');
expect(teams).toHaveProperty('update');
expect(teams).toHaveProperty('delete');
expect(teams).toHaveProperty('getAgents');
expect(teams).toHaveProperty('addAgents');
expect(teams).toHaveProperty('updateAgents');
expect(teamsAPI).toBeInstanceOf(ApiClient);
expect(teamsAPI).toHaveProperty('get');
expect(teamsAPI).toHaveProperty('show');
expect(teamsAPI).toHaveProperty('create');
expect(teamsAPI).toHaveProperty('update');
expect(teamsAPI).toHaveProperty('delete');
expect(teamsAPI).toHaveProperty('getAgents');
expect(teamsAPI).toHaveProperty('addAgents');
expect(teamsAPI).toHaveProperty('updateAgents');
});
describeWithAPIMock('API calls', context => {
it('#getAgents', () => {
teamsAPI.getAgents({ teamId: 1 });
expect(context.axiosMock.get).toHaveBeenCalledWith(
'/api/v1/teams/1/team_members'
);
});
it('#addAgents', () => {
teamsAPI.addAgents({ teamId: 1, agentsList: { user_ids: [1, 10, 21] } });
expect(context.axiosMock.post).toHaveBeenCalledWith(
'/api/v1/teams/1/team_members',
{
user_ids: { user_ids: [1, 10, 21] },
}
);
});
it('#updateAgents', () => {
const agentsList = { user_ids: [1, 10, 21] };
teamsAPI.updateAgents({
teamId: 1,
agentsList,
});
expect(context.axiosMock.patch).toHaveBeenCalledWith(
'/api/v1/teams/1/team_members',
{
user_ids: agentsList,
}
);
});
});
});

View file

@ -0,0 +1,13 @@
import webhooksAPI from '../webhooks';
import ApiClient from '../ApiClient';
describe('#webhooksAPI', () => {
it('creates correct instance', () => {
expect(webhooksAPI).toBeInstanceOf(ApiClient);
expect(webhooksAPI).toHaveProperty('get');
expect(webhooksAPI).toHaveProperty('show');
expect(webhooksAPI).toHaveProperty('create');
expect(webhooksAPI).toHaveProperty('update');
expect(webhooksAPI).toHaveProperty('delete');
});
});