forked from trailheadapps/ebikes-lwc
-
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.
Test for accountMap LWC (trailheadapps#205)
* creation of the similarProducts test * update to include test for the accountMap component * update to included the eslint prettier configuration * Delete similarProducts.test.js Removed similarProducts.test.js from the account-map-test branch. * update to included requested changes * removal of debug statements * update to include feedback
- Loading branch information
1 parent
969ea54
commit 6d716a9
Showing
3 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
force-app/main/default/lwc/accountMap/__tests__/accountMap.test.js
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,114 @@ | ||
import { createElement } from 'lwc'; | ||
import AccountMap from 'c/accountMap'; | ||
import { getRecord } from 'lightning/uiRecordApi'; | ||
import { registerLdsTestWireAdapter } from '@salesforce/sfdx-lwc-jest'; | ||
|
||
// Realistic data with an accounts address details | ||
const mockGetRecordWithAddress = require('./data/getRecordWithAddress.json'); | ||
const mockGetRecordWithoutAddress = require('./data/getRecordWithoutAddress.json'); | ||
const mockRecordId = '0031700000pJRRSAA4'; | ||
const mockWireErrorMessage = 'Error retrieving record'; | ||
|
||
// Register as a LDS wire adapter. Some tests verify the provisioned values trigger desired behavior. | ||
const getRecordAdapter = registerLdsTestWireAdapter(getRecord); | ||
describe('c-account-map', () => { | ||
afterEach(() => { | ||
// The jsdom instance is shared across test cases in a single file so reset the DOM | ||
while (document.body.firstChild) { | ||
document.body.removeChild(document.body.firstChild); | ||
} | ||
}); | ||
|
||
it('displays a lightning-map when wire adaptor returns an account record with billing street data', () => { | ||
// Create element | ||
const element = createElement('c-account-map', { | ||
is: AccountMap | ||
}); | ||
// Set public properties | ||
element.recordId = mockRecordId; | ||
document.body.appendChild(element); | ||
|
||
// Emit data from the get record adapter that includes billing street data | ||
getRecordAdapter.emit(mockGetRecordWithAddress); | ||
|
||
// Return a promise to wait for any asynchronous DOM updates. Jest | ||
// will automatically wait for the Promise chain to complete before | ||
// ending the test and fail the test if the promise rejects. | ||
return Promise.resolve().then(() => { | ||
// Select elements for validation | ||
const mapEl = element.shadowRoot.querySelector('lightning-map'); | ||
expect(mapEl).not.toBeNull(); | ||
expect(mapEl.zoomLevel).toBe(14); | ||
|
||
// Get the map markers from mapEl to check that the location data has been populated | ||
const location = mapEl.mapMarkers[0].location; | ||
expect(location).toEqual( | ||
expect.objectContaining({ | ||
City: 'San Francisco', | ||
Country: 'USA', | ||
PostalCode: '94105', | ||
State: 'California', | ||
Street: '415 Mission St.' | ||
}) | ||
); | ||
}); | ||
}); | ||
|
||
it('displays an error panel when the wire adaptor returns an empty array', () => { | ||
// Create element | ||
const element = createElement('c-account-map', { | ||
is: AccountMap | ||
}); | ||
// Set public properties | ||
element.recordId = mockRecordId; | ||
document.body.appendChild(element); | ||
|
||
// Emit data from the get record adapter that does not include billing street data | ||
getRecordAdapter.emit(mockGetRecordWithoutAddress); | ||
|
||
// Return a promise to wait for any asynchronous DOM updates. Jest | ||
// will automatically wait for the Promise chain to complete before | ||
// ending the test and fail the test if the promise rejects. | ||
return Promise.resolve().then(() => { | ||
// Select elements for validation | ||
const mapEl = element.shadowRoot.querySelector('lightning-map'); | ||
expect(mapEl).toBeNull(); | ||
const errorPanelEl = element.shadowRoot.querySelector( | ||
'c-error-panel' | ||
); | ||
expect(errorPanelEl).not.toBeNull(); | ||
expect(errorPanelEl.friendlyMessage).toBe('No address to map'); | ||
}); | ||
}); | ||
|
||
it('displays an error panel when wire adapter returns an error', () => { | ||
// Create element | ||
const element = createElement('c-account-map', { | ||
is: AccountMap | ||
}); | ||
// Set public properties | ||
element.recordId = mockRecordId; | ||
document.body.appendChild(element); | ||
|
||
// Emit an error from the getRecord adapter. | ||
getRecordAdapter.error(mockWireErrorMessage); | ||
|
||
// Return a promise to wait for any asynchronous DOM updates. Jest | ||
// will automatically wait for the Promise chain to complete before | ||
// ending the test and fail the test if the promise rejects. | ||
return Promise.resolve().then(() => { | ||
// Select elements for validation | ||
const errorPanelEl = element.shadowRoot.querySelectorAll( | ||
'c-error-panel' | ||
); | ||
// There are two error panels in the component - we need the second to check | ||
// the wire errors are displaying correctly | ||
const errorPanel = errorPanelEl[1]; | ||
expect(errorPanel).not.toBeNull(); | ||
expect(errorPanel.errors.body).toBe(mockWireErrorMessage); | ||
expect(errorPanel.friendlyMessage).toBe( | ||
'Error retrieving map data' | ||
); | ||
}); | ||
}); | ||
}); |
24 changes: 24 additions & 0 deletions
24
force-app/main/default/lwc/accountMap/__tests__/data/getRecordWithAddress.json
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,24 @@ | ||
{ | ||
"fields": { | ||
"BillingCity": { | ||
"displayValue": null, | ||
"value": "San Francisco" | ||
}, | ||
"BillingCountry": { | ||
"displayValue": null, | ||
"value": "USA" | ||
}, | ||
"BillingPostalCode": { | ||
"displayValue": null, | ||
"value": "94105" | ||
}, | ||
"BillingState": { | ||
"displayValue": null, | ||
"value": "California" | ||
}, | ||
"BillingStreet": { | ||
"displayValue": null, | ||
"value": "415 Mission St." | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
force-app/main/default/lwc/accountMap/__tests__/data/getRecordWithoutAddress.json
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,24 @@ | ||
{ | ||
"fields": { | ||
"BillingCity": { | ||
"displayValue": null, | ||
"value": null | ||
}, | ||
"BillingCountry": { | ||
"displayValue": null, | ||
"value": null | ||
}, | ||
"BillingPostalCode": { | ||
"displayValue": null, | ||
"value": null | ||
}, | ||
"BillingState": { | ||
"displayValue": null, | ||
"value": null | ||
}, | ||
"BillingStreet": { | ||
"displayValue": null, | ||
"value": null | ||
} | ||
} | ||
} |