diff --git a/force-app/main/default/lwc/productCard/__test__/getRecord.json b/force-app/main/default/lwc/productCard/__test__/getRecord.json new file mode 100644 index 00000000..ee9c7d91 --- /dev/null +++ b/force-app/main/default/lwc/productCard/__test__/getRecord.json @@ -0,0 +1,7 @@ +{ + "fields": { + "Name": { + "value": "DYNAMO X1" + } + } +} diff --git a/force-app/main/default/lwc/productCard/__test__/productCard.spec.js b/force-app/main/default/lwc/productCard/__test__/productCard.spec.js new file mode 100644 index 00000000..b8f31a48 --- /dev/null +++ b/force-app/main/default/lwc/productCard/__test__/productCard.spec.js @@ -0,0 +1,40 @@ +import { + createElement +} from 'lwc'; +import {registerLdsTestWireAdapter} from '@salesforce/sfdx-lwc-jest'; +import ProductCard from '../productCard'; // updated - grabbing locally instead of c/productCard - not extensible +import {getRecord} from 'lightning/uiRecordApi'; //<-- problem - THIS is not extensible + + + +// Import mock data to send through the wire adapter. +const mockGetRecord = require('./getRecord.json'); + +// Register a test wire adapter. - 2.0 way! +const getRecordWireAdapter = registerLdsTestWireAdapter(getRecord); + +describe('@wire demonstration test', () => { +// Disconnect the component to reset the adapter. It is also +// a best practice to clean up after each test. + afterEach(() => { + while (document.body.firstChild) { + document.body.removeChild(document.body.firstChild); + } + }); + + it('displays product name field', () => { + const element = createElement('c-product_filter', { + is: ProductCard + }); + document.body.appendChild(element); + getRecordWireAdapter.emit(mockGetRecord); + +// Resolve a promise to wait for a rerender of the new content. + return Promise.resolve().then(() => { + const content = element.shadowRoot.querySelector('.content'); + const nameField = mockGetRecord.fields.Name.value; + expect(content.textContent).toBe(`Name:${nameField}`); // both not null + + }); + }); +}); diff --git a/force-app/main/default/lwc/productCard/productCard.html b/force-app/main/default/lwc/productCard/productCard.html new file mode 100644 index 00000000..9733d1a2 --- /dev/null +++ b/force-app/main/default/lwc/productCard/productCard.html @@ -0,0 +1,11 @@ + + diff --git a/force-app/main/default/lwc/productCard/productCard.js b/force-app/main/default/lwc/productCard/productCard.js new file mode 100644 index 00000000..112004f1 --- /dev/null +++ b/force-app/main/default/lwc/productCard/productCard.js @@ -0,0 +1,23 @@ +import { LightningElement, wire } from 'lwc'; + +// Wire adapter to load records. +import { getRecord } from 'lightning/uiRecordApi'; + +export default class ProductCard extends LightningElement { + // Id of Product__c to display. + recordId; + + // Product__c to display // + product; + + // Product__c field values to display. // + name = ''; + + @wire(getRecord, { recordId: '$recordId', fields }) + wiredRecord({ data }) { + if (data) { + this.product = data; + this.name = data.fields.Name.value; + } + } +} diff --git a/force-app/stubs/lightning/uiRecordApi/uiRecordApi.js b/force-app/stubs/lightning/uiCRAPRecordApi/uiRecordApi.js similarity index 100% rename from force-app/stubs/lightning/uiRecordApi/uiRecordApi.js rename to force-app/stubs/lightning/uiCRAPRecordApi/uiRecordApi.js diff --git a/sfdx-project.json b/sfdx-project.json index 11a3f492..eee825e7 100644 --- a/sfdx-project.json +++ b/sfdx-project.json @@ -11,5 +11,5 @@ ], "namespace": "", "sfdcLoginUrl": "https://login.salesforce.com", - "sourceApiVersion": "50.0" + "sourceApiVersion": "51.0" }