Skip to content

Commit e3faaf6

Browse files
committed
Update tests
1 parent 0cbacc5 commit e3faaf6

File tree

2 files changed

+80
-26
lines changed

2 files changed

+80
-26
lines changed

src/utils/monaco-editor/__tests__/editor.spec.ts

Lines changed: 77 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,87 @@
11
import { describe, it, expect } from "vitest";
2-
import { ref, nextTick } from "vue";
2+
import { ref, nextTick, unref, isRef } from "vue";
3+
import type { Ref } from "vue";
4+
5+
import fc from "fast-check";
36

47
import { withAsyncSetup } from "@/tests/test-utils";
58
import { useMonacoEditor } from "..";
69

7-
describe("useMonacoEditor()", () => {
8-
it("Editor is created", async () => {
9-
let result!: Awaited<ReturnType<typeof useMonacoEditor>>;
10-
const element = ref(document.createElement("div"));
11-
const source = ref("# Hello, world!");
12-
await withAsyncSetup(async () => {
13-
result = await useMonacoEditor({ element, source });
14-
});
15-
const { editor, model } = result;
16-
expect(editor.value).toBeDefined();
17-
expect(model.value?.getValue()).toBe("# Hello, world!");
10+
import { fcSource, fcLanguage } from "./model.spec";
11+
12+
const fcElement = () =>
13+
fc
14+
.oneof(
15+
fc.integer().map(() => document.createElement("div")),
16+
fc.constant(undefined)
17+
)
18+
.chain(
19+
(element): fc.Arbitrary<HTMLElement | Ref<HTMLElement | undefined>> =>
20+
element === undefined
21+
? fc.constant(ref(element))
22+
: fc.oneof(fc.constant(element), fc.constant(ref(element)))
23+
);
24+
25+
const fcMode = () =>
26+
fc.option(fc.constantFrom("viewer" as const, "editor" as const), { nil: undefined });
27+
28+
const fcUseMonacoEditorOptions = () =>
29+
fc.record(
30+
{
31+
element: fcElement(),
32+
mode: fcMode(),
33+
source: fcSource(),
34+
language: fcLanguage(),
35+
},
36+
{ requiredKeys: ["element"] }
37+
);
38+
39+
describe("fcUseMonacoEditorOptions()", () => {
40+
it("Options of useMonacoEditor() are generated", () => {
41+
fc.assert(
42+
fc.property(fcUseMonacoEditorOptions(), (options) => {
43+
expect(options).toBeDefined();
44+
return true;
45+
})
46+
);
1847
});
48+
});
1949

20-
it("The 'ready' is awaited", async () => {
21-
let result!: ReturnType<typeof useMonacoEditor>;
22-
const element = ref(document.createElement("div"));
23-
const source = ref("# Hello, world!");
24-
await withAsyncSetup(async () => {
25-
result = useMonacoEditor({ element, source });
26-
});
27-
const { editor, model, ready } = result;
28-
await ready;
29-
expect(editor.value).toBeDefined();
30-
expect(model.value?.getValue()).toBe("# Hello, world!");
50+
describe("useMonacoEditor()", () => {
51+
it("Property test", async () => {
52+
await fc.assert(
53+
fc.asyncProperty(
54+
fcUseMonacoEditorOptions(),
55+
fc.boolean(),
56+
async (options, early) => {
57+
let editor!: ReturnType<typeof useMonacoEditor>["editor"];
58+
let model!: ReturnType<typeof useMonacoEditor>["model"];
59+
let source!: ReturnType<typeof useMonacoEditor>["source"];
60+
let mode!: ReturnType<typeof useMonacoEditor>["mode"];
61+
let ready!: ReturnType<typeof useMonacoEditor>["ready"];
62+
const { wrapper } = await withAsyncSetup(async () => {
63+
({ editor, model, source, mode, ready } = early
64+
? useMonacoEditor(options)
65+
: await useMonacoEditor(options));
66+
});
67+
if (early) await ready;
68+
if (isRef(options.element) && options.element.value === undefined) {
69+
options.element.value = document.createElement("div");
70+
}
71+
await nextTick();
72+
expect(editor.value).toBeDefined();
73+
74+
const expectedSource = unref(options.source) ?? "";
75+
expect(source.value).toBe(expectedSource);
76+
expect(model.value?.getValue()).toBe(expectedSource);
77+
78+
const expectedMode = unref(options.mode) ?? "viewer";
79+
expect(mode.value).toBe(expectedMode);
80+
81+
wrapper.unmount();
82+
}
83+
)
84+
);
3185
});
3286

3387
it("The mode changes between 'viewer' and 'editor'", async () => {

src/utils/monaco-editor/__tests__/model.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const fcText = () =>
1111
size: "max",
1212
});
1313

14-
const fcSource = () =>
14+
export const fcSource = () =>
1515
fc.option(
1616
fc.oneof(
1717
fcText().map((lines) => lines.join("\n")),
@@ -20,12 +20,12 @@ const fcSource = () =>
2020
{ nil: undefined }
2121
);
2222

23-
const fcLanguage = () =>
23+
export const fcLanguage = () =>
2424
fc.option(fc.constantFrom("python", "typescript"), {
2525
nil: undefined,
2626
});
2727

28-
const fcUseModelOptions = () =>
28+
export const fcUseModelOptions = () =>
2929
fc.record(
3030
{
3131
source: fcSource(),

0 commit comments

Comments
 (0)