Skip to content

Commit

Permalink
Pozil/sortable results (#28)
Browse files Browse the repository at this point in the history
* Bump node dependencies

* Bump api version to v48.0

* Increased code coverage

* Sortable search results

* Updated prettier settings

* Fixed selection public property reassignment
  • Loading branch information
pozil authored Mar 26, 2020
1 parent 1791d54 commit 0145fbc
Show file tree
Hide file tree
Showing 23 changed files with 1,737 additions and 2,345 deletions.
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"trailingComma": "none",
"singleQuote": true,
"tabWidth": 4,
"printWidth": 120,
"overrides": [
{
"files": "**/lwc/**/*.html",
Expand Down
4 changes: 1 addition & 3 deletions install-scripts/check-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ const pjson = require('../package.json');

const version = pjson.engines.node;
if (!semver.satisfies(process.version, version)) {
console.log(
`\n\nRequired node version ${version} not satisfied with current version ${process.version}.\n\n`
);
console.log(`\n\nRequired node version ${version} not satisfied with current version ${process.version}.\n\n`);
process.exit(1);
}
3,720 changes: 1,559 additions & 2,161 deletions package-lock.json

Large diffs are not rendered by default.

21 changes: 9 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "sfdc-ui-lookup-lwc",
"private": true,
"version": "1.0.7",
"version": "1.2.0",
"description": "Salesforce Lookup Component (Lightning Web Components version)",
"engines": {
"node": ">= 10.13.0",
Expand All @@ -19,14 +19,14 @@
},
"author": "Philippe Ozil",
"devDependencies": {
"@salesforce/eslint-config-lwc": "^0.4.0",
"@salesforce/sfdx-lwc-jest": "^0.6.2",
"eslint": "^5.16.0",
"husky": "^3.1.0",
"lint-staged": "^9.5.0",
"prettier": "^1.19.1",
"prettier-plugin-apex": "^1.1.0",
"semver": "^6.3.0"
"@salesforce/eslint-config-lwc": "^0.5.0",
"@salesforce/sfdx-lwc-jest": "^0.7.1",
"eslint": "^6.8.0",
"husky": "^4.2.3",
"lint-staged": "^10.0.9",
"prettier": "^2.0.2",
"prettier-plugin-apex": "^1.3.0",
"semver": "^7.1.3"
},
"husky": {
"hooks": {
Expand All @@ -39,9 +39,6 @@
],
"**/lwc/**": [
"eslint"
],
"*": [
"git add"
]
},
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion sfdx-project.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
],
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "47.0"
"sourceApiVersion": "48.0"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<AuraDefinitionBundle xmlns="urn:metadata.tooling.soap.sforce.com" fqn="SampleLookupApp">
<apiVersion>47.0</apiVersion>
<apiVersion>48.0</apiVersion>
<description>A Lightning Application Bundle</description>
</AuraDefinitionBundle>
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
<aura:component isTemplate="true" extends="aura:template">
<aura:set attribute="auraResetStyle" value="" />
<aura:set attribute="auraPreInitBlock">
<auraStorage:init
name="actions"
persistent="false"
secure="false"
maxSize="1024"
/>
<auraStorage:init name="actions" persistent="false" secure="false" maxSize="1024" />
</aura:set>
</aura:component>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<AuraDefinitionBundle xmlns="urn:metadata.tooling.soap.sforce.com" fqn="SampleLookupAppTemplate">
<apiVersion>47.0</apiVersion>
<apiVersion>48.0</apiVersion>
<description>A Lightning Component Bundle</description>
</AuraDefinitionBundle>
12 changes: 6 additions & 6 deletions src-sample/main/default/classes/SampleLookupController.cls
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ public with sharing class SampleLookupController {
private final static Integer MAX_RESULTS = 5;

@AuraEnabled(Cacheable=true)
public static List<LookupSearchResult> search(
String searchTerm,
List<String> selectedIds
) {
public static List<LookupSearchResult> search(String searchTerm, List<String> selectedIds) {
// Prepare query paramters
searchTerm += '*';

Expand All @@ -24,7 +21,7 @@ public with sharing class SampleLookupController {

// Extract Accounts & convert them into LookupSearchResult
String accountIcon = 'standard:account';
Account[] accounts = ((List<Account>) searchResults[0]);
Account[] accounts = (List<Account>) searchResults[0];
for (Account account : accounts) {
results.add(
new LookupSearchResult(
Expand All @@ -39,7 +36,7 @@ public with sharing class SampleLookupController {

// Extract Opportunities & convert them into LookupSearchResult
String opptyIcon = 'standard:opportunity';
Opportunity[] opptys = ((List<Opportunity>) searchResults[1]);
Opportunity[] opptys = (List<Opportunity>) searchResults[1];
for (Opportunity oppty : opptys) {
results.add(
new LookupSearchResult(
Expand All @@ -52,6 +49,9 @@ public with sharing class SampleLookupController {
);
}

// Optionnaly sort all results on title
results.sort();

return results;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="urn:metadata.tooling.soap.sforce.com" fqn="SampleLookupController">
<apiVersion>47.0</apiVersion>
<apiVersion>48.0</apiVersion>
<status>Active</status>
</ApexClass>
41 changes: 23 additions & 18 deletions src-sample/main/default/classes/SampleLookupControllerTest.cls
Original file line number Diff line number Diff line change
@@ -1,43 +1,48 @@
@isTest
@isTest(isParallel=true)
public class SampleLookupControllerTest {
static testMethod void search_should_return_Account() {
Id[] fixedResults = new List<Id>(1);
Account account = createTestAccount('Account');
@isTest
static void search_should_return_Account_and_Opportunity() {
List<Id> fixedResults = new List<Id>(2);
Account account = createAccount('Account');
fixedResults.add(account.Id);
Opportunity oppty = createOpportunity('Oppty');
fixedResults.add(oppty.Id);
Test.setFixedSearchResults(fixedResults);
List<String> selectedIds = new List<String>();

List<LookupSearchResult> results = SampleLookupController.search(
'Acc',
selectedIds
);
List<LookupSearchResult> results = SampleLookupController.search('Acc', selectedIds);

System.assertEquals(1, results.size());
System.assertEquals(2, results.size());
System.assertEquals(account.Id, results.get(0).getId());
System.assertEquals(oppty.Id, results.get(1).getId());
}

static testMethod void search_should_not_return_selected_item() {
Id[] fixedResults = new List<Id>(1);
Account account1 = createTestAccount('Account1');
@isTest
static void search_should_not_return_selected_item() {
List<Id> fixedResults = new List<Id>(2);
Account account1 = createAccount('Account1');
fixedResults.add(account1.Id);
Account account2 = createTestAccount('Account2');
Account account2 = createAccount('Account2');
fixedResults.add(account2.Id);
Test.setFixedSearchResults(fixedResults);
List<String> selectedIds = new List<String>();
selectedIds.add(account2.Id);

List<LookupSearchResult> results = SampleLookupController.search(
'Acc',
selectedIds
);
List<LookupSearchResult> results = SampleLookupController.search('Acc', selectedIds);

System.assertEquals(1, results.size());
System.assertEquals(account1.Id, results.get(0).getId());
}

private static Account createTestAccount(String name) {
private static Account createAccount(String name) {
Account account = new Account(Name = name);
insert account;
return account;
}

private static Opportunity createOpportunity(String name) {
Opportunity oppty = new Opportunity(Name = name, StageName = 'open', CloseDate = Date.today());
insert oppty;
return oppty;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="urn:metadata.tooling.soap.sforce.com" fqn="SampleLookupControllerTest">
<apiVersion>47.0</apiVersion>
<apiVersion>48.0</apiVersion>
<status>Active</status>
</ApexClass>
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@
>
</c-lookup>

<lightning-button
variant="brand"
label="Submit"
onclick={handleSubmit}
></lightning-button>
<lightning-button variant="brand" label="Submit" onclick={handleSubmit}></lightning-button>
</div>
</lightning-card>
</template>
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,11 @@ export default class SampleLookupContainer extends LightningElement {

handleSearch(event) {
apexSearch(event.detail)
.then(results => {
this.template
.querySelector('c-lookup')
.setSearchResults(results);
.then((results) => {
this.template.querySelector('c-lookup').setSearchResults(results);
})
.catch(error => {
this.notifyUser(
'Lookup Error',
'An error occured while searching with the lookup field.',
'error'
);
.catch((error) => {
this.notifyUser('Lookup Error', 'An error occured while searching with the lookup field.', 'error');
// eslint-disable-next-line no-console
console.error('Lookup error', JSON.stringify(error));
this.errors = [error];
Expand All @@ -57,9 +51,7 @@ export default class SampleLookupContainer extends LightningElement {
}

checkForErrors() {
const selection = this.template
.querySelector('c-lookup')
.getSelection();
const selection = this.template.querySelector('c-lookup').getSelection();
if (selection.length === 0) {
this.errors = [
{ message: 'You must make a selection before submitting!' },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="sampleLookupContainer">
<apiVersion>47.0</apiVersion>
<apiVersion>48.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
Expand Down
24 changes: 16 additions & 8 deletions src/main/default/classes/LookupSearchResult.cls
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,14 @@
* Class used to serialize a single Lookup search result item
* The Lookup controller returns a List<LookupSearchResult> when sending search result back to Lightning
*/
public class LookupSearchResult {
public class LookupSearchResult implements Comparable {
private Id id;
private String sObjectType;
private String icon;
private String title;
private String subtitle;

public LookupSearchResult(
Id id,
String sObjectType,
String icon,
String title,
String subtitle
) {
public LookupSearchResult(Id id, String sObjectType, String icon, String title, String subtitle) {
this.id = id;
this.sObjectType = sObjectType;
this.icon = icon;
Expand Down Expand Up @@ -47,4 +41,18 @@ public class LookupSearchResult {
public String getSubtitle() {
return subtitle;
}

/**
* Allow to sort search results based on title
*/
public Integer compareTo(Object compareTo) {
LookupSearchResult other = (LookupSearchResult) compareTo;
if (this.getTitle() == null) {
return (other.getTitle() == null) ? 0 : 1;
}
if (other.getTitle() == null) {
return -1;
}
return this.getTitle().compareTo(other.getTitle());
}
}
2 changes: 1 addition & 1 deletion src/main/default/classes/LookupSearchResult.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="urn:metadata.tooling.soap.sforce.com" fqn="LookupSearchResult">
<apiVersion>47.0</apiVersion>
<apiVersion>48.0</apiVersion>
<status>Active</status>
</ApexClass>
58 changes: 58 additions & 0 deletions src/main/default/classes/LookupSearchResultTests.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
@isTest(isParallel=true)
public class LookupSearchResultTests {
@isTest
static void compareTo_should_work_with_two_null_titles() {
LookupSearchResult r1 = getSearchResult(null);
LookupSearchResult r2 = getSearchResult(null);

Integer compareResult = r1.compareTo(r2);

System.assertEquals(0, compareResult);
}

@isTest
static void compareTo_should_work_with_this_null_title() {
LookupSearchResult r1 = getSearchResult(null);
LookupSearchResult r2 = getSearchResult('a');

Integer compareResult = r1.compareTo(r2);

System.assertEquals(1, compareResult);
}

@isTest
static void compareTo_should_work_with_other_null_title() {
LookupSearchResult r1 = getSearchResult('a');
LookupSearchResult r2 = getSearchResult(null);

Integer compareResult = r1.compareTo(r2);

System.assertEquals(-1, compareResult);
}

@isTest
static void compareTo_should_work_with_non_null_titles() {
LookupSearchResult r1 = getSearchResult('a');
LookupSearchResult r2 = getSearchResult('b');

Integer compareResult = r1.compareTo(r2);

System.assertEquals(-1, compareResult);
}

@isTest
static void getters_should_work() {
// For the sake of code coverage
LookupSearchResult r = new LookupSearchResult('0010R00000yvEyRQAU', 'type', 'icon', 'title', 'subtitle');

System.assertEquals('0010R00000yvEyRQAU', r.getId());
System.assertEquals('type', r.getSObjectType());
System.assertEquals('icon', r.getIcon());
System.assertEquals('title', r.getTitle());
System.assertEquals('subtitle', r.getSubtitle());
}

private static LookupSearchResult getSearchResult(String title) {
return new LookupSearchResult(null, null, null, title, null);
}
}
5 changes: 5 additions & 0 deletions src/main/default/classes/LookupSearchResultTests.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>48.0</apiVersion>
<status>Active</status>
</ApexClass>
Loading

0 comments on commit 0145fbc

Please sign in to comment.