From e10b0534505c08a0e2bb86d254ef639c70d8a917 Mon Sep 17 00:00:00 2001 From: Jeff Daley Date: Thu, 13 Jul 2023 12:27:46 -0400 Subject: [PATCH] More error tests --- .../document/sidebar/related-resources.ts | 4 +- .../document/sidebar/section-header.hbs | 1 + web/mirage/config.ts | 2 + web/mirage/factories/document.ts | 3 +- .../acceptance/authenticated/document-test.ts | 31 ++++++++++- .../sidebar/related-resources-test.ts | 51 +++++++++++++++++-- 6 files changed, 86 insertions(+), 6 deletions(-) diff --git a/web/app/components/document/sidebar/related-resources.ts b/web/app/components/document/sidebar/related-resources.ts index 6dc3b4f54..7a3025188 100644 --- a/web/app/components/document/sidebar/related-resources.ts +++ b/web/app/components/document/sidebar/related-resources.ts @@ -441,10 +441,12 @@ export default class DocumentSidebarRelatedResourcesComponent extends Component< }, } ); + throw new Error("should show error"); } catch (e: unknown) { + console.log("should show error"); this.flashMessages.add({ title: "Save error", - message: e as string, + message: (e as any).message, type: "critical", sticky: true, extendedTimeout: 1000, diff --git a/web/app/components/document/sidebar/section-header.hbs b/web/app/components/document/sidebar/section-header.hbs index fbc1ca962..00db99bf5 100644 --- a/web/app/components/document/sidebar/section-header.hbs +++ b/web/app/components/document/sidebar/section-header.hbs @@ -16,6 +16,7 @@ {{#if this.buttonIsShown}} diff --git a/web/mirage/config.ts b/web/mirage/config.ts index b1218d1a3..72efde958 100644 --- a/web/mirage/config.ts +++ b/web/mirage/config.ts @@ -111,6 +111,8 @@ export default function (mirageConfig) { * Algolia has several search hosts, e.g., appID-1.algolianet.com, * and Mirage doesn't support wildcards in routes. * So, we create a route for each host. + * + * TODO: Export this into a function that can be used in tests also. */ let algoliaSearchHosts = []; diff --git a/web/mirage/factories/document.ts b/web/mirage/factories/document.ts index 20ab984ab..c842d17aa 100644 --- a/web/mirage/factories/document.ts +++ b/web/mirage/factories/document.ts @@ -28,6 +28,7 @@ export default Factory.extend({ docType: "RFC", modifiedAgo: 1000000000, modifiedTime: 1, + appCreated: true, docNumber() { // @ts-ignore - Mirage types are wrong // See discussion at https://github.com/miragejs/miragejs/pull/525 @@ -38,5 +39,5 @@ export default Factory.extend({ value: "This is a test document", }, }, - owners: ["Test user"], + owners: ["testuser@example.com"], }); diff --git a/web/tests/acceptance/authenticated/document-test.ts b/web/tests/acceptance/authenticated/document-test.ts index e72b3df0a..d4282e989 100644 --- a/web/tests/acceptance/authenticated/document-test.ts +++ b/web/tests/acceptance/authenticated/document-test.ts @@ -1,10 +1,15 @@ -import { click, findAll, visit } from "@ember/test-helpers"; +import { click, findAll, visit, waitFor } from "@ember/test-helpers"; import { setupApplicationTest } from "ember-qunit"; import { module, test } from "qunit"; import { authenticateSession } from "ember-simple-auth/test-support"; import { MirageTestContext, setupMirage } from "ember-cli-mirage/test-support"; import { getPageTitle } from "ember-page-title/test-support"; +const ADD_RELATED_RESOURCE_BUTTON_SELECTOR = + "[data-test-section-header-button-for='Related resources']"; +const ADD_RELATED_DOCUMENT_OPTION_SELECTOR = ".related-document-option"; +const FLASH_MESSAGE_SELECTOR = "[data-test-flash-notification]"; + interface AuthenticatedDocumentRouteTestContext extends MirageTestContext {} module("Acceptance | authenticated/document", function (hooks) { @@ -112,4 +117,28 @@ module("Acceptance | authenticated/document", function (hooks) { .dom("[data-test-product-select]") .doesNotExist("published docs don't show a product select element"); }); + + test("a flash message displays when a related resource fails to save", async function (this: AuthenticatedDocumentRouteTestContext, assert) { + this.server.create("document", { + objectID: 1, + title: "Test Document", + product: "Test Product 0", + appCreated: true, + status: "In review", + }); + + await visit("/document/1"); + + this.server.put("/documents/:document_id/related-resources", {}, 500); + + await click(ADD_RELATED_RESOURCE_BUTTON_SELECTOR); + + await waitFor(ADD_RELATED_DOCUMENT_OPTION_SELECTOR); + + await click(ADD_RELATED_DOCUMENT_OPTION_SELECTOR); + + await waitFor(FLASH_MESSAGE_SELECTOR); + + assert.dom(FLASH_MESSAGE_SELECTOR).containsText("Save error"); + }); }); diff --git a/web/tests/integration/components/document/sidebar/related-resources-test.ts b/web/tests/integration/components/document/sidebar/related-resources-test.ts index a33ff3aae..50790b355 100644 --- a/web/tests/integration/components/document/sidebar/related-resources-test.ts +++ b/web/tests/integration/components/document/sidebar/related-resources-test.ts @@ -5,6 +5,7 @@ import { hbs } from "ember-cli-htmlbars"; import { MirageTestContext, setupMirage } from "ember-cli-mirage/test-support"; import { HermesDocument } from "hermes/types/document"; import { Response } from "miragejs"; +import config from "hermes/config/environment"; const LOADING_ICON_SELECTOR = "[data-test-related-resources-list-loading-icon]"; const LIST_SELECTOR = "[data-test-related-resources-list]"; @@ -36,13 +37,10 @@ const ADD_RELATED_RESOURCES_SEARCH_INPUT_SELECTOR = const NO_RESOURCES_FOUND_SELECTOR = "[data-test-no-related-resources-found]"; const ADD_EXTERNAL_RESOURCE_FORM_SELECTOR = "[data-test-add-external-resource-form]"; -const EXTERNAL_RESOURCE_PREVIEW_URL_SELECTOR = - "[data-test-add-external-resource-truncated-url]"; const ADD_EXTERNAL_RESOURCE_SUBMIT_BUTTON_SELECTOR = "[data-test-add-external-resource-submit-button]"; const ADD_EXTERNAL_RESOURCE_MODAL_DELETE_BUTTON_SELECTOR = "[data-test-edit-related-resource-modal-delete-button]"; - const ADD_EXTERNAL_RESOURCE_ERROR_SELECTOR = "[data-test-add-external-resource-error]"; @@ -518,5 +516,52 @@ module( .dom(NO_RESOURCES_FOUND_SELECTOR) .exists("the fallback message is shown"); }); + + test("it shows an error when searching fails", async function (this: DocumentSidebarRelatedResourcesTestContext, assert) { + this.server.createList("document", 3); + + this.server.post( + `https://${config.algolia.appID}-dsn.algolia.net/1/indexes/**`, + () => { + return new Response(500, {}, {}); + } + ); + + let algoliaSearchHosts = []; + + for (let i = 1; i <= 9; i++) { + algoliaSearchHosts.push( + `https://${config.algolia.appID}-${i}.algolianet.com/1/indexes/**` + ); + } + + algoliaSearchHosts.forEach((host) => { + this.server.post(host, () => { + return new Response(500, {}, {}); + }); + }); + + await render(hbs` + + `); + + await click(ADD_RESOURCE_BUTTON_SELECTOR); + + await waitFor(NO_RESOURCES_FOUND_SELECTOR); + + assert + .dom(NO_RESOURCES_FOUND_SELECTOR) + .containsText( + "Search error. Type to retry.", + "the error message is shown in the modal" + ); + }); } );