Skip to content

Commit

Permalink
Improved options on and testing of the formValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-baillie-ortoo committed Jan 10, 2022
1 parent eb67937 commit 94dced8
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 5 deletions.
7 changes: 7 additions & 0 deletions TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ Licenses that are needed with the source code and binary:
* Amoss - https://github.com/bobalicious/amoss/blob/main/LICENSE
* SObject Fabricator - https://github.com/bobalicious/SObjectFabricator/blob/master/LICENSE


Implement the passing of the ortoo id prefix into the bits
Check the spacing of buttons on the confirmation dialog
Update docs for the view and edit form (no longer has edit buttons)
Add docs for the save buttons
Add docs for the generation of Ids

Look at the use of 'MockDatabase' in fflib
Look at:
SobjectUtils.getSobjectName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,87 @@ describe('c-form-validator', () => {
expect( response ).toBe( false );
mockSelectorReturn.forEach( thisElement => expect( thisElement.reportValidity ).toHaveBeenCalledTimes( 1 ) );
});

it( 'When an element reportValidity returns false, but options say to not show a toast, will not show a toast', () => {

let mockQuerySelector = jest.fn();
let mockDispatchEvent = jest.fn();

let objectToRunAgainst = {
dispatchEvent: mockDispatchEvent,
template: {
querySelectorAll: mockQuerySelector
}
};

mockQuerySelector.mockReturnValueOnce(
[
{ reportValidity: () => true },
{ reportValidity: () => false }
]
);

reportValidity.call( objectToRunAgainst, { showToast: false } );
expect( mockDispatchEvent ).toHaveBeenCalledTimes( 0 );

});

it( 'When an element reportValidity returns false, and options say to show a toast, will show a toast', () => {

let mockQuerySelector = jest.fn();
let mockDispatchEvent = jest.fn();

let objectToRunAgainst = {
dispatchEvent: mockDispatchEvent,
template: {
querySelectorAll: mockQuerySelector
}
};

mockQuerySelector.mockReturnValueOnce(
[
{ reportValidity: () => true },
{ reportValidity: () => false }
]
);

reportValidity.call( objectToRunAgainst, { showToast: true } );
expect( mockDispatchEvent ).toHaveBeenCalledTimes( 1 );

});
it( 'When an element reportValidity returns false, and given alternative error title and message, will show a toast with the specified text', () => {

let mockQuerySelector = jest.fn();
let mockDispatchEvent = jest.fn();

let objectToRunAgainst = {
dispatchEvent: mockDispatchEvent,
template: {
querySelectorAll: mockQuerySelector
}
};

mockQuerySelector.mockReturnValueOnce(
[
{ reportValidity: () => true },
{ reportValidity: () => false }
]
);

let customTitle = 'custom title';
let customMessage = 'custom message';
let customVariant = 'warning';

let response = reportValidity.call( objectToRunAgainst, { validationErrorTitle: customTitle, validationErrorMessage: customMessage, toastVariant: customVariant } );

expect( response ).toBe( false );

expect( mockDispatchEvent ).toHaveBeenCalledTimes( 1 );

let dispatchedEvent = mockDispatchEvent.mock.calls[0][0];

expect( dispatchedEvent.detail.title ).toBe( customTitle );
expect( dispatchedEvent.detail.message ).toBe( customMessage );
expect( dispatchedEvent.detail.variant ).toBe( customVariant );
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,34 @@ import VALIDATION_MESSAGE from '@salesforce/label/c.ortoo_core_validation_errors
*
* @returns Boolean States if the bound LWC is regarded as valid.
*/
const reportValidity = function() {
const reportValidity = function( options ) {

!options && ( options = {} );

!options.hasOwnProperty( 'showToast' ) && ( options.showToast = true );
!options.hasOwnProperty( 'toastVariant' ) && ( options.toastVariant = 'error' );

!options.validationErrorTitle && ( options.validationErrorTitle = ERROR_TITLE );
!options.validationErrorMessage && ( options.validationErrorMessage = VALIDATION_MESSAGE );

const validateableElements = this.template.querySelectorAll( '[data-validateable]' );

if ( !validateableElements ) {
return true;
}

let hasValidationError = false;
validateableElements.forEach( thisElement => {
if ( ! thisElement.reportValidity() ) {
hasValidationError = true;
}
});

if ( hasValidationError ) {
if ( hasValidationError && options.showToast ) {
const toastEvent = new ShowToastEvent({
title: ERROR_TITLE,
message: VALIDATION_MESSAGE,
variant: 'error',
title: options.validationErrorTitle,
message: options.validationErrorMessage,
variant: options.toastVariant,
});
this.dispatchEvent( toastEvent );
}
Expand Down

0 comments on commit 94dced8

Please sign in to comment.