Skip to content

Commit

Permalink
Fix "No recently viewed" bug (#606)
Browse files Browse the repository at this point in the history
* Fix `forEach` error

* Add test

* Update recently-viewed-test.ts
  • Loading branch information
jeffdaley authored Feb 21, 2024
1 parent acfe718 commit f3b606d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
27 changes: 12 additions & 15 deletions web/app/services/recently-viewed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,17 @@ export default class RecentlyViewedService extends Service {
fetchAll = keepLatestTask(async () => {
try {
// Set a promise to fetch the recently viewed docs
const recentlyViewedDocsPromise =
this.fetchSvc
.fetch(
`/api/${this.configSvc.config.api_version}/me/recently-viewed-docs`,
)
.then((resp) => resp?.json()) || [];

const recentlyViewedDocsPromise = this.fetchSvc
.fetch(
`/api/${this.configSvc.config.api_version}/me/recently-viewed-docs`,
)
.then((resp) => resp?.json());
// Set a promise to fetch the recently viewed projects
const recentlyViewedProjectsPromise =
this.fetchSvc
.fetch(
`/api/${this.configSvc.config.api_version}/me/recently-viewed-projects`,
)
.then((resp) => resp?.json()) || [];
const recentlyViewedProjectsPromise = this.fetchSvc
.fetch(
`/api/${this.configSvc.config.api_version}/me/recently-viewed-projects`,
)
.then((resp) => resp?.json());

// Await both promises
const [recentlyViewedDocs, recentlyViewedProjects] = await Promise.all([
Expand All @@ -98,7 +95,7 @@ export default class RecentlyViewedService extends Service {
>[] = [];

// Promise to get each doc and return it in the RecentlyViewedDoc format
recentlyViewedDocs.forEach((d: IndexedDoc) => {
recentlyViewedDocs?.forEach((d: IndexedDoc) => {
const endpoint = d.isDraft ? "drafts" : "documents";

fullItemPromises.push(
Expand All @@ -117,7 +114,7 @@ export default class RecentlyViewedService extends Service {
});

// Promise to get each project and return it in the RecentlyViewedProject format
recentlyViewedProjects.forEach((p: IndexedProject) => {
recentlyViewedProjects?.forEach((p: IndexedProject) => {
fullItemPromises.push(
this.fetchSvc
.fetch(`/api/${this.configSvc.config.api_version}/projects/${p.id}`)
Expand Down
6 changes: 4 additions & 2 deletions web/mirage/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,8 @@ export default function (mirageConfig) {
let index = schema.recentlyViewedDocs.all().models.map((doc) => {
return doc.attrs;
});
return new Response(200, {}, index);

return new Response(200, {}, index.length === 0 ? null : index);
});

/**
Expand All @@ -920,7 +921,8 @@ export default function (mirageConfig) {
.models.map((project) => {
return project.attrs;
});
return new Response(200, {}, index);

return new Response(200, {}, index.length === 0 ? null : index);
});

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { module, test, todo } from "qunit";
import { setupRenderingTest } from "ember-qunit";
import { MirageTestContext, setupMirage } from "ember-cli-mirage/test-support";
import { render } from "@ember/test-helpers";
import { render, rerender } from "@ember/test-helpers";
import { hbs } from "ember-cli-htmlbars";
import RecentlyViewedService from "hermes/services/recently-viewed";

Expand Down Expand Up @@ -30,6 +30,15 @@ module("Integration | Component | dashboard/recently-viewed", function (hooks) {

assert.dom(NO_RECENTLY_VIEWED).exists();
assert.dom(ITEM).doesNotExist();

this.server.create("recently-viewed-doc");

await this.recentlyViewed.fetchAll.perform();

await rerender();

assert.dom(NO_RECENTLY_VIEWED).doesNotExist();
assert.dom(ITEM).exists({ count: 1 });
});

test("it lists recently viewed projects and docs", async function (this: Context, assert) {
Expand Down

0 comments on commit f3b606d

Please sign in to comment.