Skip to content

Commit

Permalink
improve data table widget mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
tomgny committed May 7, 2024
1 parent ff05339 commit 7c96c18
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@
*/

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { DataColumn, FormFieldModel, FormFieldTypes, FormModel, VariableConfig } from '@alfresco/adf-core';
import { DataColumn, DataRow, FormFieldModel, FormFieldTypes, FormModel, VariableConfig } from '@alfresco/adf-core';
import { By } from '@angular/platform-browser';
import { DataTableWidgetComponent } from './data-table.widget';
import { ProcessServiceCloudTestingModule } from '../../../../testing/process-service-cloud.testing.module';
import { TaskVariableCloud } from '../../../models/task-variable-cloud.model';
import { FormCloudService } from '../../../services/form-cloud.service';
import { WidgetDataTableAdapter } from './data-table-adapter.widget';
import {
mockEuropeCountriesData,
mockAmericaCountriesData,
mockInvalidSchemaDefinition,
mockJsonFormVariable,
Expand All @@ -36,7 +34,10 @@ import {
mockJsonNestedResponseFormVariable,
mockJsonNestedResponseEuropeCountriesData,
mockNestedEuropeCountriesData,
mockSchemaDefinitionWithNestedKeys
mockSchemaDefinitionWithNestedKeys,
expectedCountryColumns,
expectedEuropeCountriesRows,
expectedAmericaCountriesRows
} from './mocks/data-table-widget.mock';

describe('DataTableWidgetComponent', () => {
Expand Down Expand Up @@ -73,9 +74,10 @@ describe('DataTableWidgetComponent', () => {

const getPreview = () => fixture.nativeElement.querySelector('[data-automation-id="adf-data-table-widget-preview"]');

const assertDataRows = (expectedData: WidgetDataTableAdapter) => {
expectedData.getRows().forEach((row) => (row.cssClass = ''));
expect(widget.dataSource.getRows()).toEqual(expectedData.getRows());
const assertData = (expectedColumns: DataColumn[], expectedRows: DataRow[]) => {
expectedRows.forEach((row) => (row.cssClass = ''));
expect(widget.dataSource.getColumns()).toEqual(expectedColumns);
expect(widget.dataSource.getRows()).toEqual(expectedRows);
};

beforeEach(() => {
Expand Down Expand Up @@ -124,52 +126,46 @@ describe('DataTableWidgetComponent', () => {
widget.field.value = mockNestedEuropeCountriesData;
fixture.detectChanges();

const expectedData = new WidgetDataTableAdapter(mockNestedEuropeCountriesData, mockSchemaDefinitionWithNestedKeys);
assertDataRows(expectedData);
assertData(expectedCountryColumns, expectedEuropeCountriesRows);
});

it('should properly initialize data source with priority on the field value if process and form variables are provided', () => {
widget.field = getDataVariable(mockVariableConfig, mockSchemaDefinition, mockJsonProcessVariables, mockJsonFormVariable);
widget.field.value = mockAmericaCountriesData;
fixture.detectChanges();

const expectedData = new WidgetDataTableAdapter(mockAmericaCountriesData, mockSchemaDefinition);
assertDataRows(expectedData);
assertData(expectedCountryColumns, expectedAmericaCountriesRows);
});

it('should properly initialize data source based on field value', () => {
widget.field = getDataVariable(mockVariableConfig, mockSchemaDefinition, [], []);
widget.field.value = mockAmericaCountriesData;
fixture.detectChanges();

const expectedData = new WidgetDataTableAdapter(mockAmericaCountriesData, mockSchemaDefinition);
assertDataRows(expectedData);
assertData(expectedCountryColumns, expectedAmericaCountriesRows);
});

it('should properly initialize default json response data source based on field value if path is NOT provided', () => {
widget.field = getDataVariable(mockVariableConfig, mockSchemaDefinition, [], []);
widget.field.value = mockJsonResponseEuropeCountriesData;
fixture.detectChanges();

const expectedData = new WidgetDataTableAdapter(mockEuropeCountriesData, mockSchemaDefinition);
assertDataRows(expectedData);
assertData(expectedCountryColumns, expectedEuropeCountriesRows);
});

it('should properly initialize default json response data source based on variable if path is NOT provided', () => {
widget.field = getDataVariable(mockVariableConfig, mockSchemaDefinition, [], mockJsonResponseFormVariable);
fixture.detectChanges();

const expectedData = new WidgetDataTableAdapter(mockEuropeCountriesData, mockSchemaDefinition);
assertDataRows(expectedData);
assertData(expectedCountryColumns, expectedEuropeCountriesRows);
});

it('should properly initialize json response data source based on field value if path is provided', () => {
widget.field = getDataVariable({ ...mockVariableConfig, optionsPath: 'response.my-data' }, mockSchemaDefinition, [], []);
widget.field.value = mockJsonNestedResponseEuropeCountriesData;
fixture.detectChanges();

const expectedData = new WidgetDataTableAdapter(mockEuropeCountriesData, mockSchemaDefinition);
assertDataRows(expectedData);
assertData(expectedCountryColumns, expectedEuropeCountriesRows);
});

it('should properly initialize json response data source based on variable if path is provided', () => {
Expand All @@ -181,24 +177,21 @@ describe('DataTableWidgetComponent', () => {
);
fixture.detectChanges();

const expectedData = new WidgetDataTableAdapter(mockEuropeCountriesData, mockSchemaDefinition);
assertDataRows(expectedData);
assertData(expectedCountryColumns, expectedEuropeCountriesRows);
});

it('should properly initialize data source based on form variable', () => {
widget.field = getDataVariable(mockVariableConfig, mockSchemaDefinition, [], mockJsonFormVariable);
fixture.detectChanges();

const expectedData = new WidgetDataTableAdapter(mockEuropeCountriesData, mockSchemaDefinition);
assertDataRows(expectedData);
assertData(expectedCountryColumns, expectedEuropeCountriesRows);
});

it('should properly initialize data source based on process variable', () => {
widget.field = getDataVariable({ variableName: 'json-variable' }, mockSchemaDefinition, mockJsonProcessVariables);
fixture.detectChanges();

const expectedData = new WidgetDataTableAdapter(mockEuropeCountriesData, mockSchemaDefinition);
assertDataRows(expectedData);
assertData(expectedCountryColumns, expectedEuropeCountriesRows);
});

it('should NOT display error if form is in preview state', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

import { DataColumn } from '@alfresco/adf-core';
import { DataColumn, DataRow, ObjectDataColumn, ObjectDataRow } from '@alfresco/adf-core';
import { TaskVariableCloud } from '../../../../models/task-variable-cloud.model';

export const mockSchemaDefinition: DataColumn[] = [
Expand All @@ -38,20 +38,25 @@ export const mockSchemaDefinition: DataColumn[] = [
export const mockSchemaDefinitionWithNestedKeys: DataColumn[] = [
{
type: 'text',
key: 'countries.europeCountries.id',
key: 'countries.europeCountry.id',
title: 'Country ID',
sortable: true,
draggable: true
},
{
type: 'text',
key: 'countries.europeCountries.name',
key: 'countries.europeCountry.name',
title: 'Country Name',
sortable: true,
draggable: true
}
];

export const expectedCountryColumns: DataColumn[] = [
new ObjectDataColumn({ key: 'id', type: 'text', title: 'Country ID', sortable: true, draggable: true }),
new ObjectDataColumn({ key: 'name', type: 'text', title: 'Country Name', sortable: true, draggable: true })
];

export const mockInvalidSchemaDefinition: DataColumn[] = [
{
type: 'text',
Expand Down Expand Up @@ -84,6 +89,12 @@ export const mockEuropeCountriesData = [
}
];

export const expectedEuropeCountriesRows: DataRow[] = [
new ObjectDataRow({ id: 'IT', name: 'Italy' }),
new ObjectDataRow({ id: 'PL', name: 'Poland' }),
new ObjectDataRow({ id: 'UK', name: 'United Kingdom' })
];

export const mockJsonResponseEuropeCountriesData = {
data: mockEuropeCountriesData
};
Expand Down Expand Up @@ -144,6 +155,12 @@ export const mockAmericaCountriesData = [
}
];

export const expectedAmericaCountriesRows: DataRow[] = [
new ObjectDataRow({ id: 'CA', name: 'Canada' }),
new ObjectDataRow({ id: 'MX', name: 'Mexico' }),
new ObjectDataRow({ id: 'US', name: 'United States' })
];

export const mockCountriesIncorrectData = [
{
id: 'PL'
Expand Down

0 comments on commit 7c96c18

Please sign in to comment.