Fakultas Ilmu Komputer UI

Commit 03c7e009 authored by Sean Zeliq Urian's avatar Sean Zeliq Urian
Browse files

[RED] implement test for diff page (list & tabel)

parent 4f363f59
import React from 'react';
import axios from 'axios';
import renderer from 'react-test-renderer';
import ActivityList from '.';
import { useMainService } from 'services';
import { AppContext } from 'contexts';
import { mount } from 'enzyme';
const dummyLog = {
status: 200,
data: {
username: "Test",
is_admin: false,
previous: true,
next: true,
count: 1,
results: [
{
action_type: "Delete",
model_name: "Account",
object_id: "12346",
recorded_at: "2020-05-31T23:03:35.854615+07:00",
},
]
}
}
jest.mock('axios');
const mockedAxios = axios as jest.Mocked<typeof axios>;
const testProps = {
services: {
main: useMainService('dummyToken'),
},
};
it('ActivityList renders correctly', () => {
const instance = renderer.create(
<ActivityList />
);
expect(instance).toBeTruthy();
});
describe('load logs and generate log messages correctly', () => {
it('Monitoring case', () => {
mockedAxios.request.mockResolvedValue({
status: 200,
data: {
previous: null,
next: null,
count: 1,
results: [
{
action_type: "Create",
model_name: "Monitoring Case",
object_id: "1234",
recorded_at: "2020-05-31T23:03:35.854615+07:00",
},
],
investigation_case: {
case_subject: {
name: "Test"
}
}
}
});
const instance = mount(
<AppContext.Provider value={testProps}>
<ActivityList />
</AppContext.Provider>
);
expect(mockedAxios.request).toBeCalled();
});
it('Investigation case', () => {
mockedAxios.request.mockResolvedValue({
status: 200,
data: {
previous: null,
next: null,
count: 1,
results: [
{
action_type: "Create",
model_name: "Investigation Case",
object_id: "1234",
recorded_at: "2020-05-31T23:03:35.854615+07:00",
},
],
case_subject: {
name: "Test"
}
}
});
const instance = mount(
<AppContext.Provider value={testProps}>
<ActivityList />
</AppContext.Provider>
);
expect(mockedAxios.request).toBeCalled();
});
it('Case subject', () => {
mockedAxios.request.mockResolvedValue({
status: 200,
data: {
previous: null,
next: null,
count: 1,
results: [
{
action_type: "Create",
model_name: "Case Subject",
object_id: "1234",
recorded_at: "2020-05-31T23:03:35.854615+07:00",
},
],
}
});
const instance = mount(
<AppContext.Provider value={testProps}>
<ActivityList />
</AppContext.Provider>
);
expect(mockedAxios.request).toBeCalled();
});
it('Create account', () => {
mockedAxios.request.mockResolvedValue({
status: 200,
data: {
username: "Test",
is_admin: false,
previous: null,
next: null,
count: 1,
results: [
{
action_type: "Create",
model_name: "Account",
object_id: "1234",
recorded_at: "2020-05-31T23:03:35.854615+07:00",
},
]
}
});
const instance = mount(
<AppContext.Provider value={testProps}>
<ActivityList />
</AppContext.Provider>
);
expect(mockedAxios.request).toBeCalled();
});
it('Create account, but account deleted', () => {
mockedAxios.request.mockResolvedValue({
status: 200,
data: {
previous: null,
next: null,
count: 1,
results: [
{
action_type: "Create",
model_name: "Account",
object_id: "1234",
recorded_at: "2020-05-31T23:03:35.854615+07:00",
},
]
}
});
const instance = mount(
<AppContext.Provider value={testProps}>
<ActivityList />
</AppContext.Provider>
);
expect(mockedAxios.request).toBeCalled();
});
it('Edit account', () => {
mockedAxios.request.mockResolvedValue({
status: 200,
data: {
username: "Test",
is_admin: false,
previous: null,
next: null,
count: 1,
results: [
{
action_type: "Edit",
model_name: "Account",
object_id: "1234",
recorded_at: "2020-05-31T23:03:35.854615+07:00",
},
]
}
});
const instance = mount(
<AppContext.Provider value={testProps}>
<ActivityList />
</AppContext.Provider>
);
expect(mockedAxios.request).toBeCalled();
});
it('Edit account, but account deleted', () => {
mockedAxios.request.mockResolvedValue({
status: 404,
data: {
previous: null,
next: null,
count: 1,
results: [
{
action_type: "Edit",
model_name: "Account",
object_id: "1234",
recorded_at: "2020-05-31T23:03:35.854615+07:00",
},
]
}
});
const instance = mount(
<AppContext.Provider value={testProps}>
<ActivityList />
</AppContext.Provider>
);
expect(mockedAxios.request).toBeCalled();
});
it('Delete account', () => {
mockedAxios.request.mockResolvedValue({
status: 200,
data: {
username: "Test",
is_admin: false,
previous: null,
next: null,
count: 1,
results: [
{
action_type: "Delete",
model_name: "Account",
object_id: "1234",
recorded_at: "2020-05-31T23:03:35.854615+07:00",
},
]
}
});
const instance = mount(
<AppContext.Provider value={testProps}>
<ActivityList />
</AppContext.Provider>
);
expect(mockedAxios.request).toBeCalled();
});
});
it('fetch new logs and change page number when press next or previous button', () => {
mockedAxios.request.mockResolvedValue(dummyLog);
const instance = mount(
<AppContext.Provider value={testProps}>
<ActivityList />
</AppContext.Provider>
);
expect(mockedAxios.request).toBeCalled();
const prevButton = instance.find('#prev-button');
const nextButton = instance.find('#next-button');
let pageNumber = instance.find('Text').findWhere(elem => elem.prop('id') === 'page-number');
expect(pageNumber.text()).toBe('1');
nextButton.at(0).simulate('click');
expect(mockedAxios.request).toBeCalled();
prevButton.at(0).simulate('click');
pageNumber = instance.find('Text').findWhere(elem => elem.prop('id') === 'page-number');
expect(pageNumber.text()).toBe('1');
expect(mockedAxios.request).toBeCalled();
})
it('filter are displayed and clickable', () => {
mockedAxios.request.mockResolvedValue(dummyLog);
const instance = mount(
<AppContext.Provider value={testProps}>
<ActivityList />
</AppContext.Provider>
);
expect(mockedAxios.request).toBeCalled();
let filter = instance.find("#filter");
filter.simulate("click");
let list = instance.find("#list-activity");
expect(list).toBeTruthy();
});
\ No newline at end of file
import React from 'react';
import axios from 'axios';
import renderer from 'react-test-renderer';
import ActivityTabel from '.';
import { useMainService } from 'services';
import { AppContext } from 'contexts';
import { mount } from 'enzyme';
const dummyLog = {
status: 200,
data: {
username: "Test",
is_admin: false,
previous: true,
next: true,
count: 1,
results: [
{
action_type: "Delete",
model_name: "Account",
object_id: "12345",
recorded_at: "2020-05-31T23:03:35.854615+07:00",
},
]
}
}
jest.mock('axios');
const mockedAxios = axios as jest.Mocked<typeof axios>;
const testProps = {
services: {
main: useMainService('dummyToken'),
},
};
it('ActivityTabel renders correctly', () => {
const instance = renderer.create(
<ActivityTabel />
);
expect(instance).toBeTruthy();
});
describe('load logs and generate log messages correctly', () => {
it('Monitoring case', () => {
mockedAxios.request.mockResolvedValue({
status: 200,
data: {
previous: null,
next: null,
count: 1,
results: [
{
action_type: "Create",
model_name: "Monitoring Case",
object_id: "1234",
recorded_at: "2020-05-31T23:03:35.854615+07:00",
},
],
investigation_case: {
case_subject: {
name: "Test"
}
}
}
});
const instance = mount(
<AppContext.Provider value={testProps}>
<ActivityTabel />
</AppContext.Provider>
);
expect(mockedAxios.request).toBeCalled();
});
it('Investigation case', () => {
mockedAxios.request.mockResolvedValue({
status: 200,
data: {
previous: null,
next: null,
count: 1,
results: [
{
action_type: "Create",
model_name: "Investigation Case",
object_id: "1234",
recorded_at: "2020-05-31T23:03:35.854615+07:00",
},
],
case_subject: {
name: "Test"
}
}
});
const instance = mount(
<AppContext.Provider value={testProps}>
<ActivityTabel />
</AppContext.Provider>
);
expect(mockedAxios.request).toBeCalled();
});
it('Case subject', () => {
mockedAxios.request.mockResolvedValue({
status: 200,
data: {
previous: null,
next: null,
count: 1,
results: [
{
action_type: "Create",
model_name: "Case Subject",
object_id: "1234",
recorded_at: "2020-05-31T23:03:35.854615+07:00",
},
],
}
});
const instance = mount(
<AppContext.Provider value={testProps}>
<ActivityTabel />
</AppContext.Provider>
);
expect(mockedAxios.request).toBeCalled();
});
it('Create account', () => {
mockedAxios.request.mockResolvedValue({
status: 200,
data: {
username: "Test",
is_admin: false,
previous: null,
next: null,
count: 1,
results: [
{
action_type: "Create",
model_name: "Account",
object_id: "1234",
recorded_at: "2020-05-31T23:03:35.854615+07:00",
},
]
}
});
const instance = mount(
<AppContext.Provider value={testProps}>
<ActivityTabel />
</AppContext.Provider>
);
expect(mockedAxios.request).toBeCalled();
});
it('Create account, but account deleted', () => {
mockedAxios.request.mockResolvedValue({
status: 200,
data: {
previous: null,
next: null,
count: 1,
results: [
{
action_type: "Create",
model_name: "Account",
object_id: "1234",
recorded_at: "2020-05-31T23:03:35.854615+07:00",
},
]
}
});
const instance = mount(
<AppContext.Provider value={testProps}>
<ActivityTabel />
</AppContext.Provider>
);
expect(mockedAxios.request).toBeCalled();
});
it('Edit account', () => {
mockedAxios.request.mockResolvedValue({
status: 200,
data: {
username: "Test",
is_admin: false,
previous: null,
next: null,
count: 1,
results: [
{
action_type: "Edit",
model_name: "Account",
object_id: "1234",
recorded_at: "2020-05-31T23:03:35.854615+07:00",
},
]
}
});
const instance = mount(
<AppContext.Provider value={testProps}>
<ActivityTabel />
</AppContext.Provider>
);
expect(mockedAxios.request).toBeCalled();
});
it('Edit account, but account deleted', () => {
mockedAxios.request.mockResolvedValue({
status: 404,
data: {
previous: null,
next: null,
count: 1,
results: [
{
action_type: "Edit",
model_name: "Account",
object_id: "1234",
recorded_at: "2020-05-31T23:03:35.854615+07:00",
},
]
}
});
const instance = mount(
<AppContext.Provider value={testProps}>
<ActivityTabel />
</AppContext.Provider>
);
expect(mockedAxios.request).toBeCalled();
});
it('Delete account', () => {
mockedAxios.request.mockResolvedValue({
status: 200,
data: {
username: "Test",
is_admin: false,
previous: null,
next: null,
count: 1,
results: [
{
action_type: "Delete",
model_name: "Account",
object_id: "1234",
recorded_at: "2020-05-31T23:03:35.854615+07:00",
},
]
}
});
const instance = mount(
<AppContext.Provider value={testProps}>
<ActivityTabel />
</AppContext.Provider>
);