Skip to content

Commit

Permalink
Added tests for the errorRenderer
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-baillie-ortoo committed Dec 15, 2021
1 parent e8692bc commit 3affc08
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/create-org-and-deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,13 @@ jobs:
- name: Run Apex Unit Tests
run: sfdx force:apex:test:run -r human -u "${{env.ORG_ALIAS_PREFIX}}${{github.run_number}}" --codecoverage --wait 20 | grep -v ' Pass '; test ${PIPESTATUS[0]} -eq 0

# Prepate Jest
- name: Install modules
# Prepare Jest Modules

- name: Prepare Jest Modules
run: npm install

# Run Jest Unit Tests

- name: Run Jest Unit Tests
run: npm test

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import displayError from 'c/errorRenderer';

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

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

describe('c-error-renderer', () => {
afterEach(() => {
});

it( 'When the passed error is a string, will raise a toast with the message as the given text', () => {

console.error = jest.fn();

let objectToRunAgainst = {
dispatchEvent: jest.fn()
};

let error = 'An error string';

displayError.call( objectToRunAgainst, error );

expect( objectToRunAgainst.dispatchEvent ).toBeCalled();

let dispatchedEvent = objectToRunAgainst.dispatchEvent.mock.calls[0][0];

expect( dispatchedEvent.detail.title ).toBe( 'c.ortoo_core_error_title' );
expect( dispatchedEvent.detail.message ).toBe( error );
expect( dispatchedEvent.detail.variant ).toBe( 'error' );

expect( console.error ).toHaveBeenCalledTimes( 1 );
let reportedError = console.error.mock.calls[0][0];

expect( reportedError ).toBe( error );
});

it( 'When the passed error is an Apex exception object, will raise a toast with the message as the given text', () => {

console.error = jest.fn();

let objectToRunAgainst = {
dispatchEvent: jest.fn()
};

let error = {
body: {
message: 'An error message in the body'
}
}

displayError.call( objectToRunAgainst, error );

expect( objectToRunAgainst.dispatchEvent ).toBeCalled();

let dispatchedEvent = objectToRunAgainst.dispatchEvent.mock.calls[0][0];

expect( dispatchedEvent.detail.title ).toBe( 'c.ortoo_core_error_title' );
expect( dispatchedEvent.detail.message ).toBe( 'An error message in the body' );
expect( dispatchedEvent.detail.variant ).toBe( 'error' );

expect( console.error ).toHaveBeenCalledTimes( 1 );
let reportedError = console.error.mock.calls[0][0];

expect( reportedError ).toBe( error );
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const displayError = function( error ) {
let title = ERROR_TITLE;
let message = error;

console.error( 'Error reported:', error );
console.error( error );

if ( error.body ) {
message = error.body.message;
Expand Down

0 comments on commit 3affc08

Please sign in to comment.