Skip to content

Commit

Permalink
More error tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffdaley committed Jul 13, 2023
1 parent 2d9ae6e commit e10b053
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 6 deletions.
4 changes: 3 additions & 1 deletion web/app/components/document/sidebar/related-resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions web/app/components/document/sidebar/section-header.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
{{#if this.buttonIsShown}}
<Action
{{on "click" this.buttonAction}}
data-test-section-header-button-for={{@title}}
class="sidebar-section-header-button"
aria-label={{or @buttonLabel "Add"}}
>
Expand Down
2 changes: 2 additions & 0 deletions web/mirage/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
3 changes: 2 additions & 1 deletion web/mirage/factories/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -38,5 +39,5 @@ export default Factory.extend({
value: "This is a test document",
},
},
owners: ["Test user"],
owners: ["[email protected]"],
});
31 changes: 30 additions & 1 deletion web/tests/acceptance/authenticated/document-test.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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");
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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]";
Expand Down Expand Up @@ -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]";

Expand Down Expand Up @@ -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<DocumentSidebarRelatedResourcesTestContext>(hbs`
<Document::Sidebar::RelatedResources
@productArea={{this.document.product}}
@objectID={{this.document.objectID}}
@allowAddingExternalLinks={{true}}
@headerTitle="Test title"
@modalHeaderTitle="Add related resource"
@modalInputPlaceholder="Test placeholder"
/>
`);

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"
);
});
}
);

0 comments on commit e10b053

Please sign in to comment.