Skip to content

Commit

Permalink
Added the test for the elementIdGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-baillie-ortoo committed Jan 10, 2022
1 parent 94dced8 commit 87088b5
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,93 @@
import configureElementIdGenerator from 'c/elementIdGenerator';

let showToastEvent = function ShowToastEvent( details ) {
this.detail = details
};

jest.mock('lightning/platformShowToastEvent', () => ({
ShowToastEvent: showToastEvent
}));

describe('c-element-id-generator', () => {
afterEach(() => {
});

it( 'is tested', () => {
expect( false ).toBe( true );
it( 'will add properties to the passed object based on the ortooIdConfiguration property names with values built from the combination of the ortooIdConfiguration property values and the ortooElemIdPrefix', () => {

const objectToRunAgainst = {
ortooElemIdPrefix: 'theprefix',
ortooIdConfiguration: {
field1: 'thefield1',
field2: 'thefield2'
}
};

configureElementIdGenerator( objectToRunAgainst );

expect( objectToRunAgainst.field1 ).toBe( 'theprefix-thefield1' );
expect( objectToRunAgainst.field2 ).toBe( 'theprefix-thefield2' );
});

it( 'will skip the value if it is not specified', () => {

const objectToRunAgainst = {
ortooElemIdPrefix: 'theprefix',
ortooIdConfiguration: {
field1: '',
field2: 'thefield2'
}
};

configureElementIdGenerator( objectToRunAgainst );

expect( objectToRunAgainst.field1 ).toBe( 'theprefix' );
expect( objectToRunAgainst.field2 ).toBe( 'theprefix-thefield2' );
});

it( 'will throw an error if given an object with no ortooElemIdPrefix property', () => {

const objectToRunAgainst = {
ortooIdConfiguration: {
field1: '',
field2: 'thefield2'
}
};

expect( () => configureElementIdGenerator( objectToRunAgainst ) ).toThrow();
});
it( 'will throw an error if given an object with an empty ortooElemIdPrefix property', () => {

const objectToRunAgainst = {
ortooElemIdPrefix: '',
ortooIdConfiguration: {
field1: '',
field2: 'thefield2'
}
};

expect( () => configureElementIdGenerator( objectToRunAgainst ) ).toThrow();
});
it( 'will throw an error if given an object with a null ortooElemIdPrefix property', () => {

const objectToRunAgainst = {
ortooElemIdPrefix: null,
ortooIdConfiguration: {
field1: '',
field2: 'thefield2'
}
};

expect( () => configureElementIdGenerator( objectToRunAgainst ) ).toThrow();
});

it( 'will throw an error if given an object with no ortooIdConfiguration property', () => {

const objectToRunAgainst = {
ortooElemIdPrefix: 'theprefix'
};

expect( () => configureElementIdGenerator( objectToRunAgainst ) ).toThrow();
});

it( 'will not throw an error if given an object with an empty object for the ortooIdConfiguration property', () => {

const objectToRunAgainst = {
ortooElemIdPrefix: 'theprefix',
ortooIdConfiguration: {}
};

expect( () => configureElementIdGenerator( objectToRunAgainst ) ).not.toThrow();
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import reportValidity from 'c/formValidator';

let showToastEvent = function ShowToastEvent( details ) {
const showToastEvent = function ShowToastEvent( details ) {
this.detail = details
};

Expand All @@ -14,10 +14,10 @@ describe('c-form-validator', () => {

it( 'When called against an object, will ask it for all the data-validateable elements and then call reportValidity against each one. If all are true, returns true', () => {

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

let objectToRunAgainst = {
const objectToRunAgainst = {
dispatchEvent: mockDispatchEvent,
template: {
querySelectorAll: mockQuerySelector
Expand All @@ -31,7 +31,7 @@ describe('c-form-validator', () => {
]
);

let response = reportValidity.call( objectToRunAgainst );
const response = reportValidity.call( objectToRunAgainst );

expect( response ).toBe( true );

Expand All @@ -43,10 +43,10 @@ describe('c-form-validator', () => {

it( 'When an element reportValidity returns false, will return false and raise a toast', () => {

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

let objectToRunAgainst = {
const objectToRunAgainst = {
dispatchEvent: mockDispatchEvent,
template: {
querySelectorAll: mockQuerySelector
Expand All @@ -60,13 +60,13 @@ describe('c-form-validator', () => {
]
);

let response = reportValidity.call( objectToRunAgainst );
const response = reportValidity.call( objectToRunAgainst );

expect( response ).toBe( false );

expect( mockDispatchEvent ).toHaveBeenCalledTimes( 1 );

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

expect( dispatchedEvent.detail.title ).toBe( 'c.ortoo_core_error_title' );
expect( dispatchedEvent.detail.message ).toBe( 'c.ortoo_core_validation_errors_occurred' );
Expand All @@ -76,17 +76,17 @@ describe('c-form-validator', () => {
it( 'When an element reportValidity returns false, will still call against all the others', () => {
// this ensures that every object is checked - otherwise not all errors will be reported properly

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

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

let mockSelectorReturn = [
const mockSelectorReturn = [
{ reportValidity: jest.fn().mockReturnValueOnce( true ) },
{ reportValidity: jest.fn().mockReturnValueOnce( false ) },
{ reportValidity: jest.fn().mockReturnValueOnce( false ) },
Expand All @@ -96,18 +96,18 @@ describe('c-form-validator', () => {

mockQuerySelector.mockReturnValueOnce( mockSelectorReturn );

let response = reportValidity.call( objectToRunAgainst );
const response = reportValidity.call( objectToRunAgainst );

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();
const mockQuerySelector = jest.fn();
const mockDispatchEvent = jest.fn();

let objectToRunAgainst = {
const objectToRunAgainst = {
dispatchEvent: mockDispatchEvent,
template: {
querySelectorAll: mockQuerySelector
Expand All @@ -128,10 +128,10 @@ describe('c-form-validator', () => {

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();
const mockQuerySelector = jest.fn();
const mockDispatchEvent = jest.fn();

let objectToRunAgainst = {
const objectToRunAgainst = {
dispatchEvent: mockDispatchEvent,
template: {
querySelectorAll: mockQuerySelector
Expand All @@ -151,10 +151,10 @@ describe('c-form-validator', () => {
});
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();
const mockQuerySelector = jest.fn();
const mockDispatchEvent = jest.fn();

let objectToRunAgainst = {
const objectToRunAgainst = {
dispatchEvent: mockDispatchEvent,
template: {
querySelectorAll: mockQuerySelector
Expand All @@ -168,17 +168,17 @@ describe('c-form-validator', () => {
]
);

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

let response = reportValidity.call( objectToRunAgainst, { validationErrorTitle: customTitle, validationErrorMessage: customMessage, toastVariant: customVariant } );
const 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];
const dispatchedEvent = mockDispatchEvent.mock.calls[0][0];

expect( dispatchedEvent.detail.title ).toBe( customTitle );
expect( dispatchedEvent.detail.message ).toBe( customMessage );
Expand Down

0 comments on commit 87088b5

Please sign in to comment.