IEEE.org     |     IEEE Xplore Digital Library     |     IEEE Standards     |     IEEE Spectrum     |     More Sites

Unverified Commit 7004ca53 authored by tilley14's avatar tilley14 Committed by GitHub
Browse files

Merge pull request #77 from my-conservation-life/export-headers

Export headers
parents 09448e6c 575b890b
......@@ -97,6 +97,66 @@ describe('GET assetTypes', () => {
});
});
describe('POST assetPropTypes', () => {
beforeAll(async () => {
jest.setTimeout(30000);
await setup();
// create some default asset types
await loadSQL('../schema/sample-data-assetTypes.sql');
});
afterAll(async () => {
await teardown();
});
it('gets all asset property types', () => {
return request(app)
.post('/api/v1/assetPropTypes')
.expect(200)
.then((res) => {
expect(res.body.rows).toEqual(
expect.arrayContaining([
expect.objectContaining(EXPECTED_ASSET_TYPE1),
expect.objectContaining(EXPECTED_ASSET_TYPE2),
expect.objectContaining(EXPECTED_ASSET_TYPE3)
])
);
expect(res.body.rows).toHaveLength(3);
});
});
});
describe('POST assetPropsByTypeID', () => {
beforeAll(async () => {
jest.setTimeout(30000);
await setup();
// create some default asset types
await loadSQL('../schema/sample-data-assetTypes.sql');
});
afterAll(async () => {
await teardown();
});
it('gets all asset property types', () => {
return request(app)
.post('/api/v1/assetPropsByTypeID')
.expect(200)
.then((res) => {
expect(res.body.rows).toEqual(
expect.arrayContaining([
expect.objectContaining(EXPECTED_ASSET_TYPE1),
expect.objectContaining(EXPECTED_ASSET_TYPE2),
expect.objectContaining(EXPECTED_ASSET_TYPE3)
])
);
expect(res.body.rows).toHaveLength(3);
});
});
});
// TODO - tests for PUT CSV endpoint
describe('PUT CSV', () => {
......
const { getAssetTypes } = require('../assetDefinitions.controller');
const { getAssetTypes, getAssetPropsByTypeID, getAssetPropTypes } = require('../assetDefinitions.controller');
const assetDefinitionsDb = require('../../db/assetDefinitions.db');
describe('assetDefinitions.controller.getAssetTypes', () => {
......@@ -27,6 +27,62 @@ describe('assetDefinitions.controller.getAssetTypes', () => {
});
});
describe('assetDefinitions.controller.getAssetPropsByTypeID', () => {
let req;
let res;
let next;
let expectedAssetTypes;
let data;
beforeEach(() => {
req = {
'body': {
}
};
res = {
json: jest.fn()
};
next = jest.fn();
expectedAssetTypes = [{}];
data = { rows: expectedAssetTypes };
assetDefinitionsDb.findAssetPropsByTypeID = jest.fn(async () => data);
});
it('returns all asset types in DB', async () => {
await getAssetPropsByTypeID(req, res, next);
expect(res.json).toHaveBeenCalledWith(data);
});
});
describe('assetDefinitions.controller.getAssetPropsTypes', () => {
let req;
let res;
let next;
let expectedAssetTypes;
let data;
beforeEach(() => {
req = {};
res = {
json: jest.fn()
};
next = jest.fn();
expectedAssetTypes = [{}];
data = { rows: expectedAssetTypes };
assetDefinitionsDb.findAssetPropTypes = jest.fn(async () => data);
});
it('returns all asset types in DB', async () => {
await getAssetPropTypes(req, res, next);
expect(res.json).toHaveBeenCalledWith(data);
});
});
describe('assetDefinitions.controller.storeCSV', () => {
let req;
let res;
......
......@@ -171,5 +171,43 @@ describe('assetDefinitions.db.storeCSV', () => {
global.dbPool.connect = jest.fn(async () => { return client; });
});
describe('assetDefinitions.db.findAssetTypes', () => {
let rows;
let query;
beforeEach(() => {
rows = [{}];
query = jest.fn(async() => rows);
global.dbPool = { query };
});
it('finds all asset types in the database', async () => {
const actualRows = await findAssetTypes();
expect(actualRows).toEqual(rows);
expect(query).toHaveBeenCalledWith(expect.stringContaining('asset_type'));
});
});
describe('assetDefinitions.db.findAssetPropByTypeID', () => {
let rows;
let query;
let assetTypeId;
beforeEach(() => {
rows = [{}];
query = jest.fn(async() => rows);
global.dbPool = { query };
assetTypeId = 1;
});
it('finds all properties associated with the asset type ID given', async () => {
const actualRows = await findAssetPropTypes(assetTypeId);
expect(actualRows).toEqual(rows);
});
});
// TODO tests for storeCSV function
});
......@@ -21,6 +21,43 @@ describe('assetDefinitions.findAssetTypes', () => {
});
});
describe('assetDefinitions.findAssetPropTypes', () => {
const EXPECTED_ASSET_PROP_TYPES = [];
let fetch;
let URL = utils.URL + 'assetPropTypes'
beforeEach(() => {
fetch = createMockFetch(EXPECTED_ASSET_PROP_TYPES);
global.fetch = fetch;
});
it('finds all the asset properties for a given asset type', async () => {
const assetPropTypes = await assetDefinitions.fetchAssetPropTypes(1);
expect(fetch.mock.calls[0][0]).toBe(URL);
expect(fetch.mock.calls[0][1]).toEqual(expect.objectContaining(1));
expect(assetPropTypes).toEqual(EXPECTED_ASSET_PROP_TYPES);
});
});
describe('assetDefinitions.findAssetPropsByTypeID', () => {
const EXPECTED_ASSET_PROPERTIES = [];
let fetch;
let URL = utils.URL + 'assetPropsByTypeID';
beforeEach(() => {
fetch = createMockFetch(EXPECTED_ASSET_PROPERTIES);
global.fetch = fetch;
});
it('finds all the asset properties for a given type of asset', async () => {
const assetPropsByTypeID = await assetDefinitions.fetchAssetPropsByTypeID(1);
expect(fetch.mock.calls[0][0]).toBe(URL);
expect(assetPropsByTypeID).toEqual(EXPECTED_ASSET_PROPERTIES);
});
});
describe('assetDefinitions.sendCSV', () => {
const EXPECTED_RESPONSE = Promise.resolve();
let fetch;
......
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="exportCSV">
<apiVersion>46.0</apiVersion>
<apiVersion>47.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
......
<template>
<lightning:layout horizontalalign="space">
<lightning:layoutItem size="10">
<lightning-combobox
name="datasource"
label="Export a template CSV"
value={value}
placeholder={value}
options={combo_options}
onchange={handleChange}
></lightning-combobox>
</lightning:layoutItem>
<br/>
<lightning:layoutItem size="2">
<lightning-button variant="success" label="Download" onclick={download}></lightning-button>
</lightning:layoutItem>
</lightning:layout>
</template>
import { LightningElement, track } from 'lwc';
import { assetDefinitions } from 'c/controllers';
export default class ExportHeaders extends LightningElement {
@track value = 'Select an asset type...';
@track valueID = null;
@track placeholder = 'N/A';
@track combo_options = [];
/**
* Sets the combobox options.
*/
connectedCallback() {
var i;
assetDefinitions.fetchAssetTypes()
.then(data => {
var temp_options = [];
for (i = 0; i < data.rows.length; i++) {
temp_options.push({
'label': data.rows[i]['name'] + ': ' + data.rows[i]['id'],
'value': data.rows[i]['name'] + ': ' + data.rows[i]['id']
});
}
this.combo_options = temp_options;
})
.catch(e => {
console.log('Exception: ', e);
});
}
/**
* Event handler for when something is selected from the combobox.
* @param {*} event - the event object
*/
handleChange(event) {
this.value = event.detail.value;
this.valueID = event.detail.value.split(':')[1].trimLeft();
}
/**
* Event handler for when the download button is pressed.
*/
download() {
var i;
var csv_data = '';
var rows = [];
var hiddenElement;
assetDefinitions.fetchAssetPropTypes(this.valueID)
.then(properties => {
rows = ['asset_id'];
for (i = 0; i < properties.rows.length; i++) {
rows.push(properties.rows[i]['name']);
}
csv_data += rows.join(',') + '\n';
// Creates the CSV file and downloads it.
hiddenElement = document.createElement('a');
hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csv_data);
hiddenElement.target = '_blank';
hiddenElement.download = this.value + '_template.csv';
hiddenElement.click();
})
.catch(e => {
console.log('Exception: ', e);
});
}
}
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>47.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
const URL = 'https://my-conservation-life.herokuapp.com/api/v1/';
const URL = 'https://my-conservation-life-dev1.herokuapp.com/api/v1/';
export { URL };
......@@ -7,5 +7,5 @@
],
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "48.0"
"sourceApiVersion": "47.0"
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment