Skip to content

Commit

Permalink
remove Toast for inaccessible histories
Browse files Browse the repository at this point in the history
since their summaries wouldn't even be fetched it the first place

Co-authored-by: mvdbeek <m.vandenbeek@gmail.com>
  • Loading branch information
ahmedhamidawan and mvdbeek committed Sep 4, 2024
1 parent 987bbf9 commit d451c8d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 66 deletions.
60 changes: 4 additions & 56 deletions client/src/components/History/SwitchToHistoryLink.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,17 @@ import MockAdapter from "axios-mock-adapter";
import flushPromises from "flush-promises";

import { type HistorySummaryExtended } from "@/api";
import { Toast } from "@/composables/toast";
import { useUserStore } from "@/stores/userStore";

import SwitchToHistoryLink from "./SwitchToHistoryLink.vue";

jest.mock("@/composables/toast", () => ({
Toast: {
error: jest.fn(),
},
}));
jest.mock("vue-router/composables", () => ({
useRouter: jest.fn(() => ({
resolve: jest.fn((route) => ({
href: route,
})),
})),
}));

const localVue = getLocalVue(true);

const selectors = {
historyLink: ".history-link",
historyLinkClick: ".history-link > a",
} as const;

function mountSwitchToHistoryLinkForHistory(history: HistorySummaryExtended) {
const windowSpy = jest.spyOn(window, "open");
windowSpy.mockImplementation(() => null);
const pinia = createTestingPinia();

const axiosMock = new MockAdapter(axios);
Expand Down Expand Up @@ -69,8 +52,6 @@ async function expectOptionForHistory(option: string, history: HistorySummaryExt

expect(wrapper.html()).toContain(option);
expect(wrapper.text()).toContain(history.name);

return wrapper;
}

describe("SwitchToHistoryLink", () => {
Expand Down Expand Up @@ -130,51 +111,18 @@ describe("SwitchToHistoryLink", () => {
await expectOptionForHistory("View", history);
});

it("should view in new tab when the history is unowned and published", async () => {
it("should view in new tab when the history is accessible", async () => {
const history = {
id: "unowned-history-id",
id: "public-history-id",
name: "History Published",
deleted: false,
purged: false,
archived: false,
published: true,
user_id: "other_user_id",
} as HistorySummaryExtended;
const wrapper = await expectOptionForHistory("View", history);

// Wait for the history to be loaded
await flushPromises();

// Click on the history link
const link = wrapper.find(selectors.historyLinkClick);
await link.trigger("click");
await wrapper.vm.$nextTick();

// History opens in a new tab
expect(Toast.error).toHaveBeenCalledTimes(0);
await expectOptionForHistory("View", history);
});

it("should display View option and show error on click when history is unowned and not published", async () => {
const history = {
id: "published-history-id",
name: "History Unowned",
deleted: false,
purged: false,
archived: false,
user_id: "other_user_id",
} as HistorySummaryExtended;
const wrapper = await expectOptionForHistory("View", history);

// Wait for the history to be loaded
await flushPromises();

// Click on the history link
const link = wrapper.find(selectors.historyLinkClick);
await link.trigger("click");
await wrapper.vm.$nextTick();

// History does not open in a new tab and we get a Toast error
expect(Toast.error).toHaveBeenCalledTimes(1);
expect(Toast.error).toHaveBeenCalledWith("You do not have permission to view this history.");
});
// if the history is inaccessible, the HistorySummary would never be fetched in the first place
});
12 changes: 2 additions & 10 deletions client/src/components/History/SwitchToHistoryLink.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,8 @@ async function onClick(history: HistorySummary) {
}
function viewHistoryInNewTab(history: HistorySummary) {
if (
userOwnsHistory(userStore.currentUser, history) ||
history.published ||
("importable" in history && history.importable)
) {
const routeData = router.resolve(`/histories/view?id=${history.id}`);
window.open(routeData.href, "_blank");
} else {
Toast.error("You do not have permission to view this history.");
}
const routeData = router.resolve(`/histories/view?id=${history.id}`);
window.open(routeData.href, "_blank");
}
</script>

Expand Down

0 comments on commit d451c8d

Please sign in to comment.