Skip to content

Commit

Permalink
wip, I was mid-way through adapting the code to allow mocking for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
david-mears-2 committed Jul 31, 2024
1 parent 8f1a0dd commit 9f0e716
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
10 changes: 6 additions & 4 deletions composables/utils/useRApi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const rApiAddress = "http://localhost:8001";

interface RApiError {
error: string
detail: string
Expand All @@ -14,12 +12,16 @@ export interface RApiResponse {
// NB 'refresh' cannot yet be used since it would be called in the browser and
// thus run up against CORS restrictions. CORS also prevents hot module reloading, currently.
export function useRApi<T extends RApiResponse>(endpoint: string) {
const url = rApiAddress + endpoint;
const config = useRuntimeConfig(); // Must be called from inside the composable function. https://nuxt.com/docs/guide/concepts/auto-imports#vue-and-nuxt-composables
const url = `${config.public.rApiBase}${endpoint}`;

const { data, error, status: fetchStatus, refresh } = useFetch<T>(url);
const { data, error, status: fetchStatus, refresh } = useFetch<T>(url, { immediate: false },
);

const responseData = computed(() => data.value as T | null);

// return values 'responseData', 'fetchStatus', and 'error' are ComputedRefs.
// 'refresh' is a Promise.
return {
responseData, // This is the response data, not the value of the 'data' key in the response data.
fetchStatus, // This is the status of the fetch request (e.g. pending), not the value of the 'status' part of the response data.
Expand Down
13 changes: 13 additions & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,17 @@ export default defineNuxtConfig({
},
},

runtimeConfig: {
public: { // TODO: CHeck comment below is true.
rApiBase: "http://localhost:8001", // Automatically overridden if NUXT_PUBLIC_R_API_BASE is set as an environment variable
},
},

$test: {
runtimeConfig: {
public: {
rApiBase: "", // https://nuxt.com/docs/getting-started/testing#registerendpoint
},
},
},
});
38 changes: 36 additions & 2 deletions tests/components/SideBar.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @vitest-environment nuxt

import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import { mountSuspended } from "@nuxt/test-utils/runtime";
import { mountSuspended, registerEndpoint } from "@nuxt/test-utils/runtime";
import type { VueWrapper } from "@vue/test-utils";

import SideBar from "@/components/SideBar.vue";
Expand All @@ -15,12 +15,22 @@ async function mockCSidebarPageloadBehavior(coreuiSidebar: VueWrapper) {
coreuiSidebar.vm.$emit("hide");
}

registerEndpoint("/", () => {
return {
"daedalus": "99.99.99",
"daedalus.api": "99.99.99",
};
});

describe("sidebar", () => {
it("adds a resize event listener on mount and removes it on unmount", async () => {
const addEventListenerSpy = vi.spyOn(window, "addEventListener");
const removeEventListenerSpy = vi.spyOn(window, "removeEventListener");

const component = await mountSuspended(SideBar, { global: { stubs } });
const component = await mountSuspended(SideBar, {
props: { visible: false },
global: { stubs },
});
expect(addEventListenerSpy).toHaveBeenCalledWith("resize", expect.any(Function));

component.unmount();
Expand Down Expand Up @@ -53,6 +63,30 @@ describe("sidebar", () => {
expect(coreuiSidebar.props("unfoldable")).toBe(false);
});

it("includes information about the version numbers", async () => {
const component = await mountSuspended(SideBar, {
props: { visible: false },
global: { stubs },
});

const coreuiSidebar = component.findComponent({ name: "CSidebar" });
await mockCSidebarPageloadBehavior(coreuiSidebar);

await component.setProps({ visible: true });

const versionElement = component.find(`[title="Loading version data..."]`);
expect(versionElement.exists()).toBe(true);

// Wait for the version data to load
await component.vm.$nextTick();

// Find element with a title attribute containing a version number.
const expectedVersionInfo = "Version 99.99.99";
const versionElementWithMoreData = component.find(`[title="${expectedVersionInfo}"]`);

expect(versionElementWithMoreData.exists()).toBe(true);
});

afterAll(() => {
vi.unstubAllGlobals();
});
Expand Down

0 comments on commit 9f0e716

Please sign in to comment.