Skip to content

Commit

Permalink
Merge branch 'unit-test/summary' of github.com:Nauxscript/earthworm i…
Browse files Browse the repository at this point in the history
…nto Nauxscript-unit-test/summary
  • Loading branch information
cuixiaorui committed Feb 27, 2024
2 parents 29e3341 + 42def0f commit 5bf3933
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 6 deletions.
23 changes: 17 additions & 6 deletions apps/client/composables/main/summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,36 @@ export function useSummary() {
};
}

const enSentence = ref("To be, or not to be, that is the question.");
const zhSentence = ref("生存还是毁灭,这是一个问题。");
const setenceLoading = ref(false);
export const defaultEnSentence = 'To be, or not to be, that is the question.'
export const defaultZhSentence = '生存还是毁灭,这是一个问题。'

const enSentence = ref(defaultEnSentence);
const zhSentence = ref(defaultZhSentence);
const hasLoadingDailySentence = ref(false);

export const resetSentenceLoading = () => (hasLoadingDailySentence.value = false)

export function useDailySentence() {
const getDailySentence = async () => {
if (!setenceLoading.value) {
const { en, zh } = await fetchDailySentence();
if (!hasLoadingDailySentence.value) {
hasLoadingDailySentence.value = true;
const { en, zh } = await fetchDailySentence().catch((err) => {
hasLoadingDailySentence.value = false
return Promise.reject(err)
});
enSentence.value = en;
zhSentence.value = zh;
setenceLoading.value = true;
}
};


onMounted(() => {
getDailySentence();
});

return {
enSentence,
zhSentence,
getDailySentence,
};
}
78 changes: 78 additions & 0 deletions apps/client/composables/main/tests/summary.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { it, expect, describe, vi, beforeEach } from "vitest";
import { resetSentenceLoading, useDailySentence, useSummary } from "../summary";
import * as toolApi from "~/api/tool";
import { useSetup } from "~/tests/helper/component";
import { flushPromises } from "@vue/test-utils";

vi.mock("~/api/tool");

describe("summary", () => {
describe("summary sentence", () => {
const dummyRes = {
en: "en",
zh: "zh",
};
beforeEach(() => {
vi.mocked(toolApi.fetchDailySentence).mockResolvedValue(dummyRes);
return () => {
resetSentenceLoading();
vi.resetAllMocks();
};
});

it("should load the daily sentence", async () => {
const { wrapper } = useSetup(() => {
const { zhSentence, enSentence } = useDailySentence();
return {
zhSentence,
enSentence,
};
});

await flushPromises();

const { zhSentence, enSentence } = wrapper.vm;

expect(toolApi.fetchDailySentence).toBeCalled();
expect(zhSentence).toBe(dummyRes.zh);
expect(enSentence).toBe(dummyRes.en);
});

it("should only load sentence once", async () => {
useSetup(() => {
useDailySentence();
});

await flushPromises();

useSetup(() => {
useDailySentence();
});

await flushPromises();

expect(toolApi.fetchDailySentence).toBeCalledTimes(1);
});
});

describe("summary modal control", () => {
it("should show summary modal", () => {
const { showModal, showSummary } = useSummary();
showSummary();
expect(showModal.value).toBeTruthy();
});

it("should hide summary modal", () => {
const { showModal, hideSummary } = useSummary();
hideSummary();
expect(showModal.value).toBeFalsy();
});

it("should return a same value in different hook", () => {
const { showSummary } = useSummary();
showSummary();
const { showModal: anotherShowModal } = useSummary();
expect(anotherShowModal.value).toBeTruthy();
});
});
});

0 comments on commit 5bf3933

Please sign in to comment.