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

Tests/user api tests #554

Merged
merged 2 commits into from
Nov 17, 2023
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
200 changes: 200 additions & 0 deletions src/modules/directory/modules/users/api/__tests__/users.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
import axios from 'axios';
import UsersAPI from '../users';

// axios mock should be copy-pasted :(
// https://stackoverflow.com/questions/65554910/jest-referenceerror-cannot-access-before-initialization
vi.mock('axios', () => {
return {
default: {
post: vi.fn(),
get: vi.fn(),
delete: vi.fn(),
put: vi.fn(),
patch: vi.fn(),
create: vi.fn().mockReturnThis(),
interceptors: {
request: {
use: vi.fn(), eject: vi.fn(),
}, response: {
use: vi.fn(), eject: vi.fn(),
},
},
},
};
});

describe('UsersAPI', () => {
it('correctly computes "getList" method api call', async () => {
const inputParams = {
fields: ['id', 'name', 'vitest'],
};
const url = '/users?fields=id&fields=name&fields=vitest&page=1&size=10';
const mock = axios.get.mockImplementationOnce(() => Promise.resolve({
data: {},
}));
await UsersAPI.getList(inputParams);
expect(mock).toHaveBeenCalledWith(url);
});

it('correctly computes "getList" method output', async () => {
const output = {
items: [
{
dnd: false,
id: 1,
name: '',
state: true,
status: '',
shouldCaseConvert: '',
},
], next: true,
};

const response = {
data: {
items: [
{ id: 1, should_case_convert: '' },
], next: true,
},
};
axios.get.mockImplementationOnce(() => Promise.resolve(response));
expect(await UsersAPI.getList({})).toEqual(output);
});

it('correctly computes "get" method api call', async () => {
const inputParams = {
itemId: 1,
};
const url = '/users/1';
const mock = axios.get.mockImplementationOnce(() => Promise.resolve({
data: {},
}));
await UsersAPI.get(inputParams);
expect(mock).toHaveBeenCalledWith(url);
});

it('correctly computes "get" method output', async () => {
const output = {
id: 1, device: {}, devices: [], license: [], roles: [], variables: [
{
key: '', value: '',
},
],
};

const response = {
data: {
id: 1,
},
};
axios.get.mockImplementationOnce(() => Promise.resolve(response));
expect(await UsersAPI.get({})).toEqual(output);
});

it('correctly computes "add" method api call', async () => {
const input = {
itemInstance: {
name: 'test',
},
};

const body = {
name: 'test',
profile: {}, // variables field (?)
};

const url = '/users';
const mock = axios.post.mockImplementationOnce(() => Promise.resolve({
data: {},
}));
await UsersAPI.add(input);
expect(mock).toHaveBeenCalledWith(url, body);
});

it('correctly computes "add" method output', async () => {
const output = {
id: 1,
checkCase: '',
};

const response = {
data: {
id: 1,
check_case: '',
},
};
axios.post.mockImplementationOnce(() => Promise.resolve(response));
expect(await UsersAPI.add({ itemInstance: {} })).toEqual(output);
});

it('correctly computes "update" method api call', async () => {
const input = {
itemInstance: {
name: 'test',
},
itemId: 1,
};

const body = {
name: 'test',
profile: {}, // variables field (?)
};

const url = '/users/1';
const mock = axios.put.mockImplementationOnce(() => Promise.resolve({
data: {},
}));
await UsersAPI.update(input);
expect(mock).toHaveBeenCalledWith(url, body);
});

it('correctly computes "update" method output', async () => {
const output = {
id: 1,
checkCase: '',
};

const response = {
data: {
id: 1,
check_case: '',
},
};
axios.put.mockImplementationOnce(() => Promise.resolve(response));
expect(await UsersAPI.update({ itemInstance: {}, itemId: 1 }))
.toEqual(output);
});

it('correctly computes "patch" method api call', async () => {
const input = {
changes: {
name: 'test',
},
id: 1,
};

const body = {
name: 'test',
};

const url = '/users/1';
const mock = axios.patch.mockImplementationOnce(() => Promise.resolve({
data: {},
}));
await UsersAPI.patch(input);
expect(mock).toHaveBeenCalledWith(url, body);
});

it('correctly computes "delete" method api call', async () => {
const input = {
id: 1,
};

const url = '/users/1?permanent=true';
const mock = axios.delete.mockImplementationOnce(() => Promise.resolve({
data: {},
}));
await UsersAPI.delete(input);
expect(mock).toHaveBeenCalledWith(url);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import axios from 'axios';
import AgentSkillsAPI from '../agentSkills';

// axios mock should be copy-pasted :(
// https://stackoverflow.com/questions/65554910/jest-referenceerror-cannot-access-before-initialization
vi.mock('axios', () => {
return {
default: {
post: vi.fn(),
get: vi.fn(),
delete: vi.fn(),
put: vi.fn(),
patch: vi.fn(),
request: vi.fn(),
create: vi.fn().mockReturnThis(),
interceptors: {
request: {
use: vi.fn(), eject: vi.fn(),
}, response: {
use: vi.fn(), eject: vi.fn(),
},
},
},
};
});

describe('AgentSkillsAPI', () => {
beforeEach(() => {
axios.request.mockClear();
});

it('correctly computes "getList" method api call', async () => {
const inputParams = {
fields: ['id', 'name', 'vitest'],
};
const url = '/call_center/skills?page=1&size=10&fields=id&fields=name&fields=vitest';
const mock = axios.request.mockImplementationOnce(() => Promise.resolve({
data: {},
}));
await AgentSkillsAPI.getList(inputParams);
// https://stackoverflow.com/a/41939921
expect(mock.mock.calls[0][0].url).toBe(url);
});

it('correctly computes "getList" method output', async () => {
const output = {
items: [
{
id: 1,
shouldCaseConvert: '',
},
], next: true,
};

const response = {
data: {
items: [
{ id: 1, should_case_convert: '' },
], next: true,
},
};
axios.request.mockImplementationOnce(() => Promise.resolve(response));
expect(await AgentSkillsAPI.getList({})).toEqual(output);
});

it('correctly computes "get" method api call', async () => {
const inputParams = {
itemId: 1,
};
const url = '/call_center/skills/1';
const mock = axios.request.mockImplementationOnce(() => Promise.resolve({
data: {},
}));
await AgentSkillsAPI.get(inputParams);
expect(mock.mock.calls[0][0].url).toBe(url);
});

it('correctly computes "get" method output', async () => {
const output = {
id: 1,
};

const response = {
data: {
id: 1,
},
};
axios.request.mockImplementationOnce(() => Promise.resolve(response));
expect(await AgentSkillsAPI.get({ itemId: 1 })).toEqual(output);
});

it('correctly computes "add" method api call', async () => {
const input = {
itemInstance: {
name: 'test',
},
};

const body = {
name: 'test',
};

const mock = axios.request.mockImplementationOnce(() => Promise.resolve({
data: {},
}));
await AgentSkillsAPI.add(input);
expect(mock.mock.calls[0][0].data).toBe(JSON.stringify(body));
});

it('correctly computes "add" method output', async () => {
const output = {
id: 1,
checkCase: '',
};

const response = {
data: {
id: 1,
check_case: '',
},
};
axios.request.mockImplementationOnce(() => Promise.resolve(response));
expect(await AgentSkillsAPI.add({ itemInstance: {} })).toEqual(output);
});

it('correctly computes "update" method api call', async () => {
const input = {
itemInstance: {
name: 'test',
},
itemId: 1,
};

const body = {
name: 'test',
};

const mock = axios.request.mockImplementationOnce(() => Promise.resolve({
data: {},
}));
await AgentSkillsAPI.update(input);
expect(mock.mock.calls[0][0].data).toBe(JSON.stringify(body));
});

it('correctly computes "update" method output', async () => {
const output = {
id: 1,
checkCase: '',
};

const response = {
data: {
id: 1,
check_case: '',
},
};
axios.request.mockImplementationOnce(() => Promise.resolve(response));
expect(await AgentSkillsAPI.update({ itemInstance: {}, itemId: 1 }))
.toEqual(output);
});

it('correctly computes "delete" method api call', async () => {
const input = {
id: 1,
};

const url = '/call_center/skills/1';
const mock = axios.request.mockImplementationOnce(() => Promise.resolve({
data: {},
}));
await AgentSkillsAPI.delete(input);
expect(mock.mock.calls[0][0].url).toBe(url);
});
});