Skip to content

Commit

Permalink
Use correct user types in client tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davelopez committed Aug 5, 2024
1 parent a99d4e5 commit 83c7d0e
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 57 deletions.
12 changes: 4 additions & 8 deletions client/src/api/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
import { getFakeRegisteredUser } from "@tests/test-data";

import {
type AnonymousUser,
type AnyHistory,
type HistorySummary,
type HistorySummaryExtended,
isRegisteredUser,
type User,
userOwnsHistory,
} from ".";

const REGISTERED_USER_ID = "fake-user-id";
const ANOTHER_USER_ID = "another-fake-user-id";
const ANONYMOUS_USER_ID = null;

const REGISTERED_USER: User = {
id: REGISTERED_USER_ID,
email: "[email protected]",
tags_used: [],
isAnonymous: false,
total_disk_usage: 0,
};
const REGISTERED_USER = getFakeRegisteredUser({ id: REGISTERED_USER_ID });

const ANONYMOUS_USER: AnonymousUser = {
isAnonymous: true,
total_disk_usage: 0,
nice_total_disk_usage: "0.0 bytes",
};

const SESSIONLESS_USER = null;
Expand Down
9 changes: 2 additions & 7 deletions client/src/components/DatasetInformation/DatasetError.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getFakeRegisteredUser } from "@tests/test-data";
import { mount } from "@vue/test-utils";
import flushPromises from "flush-promises";
import { createPinia } from "pinia";
Expand Down Expand Up @@ -57,13 +58,7 @@ async function montDatasetError(has_duplicate_inputs = true, has_empty_inputs =
});

const userStore = useUserStore();
userStore.currentUser = {
email: user_email || "email",
id: "user_id",
tags_used: [],
isAnonymous: false,
total_disk_usage: 0,
};
userStore.currentUser = getFakeRegisteredUser({ email: user_email });

await flushPromises();

Expand Down
6 changes: 2 additions & 4 deletions client/src/components/History/HistoryView.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getFakeRegisteredUser } from "@tests/test-data";
import { mount } from "@vue/test-utils";
import axios from "axios";
import MockAdapter from "axios-mock-adapter";
Expand Down Expand Up @@ -76,10 +77,7 @@ async function createWrapper(localVue, currentUserId, history) {
pinia,
});
const userStore = useUserStore();
const userData = {
id: currentUserId,
};
userStore.currentUser = { ...userStore.currentUser, ...userData };
userStore.currentUser = getFakeRegisteredUser({ id: currentUserId });
await flushPromises();
return wrapper;
}
Expand Down
4 changes: 3 additions & 1 deletion client/src/components/History/Multiple/MultipleView.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getFakeRegisteredUser } from "@tests/test-data";
import { mount } from "@vue/test-utils";
import axios from "axios";
import MockAdapter from "axios-mock-adapter";
Expand All @@ -20,7 +21,8 @@ const getFakeHistorySummaries = (num, selectedIndex) => {
update_time: new Date().toISOString(),
}));
};
const currentUser = { id: USER_ID };

const currentUser = getFakeRegisteredUser({ id: USER_ID });

describe("MultipleView", () => {
let axiosMock;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getFakeRegisteredUser } from "@tests/test-data";
import { shallowMount } from "@vue/test-utils";
import flushPromises from "flush-promises";
import { createPinia } from "pinia";
Expand Down Expand Up @@ -31,14 +32,7 @@ async function mountJobDestinationParams() {
});

const userStore = useUserStore();
userStore.currentUser = {
email: "admin@email",
id: "1",
tags_used: [],
isAnonymous: false,
total_disk_usage: 1048576,
is_admin: true,
};
userStore.currentUser = getFakeRegisteredUser({ is_admin: true });

await flushPromises();

Expand Down
8 changes: 8 additions & 0 deletions client/src/components/Masthead/Masthead.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { createTestingPinia } from "@pinia/testing";
import { getFakeRegisteredUser } from "@tests/test-data";
import { mount } from "@vue/test-utils";
import { WindowManager } from "layout/window-manager";
import { PiniaVuePlugin } from "pinia";
import { getLocalVue } from "tests/jest/helpers";

import { mockFetcher } from "@/api/schema/__mocks__";
import { useUserStore } from "@/stores/userStore";

import { loadWebhookMenuItems } from "./_webhooks";

Expand All @@ -18,6 +20,8 @@ jest.mock("vue-router/composables", () => ({
}));
jest.mock("@/api/schema");

const currentUser = getFakeRegisteredUser();

