Skip to content

Commit

Permalink
Fixup unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmchilton committed Feb 8, 2024
1 parent d1c57e3 commit aa656ff
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 26 deletions.
3 changes: 2 additions & 1 deletion client/src/api/schema/__mocks__/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ function getMockReturn(path: Path, method: Method, args: any[]) {
}
}

return null;
// if no mock has been setup, never resolve API request
return new Promise(() => {});
}

function setMockReturn(path: Path | RegExp, method: Method, value: any) {

Check warning on line 55 in client/src/api/schema/__mocks__/fetcher.ts

View workflow job for this annotation

GitHub Actions / client-unit-test (18)

Unexpected any. Specify a different type
Expand Down
29 changes: 12 additions & 17 deletions client/src/components/Dataset/DatasetStorage/DatasetStorage.test.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
import { shallowMount } from "@vue/test-utils";

Check failure on line 1 in client/src/components/Dataset/DatasetStorage/DatasetStorage.test.js

View workflow job for this annotation

GitHub Actions / client-unit-test (18)

Run autofix to sort these imports!
import axios from "axios";
import MockAdapter from "axios-mock-adapter";
import flushPromises from "flush-promises";
import { getLocalVue } from "tests/jest/helpers";

import { mockFetcher } from "@/api/schema/__mocks__";
import DatasetStorage from "./DatasetStorage";

jest.mock("@/api/schema");

const localVue = getLocalVue();

const TEST_STORAGE_API_RESPONSE_WITHOUT_ID = {
object_store_id: null,
private: false,
};
const TEST_DATASET_ID = "1";
const TEST_STORAGE_URL = `/api/datasets/${TEST_DATASET_ID}/storage`;
const STORAGE_FETCH_URL = "/api/datasets/{dataset_id}/storage";
const TEST_ERROR_MESSAGE = "Opps all errors.";

describe("DatasetStorage.vue", () => {
let axiosMock;
let wrapper;

beforeEach(async () => {
axiosMock = new MockAdapter(axios);
});

function mount() {
wrapper = shallowMount(DatasetStorage, {
propsData: { datasetId: TEST_DATASET_ID },
Expand All @@ -32,22 +28,26 @@ describe("DatasetStorage.vue", () => {
}

async function mountWithResponse(response) {
axiosMock.onGet(TEST_STORAGE_URL).reply(200, response);
mockFetcher.path(STORAGE_FETCH_URL).method("get").mock({ data: response });
mount();
await flushPromises();
}

it("test loading...", async () => {
mount();
await wrapper.vm.$nextTick();
console.log(wrapper.html());
expect(wrapper.findAll("loadingspan-stub").length).toBe(1);
expect(wrapper.findAll("describeobjectstore-stub").length).toBe(0);
});

it("test error rendering...", async () => {
axiosMock.onGet(TEST_STORAGE_URL).reply(400, {
err_msg: TEST_ERROR_MESSAGE,
});
mockFetcher
.path(STORAGE_FETCH_URL)
.method("get")
.mock(() => {
throw Error(TEST_ERROR_MESSAGE);
});
mount();
await flushPromises();
expect(wrapper.findAll(".error").length).toBe(1);
Expand All @@ -59,10 +59,5 @@ describe("DatasetStorage.vue", () => {
await mountWithResponse(TEST_STORAGE_API_RESPONSE_WITHOUT_ID);
expect(wrapper.findAll("loadingspan-stub").length).toBe(0);
expect(wrapper.findAll("describeobjectstore-stub").length).toBe(1);
expect(wrapper.vm.storageInfo.private).toEqual(false);
});

afterEach(() => {
axiosMock.restore();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import DescribeObjectStore from "@/components/ObjectStore/DescribeObjectStore.vu
interface DatasetStorageProps {
datasetId: string;
datasetType: "hda" | "ldda";
includeTitle: boolean;
datasetType?: "hda" | "ldda";
includeTitle?: boolean;
}
const props = withDefaults(defineProps<DatasetStorageProps>(), {
Expand Down Expand Up @@ -48,8 +48,8 @@ watch(
const datasetId = props.datasetId;
const datasetType = props.datasetType;
try {
const { data } = await fetchDatasetStorage({ dataset_id: datasetId, hda_ldda: datasetType });
storageInfo.value = data;
const response = await fetchDatasetStorage({ dataset_id: datasetId, hda_ldda: datasetType });
storageInfo.value = response.data;
} catch (error) {
errorMessage.value = errorMessageAsString(error);
}
Expand Down
1 change: 1 addition & 0 deletions client/src/components/LoadingSpan.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</template>
<script>
export default {
name: "LoadingSpan",
props: {
classes: {
type: String,
Expand Down
5 changes: 5 additions & 0 deletions client/src/components/ObjectStore/DescribeObjectStore.vue
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<script lang="ts">
export default {
name: "DescribeObjectStore",
};
</script>
<script setup lang="ts">
import { computed } from "vue";

Check failure on line 7 in client/src/components/ObjectStore/DescribeObjectStore.vue

View workflow job for this annotation

GitHub Actions / client-unit-test (18)

Import in body of module; reorder to top
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ describe("ShowSelectedObjectStore", () => {
wrapper = mount(ShowSelectedObjectStore, {
propsData: { preferredObjectStoreId: TEST_OBJECT_ID, forWhat: "Data goes into..." },
localVue,
stubs: {
LoadingSpan: true,
DescribeObjectStore: true,
},
});
let loadingEl = wrapper.findComponent(LoadingSpan);
expect(loadingEl.exists()).toBeTruthy();
Expand Down

0 comments on commit aa656ff

Please sign in to comment.