From ff77bc7d1140f7eaf88fd8e06cb9c34f2c1b7b4d Mon Sep 17 00:00:00 2001 From: Rendani Gangazhe Date: Sat, 6 May 2023 14:16:25 +0200 Subject: [PATCH] Migrate job metrics store and component to use pinia and composition api --- .../components/JobMetrics/JobMetrics.test.js | 130 +++++----- .../src/components/JobMetrics/JobMetrics.vue | 239 ++++++++++-------- client/src/store/index.js | 2 - client/src/store/jobMetricsStore.js | 49 ---- client/src/store/jobMetricsStore.test.js | 19 -- client/src/stores/jobMetricsStore.test.ts | 69 +++++ client/src/stores/jobMetricsStore.ts | 65 +++++ 7 files changed, 332 insertions(+), 241 deletions(-) delete mode 100644 client/src/store/jobMetricsStore.js delete mode 100644 client/src/store/jobMetricsStore.test.js create mode 100644 client/src/stores/jobMetricsStore.test.ts create mode 100644 client/src/stores/jobMetricsStore.ts diff --git a/client/src/components/JobMetrics/JobMetrics.test.js b/client/src/components/JobMetrics/JobMetrics.test.js index 28abc77fb6a..5e2b31c2f67 100644 --- a/client/src/components/JobMetrics/JobMetrics.test.js +++ b/client/src/components/JobMetrics/JobMetrics.test.js @@ -1,12 +1,10 @@ -import { mount, createLocalVue } from "@vue/test-utils"; -import flushPromises from "flush-promises"; -import JobMetrics from "./JobMetrics"; +import { createTestingPinia } from "@pinia/testing"; import ec2 from "./ec2.json"; -import Vuex from "vuex"; -import createCache from "vuex-cache"; -import { jobMetricsStore } from "store/jobMetricsStore"; - -const JOB_ID = "moo"; +import flushPromises from "flush-promises"; +import { getLocalVue } from "tests/jest/helpers"; +import JobMetrics from "./JobMetrics"; +import { mount } from "@vue/test-utils"; +import { setActivePinia } from "pinia"; // Ignore all axios calls, data is mocked locally -- just say "OKAY!" jest.mock("axios", () => ({ @@ -15,54 +13,57 @@ jest.mock("axios", () => ({ }, })); -const localVue = createLocalVue(); -localVue.use(Vuex); -const testStore = new Vuex.Store({ - plugins: [createCache()], - modules: { - jobMetricsStore, - }, -}); +const localVue = getLocalVue(); describe("JobMetrics/JobMetrics.vue", () => { it("should not render a div if no plugins found in store", async () => { - const propsData = { - jobId: JOB_ID, - }; const wrapper = mount(JobMetrics, { - store: testStore, - propsData, + pinia: createTestingPinia(), + propsData: { + jobId: "9000", + }, localVue, }); + await wrapper.vm.$nextTick(); expect(wrapper.find("div").exists()).toBe(false); }); it("should group plugins by type", async () => { - const propsData = { - jobId: JOB_ID, - }; - const metricsResponse = [ + const JOB_ID = "9000"; + const mockMetricsResponse = [ { plugin: "core", title: "runtime", value: 145 }, { plugin: "core", title: "memory", value: 146 }, { plugin: "extended", title: "awesomeness", value: 42 }, ]; - const wrapper = mount(JobMetrics, { - store: testStore, - propsData, - localVue, - computed: { - jobMetrics() { - return metricsResponse; + + const pinia = createTestingPinia({ + initialState: { + jobMetricsStore: { + jobMetricsByJobId: { + [`${JOB_ID}`]: mockMetricsResponse, + }, + jobMetricsByHdaId: {}, + jobMetricsByLddaId: {}, }, }, }); + setActivePinia(pinia); + + const wrapper = mount(JobMetrics, { + localVue, + pinia, + propsData: { + jobId: JOB_ID, + }, + }); + // Wait for axios and rendering. await flushPromises(); - expect(wrapper.vm.jobMetrics.length).toBe(3); expect(wrapper.vm.jobId).toBe(JOB_ID); - expect(wrapper.vm.metricsByPlugins.core.runtime).toBe(145); - expect(wrapper.vm.orderedPlugins.length).toBe(2); + expect(wrapper.vm.jobMetricsGroupedByPluginType.core.runtime).toBe(145); + expect(wrapper.vm.pluginsSortedByPluginType.length).toBe(2); + // Three metrics, begin metrics for two plugins const metricsTables = wrapper.findAll(".metrics_plugin"); expect(metricsTables.length).toBe(2); @@ -72,45 +73,53 @@ describe("JobMetrics/JobMetrics.vue", () => { expect(metricsTables.at(1).findAll("tr").length).toBe(1); }); - it("should render correct AWS Estimates", async () => { + it("renders correct AWS estimates", async () => { const deriveRenderedAwsEstimate = async (cores, seconds, memory) => { const JOB_ID = Math.random().toString(36).substring(2); - const propsData = { - jobId: JOB_ID, - aws_estimate: true, - }; - const metricsResponse = [ + const mockMetricsResponse = [ { plugin: "core", name: "galaxy_slots", raw_value: cores }, { plugin: "core", name: "runtime_seconds", raw_value: seconds }, { plugin: "core", name: "galaxy_memory_mb", raw_value: memory }, ]; - const wrapper = mount(JobMetrics, { - store: testStore, - propsData, - localVue, - computed: { - jobMetrics() { - return metricsResponse; + + const pinia = createTestingPinia({ + initialState: { + jobMetricsStore: { + jobMetricsByJobId: { + [`${JOB_ID}`]: mockMetricsResponse, + }, + jobMetricsByHdaId: {}, + jobMetricsByLddaId: {}, }, }, }); + setActivePinia(pinia); + + const wrapper = mount(JobMetrics, { + localVue, + pinia, + propsData: { + jobId: JOB_ID, + aws_estimate: true, + }, + }); + // Wait for axios and rendering. await flushPromises(); - const estimates = {}; - - if (!wrapper.find("#aws-estimate").exists()) { - return false; + if (wrapper.find("#aws-estimate").exists()) { + return { + cost: wrapper.find("#aws-estimate b").text(), + vcpus: wrapper.find("#aws_vcpus").text(), + cpu: wrapper.find("#aws_cpu").text(), + mem: wrapper.find("#aws_mem").text(), + name: wrapper.find("#aws_name").text(), + }; } - estimates.cost = wrapper.find("#aws-estimate b").text(); - estimates.vcpus = wrapper.find("#aws_vcpus").text(); - estimates.cpu = wrapper.find("#aws_cpu").text(); - estimates.mem = wrapper.find("#aws_mem").text(); - estimates.name = wrapper.find("#aws_name").text(); - - return estimates; + return {}; }; + const assertAwsInstance = (estimates) => { const instance = ec2.find((instance) => estimates.name === instance.name); expect(estimates.mem).toBe(instance.mem.toString()); @@ -119,7 +128,6 @@ describe("JobMetrics/JobMetrics.vue", () => { }; const estimates_small = await deriveRenderedAwsEstimate("1.0000000", "9.0000000", "2048.0000000"); - expect(estimates_small.name).toBe("t2.small"); expect(estimates_small.cost).toBe("0.00 USD"); assertAwsInstance(estimates_small); @@ -134,6 +142,6 @@ describe("JobMetrics/JobMetrics.vue", () => { "18000.0000000", "99999.0000000" ); - expect(estimates_not_available).toBe(false); + expect(estimates_not_available).toEqual({}); }); }); diff --git a/client/src/components/JobMetrics/JobMetrics.vue b/client/src/components/JobMetrics/JobMetrics.vue index b2533db21f5..6c89feabaac 100644 --- a/client/src/components/JobMetrics/JobMetrics.vue +++ b/client/src/components/JobMetrics/JobMetrics.vue @@ -1,11 +1,127 @@ + + -