Skip to content
This repository has been archived by the owner on Aug 9, 2022. It is now read-only.

Commit

Permalink
Main_utils Test Suite (#126)
Browse files Browse the repository at this point in the history
Add test suite for main_utils functions
  • Loading branch information
davidcui1225 authored Oct 21, 2020
1 parent 3b53da3 commit 73c98eb
Show file tree
Hide file tree
Showing 6 changed files with 349 additions and 20 deletions.
2 changes: 2 additions & 0 deletions kibana-reports/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"cron-validator": "^1.1.1",
"dompurify": "^2.1.1",
"elastic-builder": "^2.7.1",
"jest-fetch-mock": "^3.0.3",
"jquery": "^3.5.0",
"jsdom": "^16.4.0",
"json-2-csv": "^3.7.6",
Expand All @@ -49,6 +50,7 @@
"regenerator-runtime": "^0.13.7",
"set-interval-async": "1.0.33",
"showdown": "^1.9.1",
"sinon": "^9.2.0",
"uuid": "^8.1.0",
"ws": "^7.2.3"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

export const reportTableMockResponse = [
{
_id: "123456",
_index: "test",
_score: 1,
_source: {
last_updated: 123456789,
query_url: "test_query_url_value.com",
report_definition: {
delivery: {
delivery_type: "Kibana user",
delivery_params: {
kibana_recipients: []
}
},
report_params: {
report_name: "Test report table response",
description: "description",
report_source: "Dashboard",
core_params: {
base_url: "test_base_url.com",
header: "",
footer: "",
report_format: "pdf",
time_duration: "PT30M",
window_height: 800,
window_width: 1200
}
},
trigger: {
trigger_type: "On demand"
},
state: "Created",
time_created: 123456780,
time_from: 123456780,
time_to: 123456799,
}
},
_type: "doc"
}
];

export const mockReportsTableItems = [
{
id: '123456',
reportName: 'Test report table response',
type: 'On demand',
sender: '—',
kibanaRecipients: '—',
emailRecipients: '—',
reportSource: 'Dashboard',
timeCreated: undefined,
state: undefined,
url: 'test_query_url_value.com',
format: 'pdf'
}
];

export const reportDefinitionsTableMockResponse = [
{
_index: 'report_definition',
_type: '_doc',
_id: '42MmKXUBDW-VXnk7pa6d',
_score: 1,
_source: {
report_definition: { report_params:
{ report_name: 'schedule definition',
report_source: 'Dashboard',
description: 'description',
core_params:
{ base_url: 'test_base_url.com',
report_format: 'pdf',
header: '',
footer: '',
time_duration: 'PT30M',
window_width: 1200,
window_height: 800 } },
delivery:
{ delivery_type: 'Kibana user',
delivery_params: { kibana_recipients: [] } },
trigger:
{ trigger_type: 'Schedule',
trigger_params:
{ enabled_time: 1602713178321,
schedule: {
period: 1,
interval: 'DAYS'
},
schedule_type: 'Recurring',
enabled: false } },
time_created: 1602713199604,
last_updated: 1602713211007,
status: 'Disabled' }
}
}
];

export const reportDefinitionsTableMockContent = [ { id: '42MmKXUBDW-VXnk7pa6d',
reportName: 'schedule definition',
type: 'Schedule',
owner: '—',
source: 'Dashboard',
baseUrl: 'test_base_url.com',
lastUpdated: 1602713211007,
details: 'Recurring',
status: 'Disabled' } ]
132 changes: 132 additions & 0 deletions kibana-reports/public/components/main/__tests__/main_utils.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

import {
humanReadableDate,
extractFilename,
extractFileFormat,
getFileFormatPrefix,
addReportsTableContent,
addReportDefinitionsTableContent,
removeDuplicatePdfFileFormat,
readStreamToFile,
generateReport,
generateReportById,
} from '../main_utils';
import {
reportDefinitionsTableMockResponse,
mockReportsTableItems,
reportTableMockResponse,
reportDefinitionsTableMockContent,
} from './__utils__/main_utils_test_utils';
import sinon from 'sinon';
import httpClientMock from '../../../../test/httpMockClient';

describe('main_utils tests', () => {
global.URL.createObjectURL = jest.fn();
let mockElement = document.createElement('a');
mockElement.download = 'string';
mockElement.click = function name() {};
sinon.stub(document, 'createElement').returns(mockElement);

test('test humanReadableDate', () => {
const readableDate = new Date(2018, 11, 24, 10, 33, 30);
const humanReadable = humanReadableDate(readableDate);

expect(humanReadable).toBe('Mon Dec 24 2018 @ 10:33:30 AM');
});

test('test extractFileName', () => {
const fullFile = 'test_file_name_extracted_correctly.pdf';
const fileName = extractFilename(fullFile);

expect(fileName).toBe('test_file_name_extracted_correctly');
});

test('test extractFileFormat', () => {
const fullFile = 'test_file_format_extracted_correctly.png';
const fileFormat = extractFileFormat(fullFile);

expect(fileFormat).toBe('png');
});

test('test getFileFormatPrefix', () => {
const fileFormat = 'pdf';
const fileFormatPrefix = getFileFormatPrefix(fileFormat);

expect(fileFormatPrefix).toBe('data:pdf;base64,');
});

test('test addReportsTableContent', () => {
const reportsTableItems = addReportsTableContent(reportTableMockResponse);

expect(reportsTableItems).toStrictEqual(mockReportsTableItems);
});

test('test addReportDefinitionsTableContent', () => {
const reportDefinitionsTableItems = addReportDefinitionsTableContent(
reportDefinitionsTableMockResponse
);

expect(reportDefinitionsTableItems).toStrictEqual(
reportDefinitionsTableMockContent
);
});

test('test removeDuplicatePdfFileFormat', () => {
const duplicateFormat = 'test_duplicate_remove.pdf.pdf';
const duplicateRemoved = removeDuplicatePdfFileFormat(duplicateFormat);

expect(duplicateRemoved).toBe('test_duplicate_remove.pdf');
});

test('test readStreamToFile csv compile', () => {
const stream =
'category,customer_gender\n' +
'c1,Male\n' +
'c2,Male\n' +
'c3,Male\n' +
'c4,Male\n' +
'c5,Male';

const fileFormat = 'csv';
const fileName = 'test_data_report.csv';
readStreamToFile(stream, fileFormat, fileName);
});

test('test readStreamToFile pdf compile', () => {
const stream = 'data:pdf;base64,zxvniaorbguw40absdoanlsdf';
const fileFormat = 'pdf';
const fileName = 'test_pdf_report.pdf';
readStreamToFile(stream, fileFormat, fileName);
});

test('test generateReport compile', () => {
const metadata = {};
generateReport(metadata, httpClientMock);
});

test('test generateReportById compile', () => {
const reportId = '1';
const handleSuccessToast = jest.fn();
const handleErrorToast = jest.fn();
generateReportById(
reportId,
httpClientMock,
handleSuccessToast,
handleErrorToast
);
});
});
16 changes: 1 addition & 15 deletions kibana-reports/public/components/main/main_utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* permissions and limitations under the License.
*/

import { get } from 'lodash';
import 'babel-polyfill';

export const fileFormatsUpper = {
Expand Down Expand Up @@ -98,24 +97,11 @@ export const addReportDefinitionsTableContent = (data: any) => {
return reportDefinitionsTableItems;
};

export const getReportSettingDashboardOptions = (data) => {
let index;
let dashboardOptions = [];
for (index = 0; index < data.length; ++index) {
let entry = {
value: get(data, [index, '_id']).substring(10),
text: get(data, [index, '_source', 'dashboard', 'title']),
};
dashboardOptions.push(entry);
}
return dashboardOptions;
};

export const removeDuplicatePdfFileFormat = (filename) => {
return filename.substring(0, filename.length - 4);
};

const readDataReportToFile = async (
export const readDataReportToFile = async (
stream: string,
fileFormat: string,
fileName: string
Expand Down
11 changes: 9 additions & 2 deletions kibana-reports/test/httpMockClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@ const httpClientMock = jest.fn();

httpClientMock.delete = jest.fn();
httpClientMock.get = jest.fn(() => ({
then: jest.fn(() => ({ catch: jest.fn() })),

then: jest.fn(() => ({
catch: jest.fn()
})),
}));
httpClientMock.head = jest.fn();
httpClientMock.post = jest.fn();
httpClientMock.post = jest.fn(() => ({
then: jest.fn(() => ({
catch: jest.fn()
})),
}));
httpClientMock.put = jest.fn();

export default httpClientMock;
Loading

0 comments on commit 73c98eb

Please sign in to comment.