Skip to content

Commit

Permalink
simplify w/ chained ?. and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamLiechty committed Sep 26, 2023
1 parent f6a3951 commit 72deaf8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { graphql } from "lightning/uiGraphQLApi";
import AccountRelatedContacts from "c/accountRelatedContacts";

const mockGraphQL = require("./data/graphql.json");
const mockGraphQLEmpty = require("./data/graphql-empty.json");

describe("c-account-related-contacts", () => {
afterEach(() => {
Expand Down Expand Up @@ -35,4 +36,22 @@ describe("c-account-related-contacts", () => {
expect(details[0].textContent).toBe("415-555-5555");
expect(details[1].textContent).toBe("[email protected]");
});

it("should handle an incomplete graphql response", async () => {
// setup
const element = createElement("c-account-related-contacts", {
is: AccountRelatedContacts,
});
element.recordId = "0011700000pJRRSAA4";

document.body.appendChild(element);

graphql.emit(mockGraphQLEmpty);
// Resolve a promise to wait for a re-render of the new content
await Promise.resolve();

// test
const title = element.shadowRoot.querySelector("h3");
expect(title).toBeNull();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"uiapi": {
"query": {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { graphql, gql } from "lightning/uiGraphQLApi";

// eslint-disable-next-line @salesforce/lwc-graph-analyzer/no-unresolved-parent-class-reference
export default class AccountRelatedContacts extends NavigationMixin(
LightningElement,
LightningElement
) {
@api recordId;

Expand Down Expand Up @@ -54,11 +54,9 @@ export default class AccountRelatedContacts extends NavigationMixin(
})
graphqlResult({ data /* errors */ }) {
this.contacts = null;
if (data && data.uiapi && data.uiapi.query && data.uiapi.query.Account) {
const accounts = data.uiapi.query.Account.edges;
if (accounts && accounts[0]) {
this.contacts = accounts[0].node.Contacts.edges.map((e) => e.node);
}
const accounts = data?.uiapi?.query?.Account?.edges;
if (accounts && accounts[0]) {
this.contacts = accounts[0].node.Contacts.edges.map((e) => e.node);
}
}
contacts;
Expand Down

0 comments on commit 72deaf8

Please sign in to comment.