describe("Masthead.vue", () => {
let wrapper;
let localVue;
Expand All @@ -42,6 +46,10 @@ describe("Masthead.vue", () => {

windowManager = new WindowManager({});
const windowTab = windowManager.getTab();

const userStore = useUserStore();
userStore.currentUser = currentUser;

wrapper = mount(Masthead, {
propsData: {
windowTab,
Expand Down
30 changes: 13 additions & 17 deletions client/src/components/Masthead/QuotaMeter.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { createTestingPinia } from "@pinia/testing";
import { getFakeRegisteredUser } from "@tests/test-data";
import { mount } from "@vue/test-utils";
import flushPromises from "flush-promises";
import { getLocalVue } from "tests/jest/helpers";

import { type RegisteredUser } from "@/api";
import { useUserStore } from "@/stores/userStore";

import QuotaMeter from "./QuotaMeter.vue";
Expand All @@ -20,11 +22,11 @@ jest.mock("@/composables/config", () => ({

const localVue = getLocalVue();

async function createQuotaMeterWrapper(config: any, userData: any) {
async function createQuotaMeterWrapper(config: any, user: RegisteredUser) {
configValues = { ...config };
const pinia = createTestingPinia();
const userStore = useUserStore();
userStore.currentUser = { ...userStore.currentUser, ...userData };
userStore.currentUser = user;
const wrapper = mount(QuotaMeter, {
localVue,
pinia,
Expand All @@ -33,55 +35,49 @@ async function createQuotaMeterWrapper(config: any, userData: any) {
return wrapper;
}

const FAKE_USER = getFakeRegisteredUser({ quota: "100 MB", total_disk_usage: 5120, quota_percent: 50 });

describe("QuotaMeter.vue", () => {
it("shows a percentage usage", async () => {
const user = {
total_disk_usage: 5120,
quota_percent: 50,
quota: "100 MB",
};
const config = { enable_quotas: true };
const wrapper = await createQuotaMeterWrapper(config, user);
const wrapper = await createQuotaMeterWrapper(config, FAKE_USER);
expect(wrapper.find(".quota-progress > span").text()).toBe("Using 50% of 100 MB");
});

it("changes appearance depending on usage", async () => {
const config = { enable_quotas: true };
{
const user = { quota_percent: 30 };
const user = { ...FAKE_USER, quota_percent: 30 };
const wrapper = await createQuotaMeterWrapper(config, user);
expect(wrapper.find(".quota-progress .progress-bar").classes()).toContain("bg-success");
}
{
const user = { quota_percent: 80 };
const user = { ...FAKE_USER, quota_percent: 80 };
const wrapper = await createQuotaMeterWrapper(config, user);
expect(wrapper.find(".quota-progress .progress-bar").classes()).toContain("bg-warning");
}
{
const user = { quota_percent: 95 };
const user = { ...FAKE_USER, quota_percent: 95 };
const wrapper = await createQuotaMeterWrapper(config, user);
expect(wrapper.find(".quota-progress .progress-bar").classes()).toContain("bg-danger");
}
});

it("displays tooltip", async () => {
const config = { enable_quotas: true };
const wrapper = await createQuotaMeterWrapper(config, {});
const wrapper = await createQuotaMeterWrapper(config, FAKE_USER);
expect(wrapper.attributes("title")).toContain("Storage");
});

it("shows total usage when there is no quota", async () => {
{
const user = { total_disk_usage: 7168 };
const user = { ...FAKE_USER, total_disk_usage: 7168 };
const config = { enable_quotas: false };
const wrapper = await createQuotaMeterWrapper(config, user);
expect(wrapper.find("span").text()).toBe("Using 7 KB");
}
{
const user = {
total_disk_usage: 21504,
quota: "unlimited",
};
const user = { ...FAKE_USER, total_disk_usage: 21504, quota: "unlimited" };
const config = { enable_quotas: true };
const wrapper = await createQuotaMeterWrapper(config, user);
expect(wrapper.find("span").text()).toBe("Using 21 KB");
Expand Down
10 changes: 3 additions & 7 deletions client/src/components/User/DiskUsage/DiskUsageSummary.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getFakeRegisteredUser } from "@tests/test-data";
import { mount } from "@vue/test-utils";
import flushPromises from "flush-promises";
import { createPinia } from "pinia";
Expand All @@ -19,16 +20,11 @@ const localVue = getLocalVue();
const quotaUsageClassSelector = ".quota-usage";
const basicDiskUsageSummaryId = "#basic-disk-usage-summary";

const fakeUserWithQuota = {
id: "fakeUser",
email: "fakeUserEmail",
tags_used: [],
isAnonymous: false,
const fakeUserWithQuota = getFakeRegisteredUser({
total_disk_usage: 1048576,
quota_bytes: 104857600,
quota_percent: 1,
quota_source_label: "Default",
};
});

// TODO: Replace this with a mockFetcher when #16608 is merged
const mockGetCurrentUser = getCurrentUser as jest.Mock;
Expand Down
8 changes: 3 additions & 5 deletions client/src/components/Workflow/List/WorkflowList.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createTestingPinia } from "@pinia/testing";
import { getLocalVue } from "@tests/jest/helpers";
import { getFakeRegisteredUser } from "@tests/test-data";
import { mount } from "@vue/test-utils";
import flushPromises from "flush-promises";
import { setActivePinia } from "pinia";
Expand All @@ -18,14 +19,11 @@ const localVue = getLocalVue();
const FAKE_USER_ID = "fake_user_id";
const FAKE_USERNAME = "fake_username";
const FAKE_USER_EMAIL = "fake_user_email";
const FAKE_USER = {
const FAKE_USER = getFakeRegisteredUser({
id: FAKE_USER_ID,
email: FAKE_USER_EMAIL,
tags_used: [],
isAnonymous: false,
username: FAKE_USERNAME,
total_disk_usage: 0,
};
});

async function mountWorkflowList() {
const pinia = createTestingPinia();
Expand Down
19 changes: 19 additions & 0 deletions client/tests/test-data/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { type RegisteredUser } from "@/api";

export function getFakeRegisteredUser(data: Partial<RegisteredUser> = {}): RegisteredUser {
return {
id: "fake_user_id",
email: "fake_user_email",
tags_used: [],
isAnonymous: false,
username: "fake_username",
total_disk_usage: 0,
nice_total_disk_usage: "0.0 bytes",
deleted: false,
purged: false,
is_admin: false,
preferences: {},
quota: "default",
...data,
};
}

0 comments on commit 83c7d0e

Please sign in to comment.