diff --git a/client/src/api/index.test.ts b/client/src/api/index.test.ts index fb16bc95edc2..6d82145560ef 100644 --- a/client/src/api/index.test.ts +++ b/client/src/api/index.test.ts @@ -1,10 +1,11 @@ +import { getFakeRegisteredUser } from "@tests/test-data"; + import { type AnonymousUser, type AnyHistory, type HistorySummary, type HistorySummaryExtended, isRegisteredUser, - type User, userOwnsHistory, } from "."; @@ -12,17 +13,12 @@ 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: "test@mail.test", - 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; diff --git a/client/src/components/DatasetInformation/DatasetError.test.ts b/client/src/components/DatasetInformation/DatasetError.test.ts index c3213e8dc6fc..3d0dac642ffc 100644 --- a/client/src/components/DatasetInformation/DatasetError.test.ts +++ b/client/src/components/DatasetInformation/DatasetError.test.ts @@ -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"; @@ -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(); diff --git a/client/src/components/History/HistoryView.test.js b/client/src/components/History/HistoryView.test.js index d74ce6d885a2..d0d371c80e36 100644 --- a/client/src/components/History/HistoryView.test.js +++ b/client/src/components/History/HistoryView.test.js @@ -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"; @@ -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; } diff --git a/client/src/components/History/Multiple/MultipleView.test.js b/client/src/components/History/Multiple/MultipleView.test.js index c8acf9c4fa4e..580314ec4e33 100644 --- a/client/src/components/History/Multiple/MultipleView.test.js +++ b/client/src/components/History/Multiple/MultipleView.test.js @@ -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"; @@ -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; diff --git a/client/src/components/JobDestinationParams/JobDestinationParams.test.ts b/client/src/components/JobDestinationParams/JobDestinationParams.test.ts index 5486f481f91d..aa0e241a9cd1 100644 --- a/client/src/components/JobDestinationParams/JobDestinationParams.test.ts +++ b/client/src/components/JobDestinationParams/JobDestinationParams.test.ts @@ -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"; @@ -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(); diff --git a/client/src/components/Masthead/Masthead.test.js b/client/src/components/Masthead/Masthead.test.js index f299b5b2500f..b5332ed65084 100644 --- a/client/src/components/Masthead/Masthead.test.js +++ b/client/src/components/Masthead/Masthead.test.js @@ -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"; @@ -18,6 +20,8 @@ jest.mock("vue-router/composables", () => ({ })); jest.mock("@/api/schema"); +const currentUser = getFakeRegisteredUser(); + describe("Masthead.vue", () => { let wrapper; let localVue; @@ -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, diff --git a/client/src/components/Masthead/QuotaMeter.test.ts b/client/src/components/Masthead/QuotaMeter.test.ts index 3599cc2d754a..b405a5b36a12 100644 --- a/client/src/components/Masthead/QuotaMeter.test.ts +++ b/client/src/components/Masthead/QuotaMeter.test.ts @@ -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"; @@ -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, @@ -33,32 +35,29 @@ 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"); } @@ -66,22 +65,19 @@ describe("QuotaMeter.vue", () => { 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"); diff --git a/client/src/components/User/DiskUsage/DiskUsageSummary.test.ts b/client/src/components/User/DiskUsage/DiskUsageSummary.test.ts index 5c81ffef4cf8..62e6e1d6b90b 100644 --- a/client/src/components/User/DiskUsage/DiskUsageSummary.test.ts +++ b/client/src/components/User/DiskUsage/DiskUsageSummary.test.ts @@ -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"; @@ -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; diff --git a/client/src/components/Workflow/List/WorkflowList.test.ts b/client/src/components/Workflow/List/WorkflowList.test.ts index b4b71d02f185..29e165b42e0b 100644 --- a/client/src/components/Workflow/List/WorkflowList.test.ts +++ b/client/src/components/Workflow/List/WorkflowList.test.ts @@ -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"; @@ -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(); diff --git a/client/tests/test-data/index.ts b/client/tests/test-data/index.ts new file mode 100644 index 000000000000..b4fa53c750de --- /dev/null +++ b/client/tests/test-data/index.ts @@ -0,0 +1,19 @@ +import { type RegisteredUser } from "@/api"; + +export function getFakeRegisteredUser(data: Partial = {}): 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, + }; +}