Skip to content

Commit

Permalink
feat(client): add try course for visitor
Browse files Browse the repository at this point in the history
  • Loading branch information
cuixiaorui committed Feb 8, 2024
1 parent e1e8246 commit b8e969a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
4 changes: 4 additions & 0 deletions apps/client/api/course.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ export async function fetchCompleteCourse(courseId: number) {
export async function fetchCourses() {
return await http.get<Course[], Course[]>("/courses");
}

export async function fetchTryCourse() {
return await http.get<Course, Course>(`/courses/try`);
}
14 changes: 11 additions & 3 deletions apps/client/store/course.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { computed, ref, watchEffect, watch } from "vue";
import { defineStore } from "pinia";
import { fetchCompleteCourse, fetchCourse } from "~/api/course";
import { fetchCompleteCourse, fetchCourse, fetchTryCourse } from "~/api/course";
import { useUserStore } from "~/store/user";

interface Statement {
id: number;
Expand Down Expand Up @@ -76,8 +77,15 @@ export const useCourseStore = defineStore("course", () => {
async function setup(courseId: number) {
if (courseId === currentCourse.value?.id) return;

const course = await fetchCourse(courseId);
currentCourse.value = course;
const userStore = useUserStore();
if (!userStore.user) {
let course = await fetchTryCourse()
currentCourse.value = course
} else {
let course = await fetchCourse(courseId);
currentCourse.value = course;
}

statementIndex.value = loadProgress(courseId);
}

Expand Down
24 changes: 21 additions & 3 deletions apps/client/store/tests/course.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { setActivePinia, createPinia } from "pinia";
import { describe, it, expect, beforeEach, vi } from "vitest";
import { useCourseStore } from "../course";
import { fetchCourse, fetchCompleteCourse } from "~/api/course";
import { fetchCourse, fetchCompleteCourse, fetchTryCourse } from "~/api/course";
import { type Course } from "../course";
import { useUserStore } from "../user";

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

Expand Down Expand Up @@ -39,6 +40,23 @@ describe("course", () => {
setActivePinia(createPinia());
const store = useCourseStore();
store.cleanProgress();

const userStore = useUserStore();
userStore.initUser({
userId: "1",
username: "cxr",
phone: "18518518521",
});
});

it("should be fetch try course when user is a tourist", async () => {
const userStore = useUserStore();
userStore.logoutUser();

const store = useCourseStore();
await store.setup(1);

expect(fetchTryCourse).toBeCalled();
});

it("initializes with a course", async () => {
Expand Down Expand Up @@ -122,8 +140,8 @@ describe("course", () => {

await store.completeCourse(1);

// 重新开始之后 才可以在 setup
store.doAgain()
// 重新开始之后 才可以在 setup
store.doAgain();
await store.setup(firstCourse.id); // 在重新加载

expect(store.statementIndex).toBe(0); // 验证 statementIndex 是否重置
Expand Down

0 comments on commit b8e969a

Please sign in to comment.