Skip to content

Commit

Permalink
add GraphConfigs tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmaLRussell committed Jun 28, 2024
1 parent 1db8c55 commit b9df2c3
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 17 deletions.
9 changes: 8 additions & 1 deletion app/static/src/app/components/code/CodeTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
</div>
<error-info :error="error"></error-info>
<div class="mt-3">
<graph-configs></graph-configs>
<vertical-collapse v-if="showGraphs" title="Graphs" collapse-id="graphs">
<graph-configs></graph-configs>
</vertical-collapse>
</div>
</div>
</template>
Expand All @@ -32,10 +34,12 @@ import userMessages from "../../userMessages";
import ErrorInfo from "../ErrorInfo.vue";
import GraphConfigs from "../graphConfig/GraphConfigs.vue";
import GenericHelp from "../help/GenericHelp.vue";
import VerticalCollapse from "@/app/components/VerticalCollapse.vue";
export default defineComponent({
name: "CodeTab",
components: {
VerticalCollapse,
GenericHelp,
GraphConfigs,
ErrorInfo,
Expand All @@ -54,6 +58,8 @@ export default defineComponent({
const appIsConfigured = computed(() => store.state.configured);
const compile = () => store.dispatch(`model/${ModelAction.CompileModel}`);
const loadingMessage = userMessages.code.isValidating;
const allVariables = computed<string[]>(() => store.state.model.odinModelResponse?.metadata?.variables || []);
const showGraphs = computed(() => allVariables.value.length && !store.state.model.compileRequired);
return {
appIsConfigured,
Expand All @@ -66,6 +72,7 @@ export default defineComponent({
codeHelp,
codeValidating,
loadingMessage,
showGraphs
};
}
});
Expand Down
15 changes: 5 additions & 10 deletions app/static/src/app/components/graphConfig/GraphConfigs.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<vertical-collapse v-if="showGraphs" title="Graphs" collapse-id="graphs">
<div class="ms-2">

<div id="graph-configs-instruction" class="ms-2">
Drag variables to 'Hidden variables' to remove them from your graph, or click 'Add Graph' to create a
new graph to move them to.
</div>
Expand All @@ -19,14 +19,13 @@
@setDragging="setDraggingVariable"
:dragging="draggingVariable"
></hidden-variables>
</vertical-collapse>

</template>

<script lang="ts">
import {computed, defineComponent, ref} from "vue";
import {useStore} from "vuex";
import VueFeather from "vue-feather";
import VerticalCollapse from "../VerticalCollapse.vue";
import VueFeather from "vue-feather";;
import HiddenVariables from "./HiddenVariables.vue";
import GraphConfig from "./GraphConfig.vue";
import {GraphsAction} from "../../store/graphs/actions";
Expand All @@ -35,21 +34,17 @@ export default defineComponent({
components: {
VueFeather,
GraphConfig,
HiddenVariables,
VerticalCollapse
HiddenVariables
},
setup() {
const store = useStore();
const allVariables = computed<string[]>(() => store.state.model.odinModelResponse?.metadata?.variables || []);
const showGraphs = computed(() => allVariables.value.length && !store.state.model.compileRequired);
const draggingVariable = ref(false); // indicates whether a child component is dragging a variable
const setDraggingVariable = (value: boolean) => (draggingVariable.value = value);
const graphConfigs = computed(() => store.state.graphs.config);
const addGraph = () => {
store.dispatch(`graphs/${GraphsAction.NewGraph}`);
};
return {
showGraphs,
draggingVariable,
setDraggingVariable,
graphConfigs,
Expand Down
6 changes: 0 additions & 6 deletions app/static/src/app/components/graphConfig/HiddenVariables.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,12 @@
import { computed, defineComponent } from "vue";
import { useStore } from "vuex";
import VueFeather from "vue-feather";
import tooltip from "../../directives/tooltip";
import * as Color from "color";
import { GraphsGetter } from "../../store/graphs/getters";
import SelectVariables from "../mixins/selectVariables";
export default defineComponent({
name: "HiddenVariables",
computed: {
tooltip() {
return tooltip;
}
},
components: { VueFeather },
props: {
dragging: {
Expand Down
4 changes: 4 additions & 0 deletions app/static/tests/unit/components/code/codeTab.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,8 @@ describe("CodeTab", () => {
});
expect(wrapper.findAll("div").length).toBe(0);
});

// TODO
// Graphs renders nothing if no variables
// Graphs renders nothing if compile required
});
83 changes: 83 additions & 0 deletions app/static/tests/unit/components/graphConfig/graphConfigs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import Vuex from "vuex";
import {BasicState} from "../../../../src/app/store/basic/state";
import {mockBasicState} from "../../../mocks";
import {GraphsAction} from "../../../../src/app/store/graphs/actions";
import {shallowMount} from "@vue/test-utils";
import GraphConfigs from "../../../../src/app/components/graphConfig/GraphConfigs.vue";
import GraphConfig from "../../../../src/app/components/graphConfig/GraphConfig.vue";
import HiddenVariables from "../../../../src/app/components/graphConfig/HiddenVariables.vue";
import {nextTick} from "vue";

describe("GraphConfigs", () => {
beforeEach(() => {
jest.clearAllMocks();
});

const mockNewGraph = jest.fn();
const namespaced = true;
const getWrapper = () => {
const store = new Vuex.Store<BasicState>({
state: mockBasicState({
configured: true
}),
modules: {
graphs: {
namespaced,
state: {
config: [
{ selectedVariables: ["S"], unselectedVariables: ["I", "R"] },
{ selectedVariables: ["I"], unselectedVariables: ["S", "R"] }
]
},
actions: {
[GraphsAction.NewGraph]: mockNewGraph
}
}
}
});
return shallowMount(GraphConfigs, {
global: {
plugins: [store]
}
});
};

it("renders as expected", () => {
const wrapper = getWrapper();
expect(wrapper.find("#graph-configs-instruction").text()).toContain("Drag variables to 'Hidden variables' to remove");
const graphConfigComps = wrapper.findAllComponents(GraphConfig);
expect(graphConfigComps.length).toBe(2);
expect(graphConfigComps.at(0)!.props("graphIndex")).toBe(0);
expect(graphConfigComps.at(0)!.props("dragging")).toBe(false)
expect(graphConfigComps.at(1)!.props("graphIndex")).toBe(1);
expect(graphConfigComps.at(1)!.props("dragging")).toBe(false);
expect(wrapper.find("button").text()).toBe("Add Graph");
expect(wrapper.findComponent(HiddenVariables).props("dragging")).toBe(false);
});

it("sets dragging on emit from GraphConfig", async () => {
const wrapper = getWrapper();
const graphConfig = wrapper.findAllComponents(GraphConfig).at(0)!;
graphConfig.vm.$emit("setDragging", true);
await nextTick();
expect(graphConfig.props("dragging")).toBe(true);
expect(wrapper.findComponent(HiddenVariables).props("dragging")).toBe(true);
});

it("sets dragging on emit from HiddenVariables", async () => {
const wrapper = getWrapper();
const hiddenVars = wrapper.findComponent(HiddenVariables);
hiddenVars.vm.$emit("setDragging", true);
await nextTick();
expect(hiddenVars.props("dragging")).toBe(true);
expect(wrapper.findAllComponents(GraphConfig).at(0)!.props("dragging")).toBe(true);
});

it("Add Graph click invokes action", async () => {
const wrapper = getWrapper();
expect(mockNewGraph).toHaveBeenCalledTimes(0);
await wrapper.find("button").trigger("click");
expect(mockNewGraph).toHaveBeenCalledTimes(1);
});

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import Vuex from "vuex";
import {BasicState} from "../../../../src/app/store/basic/state";
import {GraphsAction} from "../../../../src/app/store/graphs/actions";
import {shallowMount} from "@vue/test-utils";
import HiddenVariables from "../../../../src/app/components/graphConfig/HiddenVariables.vue";
import {GraphsGetter} from "../../../../src/app/store/graphs/getters";

describe("HiddenVariables", () => {
beforeEach(() => {
jest.clearAllMocks();
});

const mockUpdateSelectedVariables = jest.fn();

const getWrapper = () => {
const store = new Vuex.Store<BasicState>({
state: {} as any,
modules: {
graphs: {
namespaced: true,
state: {
config: [
{
selectedVariables: ["S"]
}
]
},
actions: {
[GraphsAction.UpdateSelectedVariables]: mockUpdateSelectedVariables
},
getters: {
[GraphsGetter.hiddenVariables]: ["I", "R"]
} as any
},
model: {
namespaced: true,
state: {
paletteModel: {
S: "#ff0000",
I: "#00ff00",
R: "#0000ff"
}
}
}
}
});

return shallowMount(HiddenVariables, {
props: {
dragging: false
},
global: {
plugins: [store]
}
});
};


it("renders as expected", () => {
const wrapper = getWrapper();
});

/*
it("starting drag sets values in event and emits setDragging");
it("ending drag emits setDragging");
it("onDrop removes variable from source");
it("shows drop zone when dragging", () => {
const wrapper = getWrapper({dragging: true});
expect(wrapper.find(".drop-zone").classes()).toStrictEqual(["drop-zone", "drop-zone-active"]);
});
it("shows instruction if no hidden variables", () =>{
const wrapper = getWrapper({}, []);
expect(wrapper.find(".drop-zone-instruction").text()).toBe("Drag variables here to select them for this graph.");
});*/
});

0 comments on commit b9df2c3

Please sign in to comment.