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

Commit ae4d2958 authored by James C. De Ricco's avatar James C. De Ricco
Browse files

Remove unfinished and failing create asset LWC unit tests

parent eaeb0277
/* eslint-disable @lwc/lwc/no-async-operation */
/* eslint-disable no-console */
import { createElement } from 'lwc';
import CreateAsset from 'c/createAsset';
import { assets, dataTypes } from 'c/controllers';
describe('c-create-asset', () => {
let element;
let CONSOLE_WARN;
const LWC_STARTUP_WAIT = 10;
const DATA_TYPES = ['boolean', 'number', 'datetime', 'location', 'text'];
const ATTRIBUTES = {
name: 'testname',
description: 'testdescription'
};
const PROPERTY_ATTRIBUTES = {
name: '',
data_type: '',
required: false,
is_private: false
};
const ASSET_DEFINITION = {
assetDefinition: {
...ATTRIBUTES,
properties: [
PROPERTY_ATTRIBUTES
]
}
};
const wait = (ms = LWC_STARTUP_WAIT) => new Promise((r) => setTimeout(r, ms));
beforeAll(() => {
CONSOLE_WARN = console.warn;
console.warn = () => { };
});
afterAll(() => {
console.warn = CONSOLE_WARN;
});
beforeEach(() => {
assets.create = jest.fn(async () => 1);
assetDefinitions.find = jest.fn(async () => DATA_TYPES);
element = createElement('c-create-asset', { is: CreateAsset });
document.body.appendChild(element);
});
afterEach(() => {
// The jsdom instance is shared across test cases in a single file so reset the DOM
while (document.body.firstChild) {
document.body.removeChild(document.body.firstChild);
}
});
it('creates a component with 2 inputs and 1 list component', () => {
const inputElements = element.shadowRoot.querySelectorAll('lightning-input');
const listElements = element.shadowRoot.querySelectorAll('c-create-asset-list');
expect(inputElements.length).toBe(2);
expect(listElements.length).toBe(1);
});
it('saves and gets correct attributes', async () => {
const inputElements = element.shadowRoot.querySelectorAll('lightning-input');
for (let input of inputElements) {
input.value = ATTRIBUTES[input.name];
input.dispatchEvent(new Event('change'));
}
await wait();
element.sendAssetDefinition();
expect(assetDefinitions.create.mock.calls.length).toBe(1);
expect(assetDefinitions.create.mock.calls[0][0]).toEqual(ASSET_DEFINITION);
});
});
/* eslint-disable @lwc/lwc/no-async-operation */
/* eslint-disable no-console */
import { createElement } from 'lwc';
import CreateAssetList from 'c/createAssetDefinitionList';
import { dataTypes } from 'c/controllers';
describe('c-create-asset-list', () => {
let element;
let CONSOLE_WARN;
const LWC_STARTUP_WAIT = 10;
const DATA_TYPES = ['boolean', 'number', 'datetime', 'location', 'text'];
const wait = (ms = LWC_STARTUP_WAIT) => new Promise((r) => setTimeout(r, ms));
beforeAll(() => {
CONSOLE_WARN = console.warn;
console.warn = () => { };
});
afterAll(() => {
console.warn = CONSOLE_WARN;
});
beforeEach(() => {
dataTypes.find = jest.fn(async () => DATA_TYPES);
element = createElement('c-create-asset-definition-list', { is: CreateAssetDefinitionList });
document.body.appendChild(element);
});
afterEach(() => {
// The jsdom instance is shared across test cases in a single file so reset the DOM
while (document.body.firstChild) {
document.body.removeChild(document.body.firstChild);
}
});
it('creates a list with one prefilled property and one custom property', () => {
const propertyElements = element.shadowRoot.querySelectorAll('c-create-asset-definition-property');
const customProperties = element.getCustomProperties();
// We have 2 properties
expect(propertyElements.length).toBe(2);
// We have only 1 custom property
expect(customProperties.length).toBe(1);
// A truthy value returns for the custom property
// We don't care about what it contains
expect(customProperties[0]).toBeTruthy();
});
it('calls controller to get dataTypes', () => {
expect(dataTypes.find.mock.calls.length).toBe(1);
});
it('adds custom properties', async () => {
const addPropertyButton = element.shadowRoot.querySelector('lightning-button');
addPropertyButton.dispatchEvent(new Event('click'));
await wait();
expect(element.getCustomProperties().length).toBe(2);
});
it('handles CustomEvent removeproperty', async () => {
const customProperties = element.shadowRoot.querySelectorAll('c-create-asset-definition-property');
let customProperty;
for (let property of customProperties) {
if (property.getIsCustomProperty()) {
customProperty = property;
break;
}
}
const removeEvent = new CustomEvent('removeproperty', { detail: 1 });
customProperty.dispatchEvent(removeEvent);
await wait();
expect(element.getCustomProperties().length).toBe(0);
});
});
\ No newline at end of file
/* eslint-disable no-console */
import { createElement } from 'lwc';
import CreateAssetProperty from 'c/createAssetDefinitionProperty';
describe('c-create-asset-property', () => {
let element;
let CONSOLE_WARN;
const ATTRIBUTES = {
name: 'testname',
data_type: 'testdatatype',
required: true,
is_private: true,
value: 'testvalue'
};
const OPTIONS = ['boolean', 'number', 'datetime', 'location', 'text'];
const getInputs = (ele) => ele.shadowRoot.querySelectorAll('lightning-input, lightning-combobox');
const removeComponents = () => {
while (document.body.firstChild) {
document.body.removeChild(document.body.firstChild);
}
};
beforeAll(() => {
CONSOLE_WARN = console.warn;
console.warn = () => { };
});
afterAll(() => {
console.warn = CONSOLE_WARN;
});
beforeEach(() => {
element = createElement('c-create-asset-property', { is: CreateAssetProperty });
element.propertyDataTypes = JSON.stringify(OPTIONS);
document.body.appendChild(element);
});
afterEach(() => {
// The jsdom instance is shared across test cases in a single file so reset the DOM
removeComponents();
});
it('saves and gets correct attributes', () => {
// set all inputs' values to the values in attributes
const inputs = getInputs(element);
for (let input of inputs) {
const value = ATTRIBUTES[input.name];
if (input.type === 'checkbox') input.checked = value;
else input.value = value;
input.dispatchEvent(new Event('change'));
}
expect(element.getAttributes()).toEqual(ATTRIBUTES);
});
it('creates custom event when remove button is clicked', (done) => {
element.propertyKey = 1;
element.addEventListener('removeproperty', (event) => {
const receivedKey = event.detail;
expect(receivedKey).toBe(element.propertyKey);
done();
});
const removeButton = element.shadowRoot.querySelector('lightning-button');
removeButton.dispatchEvent(new Event('click'));
});
it('gets correct combobox options', () => {
const comboboxElement = element.shadowRoot.querySelector('lightning-combobox');
const receivedOptions = comboboxElement.options;
for (let option of receivedOptions)
expect(OPTIONS).toContain(option.value);
});
it('correctly sets for prefilled properties', () => {
// Remount component to the DOM so we can set propertyData and have renderedCallback called
removeComponents();
element.propertyData = JSON.stringify(ATTRIBUTES);
document.body.appendChild(element);
const inputs = getInputs(element);
for (let input of inputs) expect(input.disabled).toBe(true);
expect(element.getIsCustomProperty()).toBe(false);
expect(element.getAttributes()).toEqual(ATTRIBUTES);
});
});
\ No newline at end of file
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