-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3240ef9
commit 4d55a02
Showing
14 changed files
with
422 additions
and
3 deletions.
There are no files selected for viewing
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
98 changes: 98 additions & 0 deletions
98
src-lookup-sample/main/default/classes/AccountContactLookup.cls
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,98 @@ | ||
public class AccountContactLookup implements LookupResult.ILookupResult { | ||
|
||
public final static Map<String, Object> CONTACT_ICON = new Map<String, Object>{ | ||
'iconName' => 'standard:contact' | ||
}; | ||
|
||
public List<LookupResult> getSelection(List<String> selectedIds, Map<String, Object> payload) { | ||
|
||
String accountId = (String) payload.get('accountId'); | ||
List<LookupResult> result = new List<LookupResult>(); | ||
|
||
if (accountId == null) { | ||
return result; | ||
} | ||
|
||
List<Contact> contacts = [ | ||
SELECT Name, Id, Phone | ||
FROM Contact | ||
WHERE AccountId = :accountId | ||
AND Id IN: selectedIds | ||
]; | ||
|
||
for (Contact contact : contacts) { | ||
result.add(buildResult(contact)); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public List<LookupResult> getSearchResults( | ||
String searchTerm, | ||
List<String> selectedIds, | ||
Map<String, Object> payload) { | ||
|
||
String accountId = (String) payload.get('accountId'); | ||
List<LookupResult> result = new List<LookupResult>(); | ||
|
||
if (accountId == null) { | ||
return result; | ||
} | ||
|
||
searchTerm = '%' + searchTerm + '%'; | ||
|
||
List<Contact> contacts = [ | ||
SELECT Name, Email, Phone | ||
FROM Contact | ||
WHERE | ||
(Name LIKE :searchTerm | ||
OR Phone LIKE :searchTerm | ||
OR Email LIKE :searchTerm) | ||
AND AccountId = :accountId | ||
AND Id NOT IN :selectedIds | ||
]; | ||
|
||
for (Contact contact : contacts) { | ||
result.add(buildResult(contact)); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public List<LookupResult> getDefaultSearchResults(Map<String, Object> payload) { | ||
|
||
String accountId = (String) payload.get('accountId'); | ||
List<LookupResult> result = new List<LookupResult>(); | ||
|
||
if (accountId == null) { | ||
return result; | ||
} | ||
|
||
List<Contact> contacts = [ | ||
SELECT Name, id, Phone | ||
FROM Contact | ||
WHERE AccountId = :accountId | ||
LIMIT 10 | ||
]; | ||
|
||
for (Contact contact : contacts) { | ||
result.add(buildResult(contact)); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private LookupResult buildResult(Contact contact) { | ||
|
||
LookupResult result = new LookupResult(); | ||
result.id = contact.Id; | ||
result.icon = CONTACT_ICON; | ||
result.title = contact.Name; | ||
result.recordPayload = new Map<String, Object> { | ||
'name' => contact.Name, | ||
'phone' => contact.Phone | ||
}; | ||
|
||
return result; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src-lookup-sample/main/default/classes/AccountContactLookup.cls-meta.xml
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,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<apiVersion>59.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
56 changes: 56 additions & 0 deletions
56
src-lookup-sample/main/default/classes/AccountContactLookupTest.cls
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,56 @@ | ||
@IsTest | ||
private class AccountContactLookupTest { | ||
@IsTest | ||
static void shouldCreateLookupResults() { | ||
// DATA | ||
Account account = new Account(Name = 'account name test'); | ||
insert account; | ||
|
||
Contact contact = new Contact( | ||
FirstName = 'FirstName', | ||
LastName = 'LastName', | ||
Phone = '123456789', | ||
Email = '[email protected]', | ||
HasOptedOutOfEmail = false, | ||
AccountId = account.Id | ||
); | ||
insert contact; | ||
|
||
Map<String, Object> payload = new Map<String, Object>(); | ||
payload.put('accountId', account.Id); | ||
|
||
// TEST | ||
Test.startTest(); | ||
AccountContactLookup accountContactLookup = new AccountContactLookup(); | ||
List<LookupResult> selectionResult = AccountContactLookup.getSelection( | ||
new List<String>{ contact.id }, | ||
payload | ||
); | ||
List<LookupResult> defaultSearchResultsResult = AccountContactLookup.getDefaultSearchResults( | ||
payload | ||
); | ||
List<LookupResult> searchResultsResult = AccountContactLookup.getSearchResults( | ||
'123', | ||
new List<String>(), | ||
payload | ||
); | ||
Test.stopTest(); | ||
|
||
// ASSERT | ||
Assert.areEqual( | ||
selectionResult[0].id, | ||
contact.id, | ||
'should get the selection' | ||
); | ||
Assert.areEqual( | ||
defaultSearchResultsResult[0].id, | ||
contact.id, | ||
'should set getDefaultResults from recentlyViewed' | ||
); | ||
Assert.areEqual( | ||
searchResultsResult[0].id, | ||
contact.id, | ||
'filters contacts using searchKey' | ||
); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src-lookup-sample/main/default/classes/AccountContactLookupTest.cls-meta.xml
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,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<apiVersion>61.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
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,62 @@ | ||
public class AccountLookup implements LookupResult.ILookupResult { | ||
|
||
public final static Map<String, Object> ACCOUNT_ICON = new Map<String, Object>{ | ||
'iconName' => 'standard:account' | ||
}; | ||
|
||
public List<LookupResult> getSelection(List<String> selectedIds, Map<String, Object> payload) { | ||
|
||
List<LookupResult> result = new List<LookupResult>(); | ||
|
||
for (Account account : [SELECT Id, Name FROM Account WHERE Id IN: selectedIds]) { | ||
result.add(buildResult(account)); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public List<LookupResult> getSearchResults( | ||
String searchTerm, | ||
List<String> selectedIds, | ||
Map<String, Object> payload) { | ||
|
||
List<LookupResult> result = new List<LookupResult>(); | ||
searchTerm = '%' + searchTerm + '%'; | ||
|
||
for (Account account : [ | ||
SELECT Id, Name | ||
FROM Account | ||
WHERE | ||
Name LIKE :searchTerm | ||
AND Id NOT IN :selectedIds | ||
]) { | ||
result.add(buildResult(account)); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public List<LookupResult> getDefaultSearchResults(Map<String, Object> payload) { | ||
|
||
List<LookupResult> result = new List<LookupResult>(); | ||
|
||
for (Account account : [SELECT Id, Name FROM Account LIMIT 10]) { | ||
result.add(buildResult(account)); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private LookupResult buildResult(Account account) { | ||
|
||
LookupResult result = new LookupResult(); | ||
result.id = account.Id; | ||
result.icon = ACCOUNT_ICON; | ||
result.title = account.Name; | ||
result.recordPayload = new Map<String, Object> { | ||
'name' => account.Name | ||
}; | ||
|
||
return result; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src-lookup-sample/main/default/classes/AccountLookup.cls-meta.xml
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,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<apiVersion>59.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
46 changes: 46 additions & 0 deletions
46
src-lookup-sample/main/default/classes/AccountLookupTest.cls
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,46 @@ | ||
@IsTest | ||
private class AccountLookupTest { | ||
@IsTest | ||
static void shouldCreateLookupResults() { | ||
// DATA | ||
Account account = new Account(Name = 'account name test'); | ||
insert account; | ||
|
||
Map<String, Object> payload = new Map<String, Object>(); | ||
payload.put('name', account.Name); | ||
|
||
// TEST | ||
Test.startTest(); | ||
AccountLookup accountLookup = new AccountLookup(); | ||
List<LookupResult> selectionResult = AccountLookup.getSelection( | ||
new List<String>{ account.Id }, | ||
payload | ||
); | ||
List<LookupResult> defaultSearchResultsResult = AccountLookup.getDefaultSearchResults( | ||
payload | ||
); | ||
List<LookupResult> searchResultsResult = AccountLookup.getSearchResults( | ||
'accou', | ||
new List<String>(), | ||
payload | ||
); | ||
Test.stopTest(); | ||
|
||
// ASSERT | ||
Assert.areEqual( | ||
selectionResult[0].Id, | ||
account.Id, | ||
'should get the selection' | ||
); | ||
Assert.areEqual( | ||
defaultSearchResultsResult[0].Id, | ||
account.Id, | ||
'should set getDefaultResults from recentlyViewed' | ||
); | ||
Assert.areEqual( | ||
searchResultsResult[0].Id, | ||
account.Id, | ||
'filters contacts using searchKey' | ||
); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src-lookup-sample/main/default/classes/AccountLookupTest.cls-meta.xml
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,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<apiVersion>61.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
52 changes: 52 additions & 0 deletions
52
src-lookup-sample/main/default/lwc/workingWithPayloads/__tests__/workingWithPayloads.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,52 @@ | ||
import { createElement } from 'lwc'; | ||
import WorkingWithPayloads from 'c/workingWithPayloads'; | ||
|
||
describe('c-working-with-payloads', () => { | ||
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('this should show a text with account name when selected the account and contact name and phone when selected contact', async () => { | ||
// Arrange | ||
const element = createElement('c-working-with-payloads', { | ||
is: WorkingWithPayloads | ||
}); | ||
|
||
document.body.appendChild(element); | ||
|
||
const accountChangeEvent = new CustomEvent('change', { | ||
detail: { | ||
value: ['accountId'], | ||
payload: { | ||
'accountId': { name: 'Account name' } | ||
} | ||
} | ||
}); | ||
|
||
const contactChangeEvent = new CustomEvent('change', { | ||
detail: { | ||
value: ['contactId'], | ||
payload: { | ||
'contactId': { name: 'Contact Name', phone: 'Phone' } | ||
} | ||
} | ||
}); | ||
|
||
// Act | ||
const accountLookup = element.shadowRoot.querySelector('c-lookup[data-id="accountLookup"]'); | ||
accountLookup.dispatchEvent(accountChangeEvent); | ||
|
||
const contactLookup = element.shadowRoot.querySelector('c-lookup[data-id="contactLookup"]'); | ||
contactLookup.dispatchEvent(contactChangeEvent); | ||
|
||
await Promise.resolve(); | ||
// Assert | ||
const selectedAccount = element.shadowRoot.querySelector('p[data-id="selectedAccount"]'); | ||
const selectedContact = element.shadowRoot.querySelector('p[data-id="selectedContact"]'); | ||
expect(selectedAccount.textContent).toBe('Your account selected was Account name'); | ||
expect(selectedContact.textContent).toBe('Your contact selected was Contact Name and its phone number is Phone'); | ||
}); | ||
}); |
Oops, something went wrong.