-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Dataset should load with a delay
- Loading branch information
Showing
13 changed files
with
218 additions
and
29 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
__sample-components__/GridControl/generated/ManifestTypes.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
*This is auto generated from the ControlManifest.Input.xml file | ||
*/ | ||
|
||
// Define IInputs and IOutputs Type. They should match with ControlManifest. | ||
export interface IInputs { | ||
records: ComponentFramework.PropertyTypes.DataSet; | ||
} | ||
export interface IOutputs { | ||
FilteredRecordCount?: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { IInputs, IOutputs } from './generated/ManifestTypes'; | ||
|
||
export class GridControl implements ComponentFramework.StandardControl<IInputs, IOutputs> { | ||
notifyOutputChanged: () => void; | ||
container: HTMLDivElement; | ||
filteredRecordCount?: number; | ||
|
||
/** | ||
* Used to initialize the control instance. Controls can kick off remote server calls and other initialization actions here. | ||
* Data-set values are not initialized here, use updateView. | ||
* @param context The entire property bag available to control via Context Object; It contains values as set up by the customizer mapped to property names defined in the manifest, as well as utility functions. | ||
* @param notifyOutputChanged A callback method to alert the framework that the control has new outputs ready to be retrieved asynchronously. | ||
* @param state A piece of data that persists in one session for a single user. Can be set at any point in a controls life cycle by calling 'setControlState' in the Mode interface. | ||
* @param container If a control is marked control-type='standard', it will receive an empty div element within which it can render its content. | ||
*/ | ||
public init( | ||
context: ComponentFramework.Context<IInputs>, | ||
notifyOutputChanged: () => void, | ||
state: ComponentFramework.Dictionary, | ||
container: HTMLDivElement, | ||
): void { | ||
this.notifyOutputChanged = notifyOutputChanged; | ||
this.container = container; | ||
} | ||
|
||
/** | ||
* Called when any value in the property bag has changed. This includes field values, data-sets, global values such as container height and width, offline status, control metadata values such as label, visible, etc. | ||
* @param context The entire property bag available to control via Context Object; It contains values as set up by the customizer mapped to names defined in the manifest, as well as utility functions | ||
*/ | ||
public updateView(context: ComponentFramework.Context<IInputs>): void { | ||
const dataset = context.parameters.records; | ||
const datasetChanged = context.updatedProperties.indexOf('dataset') > -1; | ||
if (datasetChanged) { | ||
if (this.filteredRecordCount !== dataset.sortedRecordIds.length) { | ||
this.filteredRecordCount = dataset.sortedRecordIds.length; | ||
this.notifyOutputChanged(); | ||
} | ||
} | ||
this.container.innerHTML = `${this.filteredRecordCount}`; | ||
} | ||
|
||
/** | ||
* It is called by the framework prior to a control receiving new data. | ||
* @returns an object based on nomenclature defined in manifest, expecting object[s] for property marked as “bound” or “output” | ||
*/ | ||
public getOutputs(): IOutputs { | ||
return { | ||
FilteredRecordCount: this.filteredRecordCount, | ||
} as IOutputs; | ||
} | ||
|
||
/** | ||
* Called when the control is to be removed from the DOM tree. Controls should use this call for cleanup. | ||
* i.e. cancelling any pending remote calls, removing listeners, etc. | ||
*/ | ||
destroy(): void {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
Copyright (c) 2022 Betim Beja and Shko Online LLC | ||
Licensed under the MIT license. | ||
*/ | ||
|
||
import { it, expect, describe, beforeEach } from '@jest/globals'; | ||
import { ComponentFrameworkMockGenerator, DataSetMock } from '../../src'; | ||
import { GridControl } from '../../__sample-components__/GridControl'; | ||
import { IInputs, IOutputs } from '../../__sample-components__/GridControl/generated/ManifestTypes'; | ||
|
||
describe('GridControl', () => { | ||
let mockGenerator: ComponentFrameworkMockGenerator<IInputs, IOutputs>; | ||
beforeEach(() => { | ||
const container = document.createElement('div'); | ||
mockGenerator = new ComponentFrameworkMockGenerator( | ||
GridControl, | ||
{ | ||
records: DataSetMock, | ||
}, | ||
container, | ||
); | ||
mockGenerator.context._parameters.records._InitItems([ | ||
{ | ||
name: 'Betim', | ||
surname: 'Beja', | ||
}, | ||
]); | ||
document.body.appendChild(container); | ||
}); | ||
|
||
afterEach(() => { | ||
document.body.innerHTML = null; | ||
}); | ||
|
||
it('Should render count', (done) => { | ||
mockGenerator.context._parameters.records._onLoaded.callsFake(() => { | ||
expect(mockGenerator.container).toMatchSnapshot(); | ||
done(); | ||
}); | ||
mockGenerator.ExecuteInit(); | ||
mockGenerator.ExecuteUpdateView(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`GridControl Should render count 1`] = ` | ||
<div> | ||
1 | ||
</div> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/ComponentFramework-Mock-Generator/mockRefreshDatasets.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
Copyright (c) 2022 Betim Beja and Shko Online LLC | ||
Licensed under the MIT license. | ||
*/ | ||
|
||
/// <reference path="../global.d.ts" /> | ||
|
||
import type { PropertyToMock } from '../ComponentFramework-Mock'; | ||
import type { MockGenerator } from './MockGenerator'; | ||
import type { ShkoOnline } from '../ShkoOnline'; | ||
|
||
import { stub } from 'sinon'; | ||
import { DataSetMock } from '../ComponentFramework-Mock'; | ||
|
||
export const mockRefreshDatasets = < | ||
TInputs extends ShkoOnline.PropertyTypes<TInputs>, | ||
TOutputs extends ShkoOnline.KnownTypes<TOutputs>, | ||
>( | ||
mockGenerator: MockGenerator<TInputs, TOutputs>, | ||
callback: () => void, | ||
) => { | ||
mockGenerator.RefreshDatasets = stub(); | ||
mockGenerator.RefreshDatasets.callsFake(() => { | ||
Object.getOwnPropertyNames<PropertyToMock<TInputs>>(mockGenerator.context._parameters).forEach( | ||
(propertyName) => { | ||
const mock = mockGenerator.context._parameters[propertyName]; | ||
if (!(mock instanceof DataSetMock) || !mock._loading) { | ||
return; | ||
} | ||
setTimeout(() => { | ||
mock._loading = !mock._loading; | ||
mockGenerator.RefreshParameters(); | ||
mockGenerator.context.updatedProperties = [propertyName as string, 'dataset']; | ||
callback(); | ||
mock._onLoaded(); | ||
}, mock._delay); | ||
}, | ||
); | ||
}); | ||
}; |
Oops, something went wrong.